Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformers): add Style to Class transformer #826

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

antfu
Copy link
Member

@antfu antfu commented Nov 4, 2024

fix #824

Introduce a new transformer transformerStyleToClass that converts Shiki's inline styles to unique classes.

Class names are generated based on the hash value of the style object with the prefix/suffix you provide. You can put this transformer in multiple highlights passes and then get the CSS at the end to reuse the exact same styles. As Shiki doesn't handle CSS, it's on your integration to decide how to extract and apply/bundle the CSS.

For example:

import { transformerStyleToClass } from '@shikijs/transformers'
import { codeToHtml } from 'shiki'

const toClass = transformerStyleToClass({ // [!code highlight:3]
  classPrefix: '__shiki_',
})

const code = `console.log('hello')`
const html = await codeToHtml(code, {
  lang: 'ts',
  themes: {
    dark: 'vitesse-dark',
    light: 'vitesse-light',
  },
  defaultColor: false,
  transformers: [toClass], // [!code highlight]
})

// The transformer instance exposes some methods to get the CSS
const css = toClass.getCSS() // [!code highlight]

// use `html` and `css` in your app

HTML output:

<pre class="shiki shiki-themes vitesse-dark vitesse-light __shiki_9knfln" tabindex="0"><code><span class="line">
  <span class="__shiki_14cn0u">console</span>
  <span class="__shiki_ps5uht">.</span>
  <span class="__shiki_1zrdwt">log</span>
  <span class="__shiki_ps5uht">(</span>
  <span class="__shiki_236mh3">'</span>
  <span class="__shiki_1g4r39">hello</span>
  <span class="__shiki_236mh3">'</span>
  <span class="__shiki_ps5uht">)</span>
</span></code></pre>

CSS output:

.__shiki_14cn0u {
  --shiki-dark: #bd976a;
  --shiki-light: #b07d48;
}
.__shiki_ps5uht {
  --shiki-dark: #666666;
  --shiki-light: #999999;
}
.__shiki_1zrdwt {
  --shiki-dark: #80a665;
  --shiki-light: #59873a;
}
.__shiki_236mh3 {
  --shiki-dark: #c98a7d77;
  --shiki-light: #b5695977;
}
.__shiki_1g4r39 {
  --shiki-dark: #c98a7d;
  --shiki-light: #b56959;
}
.__shiki_9knfln {
  --shiki-dark: #dbd7caee;
  --shiki-light: #393a34;
  --shiki-dark-bg: #121212;
  --shiki-light-bg: #ffffff;
}

Copy link

netlify bot commented Nov 4, 2024

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit 7f86e71
🔍 Latest deploy log https://app.netlify.com/sites/shiki-next/deploys/6728f3183c0b440008d6534b
😎 Deploy Preview https://deploy-preview-826--shiki-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Nov 4, 2024

Codecov Report

Attention: Patch coverage is 93.82716% with 5 lines in your changes missing coverage. Please review.

Project coverage is 94.85%. Comparing base (9b55bf8) to head (7f86e71).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...es/transformers/src/transformers/style-to-class.ts 93.75% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #826      +/-   ##
==========================================
- Coverage   94.86%   94.85%   -0.02%     
==========================================
  Files          78       79       +1     
  Lines        6608     6689      +81     
  Branches     1319     1340      +21     
==========================================
+ Hits         6269     6345      +76     
- Misses        330      335       +5     
  Partials        9        9              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

netlify bot commented Nov 4, 2024

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit 7f86e71
🔍 Latest deploy log https://app.netlify.com/sites/shiki-matsu/deploys/6728f3183c0b440008d65347
😎 Deploy Preview https://deploy-preview-826--shiki-matsu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@antfu antfu merged commit 48d6c57 into main Nov 14, 2024
11 of 13 checks passed
@Master-Hash
Copy link

Master-Hash commented Nov 15, 2024

How can this new Style to Class transformer use with rehype plugin? Rehype is configured in my vite.config.js, which finishes before transform the html content, and the css output (also in vite.config.js) turns out to be empty string. Any method to use the transformer in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert inline styles to classes to reduce output size
2 participants