-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce Decorations API (#574)
- Loading branch information
Showing
22 changed files
with
1,551 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# API References | ||
|
||
## `codeToHast` | ||
|
||
You can also get the intermediate `hast` to do custom rendering without serializing them into HTML with `codeToHast`. You can also further integrate the AST with the [unified](https://github.com/unifiedjs) ecosystem. | ||
|
||
```ts twoslash | ||
import { getHighlighter } from 'shiki' | ||
|
||
const highlighter = await getHighlighter({ | ||
themes: ['nord', 'min-light'], | ||
langs: ['javascript'], | ||
}) | ||
// ---cut--- | ||
const root = highlighter.codeToHast( | ||
'const a = 1', | ||
{ lang: 'javascript', theme: 'nord' } | ||
) | ||
|
||
console.log(root) | ||
``` | ||
|
||
<!-- eslint-skip --> | ||
|
||
```ts | ||
{ | ||
type: 'root', | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'pre', | ||
properties: { | ||
class: 'shiki vitesse-light', | ||
style: 'background-color:#ffffff;color:#393a34', | ||
tabindex: '0' | ||
}, | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'code', | ||
properties: {}, | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { class: 'line' }, | ||
children: [ | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { style: 'color:#AB5959' }, | ||
children: [ { type: 'text', value: 'const' } ] | ||
}, | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { style: 'color:#B07D48' }, | ||
children: [ { type: 'text', value: ' a' } ] | ||
}, | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { style: 'color:#999999' }, | ||
children: [ { type: 'text', value: ' =' } ] | ||
}, | ||
{ | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { style: 'color:#2F798A' }, | ||
children: [ { type: 'text', value: ' 1' } ] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Decorations | ||
|
||
We provide a decorations API allowing you to warp custom class and attributes to ranges of your code. | ||
|
||
```ts twoslash | ||
import { codeToHtml } from 'shiki' | ||
|
||
const code = ` | ||
const x = 10 | ||
console.log(x) | ||
`.trim() | ||
|
||
const html = await codeToHtml(code, { | ||
theme: 'vitesse-light', | ||
lang: 'ts', | ||
decorations: [ // [!code hl:8] | ||
{ | ||
// line and character are 0-indexed | ||
start: { line: 1, character: 0 }, | ||
end: { line: 1, character: 11 }, | ||
properties: { class: 'highlighted-word' } | ||
} | ||
] | ||
}) | ||
``` | ||
|
||
The result will be (styled with CSS in this example): | ||
|
||
```ts | ||
// @decorations:[{"start":{"line":1,"character":0},"end":{"line":1,"character":11},"properties":{"class":"highlighted-word"}}] | ||
const x = 10 | ||
console.log(x) | ||
``` | ||
|
||
The positions can also be 0-indexed offsets relative to the code: | ||
|
||
```ts twoslash | ||
import { codeToHtml } from 'shiki' | ||
|
||
const code = ` | ||
const x = 10 | ||
console.log(x) | ||
`.trim() | ||
// ---cut--- | ||
const html = await codeToHtml(code, { | ||
theme: 'vitesse-light', | ||
lang: 'ts', | ||
decorations: [ // [!code hl:7] | ||
{ | ||
start: 21, | ||
end: 24, | ||
properties: { class: 'highlighted-word' } | ||
} | ||
] | ||
}) | ||
``` | ||
|
||
It renders: | ||
|
||
```ts | ||
// @decorations:[{"start":21,"end":24,"properties":{"class":"highlighted-word"}}] | ||
const x = 10 | ||
console.log(x) | ||
``` | ||
|
||
## Use Decorations in Transformers | ||
|
||
For advanced use cases, you can use the [Transformers API](./transformers.md) to have full access to the tokens and the HAST tree. | ||
|
||
Meanwhile, if you want to append decorations within a transformer, you can do that with: | ||
|
||
```ts twoslash | ||
/* eslint-disable import/no-duplicates */ | ||
import { DecorationItem } from 'shiki' | ||
function doSomethingWithCode(code: string): DecorationItem[] { | ||
return [] | ||
} | ||
const code: string = '' | ||
|
||
// ---cut--- | ||
import { ShikiTransformer, codeToHtml } from 'shiki' | ||
|
||
const myTransformer: ShikiTransformer = { | ||
name: 'my-transformer', | ||
preprocess(code, options) { | ||
// Generate the decorations somehow | ||
const decorations = doSomethingWithCode(code) | ||
|
||
// Make sure the decorations array exists | ||
options.decorations ||= [] | ||
// Append the decorations | ||
options.decorations.push(...decorations) | ||
} | ||
} | ||
|
||
const html = await codeToHtml(code, { | ||
theme: 'vitesse-light', | ||
lang: 'ts', | ||
transformers: [ | ||
myTransformer | ||
] | ||
}) | ||
``` | ||
|
||
Note that you can only provide decorations in or before the `preprocess` hook. In later hooks, changes to the decorations arrary will be ignored. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.