UNPKG

histoire

Version:

A fast story book powered by Vite

38 lines (37 loc) • 1.54 kB
import { relative } from 'pathe'; import { createPath } from '../tree.js'; /** Normalize layout while retaining whether author/project supplied it. */ export function finalizeCollectedStoryFile(storyFile, ctx, storyData = storyFile.story) { if (!storyData) { return; } if (ctx.config.defaultStoryProps) { for (const key in ctx.config.defaultStoryProps) { if (storyData[key] == null) { storyData[key] = ctx.config.defaultStoryProps[key]; } } } // Finalization can repeat; its own fallback must never become an author declaration. storyData.layoutExplicit ??= !!storyData.layout; storyData.layout ??= { type: 'single' }; storyFile.id = storyData.id; storyFile.story = storyData; storyFile.treeFile = { title: storyData.title, path: relative(ctx.root, storyFile.path), }; storyFile.treePath = createPath(ctx.config, storyFile.treeFile); storyFile.story.title = storyFile.treePath[storyFile.treePath.length - 1]; } /** Applies a cached collection outcome to a story file, as if it had just been collected. */ export function restoreCollectedStoryFile(storyFile, ctx, outcome) { if (outcome?.status === 'collected' && outcome.story) { // Finalization mutates its input; the cached copy must stay raw. finalizeCollectedStoryFile(storyFile, ctx, structuredClone(outcome.story)); return; } delete storyFile.story; delete storyFile.treePath; delete storyFile.treeFile; }