shikiji-transformers
Collective of common transformers for Shikiji, inspired by shiki-processor.
Install
npm i -D shikiji-transformersimport {
codeToHtml,
} from 'shikiji'
import {
transformerNotationDiff,
// ...
} from 'shikiji-transformers'
const code = `console.log('hello')`
const html = await codeToHtml(code, {
lang: 'ts',
theme: 'nord',
transformers: [
transformerNotationDiff(),
// ...
],
})Transformers
transformerNotationDiff
Use [!code ++] and [!code --] to mark added and removed lines.
For example, the following code
```ts
export function foo() {
console.log('hewwo') // [!code --]
console.log('hello') // [!code ++]
}
```will be transformed to
export function foo() {
console.log('hewwo')
console.log('hello')
}HTML Output
<!-- Output (stripped of `style` attributes for clarity) -->
<pre class="shiki has-diff"> <!-- Notice `has-diff` -->
<code>
<span class="line"></span>
<span class="line"><span>function</span><span>()</span><span></span><span>{</span></span>
<span class="line diff remove"> <!-- Notice `diff` and `remove` -->
<span></span><span>console</span><span>.</span><span>log</span><span>(</span><span>'</span><span>hewwo</span><span>'</span><span>) </span>
</span>
<span class="line diff add"> <!-- Notice `diff` and `add` -->
<span></span><span>console</span><span>.</span><span>log</span><span>(</span><span>'</span><span>hello</span><span>'</span><span>) </span>
</span>
<span class="line"><span></span><span>}</span></span>
<span class="line"><span></span></span>
</code>
</pre>transformerNotationHighlight
Use [!code highlight] to highlight a line (adding highlighted class).
```ts
export function foo() {
console.log('Highlighted') // [!code highlight]
}
```Results in
export function foo() {
console.log('Highlighted')
}Alternatively, you can use the transformerMetaHighlight to highlight lines based on the meta string.
transformerNotationWordHighlight
Use [!code word:xxx] to highlight a word (adding highlighted-word class).
```ts
export function foo() { // [!code word:Hello]
const msg = 'Hello World'
console.log(msg) // prints Hello World
}
```Results in
export function foo() {
const msg = 'Hello World'
console.log(msg) // prints Hello World
}You can also specify the number of occurrences to highlight, e.g. [!code word:options:2] will highlight the next 2 occurrences of options.
```ts
// [!code word:options:2]
const options = { foo: 'bar' }
options.foo = 'baz'
console.log(options.foo) // this one will not be highlighted
```const options = { foo: 'bar' }
options.foo = 'baz'
console.log(options.foo) // this one will not be highlightedtransformerNotationFocus
Use [!code focus] to focus a line (adding focused class).
```ts
export function foo() {
console.log('Focused') // [!code focus]
}
```Results in
export function foo() {
console.log('Focused')
}transformerNotationErrorLevel
Use [!code error], [!code warning], to mark a line with an error level (adding highlighted error, highlighted warning class).
```ts
export function foo() {
console.error('Error') // [!code error]
console.warn('Warning') // [!code warning]
}
```Results in
export function foo() {
console.error('Error')
console.warn('Warning')
}transformerRenderWhitespace
Render whitespaces (tabs and spaces) as individual spans, with classes tab and space.
With some CSS, you can make it look like this:
function block( ) {
space( )
table( )
}Example CSS
.vp-code .tab,
.vp-code .space {
position: relative;
}
.vp-code .tab::before {
content: '⇥';
position: absolute;
opacity: 0.3;
}
.vp-code .space::before {
content: '·';
position: absolute;
opacity: 0.3;
}transformerMetaHighlight
Highlight lines based on the meta string provided on the code snippet. Requires integrations supports.
```js {1,3-4}
console.log('1')
console.log('2')
console.log('3')
console.log('4')
```Results in
console.log('1')
console.log('2')
console.log('3')
console.log('4')transformerMetaWordHighlight
Highlight words based on the meta string provided on the code snippet. Requires integrations supports.
```js /Hello/
const msg = 'Hello World'
console.log(msg)
console.log(msg) // prints Hello World
```Results in
const msg = 'Hello World'
console.log(msg) // prints Hello WorldtransformerCompactLineOptions
Support for shiki's lineOptions that is removed in shikiji.
transformerRemoveLineBreak
Remove line breaks between <span class="line">. Useful when you set display: block to .line in CSS.