diff --git a/package.json b/package.json index bfc8ec25..9883dc6f 100644 --- a/package.json +++ b/package.json @@ -162,7 +162,6 @@ "gray-matter": "^4.0.3", "lint-staged": "^15.2.0", "lodash.template": "^4.5.0", - "lru-cache": "^10.1.0", "markdown-it": "^14.0.0", "markdown-it-anchor": "^8.6.7", "markdown-it-attrs": "^4.1.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba4c0aaf..02114dcc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: false - excludeLinksFromLockfile: false - overrides: ora>string-width: ^5 @@ -186,9 +182,6 @@ importers: lodash.template: specifier: ^4.5.0 version: 4.5.0 - lru-cache: - specifier: ^10.1.0 - version: 10.1.0 markdown-it: specifier: ^14.0.0 version: 14.0.0 @@ -4758,3 +4751,7 @@ packages: resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==} engines: {node: '>=12.20'} dev: true + +settings: + autoInstallPeers: false + excludeLinksFromLockfile: false diff --git a/src/client/theme-default/components/VPLocalSearchBox.vue b/src/client/theme-default/components/VPLocalSearchBox.vue index 2f49250c..b1c8a253 100644 --- a/src/client/theme-default/components/VPLocalSearchBox.vue +++ b/src/client/theme-default/components/VPLocalSearchBox.vue @@ -29,7 +29,7 @@ import { import type { ModalTranslations } from '../../../../types/local-search' import { pathToFile } from '../../app/utils' import { useData } from '../composables/data' -import { LRUCache } from '../support/lru' +import { LRUCache } from '../../shared' import { createSearchTranslate } from '../support/translation' const emit = defineEmits<{ diff --git a/src/client/theme-default/support/lru.ts b/src/client/theme-default/support/lru.ts deleted file mode 100644 index b6673512..00000000 --- a/src/client/theme-default/support/lru.ts +++ /dev/null @@ -1,37 +0,0 @@ -// adapted from https://stackoverflow.com/a/46432113/11613622 - -export class LRUCache { - private max: number - private cache: Map - - constructor(max: number = 10) { - this.max = max - this.cache = new Map() - } - - get(key: K): V | undefined { - let item = this.cache.get(key) - if (item !== undefined) { - // refresh key - this.cache.delete(key) - this.cache.set(key, item) - } - return item - } - - set(key: K, val: V): void { - // refresh key - if (this.cache.has(key)) this.cache.delete(key) - // evict oldest - else if (this.cache.size === this.max) this.cache.delete(this.first()!) - this.cache.set(key, val) - } - - first(): K | undefined { - return this.cache.keys().next().value - } - - clear(): void { - this.cache.clear() - } -} diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 6901f464..1f89c2d9 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -1,7 +1,6 @@ import { resolveTitleFromToken } from '@mdit-vue/shared' import _debug from 'debug' import fs from 'fs-extra' -import { LRUCache } from 'lru-cache' import path from 'path' import type { SiteConfig } from './config' import { @@ -12,6 +11,7 @@ import { import { EXTERNAL_URL_RE, slash, + LRUCache, type HeadConfig, type MarkdownEnv, type PageData @@ -20,7 +20,7 @@ import { getGitTimestamp } from './utils/getGitTimestamp' import { processIncludes } from './utils/processIncludes' const debug = _debug('vitepress:md') -const cache = new LRUCache({ max: 1024 }) +const cache = new LRUCache(1024) export interface MarkdownCompileResult { vueSrc: string diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 9dd05e34..6011e810 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -174,3 +174,58 @@ export function sanitizeFileName(name: string): string { export function slash(p: string): string { return p.replace(/\\/g, '/') } + +// adapted from https://stackoverflow.com/a/46432113/11613622 + +export class LRUCache { + private max: number + private cache: Map + + constructor(max: number = 10) { + this.max = max + this.cache = new Map() + } + + get(key: K): V | undefined { + let item = this.cache.get(key) + if (item !== undefined) { + // refresh key + this.cache.delete(key) + this.cache.set(key, item) + } + return item + } + + set(key: K, val: V): void { + // refresh key + if (this.cache.has(key)) this.cache.delete(key) + // evict oldest + else if (this.cache.size === this.max) this.cache.delete(this.first()!) + this.cache.set(key, val) + } + + first(): K | undefined { + return this.cache.keys().next().value + } + + find(fn: (v: V, k: K, self: LRUCache) => boolean): V | undefined { + for (const [key, value] of this.cache.entries()) { + if (fn(value, key, this)) { + return value + } + } + return undefined + } + + delete(key: K): boolean { + if (this.cache.has(key)) { + const deleted = this.cache.delete(key) + return deleted + } + return false + } + + clear(): void { + this.cache.clear() + } +}