UNPKG

@shikijs/vitepress-twoslash

Version:

Enable Twoslash support in VitePress

30 lines (27 loc) 1.01 kB
import { createHash } from 'node:crypto'; import { writeFileSync, existsSync, readFileSync, mkdirSync } from 'node:fs'; import { resolve, join } from 'node:path'; import process from 'node:process'; function createFileSystemTypesCache(options = {}) { const dir = resolve(process.cwd(), options.dir ?? ".vitepress/cache/twoslash"); return { init() { mkdirSync(dir, { recursive: true }); }, read(code) { const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12); const filePath = join(dir, `${hash}.json`); if (!existsSync(filePath)) { return null; } return JSON.parse(readFileSync(filePath, { encoding: "utf-8" })); }, write(code, data) { const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12); const filePath = join(dir, `${hash}.json`); const json = JSON.stringify(data); writeFileSync(filePath, json, { encoding: "utf-8" }); } }; } export { createFileSystemTypesCache };