Skip to content

Commit

Permalink
feat: allow preparsers to access the run mode (dev,export,build) (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
twitwi committed Feb 27, 2024
1 parent 4b2f4b4 commit f5d80d8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/parser/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function injectPreparserExtensionLoader(fn: PreparserExtensionLoader) {
*/
export type LoadedSlidevData = Omit<SlidevData, 'config' | 'themeMeta'>

export async function load(userRoot: string, filepath: string, content?: string): Promise<LoadedSlidevData> {
export async function load(userRoot: string, filepath: string, content?: string, mode?: string): Promise<LoadedSlidevData> {
const markdown = content ?? await fs.readFile(filepath, 'utf-8')

let extensions: SlidevPreparserExtension[] | undefined
Expand All @@ -36,7 +36,7 @@ export async function load(userRoot: string, filepath: string, content?: string)
hm = lines.slice(1, hEnd).join('\n')
}
const o = YAML.load(hm) as Record<string, unknown> ?? {}
extensions = await preparserExtensionLoader(o, filepath)
extensions = await preparserExtensionLoader(o, filepath, mode)
}

const markdownFiles: Record<string, SlidevMarkdown> = {}
Expand Down
6 changes: 3 additions & 3 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
'theme',
]

injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, filepath?: string) => {
injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, filepath?: string, mode?: string) => {
const addons = headmatter?.addons as string[] ?? []
const { clientRoot, userRoot } = await getRoots()
const roots = uniq([
Expand All @@ -47,7 +47,7 @@ injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, file
...await resolveAddons(addons),
])
const mergeArrays = (a: SlidevPreparserExtension[], b: SlidevPreparserExtension[]) => a.concat(b)
return await loadSetups(clientRoot, roots, 'preparser.ts', { filepath, headmatter }, [], mergeArrays)
return await loadSetups(clientRoot, roots, 'preparser.ts', { filepath, headmatter, mode }, [], mergeArrays)
})

const cli = yargs(process.argv.slice(2))
Expand Down Expand Up @@ -144,7 +144,7 @@ cli.command(
{
async loadData() {
const { data: oldData, entry } = options
const loaded = await parser.load(options.userRoot, entry)
const loaded = await parser.load(options.userRoot, entry, undefined, 'dev')

const themeRaw = theme || loaded.headmatter.theme as string || 'default'
if (options.themeRaw !== themeRaw) {
Expand Down
2 changes: 1 addition & 1 deletion packages/slidev/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function resolveOptions(
): Promise<ResolvedSlidevOptions> {
const rootsInfo = await getRoots()
const entry = await resolveEntry(options.entry || 'slides.md', rootsInfo)
const loaded = await parser.load(rootsInfo.userRoot, entry)
const loaded = await parser.load(rootsInfo.userRoot, entry, undefined, mode)

// Load theme data first, because it may affect the config
const themeRaw = options.theme || loaded.headmatter.theme as string || 'default'
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface SlidevPreparserExtension {
transformSlide?: (content: string, frontmatter: any) => Promise<string | undefined>
}

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

export type RenderContext = 'none' | 'slide' | 'overview' | 'presenter' | 'previewNext'

Expand Down

0 comments on commit f5d80d8

Please sign in to comment.