Skip to content

Commit

Permalink
fix: properly pass headmatter to the extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
twitwi committed Dec 11, 2022
1 parent 3900b41 commit cf81a72
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
17 changes: 9 additions & 8 deletions packages/parser/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,17 @@ export async function parse(
}

// identify the headmatter, to be able to load preparser extensions
// (strict parsing based on the parsing code)
{
let hStart = 0
while (hStart < lines.length && lines[hStart].match(/^(---|\w*)+/))
hStart++
let hEnd = hStart + 1
while (hEnd < lines.length && !lines[hEnd].match(/^---+/))
hEnd++
let hm = ''
if (lines[0].match(/^---([^-].*)?$/) && !lines[1]?.match(/^\s*$/)) {
let hEnd = 1
while (hEnd < lines.length && !lines[hEnd].trimEnd().match(/^---$/))
hEnd++
hm = lines.slice(1, hEnd).join('\n')
}
if (onHeadmatter) {
/// //// TODO call matter()
const o = YAML.load(lines.slice(hStart, hEnd).join('\n')) ?? {}
const o = YAML.load(hm) ?? {}
extensions = await onHeadmatter(o, extensions ?? [], filepath)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function load(filepath: string, themeMeta?: SlidevThemeMeta, conten
const data = await parse(markdown, filepath, themeMeta, [], async (headmatter, exts: SlidevPreparserExtension[], filepath: string | undefined) => {
return [
...exts,
...preparserExtensionLoader ? await preparserExtensionLoader(headmatter.addons ?? [], filepath) : [],
...preparserExtensionLoader ? await preparserExtensionLoader(headmatter, filepath) : [],
]
})

Expand Down
5 changes: 3 additions & 2 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
'css',
]

injectPreparserExtensionLoader(async (addons: string[], filepath?: string) => {
injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, filepath?: string) => {
const addons = headmatter?.addons as string[] ?? []
const roots = /* uniq */([
getUserRoot({}).userRoot,
...getAddonRoots(addons, ''),
getClientRoot(),
])
const mergeArrays = (a: SlidevPreparserExtension[], b: SlidevPreparserExtension[]) => a.concat(b)
return await loadSetups(roots, 'preparser.ts', filepath, [], false, mergeArrays)
return await loadSetups(roots, 'preparser.ts', { filepath, headmatter }, [], false, mergeArrays)
})

const cli = yargs
Expand Down
2 changes: 1 addition & 1 deletion packages/slidev/node/plugins/setupNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function deepMerge(a: any, b: any, rootPath = '') {
return a
}

export async function loadSetups<T, R extends object>(roots: string[], name: string, arg: T, initial: R, merge = true, accumulate: (a: R, o: R) => R = undefined): Promise<R> {
export async function loadSetups<T, R extends object>(roots: string[], name: string, arg: T, initial: R, merge = true, accumulate?: (a: R, o: R) => R): Promise<R> {
let returns = initial
for (const root of roots) {
const path = resolve(root, 'setup', name)
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export interface SlidevPreparserExtension {
transformSlide?(content: string, frontmatter: any): Promise<string | undefined>
}

export type PreparserExtensionLoader = (addons: string[], filepath?: string) => Promise<SlidevPreparserExtension[]>
export type PreparserExtensionLoader = (headmatter?: Record<string, unknown>, filepath?: string) => Promise<SlidevPreparserExtension[]>

// internal type?
export type PreparserExtensionFromHeadmatter = (headmatter: any, exts: SlidevPreparserExtension[], filepath: string | undefined) => Promise<SlidevPreparserExtension[]>
export type PreparserExtensionFromHeadmatter = (headmatter: any, exts: SlidevPreparserExtension[], filepath?: string) => Promise<SlidevPreparserExtension[]>

export type RenderContext = 'slide' | 'overview' | 'presenter' | 'previewNext'
2 changes: 1 addition & 1 deletion test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ b
it('parse with-extension custom-separator', async () => {
const data = await parseWithExtension(`---
ga: bu
SEPARATOR
---
a @@v@@
SEPARATOR
Expand Down

0 comments on commit cf81a72

Please sign in to comment.