This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
249 additions
and
393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { usePlayground } from '../store/playground' | ||
const play = usePlayground() | ||
const showModel = ref(false) | ||
function preview(id: string) { | ||
play.lang = id | ||
showModel.value = true | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>ID</th> | ||
<th>Alias</th> | ||
<th>Preview</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr v-for="l in play.allLanguages" :key="l.id"> | ||
<td>{{ l.name }}</td> | ||
<td><code>{{ l.id }}</code></td> | ||
<td> | ||
<code v-for="alias in l.aliases" :key="alias">{{ alias }}</code> | ||
</td> | ||
<td> | ||
<div flex> | ||
<button | ||
title="Preview Example" | ||
ma text-lg | ||
@click="preview(l.id)" | ||
> | ||
<div i-carbon:code /> | ||
</button> | ||
</div> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<div v-if="showModel" fixed inset-0 z-100 flex items-center justify-center> | ||
<div bg-black:50 absolute inset-0 backdrop-blur-sm @click="showModel = false" /> | ||
<ShikijiMiniPlayground max-h-80vh w-full md:w-150 lg:w-200 /> | ||
</div> | ||
</div> | ||
</template> |
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,47 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { usePlayground } from '../store/playground' | ||
const play = usePlayground() | ||
const showModel = ref(false) | ||
function preview(id: string) { | ||
play.theme = id | ||
showModel.value = true | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>ID</th> | ||
<th>Preview</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr v-for="l in play.allThemes" :key="l.id"> | ||
<td>{{ l.name }}</td> | ||
<td><code>{{ l.id }}</code></td> | ||
<td> | ||
<div flex> | ||
<button | ||
title="Preview Example" | ||
ma text-lg | ||
@click="preview(l.id)" | ||
> | ||
<div i-carbon:code /> | ||
</button> | ||
</div> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<div v-if="showModel" fixed inset-0 z-100 flex items-center justify-center> | ||
<div bg-black:50 absolute inset-0 backdrop-blur-sm @click="showModel = false" /> | ||
<ShikijiMiniPlayground max-h-80vh w-full md:w-150 lg:w-200 /> | ||
</div> | ||
</div> | ||
</template> |
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
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,84 @@ | ||
# Load Custom Languages | ||
|
||
Check [All Builtin Languages](/languages) as well. | ||
|
||
You can load custom languages by passing a TextMate grammar object into the langs array. | ||
|
||
```ts | ||
import { getHighlighter } from 'shikiji' | ||
|
||
const myLang = JSON.parse(fs.readFileSync('my-lang.json', 'utf8')) | ||
|
||
const highlighter = await getHighlighter({ | ||
langs: [myLang] | ||
}) | ||
|
||
const html = highlighter.codeToHtml(code, { | ||
lang: 'my-lang', | ||
}) | ||
``` | ||
|
||
You can also load languages after the highlighter has been created. | ||
|
||
```ts | ||
import { getHighlighter } from 'shikiji' | ||
|
||
const myLang = JSON.parse(fs.readFileSync('my-lang.json', 'utf8')) | ||
|
||
const highlighter = await getHighlighter() | ||
|
||
await highlighter.loadLanguage(myLang) // <-- | ||
|
||
const html = highlighter.codeToHtml(code, { | ||
lang: 'my-lang', | ||
}) | ||
``` | ||
|
||
## Migrate from Shiki | ||
|
||
Since `shikiji` is environment agnostic, that means we don't have access to the file system. That means the `path` property `shiki` supports is not available in `shikiji`. Instead, you need to read them in yourself and pass the object. For example: | ||
|
||
```ts | ||
const highlighter = await getHighlighter({ | ||
langs: [ | ||
{ | ||
name: 'vue-vine', | ||
scopeName: 'source.vue-vine', | ||
// ‼️ This would not work! | ||
path: join(__dirname, './vine-ts.tmLanguage.json'), | ||
embeddedLangs: [ | ||
'vue-html', | ||
'css', | ||
'scss', | ||
'sass', | ||
'less', | ||
'stylus', | ||
], | ||
}, | ||
] | ||
}) | ||
``` | ||
|
||
Instead, load that file yourself (via `fs`, `import()`, `fetch()`, etc.) and pass the object: | ||
|
||
```ts | ||
const vineGrammar = JSON.parse(fs.readFileSync(join(__dirname, './vine-ts.tmLanguage.json'), 'utf8')) | ||
|
||
const highlighter = await getHighlighter({ | ||
langs: [ | ||
{ | ||
name: 'vue-vine', | ||
scopeName: 'source.vue-vine', | ||
embeddedLangs: [ | ||
'vue-html', | ||
'css', | ||
'scss', | ||
'sass', | ||
'less', | ||
'stylus', | ||
], | ||
...vineGrammar | ||
}, | ||
] | ||
}) | ||
``` |
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,39 @@ | ||
# Load Custom Themes | ||
|
||
Check [All Builtin Themes](/themes) as well. | ||
|
||
You can load custom themes by passing a `Theme` object into the themes array. | ||
|
||
```ts | ||
import { getHighlighter } from 'shikiji' | ||
|
||
const myTheme = JSON.parse(fs.readFileSync('my-theme.json', 'utf8')) | ||
|
||
const highlighter = await getHighlighter({ | ||
themes: [myTheme] | ||
}) | ||
|
||
const html = highlighter.codeToHtml(code, { | ||
lang: 'javascript', | ||
theme: 'my-theme' | ||
}) | ||
``` | ||
|
||
You can also load themes after the highlighter has been created. | ||
|
||
```ts | ||
import { getHighlighter } from 'shikiji' | ||
|
||
const myTheme = JSON.parse(fs.readFileSync('my-theme.json', 'utf8')) | ||
|
||
const highlighter = await getHighlighter() | ||
|
||
await highlighter.loadTheme(myTheme) // <-- | ||
|
||
const html = highlighter.codeToHtml(code, { | ||
lang: 'javascript', | ||
theme: 'my-theme' | ||
}) | ||
``` | ||
|
||
The theme should be a TextMate theme in JSON object. For example, [it should looks like this](https://github.com/antfu/vscode-theme-vitesse/blob/main/themes/vitesse-dark.json). |
Oops, something went wrong.