context.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * 草稿干净上下文(插件规格 §5):写稿只收确认细纲 + 材料包,不含审核/讨论。
  3. */
  4. import * as fs from 'node:fs'
  5. import * as path from 'node:path'
  6. import {
  7. hardConstraintCore,
  8. parseOutline,
  9. paths,
  10. type ChapterKey,
  11. } from '@webnovel/core'
  12. import { loadMaterialPackage } from '@webnovel/core'
  13. export interface DraftContext {
  14. readonly 细纲: string
  15. readonly 材料段: Readonly<Record<string, string>>
  16. readonly 硬约束: readonly string[]
  17. }
  18. function readText(root: string, rel: string): string | null {
  19. try {
  20. return fs.readFileSync(path.join(root, rel), 'utf-8')
  21. } catch {
  22. return null
  23. }
  24. }
  25. export function loadDraftContext(bookRoot: string, key: Pick<ChapterKey, '卷' | '章' | '章名'>): DraftContext {
  26. const 细纲Rel = paths.确认细纲(key.卷, key.章, key.章名)
  27. const 细纲 = readText(bookRoot, 细纲Rel) ?? ''
  28. const parsed = 细纲 === '' ? null : parseOutline(细纲)
  29. const 硬约束 = parsed === null
  30. ? []
  31. : parsed.constraints.filter((c) => c.mark === '硬').map((c) => hardConstraintCore(c.line)).filter((s) => s !== '')
  32. const materials = loadMaterialPackage(bookRoot, key)
  33. const 材料段: Record<string, string> = { ...materials.段 }
  34. const 清单 = materials.清单
  35. if (清单 !== null) 材料段['材料清单'] = 清单
  36. return { 细纲, 材料段, 硬约束 }
  37. }