Skip to content

Commit

Permalink
feat: add transformers option
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Mar 6, 2024
1 parent b919b9c commit c14d41a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Features:
- [x] Wrap code block in custom tag
- [x] Default color theme
- [x] Decorations
- [x] Transformers
- [ ] Custom themes
- [ ] Custom languages

Expand Down Expand Up @@ -351,6 +352,38 @@ posthtml([

The word `const` will be wrapped in a `<span class="highlighted-word">` tag.

### `transformers`

Type: `array`\
Default: `[]`

Use this option to transform the highlighted code block with Shiki's [Transformers](https://shiki.style/guide/transformers).

```js
import posthtml from 'posthtml'
import shiki from 'posthtml-shiki'

posthtml([
shiki({
transformers: [
{
code(node) {
this.addClassToHast(node, 'language-js')
},
}
]
})
])
.process(`
<shiki>
const foo = 'bar'
</shiki>
`)
.then(result => result.html)
```

The generated `<code>` tag will have the `language-js` class added.

[npm]: https://www.npmjs.com/package/posthtml-shiki
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-shiki.svg
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-shiki
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const plugin = (options = {}) => tree => {
options.wrapTag = options.wrapTag || false
options.tag = options.tag || 'shiki'
options.decorations = options.decorations || []
options.transformers = options.transformers || []

const promises = []

Expand Down Expand Up @@ -65,6 +66,10 @@ const plugin = (options = {}) => tree => {
highlighterOptions.decorations = options.decorations
}

if (options.transformers) {
highlighterOptions.transformers = options.transformers
}

node.attrs = {}
node.tag = wrapTag
node.content = highlighter.codeToHtml(source, highlighterOptions)
Expand Down
3 changes: 3 additions & 0 deletions test/expected/transformers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<pre class="shiki nord" style="background-color:#2e3440ff;color:#d8dee9ff" tabindex="0"><code class="language-js"><span class="line"></span>
<span class="line"><span style="color:#81A1C1"> import</span><span style="color:#ECEFF4"> {</span><span style="color:#8FBCBB"> codeToHtml</span><span style="color:#ECEFF4"> }</span><span style="color:#81A1C1"> from</span><span style="color:#ECEFF4"> '</span><span style="color:#A3BE8C">shiki</span><span style="color:#ECEFF4">'</span></span>
<span class="line"></span></code></pre>
3 changes: 3 additions & 0 deletions test/fixtures/transformers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<shiki lang="js">
import { codeToHtml } from 'shiki'
</shiki>
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ test('decorations', t => {
]
})
})

test('transformers', t => {
return process(t, 'transformers', {
transformers: [
{
code(node) {
this.addClassToHast(node, 'language-js')
},
}
]
})
})

0 comments on commit c14d41a

Please sign in to comment.