Skip to content

Commit bfb0220

Browse files
authored
feat(build): support markdown frontmatter options (#1218)
1 parent a4af194 commit bfb0220

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
},
9494
"devDependencies": {
9595
"@mdit-vue/plugin-component": "^0.9.2",
96+
"@mdit-vue/plugin-frontmatter": "^0.9.2",
9697
"@mdit-vue/plugin-toc": "^0.9.2",
98+
"@mdit-vue/types": "^0.9.2",
9799
"@rollup/plugin-alias": "^3.1.9",
98100
"@rollup/plugin-commonjs": "^22.0.2",
99101
"@rollup/plugin-json": "^4.1.0",
@@ -129,7 +131,6 @@
129131
"execa": "^6.1.0",
130132
"fast-glob": "^3.2.11",
131133
"fs-extra": "^10.1.0",
132-
"gray-matter": "^4.0.3",
133134
"lint-staged": "^13.0.3",
134135
"lru-cache": "^7.13.2",
135136
"markdown-it": "^13.0.1",

pnpm-lock.yaml

+13-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node/markdown/env.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { MarkdownItEnv } from '@mdit-vue/types'
2+
import { CleanUrlsMode } from '../shared'
3+
4+
export interface MarkdownEnv extends MarkdownItEnv {
5+
path: string
6+
relativePath: string
7+
cleanUrls: CleanUrlsMode
8+
}

src/node/markdown/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './env'
2+
export * from './markdown'

src/node/markdown/markdown.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import anchorPlugin from 'markdown-it-anchor'
33
import attrsPlugin from 'markdown-it-attrs'
44
import emojiPlugin from 'markdown-it-emoji'
55
import { componentPlugin } from '@mdit-vue/plugin-component'
6+
import {
7+
frontmatterPlugin,
8+
type FrontmatterPluginOptions
9+
} from '@mdit-vue/plugin-frontmatter'
610
import { tocPlugin, type TocPluginOptions } from '@mdit-vue/plugin-toc'
711
import { IThemeRegistration } from 'shiki'
812
import { highlight } from './plugins/highlight'
@@ -32,6 +36,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
3236
allowedAttributes?: string[]
3337
disable?: boolean
3438
}
39+
frontmatter?: FrontmatterPluginOptions
3540
theme?: ThemeOptions
3641
toc?: TocPluginOptions
3742
externalLinks?: Record<string, string>
@@ -92,6 +97,9 @@ export const createMarkdownRenderer = async (
9297
permalink: anchorPlugin.permalink.ariaHidden({}),
9398
...options.anchor
9499
} as anchorPlugin.AnchorOptions)
100+
.use(frontmatterPlugin, {
101+
...options.frontmatter
102+
} as FrontmatterPluginOptions)
95103
.use(tocPlugin, {
96104
slugify,
97105
...options.toc

src/node/markdown/plugins/link.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// 2. normalize internal links to end with `.html`
44

55
import MarkdownIt from 'markdown-it'
6-
import { MarkdownRenderer } from '../markdown'
6+
import type { MarkdownEnv } from '../env'
7+
import type { MarkdownRenderer } from '../markdown'
78
import { URL } from 'url'
89
import { EXTERNAL_URL_RE, CleanUrlsMode } from '../../shared'
910

@@ -14,7 +15,13 @@ export const linkPlugin = (
1415
externalAttrs: Record<string, string>,
1516
base: string
1617
) => {
17-
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
18+
md.renderer.rules.link_open = (
19+
tokens,
20+
idx,
21+
options,
22+
env: MarkdownEnv,
23+
self
24+
) => {
1825
const token = tokens[idx]
1926
const hrefIndex = token.attrIndex('href')
2027
if (hrefIndex >= 0) {
@@ -37,7 +44,7 @@ export const linkPlugin = (
3744
// links to files (other than html/md)
3845
!/\.(?!html|md)\w+($|\?)/i.test(url)
3946
) {
40-
normalizeHref(hrefAttr, env.cleanUrl)
47+
normalizeHref(hrefAttr, env.cleanUrls)
4148
}
4249

4350
// encode vite-specific replace strings in case they appear in URLs

src/node/markdownToVue.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import fs from 'fs'
22
import path from 'path'
33
import c from 'picocolors'
4-
import matter from 'gray-matter'
54
import LRUCache from 'lru-cache'
65
import { PageData, HeadConfig, EXTERNAL_URL_RE, CleanUrlsMode } from './shared'
76
import { slash } from './utils/slash'
87
import { deeplyParseHeader } from './utils/parseHeader'
98
import { getGitTimestamp } from './utils/getGitTimestamp'
10-
import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown'
9+
import {
10+
createMarkdownRenderer,
11+
type MarkdownEnv,
12+
type MarkdownOptions
13+
} from './markdown'
1114
import _debug from 'debug'
1215

1316
const debug = _debug('vitepress:md')
@@ -69,19 +72,18 @@ export async function createMarkdownToVueRenderFn(
6972
}
7073
})
7174

72-
const { content, data: frontmatter } = matter(src)
73-
7475
// reset state before render
7576
md.__path = file
7677
md.__relativePath = relativePath
7778

78-
const html = md.render(content, {
79+
const env: MarkdownEnv = {
7980
path: file,
8081
relativePath,
81-
cleanUrls,
82-
frontmatter
83-
})
82+
cleanUrls
83+
}
84+
const html = md.render(src, env)
8485
const data = md.__data
86+
const { content = '', frontmatter = {} } = env
8587

8688
// validate data.links
8789
const deadLinks: string[] = []
@@ -128,7 +130,7 @@ export async function createMarkdownToVueRenderFn(
128130

129131
const pageData: PageData = {
130132
title: inferTitle(frontmatter, content),
131-
titleTemplate: frontmatter.titleTemplate,
133+
titleTemplate: frontmatter.titleTemplate as any,
132134
description: inferDescription(frontmatter),
133135
frontmatter,
134136
headers: data.headers || [],

0 commit comments

Comments
 (0)