Skip to content

Commit

Permalink
fix(highlight): do not include shiki in bundle when highlight is disa…
Browse files Browse the repository at this point in the history
…bled
  • Loading branch information
farnabaz committed Nov 9, 2023
1 parent 2e7dcdb commit 7cb2b9d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ export default defineNuxtModule<ModuleOptions>({
)
})

// Enable wasm for shikiji
if (options.highlight) {
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {}
nuxt.options.nitro.experimental.wasm = true
// Add server handlers
addServerHandler({ route: '/api/_mdc/highlight', handler: resolver.resolve('./runtime/shiki/event-handler') })

options.rehypePlugins = options.rehypePlugins || {}
options.rehypePlugins.highlight = options.rehypePlugins.highlight || {}
options.rehypePlugins.highlight.src = options.rehypePlugins.highlight.src || resolver.resolve('./runtime/shiki/index')
}

// Add imports template
const { dst: templatePath } = addTemplate({ filename: 'mdc-imports.mjs', getContents: mdcImportTemplate, options, write: true })
nuxt.options.alias['#mdc-imports'] = process.env.NODE_ENV === 'development' ? pathToFileURL(templatePath).href : templatePath
Expand All @@ -82,14 +94,6 @@ export default defineNuxtModule<ModuleOptions>({
})
}

// Enable wasm for shikiji
if (options.highlight) {
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {}
nuxt.options.nitro.experimental.wasm = true
// Add server handlers
addServerHandler({ route: '/api/_mdc/highlight', handler: resolver.resolve('./runtime/shiki/event-handler') })
}

extendViteConfig((config) => {
config.optimizeDeps = config.optimizeDeps || {}
config.optimizeDeps.exclude = config.optimizeDeps.exclude || []
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { defu } from 'defu'
import { useProcessorPlugins } from './utils/plugins'
import { compileHast } from './compiler'
import { defaults } from './options'
import { rehypeShiki } from '../shiki'
import { generateToc } from './toc'
import { nodeTextContent } from '../utils/node'

Expand All @@ -23,6 +22,11 @@ export const parseMarkdown = async (md: string, opts: MDCParseOptions = {}) => {
highlight: moduleOptions?.highlight,
}, defaults)

if (options.rehype?.plugins?.highlight) {
options.rehype.plugins.highlight.options = options.highlight || {}
}


// Extract front matter data
const { content, data: frontmatter } = await parseFrontMatter(md)

Expand All @@ -37,10 +41,6 @@ export const parseMarkdown = async (md: string, opts: MDCParseOptions = {}) => {
// Turns markdown into HTML to support rehype
processor.use(remark2rehype as any, (options.rehype as any)?.options)

if (options.highlight) {
processor.use(rehypeShiki, options.highlight)
}

// Apply custom plguins to extend rehybe capabilities
await useProcessorPlugins(processor as any, options.rehype?.plugins)

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/shiki/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const defaults: RehypeShikiOption = {
return Promise.resolve({ tree: [{ type: 'text', value: code }], className: '', style: '' } as HighlightResult)
}
}

export default rehypeShiki
export function rehypeShiki(opts: RehypeShikiOption = {}) {
const options = { ...defaults, ...opts }

Expand Down
5 changes: 5 additions & 0 deletions test/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseMarkdown as _parseMarkDown } from '../../src/runtime/parser'
import type { MDCParseOptions } from '../../src/runtime/types'
import type { Theme } from '../../src/runtime/shiki/types'
import { useShikiHighlighter } from '../../src/runtime/shiki/highlighter'
import rehypeShiki from '../../src/runtime/shiki'

vi.mock('#mdc-imports', () => {
return {
Expand All @@ -21,6 +22,10 @@ export const parseMarkdown = (md: string, options: MDCParseOptions = {}) => {
const shikiHighlighter = useShikiHighlighter({})
return await shikiHighlighter.getHighlightedAST(code as string, lang as any, theme as Theme, { highlights })
}
options.rehype = options.rehype || {}
options.rehype.plugins = options.rehype?.plugins || {}
options.rehype.plugins.highlight = options.rehype?.plugins.highlight || {}
options.rehype.plugins.highlight.instance = rehypeShiki
}

return _parseMarkDown(md, options)
Expand Down

0 comments on commit 7cb2b9d

Please sign in to comment.