Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(theme): optimize the startup time of auto-frontmatter #185

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions theme/src/node/autoFrontmatter/baseFrontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { format } from 'date-fns'
import type {
AutoFrontmatter,
AutoFrontmatterObject,
} from '../../shared/index.js'

export function createBaseFrontmatter(options: AutoFrontmatter): AutoFrontmatterObject {
const res: AutoFrontmatterObject = {}

if (options.createTime !== false) {
res.createTime = (formatTime: string, { createTime }, data) => {
if (formatTime)
return formatTime
if (data.friends || data.pageLayout === 'friends')
return
return format(new Date(createTime), 'yyyy/MM/dd HH:mm:ss')
}
}

return res
}
50 changes: 46 additions & 4 deletions theme/src/node/autoFrontmatter/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chokidar from 'chokidar'
import { createFilter } from 'create-filter'
import grayMatter from 'gray-matter'
import jsonToYaml from 'json2yaml'
import { fs, hash } from 'vuepress/utils'
import { fs, hash, path } from 'vuepress/utils'
import type { App } from 'vuepress'
import { getThemeConfig } from '../loadConfig/index.js'
import { readMarkdown, readMarkdownList } from './readFile.js'
Expand All @@ -16,6 +16,8 @@ import type {
PlumeThemeLocaleOptions,
} from '../../shared/index.js'

const CACHE_FILE = 'markdown/auto-frontmatter.json'

export interface Generate {
globFilter: (id?: string) => boolean
global: AutoFrontmatterObject
Expand All @@ -24,6 +26,9 @@ export interface Generate {
filter: (id?: string) => boolean
frontmatter: AutoFrontmatterObject
}[]
cache: Record<string, string>
checkCache: (id: string) => boolean
updateCache: (app: App) => Promise<void>
}

let generate: Generate | null = null
Expand Down Expand Up @@ -53,21 +58,46 @@ export function initAutoFrontmatter(
}
})

const cache: Record<string, string> = {}

function checkCache(filepath: string): boolean {
const stats = fs.statSync(filepath)

if (cache[filepath] && cache[filepath] === stats.mtimeMs.toString())
return false
cache[filepath] = stats.mtimeMs.toString()
return true
}

async function updateCache(app: App): Promise<void> {
await fs.writeFile(app.dir.cache(CACHE_FILE), JSON.stringify(cache), 'utf-8')
}

generate = {
globFilter,
global: globalConfig,
rules,
cache,
checkCache,
updateCache,
}
}

export async function generateAutoFrontmatter(app: App) {
if (!generate)
return
const markdownList = await readMarkdownList(app.dir.source(), generate.globFilter)

const cachePath = app.dir.cache(CACHE_FILE)
if (fs.existsSync(cachePath)) {
generate.cache = JSON.parse(await fs.readFile(cachePath, 'utf-8'))
}
const markdownList = await readMarkdownList(app, generate)
await promiseParallel(
markdownList.map(file => () => generator(file)),
64,
)

await generate.updateCache(app)
}

export async function watchAutoFrontmatter(app: App, watchers: any[]) {
Expand All @@ -88,6 +118,14 @@ export async function watchAutoFrontmatter(app: App, watchers: any[]) {
await generator(file)
})

watcher.on('change', async (relativePath) => {
const enabled = getThemeConfig().autoFrontmatter !== false
if (!generate!.globFilter(relativePath) || !enabled)
return
if (generate!.checkCache(path.join(app.dir.source(), relativePath)))
await generate!.updateCache(app)
})

watchers.push(watcher)
}

Expand All @@ -103,8 +141,11 @@ async function generator(file: AutoFrontmatterMarkdownFile): Promise<void> {
const beforeHash = hash(data)

for (const key in formatter) {
const value = await formatter[key](data[key], file, data)
data[key] = value ?? data[key]
const value = (await formatter[key](data[key], file, data)) ?? data[key]
if (typeof value !== 'undefined')
data[key] = value
else
delete data[key]
}

if (beforeHash === hash(data))
Expand All @@ -121,6 +162,7 @@ async function generator(file: AutoFrontmatterMarkdownFile): Promise<void> {
const newContent = yaml ? `${yaml}---\n${content}` : content

await fs.promises.writeFile(filepath, newContent, 'utf-8')
generate.checkCache(filepath)
}
catch (e) {
console.error(e)
Expand Down
17 changes: 12 additions & 5 deletions theme/src/node/autoFrontmatter/readFile.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import fg from 'fast-glob'
import { fs, path } from 'vuepress/utils'
import type { App } from 'vuepress'
import type { AutoFrontmatterMarkdownFile } from '../../shared/index.js'
import type { Generate } from './generator.js'

export async function readMarkdownList(
sourceDir: string,
filter: (id: string) => boolean,
app: App,
{ globFilter, checkCache }: Generate,
): Promise<AutoFrontmatterMarkdownFile[]> {
const source = app.dir.source()
const files: string[] = await fg(['**/*.md'], {
cwd: sourceDir,
cwd: source,
ignore: ['node_modules', '.vuepress'],
})

return await Promise.all(
files
.filter(filter)
.map(file => readMarkdown(sourceDir, file)),
.filter((id) => {
if (!globFilter(id))
return false
return checkCache(path.join(source, id))
})
.map(file => readMarkdown(source, file)),
)
}

Expand Down
51 changes: 51 additions & 0 deletions theme/src/node/autoFrontmatter/resolveLinkBySidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { pathJoin } from '../utils/index.js'
import type { SidebarItem } from '../../shared/index.js'

export function resolveLinkBySidebar(
sidebar: 'auto' | (string | SidebarItem)[],
_prefix: string,
) {
const res: Record<string, string> = {}

if (sidebar === 'auto') {
return res
}

for (const item of sidebar) {
if (typeof item !== 'string') {
const { prefix, dir = '', link = '/', items, text = '' } = item
getSidebarLink(items, link, text, pathJoin(_prefix, prefix || dir), res)
}
}
return res
}

function getSidebarLink(items: 'auto' | (string | SidebarItem)[] | undefined, link: string, text: string, dir = '', res: Record<string, string> = {}) {
if (items === 'auto')
return

if (!items) {
res[pathJoin(dir, `${text}.md`)] = link
return
}

for (const item of items) {
if (typeof item === 'string') {
if (!link)
continue
if (item) {
res[pathJoin(dir, `${item}.md`)] = link
}
else {
res[pathJoin(dir, 'README.md')] = link
res[pathJoin(dir, 'index.md')] = link
res[pathJoin(dir, 'readme.md')] = link
}
res[dir] = link
}
else {
const { prefix, dir: subDir = '', link: subLink = '/', items: subItems, text: subText = '' } = item
getSidebarLink(subItems, pathJoin(link, subLink), subText, pathJoin(prefix || dir, subDir), res)
}
}
}
Loading