export interface TemplateContext { chapterNumber: number chapterTitle: string penName: string date: string volumeName: string previousChapterTitle: string outlineItem: string characterList: string } const VARS = [ 'chapterNumber', 'chapterTitle', 'penName', 'date', 'volumeName', 'previousChapterTitle', 'outlineItem', 'characterList' ] as const export function replaceChapterTemplate(body: string, ctx: TemplateContext): string { let out = body for (const key of VARS) { out = out.split(`{${key}}`).join(String(ctx[key])) } return out } export function plainTextToEditorHtml(text: string): string { const trimmed = text.trim() if (!trimmed) return '

' return trimmed .split(/\n{2,}/) .map((p) => `

${escapeHtml(p).replace(/\n/g, '
')}

`) .join('') } function escapeHtml(text: string): string { return text .replace(/&/g, '&') .replace(//g, '>') } export function bodyPlainToChapterHtml(bodyPlain: string): string { return plainTextToEditorHtml(bodyPlain) }