refactor: rename option to `eagerFrontmatterInterpolation`

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5412/head
Divyansh Singh 2 weeks ago
parent 914b81c54b
commit 02d1763be5

@ -42,11 +42,13 @@ describe('node/markdown/markdown', () => {
expect(await render(':tada:', { emoji: false })).toContain(':tada:') expect(await render(':tada:', { emoji: false })).toContain(':tada:')
}) })
test('frontmatterExpressions', async () => { test('eagerFrontmatterInterpolation', async () => {
const src = '---\ntitle: Hello\n---\n\n{{ $frontmatter.title }}' const src = '---\ntitle: Hello\n---\n\n{{ $frontmatter.title }}'
expect(await render(src)).toContain('<p>Hello</p>') expect(await render(src)).toContain('<p>Hello</p>')
const disabled = await render(src, { frontmatterExpressions: false }) const disabled = await render(src, {
eagerFrontmatterInterpolation: false
})
expect(disabled).toContain('<p>{{ $frontmatter.title }}</p>') expect(disabled).toContain('<p>{{ $frontmatter.title }}</p>')
}) })

@ -45,7 +45,7 @@ async function renderBody(body: string) {
return (await render(frontmatter + body)).trim() return (await render(frontmatter + body)).trim()
} }
describe('node/markdown/plugins/frontmatterExpressions', () => { describe('node/markdown/plugins/eagerFrontmatterInterpolation', () => {
test('resolves property paths and escapes the value', async () => { test('resolves property paths and escapes the value', async () => {
const html = await render(`\ const html = await render(`\
--- ---
@ -218,7 +218,7 @@ logo: /logo.png
].join('\n\n') ].join('\n\n')
const runtimeEnv: any = {} const runtimeEnv: any = {}
const runtimeMd = await createMd({ frontmatterExpressions: false }) const runtimeMd = await createMd({ eagerFrontmatterInterpolation: false })
const runtimeHtml = await runtimeMd.renderAsync( const runtimeHtml = await runtimeMd.renderAsync(
frontmatter + body, frontmatter + body,
runtimeEnv runtimeEnv

@ -51,7 +51,7 @@ import {
gitHubAlertsPlugin, gitHubAlertsPlugin,
type ContainerOptions type ContainerOptions
} from './plugins/containers' } from './plugins/containers'
import { frontmatterExpressionsPlugin } from './plugins/frontmatterExpressions' import { eagerFrontmatterInterpolationPlugin } from './plugins/eagerFrontmatterInterpolation'
import { highlight as createHighlighter } from './plugins/highlight' import { highlight as createHighlighter } from './plugins/highlight'
import { imagePlugin, type Options as ImageOptions } from './plugins/image' import { imagePlugin, type Options as ImageOptions } from './plugins/image'
import { import {
@ -341,7 +341,7 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions {
* @experimental * @experimental
* @default true * @default true
*/ */
frontmatterExpressions?: boolean eagerFrontmatterInterpolation?: boolean
/** /**
* Options for `@mdit-vue/plugin-sfc`. * Options for `@mdit-vue/plugin-sfc`.
* @see https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-sfc * @see https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-sfc
@ -553,8 +553,8 @@ export async function createMarkdownRenderer(
// https://github.com/jonschlinkert/gray-matter/blob/310f9349381775d10a221cef903989eb5acc8843/index.js#L44-L47 // https://github.com/jonschlinkert/gray-matter/blob/310f9349381775d10a221cef903989eb5acc8843/index.js#L44-L47
;(options.frontmatter ??= {}).grayMatterOptions ??= {} ;(options.frontmatter ??= {}).grayMatterOptions ??= {}
frontmatterPlugin(md, options.frontmatter) frontmatterPlugin(md, options.frontmatter)
if (options.frontmatterExpressions !== false) { if (options.eagerFrontmatterInterpolation !== false) {
frontmatterExpressionsPlugin(md) eagerFrontmatterInterpolationPlugin(md)
} }
if (options.headers) { if (options.headers) {
headersPlugin(md, { headersPlugin(md, {

@ -29,11 +29,13 @@ const vPreRE = /\bv-pre\b/
type Resolve = (expr: string) => string | undefined type Resolve = (expr: string) => string | undefined
export const frontmatterExpressionsPlugin = (md: MarkdownItAsync) => { export const eagerFrontmatterInterpolationPlugin = (md: MarkdownItAsync) => {
// before the rules other plugins push (anchor, toc, ...), so slugs and // before the rules other plugins push (anchor, toc, ...), so slugs and
// extracted titles are derived from the resolved text // extracted titles are derived from the resolved text
md.core.ruler.after('text_join', 'vp_frontmatter_expressions', (state) => md.core.ruler.after(
frontmatterExpressions(md, state) 'text_join',
'vp_eager_frontmatter_interpolation',
(state) => eagerFrontmatterInterpolation(md, state)
) )
// resolved values render with their own escaping (see `escapeValue`); // resolved values render with their own escaping (see `escapeValue`);
@ -45,7 +47,10 @@ export const frontmatterExpressionsPlugin = (md: MarkdownItAsync) => {
: textRule(tokens, idx, options, env, self) : textRule(tokens, idx, options, env, self)
} }
function frontmatterExpressions(md: MarkdownItAsync, state: StateCore): void { function eagerFrontmatterInterpolation(
md: MarkdownItAsync,
state: StateCore
): void {
const { frontmatter } = state.env as MarkdownEnv const { frontmatter } = state.env as MarkdownEnv
if (!frontmatter || !state.src.includes('{{')) return if (!frontmatter || !state.src.includes('{{')) return
Loading…
Cancel
Save