UNPKG

histoire

Version:

A fast story book powered by Vite

30 lines (29 loc) • 1.53 kB
/** Clone mutable plain metadata, retaining opaque handles/functions outside that data graph. */ function cloneMetadata(value, seen = new WeakMap()) { if (!value || typeof value !== 'object') return value; const prototype = Object.getPrototypeOf(value); if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) return value; if (seen.has(value)) return seen.get(value); const copy = Array.isArray(value) ? [] : Object.create(prototype); seen.set(value, copy); for (const [key, item] of Object.entries(value)) copy[key] = cloneMetadata(item, seen); return copy; } /** Detach project link metadata; runner filters targets after docs resolve neighbors. */ export function createTestContextSnapshot(ctx) { const storyFiles = ctx.storyFiles .map(file => ({ ...file, treePath: file.treePath ? [...file.treePath] : undefined, story: cloneMetadata(file.story), treeFile: cloneMetadata(file.treeFile) })); const markdownFiles = ctx.markdownFiles .map(file => ({ ...file, frontmatter: cloneMetadata(file.frontmatter) })); for (const file of storyFiles) { file.markdownFile = markdownFiles.find(markdown => markdown.absolutePath === file.markdownFile?.absolutePath); } for (const file of markdownFiles) { file.storyFile = storyFiles.find(story => story.path === file.storyFile?.path); } return { ...ctx, storyFiles, markdownFiles, registeredCommands: [...(ctx.registeredCommands ?? [])] }; }