Skip to content

Commit 6baeb85

Browse files
authored
feat: load preparser also from theme (#2558)
1 parent a0c3857 commit 6baeb85

7 files changed

Lines changed: 25 additions & 25 deletions

File tree

packages/parser/src/fs.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import type { PreparserExtensionLoader, SlideInfo, SlidevData, SlidevMarkdown, SlidevPreparserExtension, SourceSlideInfo } from '@slidev/types'
1+
import type {
2+
PreparserExtensionLoader,
3+
SlideInfo,
4+
SlidevData,
5+
SlidevMarkdown,
6+
SlidevPreparserExtension,
7+
SourceSlideInfo,
8+
} from '@slidev/types'
29
import { existsSync } from 'node:fs'
310
import { readFile, writeFile } from 'node:fs/promises'
411
import { dirname, resolve } from 'node:path'
@@ -19,14 +26,19 @@ export function injectPreparserExtensionLoader(fn: PreparserExtensionLoader) {
1926
preparserExtensionLoader = fn
2027
}
2128

29+
export interface LoadRootsInfo {
30+
roots: string[]
31+
userRoot: string
32+
}
33+
2234
/**
2335
* Slidev data without config and themeMeta,
2436
* because config and themeMeta depends on the theme to be loaded.
2537
*/
2638
export type LoadedSlidevData = Omit<SlidevData, 'config' | 'themeMeta'>
2739

2840
export async function load(
29-
userRoot: string,
41+
options: LoadRootsInfo,
3042
filepath: string,
3143
sources: Record<string, string> | ((path: string) => Promise<string>) = {},
3244
mode?: string,
@@ -49,7 +61,7 @@ export async function load(
4961
hm = lines.slice(1, hEnd).join('\n')
5062
}
5163
const o = YAML.parse(hm) as Record<string, unknown> ?? {}
52-
extensions = await preparserExtensionLoader(o, filepath, mode)
64+
extensions = await preparserExtensionLoader(options.roots, o, filepath, mode)
5365
}
5466

5567
const markdownFiles: Record<string, SlidevMarkdown> = {}
@@ -93,7 +105,7 @@ export async function load(
93105
const [rawPath, rangeRaw] = slide.frontmatter.src.split('#')
94106
const path = slash(
95107
rawPath.startsWith('/')
96-
? resolve(userRoot, rawPath.substring(1))
108+
? resolve(options.userRoot, rawPath.substring(1))
97109
: resolve(dirname(slide.filepath), rawPath),
98110
)
99111

packages/slidev/node/cli.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { createServer } from './commands/serve'
1818
import { getThemeMeta, resolveTheme } from './integrations/themes'
1919
import { resolveOptions } from './options'
2020
import { parser } from './parser'
21-
import { getRoots, isInstalledGlobally, resolveEntry } from './resolver'
21+
import { isInstalledGlobally, resolveEntry } from './resolver'
2222
import setupPreparser from './setups/preparser'
2323
import { updateFrontmatterPatch } from './utils'
2424

@@ -157,7 +157,7 @@ cli.command(
157157
{
158158
async loadData(loadedSource) {
159159
const { data: oldData, entry } = options
160-
const loaded = await parser.load(options.userRoot, entry, loadedSource, 'dev')
160+
const loaded = await parser.load(options, entry, loadedSource, 'dev')
161161

162162
const themeRaw = theme || loaded.headmatter.theme as string || 'default'
163163
if (options.themeRaw !== themeRaw) {
@@ -427,8 +427,8 @@ cli.command(
427427
}),
428428
async ({ entry: entryRaw, dir, theme: themeInput }) => {
429429
const entry = await resolveEntry(entryRaw)
430-
const roots = await getRoots(entry)
431-
const data = await parser.load(roots.userRoot, entry)
430+
const options = await resolveOptions({ entry }, 'dev')
431+
const data = await parser.load(options, entry)
432432
let themeRaw = themeInput || data.headmatter.theme as string | null | undefined
433433
themeRaw = themeRaw === null ? 'none' : (themeRaw || 'default')
434434
if (themeRaw === 'none') {

packages/slidev/node/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function resolveOptions(
2222
): Promise<ResolvedSlidevOptions> {
2323
const entry = await resolveEntry(entryOptions.entry)
2424
const rootsInfo = await getRoots(entry)
25-
const loaded = await parser.load(rootsInfo.userRoot, entry, undefined, mode)
25+
const loaded = await parser.load({ userRoot: rootsInfo.userRoot, roots: [rootsInfo.userRoot] }, entry, undefined, mode)
2626

2727
// Load theme data first, because it may affect the config
2828
let themeRaw = entryOptions.theme || loaded.headmatter.theme as string | null | undefined

packages/slidev/node/setups/preparser.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import type { PreparserSetup } from '@slidev/types'
2-
import { uniq } from '@antfu/utils'
32
import { injectPreparserExtensionLoader } from '@slidev/parser/fs'
4-
import { resolveAddons } from '../integrations/addons'
5-
import { getRoots } from '../resolver'
63
import { loadSetups } from './load'
74

85
export default function setupPreparser() {
9-
injectPreparserExtensionLoader(async (headmatter: Record<string, unknown>, filepath: string, mode?: string) => {
10-
// Ensure addons is an array or an empty array if undefined
11-
const addons = Array.isArray(headmatter?.addons) ? headmatter.addons as string[] : []
12-
13-
const { userRoot } = await getRoots()
14-
const roots = uniq([
15-
...await resolveAddons(addons),
16-
userRoot,
17-
])
18-
6+
injectPreparserExtensionLoader(async (roots: string[], headmatter: Record<string, unknown>, filepath: string, mode?: string) => {
197
const returns = await loadSetups<PreparserSetup>(roots, 'preparser.ts', [{ filepath, headmatter, mode }])
208
return returns.flat()
219
})

packages/types/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface SlidevPreparserExtension {
135135
transformNote?: (note: string | undefined, frontmatter: any) => Promise<string | undefined>
136136
}
137137

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

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

packages/vscode/src/projects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function removeProject(entry: string) {
195195

196196
async function loadProject(entry: string) {
197197
const userRoot = dirname(entry)
198-
return markRaw(await load(userRoot, entry, async (path: string) => {
198+
return markRaw(await load({ userRoot, roots: [userRoot] }, entry, async (path: string) => {
199199
const document = workspace.textDocuments.find(d => slash(d.uri.fsPath) === path)
200200
if (document) {
201201
return document.getText()

test/parser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('md parser', () => {
2929

3030
for (const file of files) {
3131
it(basename(file), async () => {
32-
const data = await load(userRoot, file)
32+
const data = await load({ userRoot, roots: [userRoot] }, file)
3333

3434
expect(stringify(data.entry).trim()).toEqual(replaceCRLF(data.entry.raw.trim()))
3535

0 commit comments

Comments
 (0)