Skip to content

Commit

Permalink
feat(parser): new createProcessor util (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 authored Nov 21, 2024
1 parent 9b24930 commit 4e55601
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/runtime/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { compileHast } from './compiler'
let moduleOptions: Partial<typeof import('#mdc-imports')> | undefined
let generatedMdcConfigs: MdcConfig[] | undefined

export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {}) => {
export const createProcessor = async (inlineOptions: MDCParseOptions = {}) => {
if (!moduleOptions) {
moduleOptions = await import('#mdc-imports' /* @vite-ignore */).catch(() => ({}))
}
Expand Down Expand Up @@ -93,6 +93,12 @@ export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {})
processor = await config.unified?.post?.(processor) || processor
}

return processor
}

export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {}) => {
const processor = await createProcessor(inlineOptions)

return async function parse(md: string, { fileOptions }: { fileOptions?: VFileOptions } = {}): Promise<MDCParserResult> {
// Extract front matter data
const { content, data: frontmatter } = await parseFrontMatter(md)
Expand All @@ -112,7 +118,7 @@ export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {})
// Generate toc if it is not disabled in front-matter
let toc: Toc | undefined
if (data.toc !== false) {
const tocOption = defu(data.toc || {}, options.toc)
const tocOption = defu(data.toc || {}, inlineOptions.toc, defaults.toc)
toc = generateToc(result.body, tocOption)
}

Expand Down

0 comments on commit 4e55601

Please sign in to comment.