Skip to content

Commit fd6b145

Browse files
PBK-Bfarnabaz
andauthored
fix: check highlight result not empty #451 (#452)
Co-authored-by: Farnabaz <farnabaz@gmail.com>
1 parent 7a40fb0 commit fd6b145

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default defineNuxtModule<ModuleOptions>({
6666
map: options.components!.map!,
6767
},
6868
headings: options.headings!,
69+
highlight: options.highlight!,
6970
})
7071

7172
nuxt.options.build.transpile ||= []
@@ -273,6 +274,7 @@ declare module '@nuxt/schema' {
273274
map: Record<string, string>
274275
}
275276
headings: ModuleOptions['headings']
277+
highlight: ModuleOptions['highlight']
276278
}
277279
}
278280

src/runtime/highlighter/rehype-nuxt.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import type { HighlightResult, RehypeHighlightOption } from '@nuxtjs/mdc'
22
import { rehypeHighlight as rehypeHighlightUniversal } from './rehype'
3+
import { useRuntimeConfig } from '#imports'
4+
5+
class HighlighterError extends Error {
6+
constructor(
7+
message: string,
8+
public readonly httpStatus?: number,
9+
) {
10+
super(message)
11+
this.name = 'HighlighterError'
12+
}
13+
}
14+
15+
function isHighlightResult(res?: HighlightResult): res is HighlightResult {
16+
if (!res) return false
17+
return 'tree' in res
18+
}
319

420
const defaults: RehypeHighlightOption = {
521
theme: {},
@@ -9,17 +25,32 @@ const defaults: RehypeHighlightOption = {
925
return import('#mdc-highlighter').then(h => h.default(code, lang, theme, options)).catch(() => ({}))
1026
}
1127

12-
return await $fetch('/api/_mdc/highlight', {
28+
if (import.meta.client) {
29+
const highlight = useRuntimeConfig().public.mdc.highlight
30+
if (highlight === false) {
31+
return Promise.resolve({ tree: [{ type: 'text', value: code }], className: '', style: '' } as HighlightResult)
32+
}
33+
// https://github.com/nuxt-content/mdc/blob/690fd5359e743db04edf21bcd488f2c5292db176/src/module.ts#L78
34+
if (highlight?.noApiRoute === true) {
35+
return import('#mdc-highlighter').then(h => h.default(code, lang, theme, options)).catch(() => ({}))
36+
}
37+
}
38+
39+
const result = await $fetch<HighlightResult | undefined>('/api/_mdc/highlight', {
1340
params: {
1441
code,
1542
lang,
1643
theme: JSON.stringify(theme),
1744
options: JSON.stringify(options),
1845
},
1946
})
47+
if (!isHighlightResult(result)) {
48+
throw new HighlighterError(`result:${result}`)
49+
}
50+
return result
2051
}
2152
catch (e: any) {
22-
if (import.meta.client && e?.response?.status === 404) {
53+
if (import.meta.client && (e?.response?.status > 399 || e?.name == 'HighlighterError')) {
2354
window.sessionStorage.setItem('mdc-shiki-highlighter', 'browser')
2455
return this.highlighter?.(code, lang, theme, options)
2556
}

test/utils/parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import type { MDCParseOptions } from '../../src/types'
66
import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
77
import { stringifyMarkdown as _stringifyMarkDown } from '../../src/runtime/stringify'
88

9+
vi.mock('#imports', () => {
10+
return {
11+
useRuntimeConfig: vi.fn().mockReturnValue({
12+
public: {
13+
mdc: {},
14+
},
15+
}),
16+
}
17+
})
18+
919
vi.mock('#mdc-imports', () => {
1020
return {
1121
remarkPlugins: {},

0 commit comments

Comments
 (0)