Skip to content

Commit 4b46bd9

Browse files
braebobluwyTheOtterlordmatthewpsarah11918
authoredJun 19, 2024··
feat: add ShikiTransformer support to the <Code /> component (#11197)
* feat: add `transformers` prop to `<Code />` component * chore: changeset * Update .changeset/fluffy-carrots-search.md Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> * chore: add example * fix: changelog example typo Co-authored-by: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> * fix: change backdrop-filter to filter Co-authored-by: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> * Update .changeset/fluffy-carrots-search.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> Co-authored-by: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> Co-authored-by: Matthew Phillips <matthew@matthewphillips.info> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
1 parent 5c56324 commit 4b46bd9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎.changeset/fluffy-carrots-search.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Adds [`ShikiTransformer`](https://shiki.style/packages/transformers#shikijs-transformers) support to the [`<Code />`](https://docs.astro.build/en/reference/api-reference/#code-) component with a new `transformers` prop.
6+
7+
Note that `transformers` only applies classes and you must provide your own CSS rules to target the elements of your code block.
8+
9+
```astro
10+
---
11+
import { transformerNotationFocus } from '@shikijs/transformers'
12+
import { Code } from 'astro:components'
13+
14+
const code = `const foo = 'hello'
15+
const bar = ' world'
16+
console.log(foo + bar) // [!code focus]
17+
`
18+
---
19+
20+
<Code {code} lang="js" transformers={[transformerNotationFocus()]} />
21+
22+
<style is:global>
23+
pre.has-focused .line:not(.focused) {
24+
filter: blur(1px);
25+
}
26+
</style>
27+
```

‎packages/astro/components/Code.astro

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
SpecialLanguage,
77
ThemeRegistration,
88
ThemeRegistrationRaw,
9+
ShikiTransformer,
910
} from 'shiki';
1011
import { bundledLanguages } from 'shiki/langs';
1112
import { getCachedHighlighter } from '../dist/core/shiki.js';
@@ -50,6 +51,12 @@ interface Props extends Omit<HTMLAttributes<'pre'>, 'lang'> {
5051
* @default false
5152
*/
5253
inline?: boolean;
54+
/**
55+
* Shiki transformers to customize the generated HTML by manipulating the hast tree.
56+
* Supports all transformers listed here: https://shiki.style/packages/transformers#transformers
57+
* Instructions for custom transformers: https://shiki.style/guide/transformers
58+
*/
59+
transformers?: ShikiTransformer[];
5360
}
5461
5562
const {
@@ -59,6 +66,7 @@ const {
5966
themes = {},
6067
wrap = false,
6168
inline = false,
69+
transformers = [],
6270
...rest
6371
} = Astro.props;
6472
@@ -85,6 +93,7 @@ const highlighter = await getCachedHighlighter({
8593
theme,
8694
themes,
8795
wrap,
96+
transformers,
8897
});
8998
9099
const html = await highlighter.highlight(code, typeof lang === 'string' ? lang : lang.name, {

0 commit comments

Comments
 (0)
Please sign in to comment.