diff --git a/package.json b/package.json index 3c499689eef8..0930b698895d 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,7 @@ "fast-glob": "^3.2.7", "fs-extra": "^10.0.0", "gray-matter": "^4.0.3", + "is-plain-object": "^5.0.0", "lint-staged": "^11.0.0", "lru-cache": "^6.0.0", "markdown-it": "^12.3.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0a2e37026ddd..274bd08c5d33 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,7 @@ importers: fast-glob: ^3.2.7 fs-extra: ^10.0.0 gray-matter: ^4.0.3 + is-plain-object: ^5.0.0 lint-staged: ^11.0.0 lru-cache: ^6.0.0 markdown-it: ^12.3.2 @@ -114,6 +115,7 @@ importers: fast-glob: 3.2.7 fs-extra: 10.0.0 gray-matter: 4.0.3 + is-plain-object: 5.0.0 lint-staged: 11.1.2_supports-color@9.2.2 lru-cache: 6.0.0 markdown-it: 12.3.2 @@ -2564,6 +2566,11 @@ packages: engines: {node: '>=0.10.0'} dev: true + /is-plain-object/5.0.0: + resolution: {integrity: sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==} + engines: {node: '>=0.10.0'} + dev: true + /is-reference/1.2.1: resolution: {integrity: sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==} dependencies: diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index eeb567a30dac..7996da34955f 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -9,6 +9,7 @@ import { deeplyParseHeader } from './utils/parseHeader' import { getGitTimestamp } from './utils/getGitTimestamp' import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown' import _debug from 'debug' +import { isPlainObject } from 'is-plain-object' const debug = _debug('vitepress:md') const cache = new LRUCache({ max: 1024 }) @@ -21,6 +22,22 @@ export interface MarkdownCompileResult { includes: string[] } +/** + * copied from https://github.com/TanStack/query/blob/cca1bbfb9a/src/core/utils.ts + */ +export function hashObject(queryKey: any): string { + return JSON.stringify(queryKey, (_, val) => + isPlainObject(val) + ? Object.keys(val) + .sort() + .reduce((result, key) => { + result[key] = val[key] + return result + }, {} as any) + : val + ) +} + export async function createMarkdownToVueRenderFn( srcDir: string, options: MarkdownOptions = {}, @@ -43,8 +60,9 @@ export async function createMarkdownToVueRenderFn( ): Promise => { const relativePath = slash(path.relative(srcDir, file)) const dir = path.dirname(file) + const cacheKey = hashObject({ src, file, publicDir }) - const cached = cache.get(src) + const cached = cache.get(cacheKey) if (cached) { debug(`[cache hit] ${relativePath}`) return cached @@ -148,7 +166,8 @@ export async function createMarkdownToVueRenderFn( deadLinks, includes } - cache.set(src, result) + + cache.set(cacheKey, result) return result } }