Skip to content

Commit

Permalink
fix(vitepress): avoid hard errors on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 9, 2024
1 parent a728041 commit 75b3408
Showing 8 changed files with 60 additions and 28 deletions.
5 changes: 4 additions & 1 deletion docs/guide/install.md
Original file line number Diff line number Diff line change
@@ -118,7 +118,10 @@ Unlike `shiki` that loads all themes and languages by default, `shiki` requires
```ts theme:slack-dark twoslash
import { getHighlighter } from 'shiki'

const highlighter = await getHighlighter({ /* ... */ })
const highlighter = await getHighlighter({
themes: [],
langs: []
})

highlighter.codeToHtml(
'const a = 1',
17 changes: 13 additions & 4 deletions docs/guide/load-lang.md
Original file line number Diff line number Diff line change
@@ -11,11 +11,13 @@ import { getHighlighter } from 'shiki'
const myLang = JSON.parse(fs.readFileSync('my-lang.json', 'utf8'))

const highlighter = await getHighlighter({
langs: [myLang]
langs: [myLang],
themes: ['vitesse-light']
})

const html = highlighter.codeToHtml(code, {
lang: 'my-lang',
theme: 'vitesse-light'
})
```

@@ -27,12 +29,16 @@ import { getHighlighter } from 'shiki'

const myLang = JSON.parse(fs.readFileSync('my-lang.json', 'utf8'))

const highlighter = await getHighlighter()
const highlighter = await getHighlighter({
langs: [],
themes: ['vitesse-light'],
})

await highlighter.loadLanguage(myLang) // <--

const html = highlighter.codeToHtml(code, {
lang: 'my-lang',
theme: 'vitesse-light'
})
```

@@ -59,7 +65,8 @@ const highlighter = await getHighlighter({
'stylus',
],
},
]
],
themes: []
})
```

@@ -83,7 +90,8 @@ const highlighter = await getHighlighter({
],
...vineGrammar
},
]
],
themes: []
})
```

@@ -99,6 +107,7 @@ const highlighter = await getHighlighter({
langAlias: { // [!code hl:3]
mylang: 'javascript',
},
themes: ['nord']
})

const code = highlighter.codeToHtml('const a = 1', {
8 changes: 6 additions & 2 deletions docs/guide/load-theme.md
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@ const myTheme = {
}

const highlighter = await getHighlighter({
themes: [myTheme]
themes: [myTheme],
langs: [],
})

const code = `console.log('hello')`
@@ -40,7 +41,10 @@ import { getHighlighter } from 'shiki'
// Load the theme object from a file, a network request, or anywhere
const myTheme = JSON.parse(fs.readFileSync('my-theme.json', 'utf8'))

const highlighter = await getHighlighter()
const highlighter = await getHighlighter({
langs: ['javascript'],
themes: [],
})

await highlighter.loadTheme(myTheme) // <--

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@
"vitepress-plugin-mermaid": "^2.0.16",
"vitest": "^1.2.2",
"vue-tsc": "^1.8.27",
"wrangler": "^3.28.0"
"wrangler": "^3.28.1"
},
"resolutions": {
"@shikijs/compat": "workspace:*",
2 changes: 1 addition & 1 deletion packages/twoslash/package.json
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@
},
"dependencies": {
"@shikijs/core": "workspace:*",
"twoslash": "^0.2.0"
"twoslash": "^0.2.1"
},
"devDependencies": {
"@iconify-json/carbon": "^1.1.29",
3 changes: 2 additions & 1 deletion packages/vitepress-twoslash/package.json
Original file line number Diff line number Diff line change
@@ -59,7 +59,8 @@
"mdast-util-gfm": "^3.0.0",
"mdast-util-to-hast": "^13.1.0",
"shiki": "workspace:*",
"twoslash-vue": "^0.2.0",
"twoslash": "^0.2.1",
"twoslash-vue": "^0.2.1",
"vue": "^3.4.18"
}
}
14 changes: 13 additions & 1 deletion packages/vitepress-twoslash/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable node/prefer-global/process */
import type { TransformerTwoslashOptions } from '@shikijs/twoslash/core'
import { createTransformerFactory } from '@shikijs/twoslash/core'
import { ShikiTwoslashError, createTransformerFactory } from '@shikijs/twoslash/core'

Check failure on line 3 in packages/vitepress-twoslash/src/index.ts

GitHub Actions / lint

'ShikiTwoslashError' is defined but never used
import { createTwoslasher } from 'twoslash-vue'
import type { ShikiTransformer } from 'shiki'
import { removeTwoslashNotations } from 'twoslash'
import type { TwoslashFloatingVueRendererOptions } from './renderer-floating-vue'
import { rendererFloatingVue } from './renderer-floating-vue'

@@ -30,6 +32,16 @@ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}
)({
langs: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'],
renderer: rendererFloatingVue(options),
onTwoslashError: (error, code) => {
const isCI = typeof process !== 'undefined' && process?.env?.CI
const isDev = typeof process !== 'undefined' && process?.env?.NODE_ENV === 'development'
console.error(String(error), `\n\n--------\nTwoslash error in code:\n--------\n${code.split(/\n/g).slice(0, 15).join('\n').trim()}\n--------\n`)
if (isCI || !isDev || options.throws)
throw error
if (typeof process !== 'undefined')
process.exitCode = 1
return removeTwoslashNotations(code)
},
...options,
explicitTrigger,
})
37 changes: 20 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75b3408

Please sign in to comment.