Skip to content

Commit a0601df

Browse files
committed
fix: export processors
1 parent ca38899 commit a0601df

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"detab": "^3.0.2",
8484
"github-slugger": "^2.0.0",
8585
"hast-util-format": "^1.1.0",
86+
"hast-util-to-mdast": "^10.1.1",
8687
"hast-util-to-string": "^3.0.1",
8788
"mdast-util-to-hast": "^13.2.0",
8889
"micromark-util-sanitize-uri": "^2.0.1",
@@ -102,6 +103,7 @@
102103
"remark-mdc": "^3.5.0",
103104
"remark-parse": "^11.0.0",
104105
"remark-rehype": "^11.1.1",
106+
"remark-stringify": "^11.0.0",
105107
"scule": "^1.3.0",
106108
"shiki": "^1.24.0",
107109
"ufo": "^1.5.4",
@@ -122,7 +124,6 @@
122124
"@types/node": "^22.10.1",
123125
"changelogen": "^0.5.7",
124126
"eslint": "^9.16.0",
125-
"hast-util-to-mdast": "^10.1.1",
126127
"nuxt": "^3.14.1592",
127128
"rehype": "^13.0.2",
128129
"release-it": "^17.10.0",
@@ -131,8 +132,7 @@
131132
"vue-tsc": "^2.1.10"
132133
},
133134
"resolutions": {
134-
"@nuxtjs/mdc": "workspace:*",
135-
"remark-mdc": "npm:remark-mdc-edge@latest"
135+
"@nuxtjs/mdc": "workspace:*"
136136
},
137137
"packageManager": "pnpm@9.14.4",
138138
"release-it": {

pnpm-lock.yaml

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { parseMarkdown, createMarkdownParser } from './parser'
1+
export { parseMarkdown, createMarkdownParser, createParseProcessor } from './parser'
2+
export { stringifyMarkdown, createMarkdownStringifier, createStringifyProcessor } from './stringify'
23
export { rehypeHighlight } from './highlighter/rehype'
34
export { createShikiHighlighter } from './highlighter/shiki'
45
export * from './utils/node'

src/runtime/parser/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { compileHast } from './compiler'
1414
let moduleOptions: Partial<typeof import('#mdc-imports')> | undefined
1515
let generatedMdcConfigs: MdcConfig[] | undefined
1616

17-
export const createProcessor = async (inlineOptions: MDCParseOptions = {}) => {
17+
export const createParseProcessor = async (inlineOptions: MDCParseOptions = {}) => {
1818
if (!moduleOptions) {
1919
moduleOptions = await import('#mdc-imports' /* @vite-ignore */).catch(() => ({}))
2020
}
@@ -97,7 +97,7 @@ export const createProcessor = async (inlineOptions: MDCParseOptions = {}) => {
9797
}
9898

9999
export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {}) => {
100-
const processor = await createProcessor(inlineOptions)
100+
const processor = await createParseProcessor(inlineOptions)
101101

102102
return async function parse(md: string, { fileOptions }: { fileOptions?: VFileOptions } = {}): Promise<MDCParserResult> {
103103
// Extract front matter data

src/runtime/stringify/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { unified, type Processor } from 'unified'
22
import gfm from 'remark-gfm'
33
import mdc, { stringifyFrontMatter } from 'remark-mdc'
44
import stringify from 'remark-stringify'
5-
import type { MDCParseOptions, MDCRoot } from '@nuxtjs/mdc'
5+
import type { MDCStringifyOptions, MDCRoot } from '@nuxtjs/mdc'
66
import { mdcRemark } from './mdc-remark'
77

8-
export function createStringifyProcessor(_inlineOptions: MDCParseOptions = {}) {
8+
export function createStringifyProcessor(options: MDCStringifyOptions = {}) {
99
return unified()
1010
.use(function jsonParser(this: Processor) {
1111
this.parser = function (root: string) {
@@ -21,23 +21,29 @@ export function createStringifyProcessor(_inlineOptions: MDCParseOptions = {}) {
2121
rule: '-',
2222
listItemIndent: 'one',
2323
fence: '`',
24-
fences: true
24+
fences: true,
25+
...options?.plugins?.remarkStringify?.options
2526
})
2627
}
2728

28-
export function createMarkdownStringifier(options: MDCParseOptions = {}) {
29+
export function createMarkdownStringifier(options: MDCStringifyOptions = {}) {
2930
const processor = createStringifyProcessor(options)
3031

3132
async function stringify(value: any, data: Record<string, any> = {}): Promise<string> {
3233
const result = await processor.process({ value: JSON.stringify(value) })
3334

34-
return stringifyFrontMatter(data, result.value as string)
35+
// Stringify front matter returns empty string if no data is provided
36+
if (Object.keys(data).length) {
37+
return stringifyFrontMatter(data, result.value as string)
38+
}
39+
40+
return result.value as string
3541
}
3642

3743
return stringify
3844
}
3945

40-
export async function stringifyMarkdown(MDCAst: MDCRoot, data: Record<string, any>, options: MDCParseOptions = {}) {
46+
export async function stringifyMarkdown(MDCAst: MDCRoot, data: Record<string, any>, options: MDCStringifyOptions = {}) {
4147
const processor = createMarkdownStringifier(options)
4248
if (!MDCAst) return null
4349
return await processor(MDCAst, data)

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type * from './tree'
22
export type * from './parser'
3+
export type * from './stringify'
34
export type * from './toc'
45
export type * from './module'
56
export type * from './highlighter'

src/types/stringify.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Options as RemarkStringifyOptions } from 'remark-stringify'
2+
3+
export interface MDCStringifyOptions {
4+
plugins?: {
5+
remarkStringify?: {
6+
options?: RemarkStringifyOptions
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)