dexin-content 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -112
- package/cli/build.ts +4 -4
- package/cli/main.ts +197 -207
- package/core/artifact/store.ts +2 -2
- package/core/parser/markdown.ts +343 -327
- package/index.ts +57 -61
- package/package.json +2 -3
- package/core/validator/integrity.ts +0 -199
- package/domains/document/identity.ts +0 -27
- package/domains/document/index.ts +0 -5
- package/domains/document/parser.ts +0 -105
package/index.ts
CHANGED
|
@@ -1,61 +1,57 @@
|
|
|
1
|
-
// dexin-content — Universal content compilation layer
|
|
2
|
-
// Re-exports core types and interfaces for host consumption.
|
|
3
|
-
// Domain parsers are injected by the host via DomainParserRegistry.
|
|
4
|
-
|
|
5
|
-
export type {
|
|
6
|
-
DomainName, DocumentIdentity, Schema, Artifact, ParseError,
|
|
7
|
-
DomainParser, ParseContext,
|
|
8
|
-
Meta, Identity, StructuredIdentity, TEX_INLINE_TYPE
|
|
9
|
-
} from './core/types/index'
|
|
10
|
-
|
|
11
|
-
// LessonAST 类型
|
|
12
|
-
export type {
|
|
13
|
-
LessonContent, Block, BlockType,
|
|
14
|
-
ParagraphBlock, HeadingBlock, QuoteBlock, HintBlock,
|
|
15
|
-
DefinitionBlock, ExampleBlock, QuestionBlock,
|
|
16
|
-
ImageBlock, CodeBlock, FormulaBlock, ListBlock,
|
|
17
|
-
TableBlock, TableCell, SectionBlock, DividerBlock,
|
|
18
|
-
ContainerBlock, ExerciseContent,
|
|
19
|
-
Inline, TextInline, BoldInline, ItalicInline,
|
|
20
|
-
CodeInline, LinkInline, FormulaInline, MathInline,
|
|
21
|
-
BaseBlock
|
|
22
|
-
} from './core/types/lessonAST'
|
|
23
|
-
|
|
24
|
-
export {
|
|
25
|
-
BLOCK_TYPES, HINT_LEVELS, HEADING_LEVELS
|
|
26
|
-
} from './core/types/lessonAST'
|
|
27
|
-
|
|
28
|
-
export { DomainParserRegistry, compile } from './core/compiler/compiler'
|
|
29
|
-
export type { CompileInput, CompileResult } from './core/compiler/compiler'
|
|
30
|
-
export { normaliseRel, buildDocumentIdentity as buildDocPageIdentity, readSourceFile } from './core/compiler/discovery'
|
|
31
|
-
export { splitFrontmatter, parseFrontmatter, validateSchema } from './core/parser/frontmatter'
|
|
32
|
-
export { parseDocument } from './core/parser/markdown'
|
|
33
|
-
|
|
34
|
-
export type { ArtifactStore, ContentIndex, IndexEntry } from './core/artifact/store'
|
|
35
|
-
export { createFsArtifactStore, createMemoryArtifactStore } from './core/artifact/store'
|
|
36
|
-
|
|
37
|
-
export type { CollectionDefinition, SourceAdapter, ResolvedCollection } from './core/artifact/collection'
|
|
38
|
-
export { defineCollection, resolveCollections, discover, compileCollections } from './core/artifact/collection'
|
|
39
|
-
|
|
40
|
-
export type { ContentQuery as ContentQueryType } from './core/artifact/query'
|
|
41
|
-
export { ContentQuery } from './core/artifact/query'
|
|
42
|
-
|
|
43
|
-
export { toCanonicalJSON, firstDiff, sortKeysDeep, stripUnderscoreKeysGolden } from './core/artifact/diff'
|
|
44
|
-
|
|
45
|
-
// Lesson domain
|
|
46
|
-
export { lessonParser } from './domains/lesson/parser'
|
|
47
|
-
export { buildLessonIdentity } from './domains/lesson/identity'
|
|
48
|
-
export { mapBlocks, lessonError, KNOWN_CONTAINERS } from './domains/lesson/blocks'
|
|
49
|
-
|
|
50
|
-
//
|
|
51
|
-
export {
|
|
52
|
-
export {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export
|
|
56
|
-
export {
|
|
57
|
-
export
|
|
58
|
-
export { flowPackage } from './cli/package'
|
|
59
|
-
export { flowValidate } from './cli/validate'
|
|
60
|
-
export { flowCheck } from './cli/check'
|
|
61
|
-
export { flowServe } from './cli/serve'
|
|
1
|
+
// dexin-content — Universal content compilation layer
|
|
2
|
+
// Re-exports core types and interfaces for host consumption.
|
|
3
|
+
// Domain parsers are injected by the host via DomainParserRegistry.
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
DomainName, DocumentIdentity, Schema, Artifact, ParseError,
|
|
7
|
+
DomainParser, ParseContext,
|
|
8
|
+
Meta, Identity, StructuredIdentity, TEX_INLINE_TYPE
|
|
9
|
+
} from './core/types/index'
|
|
10
|
+
|
|
11
|
+
// LessonAST 类型
|
|
12
|
+
export type {
|
|
13
|
+
LessonContent, Block, BlockType,
|
|
14
|
+
ParagraphBlock, HeadingBlock, QuoteBlock, HintBlock,
|
|
15
|
+
DefinitionBlock, ExampleBlock, QuestionBlock,
|
|
16
|
+
ImageBlock, CodeBlock, FormulaBlock, ListBlock,
|
|
17
|
+
TableBlock, TableCell, SectionBlock, DividerBlock,
|
|
18
|
+
ContainerBlock, ExerciseContent,
|
|
19
|
+
Inline, TextInline, BoldInline, ItalicInline,
|
|
20
|
+
CodeInline, LinkInline, FormulaInline, MathInline,
|
|
21
|
+
BaseBlock
|
|
22
|
+
} from './core/types/lessonAST'
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
BLOCK_TYPES, HINT_LEVELS, HEADING_LEVELS
|
|
26
|
+
} from './core/types/lessonAST'
|
|
27
|
+
|
|
28
|
+
export { DomainParserRegistry, compile } from './core/compiler/compiler'
|
|
29
|
+
export type { CompileInput, CompileResult } from './core/compiler/compiler'
|
|
30
|
+
export { normaliseRel, buildDocumentIdentity as buildDocPageIdentity, readSourceFile } from './core/compiler/discovery'
|
|
31
|
+
export { splitFrontmatter, parseFrontmatter, validateSchema } from './core/parser/frontmatter'
|
|
32
|
+
export { parseDocument } from './core/parser/markdown'
|
|
33
|
+
|
|
34
|
+
export type { ArtifactStore, ContentIndex, IndexEntry } from './core/artifact/store'
|
|
35
|
+
export { createFsArtifactStore, createMemoryArtifactStore } from './core/artifact/store'
|
|
36
|
+
|
|
37
|
+
export type { CollectionDefinition, SourceAdapter, ResolvedCollection } from './core/artifact/collection'
|
|
38
|
+
export { defineCollection, resolveCollections, discover, compileCollections } from './core/artifact/collection'
|
|
39
|
+
|
|
40
|
+
export type { ContentQuery as ContentQueryType } from './core/artifact/query'
|
|
41
|
+
export { ContentQuery } from './core/artifact/query'
|
|
42
|
+
|
|
43
|
+
export { toCanonicalJSON, firstDiff, sortKeysDeep, stripUnderscoreKeysGolden } from './core/artifact/diff'
|
|
44
|
+
|
|
45
|
+
// Lesson domain
|
|
46
|
+
export { lessonParser } from './domains/lesson/parser'
|
|
47
|
+
export { buildLessonIdentity } from './domains/lesson/identity'
|
|
48
|
+
export { mapBlocks, lessonError, KNOWN_CONTAINERS } from './domains/lesson/blocks'
|
|
49
|
+
|
|
50
|
+
// CLI
|
|
51
|
+
export type { FlowBuildInput, FlowBuildResult } from './cli/build'
|
|
52
|
+
export { flowBuild } from './cli/build'
|
|
53
|
+
export type { FlowManifest, OutLesson, OutPage, OutPackage } from './cli/package'
|
|
54
|
+
export { flowPackage } from './cli/package'
|
|
55
|
+
export { flowValidate } from './cli/validate'
|
|
56
|
+
export { flowCheck } from './cli/check'
|
|
57
|
+
export { flowServe } from './cli/serve'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dexin-content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Open content toolchain — Markdown → LessonAST/ContentPackage compiler, CLI and validator (dexinlabs ecosystem).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown",
|
|
@@ -53,8 +53,7 @@
|
|
|
53
53
|
"./cli/check": "./cli/check.ts",
|
|
54
54
|
"./core/discovery": "./core/compiler/discovery.ts",
|
|
55
55
|
"./core/types/lessonAST": "./core/types/lessonAST.ts",
|
|
56
|
-
"./domains/lesson": "./domains/lesson/index.ts"
|
|
57
|
-
"./domains/document": "./domains/document/index.ts"
|
|
56
|
+
"./domains/lesson": "./domains/lesson/index.ts"
|
|
58
57
|
},
|
|
59
58
|
"scripts": {
|
|
60
59
|
"typecheck": "tsc --noEmit",
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
// ─────────────────────────────────────────────────────────────
|
|
2
|
-
// dexin-content/runtime-check.ts — Runtime flow (B1-SPEC T5.3 / ARCH-REPORT §8)
|
|
3
|
-
//
|
|
4
|
-
// Reads pre-compiled artifacts from output/dexin-content/ via ContentQuery.
|
|
5
|
-
// Asserts structural correctness of the read-back artifacts.
|
|
6
|
-
//
|
|
7
|
-
// ZERO MARKDOWN PARSING PROOF (B1-SPEC T5.3):
|
|
8
|
-
// This module imports ONLY from:
|
|
9
|
-
// - ./store (fs/memory store + ContentIndex types; markdown-free)
|
|
10
|
-
// - ./query (ContentQuery; markdown-free)
|
|
11
|
-
// - ./core/types (type-only imports, erased at compile time)
|
|
12
|
-
// - node:* (built-in modules)
|
|
13
|
-
// It does NOT import: markdown, remark, unified, core/compiler,
|
|
14
|
-
// core/frontmatter, core/markdown, domain/*, collection (which
|
|
15
|
-
// transitively imports the compiler). The runtime path reads
|
|
16
|
-
// pre-compiled artifacts from output/ and never reparses source.
|
|
17
|
-
//
|
|
18
|
-
// Transitive dependency audit:
|
|
19
|
-
// store.ts → node:fs/promises, node:path, ./core/types (types), ./diff
|
|
20
|
-
// query.ts → ./core/types (types), ./store
|
|
21
|
-
// diff.ts → (zero imports — pure JSON/Object operations)
|
|
22
|
-
// No markdown/remark/unified anywhere in the runtime import graph.
|
|
23
|
-
//
|
|
24
|
-
// grep-zero: no business vocabulary appears anywhere in this
|
|
25
|
-
// module — including error messages.
|
|
26
|
-
// ─────────────────────────────────────────────────────────────
|
|
27
|
-
|
|
28
|
-
import { fileURLToPath } from 'node:url'
|
|
29
|
-
import { join } from 'node:path'
|
|
30
|
-
|
|
31
|
-
import type { PositiveArtifact } from '../types'
|
|
32
|
-
import { createFsArtifactStore, createMemoryArtifactStore, type ArtifactStore } from '../artifact/store'
|
|
33
|
-
import { ContentQuery } from '../artifact/query'
|
|
34
|
-
|
|
35
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
36
|
-
const ROOT = join(__filename, '..', '..')
|
|
37
|
-
const OUTPUT_DIR = join(ROOT, 'output', 'dexin-content')
|
|
38
|
-
|
|
39
|
-
// ── Helpers ──────────────────────────────────────────────
|
|
40
|
-
|
|
41
|
-
let pass = 0
|
|
42
|
-
let fail = 0
|
|
43
|
-
const errors: string[] = []
|
|
44
|
-
|
|
45
|
-
function ok (label: string): void {
|
|
46
|
-
pass++
|
|
47
|
-
console.log(` ✅ ${label}`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function ko (label: string, detail: string): void {
|
|
51
|
-
fail++
|
|
52
|
-
errors.push(`${label}: ${detail}`)
|
|
53
|
-
console.log(` ❌ ${label}: ${detail}`)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function assertArtifact (label: string, doc: PositiveArtifact | null, expectDomain: string, expectId: string): void {
|
|
57
|
-
if (doc === null) { ko(label, 'returned null'); return }
|
|
58
|
-
if (doc.domain !== expectDomain) { ko(label, `domain=${doc.domain} (expected ${expectDomain})`); return }
|
|
59
|
-
if (doc.fixture !== expectId) { ko(label, `fixture=${doc.fixture} (expected ${expectId})`); return }
|
|
60
|
-
if (!doc.content?.blocks?.length) { ko(label, 'content.blocks empty or missing'); return }
|
|
61
|
-
if (!doc.identity) { ko(label, 'identity missing'); return }
|
|
62
|
-
if (!doc.meta) { ko(label, 'meta missing'); return }
|
|
63
|
-
ok(label)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// ── R1: byPath on legal/privacy + about ───────────────────
|
|
67
|
-
|
|
68
|
-
async function checkR1 (query: ContentQuery): Promise<void> {
|
|
69
|
-
console.log('── R1: query.byPath ──')
|
|
70
|
-
const privacy = await query.byPath('/legal/privacy')
|
|
71
|
-
assertArtifact('R1 byPath(/legal/privacy)', privacy, 'document', 'legal/privacy')
|
|
72
|
-
|
|
73
|
-
const about = await query.byPath('/about')
|
|
74
|
-
assertArtifact('R1 byPath(/about)', about, 'document', 'about')
|
|
75
|
-
|
|
76
|
-
const notFound = await query.byPath('/nonexistent')
|
|
77
|
-
if (notFound !== null) ko('R1 byPath(/nonexistent)', 'should be null')
|
|
78
|
-
else ok('R1 byPath(/nonexistent) → null')
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// ── R2: byId ─────────────────────────────────────────────
|
|
82
|
-
|
|
83
|
-
async function checkR2 (query: ContentQuery): Promise<void> {
|
|
84
|
-
console.log('── R2: query.byId ──')
|
|
85
|
-
const byIdPrivacy = await query.byId('legal/privacy')
|
|
86
|
-
assertArtifact('R2 byId(legal/privacy)', byIdPrivacy, 'document', 'legal/privacy')
|
|
87
|
-
|
|
88
|
-
const byIdAbout = await query.byId('about')
|
|
89
|
-
assertArtifact('R2 byId(about)', byIdAbout, 'document', 'about')
|
|
90
|
-
|
|
91
|
-
const byIdMissing = await query.byId('does/not/exist')
|
|
92
|
-
if (byIdMissing !== null) ko('R2 byId(missing)', 'should be null')
|
|
93
|
-
else ok('R2 byId(missing) → null')
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// ── R3: collection ───────────────────────────────────────
|
|
97
|
-
|
|
98
|
-
async function checkR3 (query: ContentQuery): Promise<void> {
|
|
99
|
-
console.log('── R3: query.collection ──')
|
|
100
|
-
const legal = await query.collection('legal')
|
|
101
|
-
if (legal.length !== 1) ko('R3 collection(legal)', `len=${legal.length}`)
|
|
102
|
-
else if (legal[0].id !== 'legal/privacy') ko('R3 collection(legal)', `id=${legal[0].id}`)
|
|
103
|
-
else ok('R3 collection(legal) → 1 entry (legal/privacy)')
|
|
104
|
-
|
|
105
|
-
const about = await query.collection('about')
|
|
106
|
-
if (about.length !== 1) ko('R3 collection(about)', `len=${about.length}`)
|
|
107
|
-
else if (about[0].id !== 'about') ko('R3 collection(about)', `id=${about[0].id}`)
|
|
108
|
-
else ok('R3 collection(about) → 1 entry (about)')
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// ── R4: list with where/sort/limit ───────────────────────
|
|
112
|
-
|
|
113
|
-
async function checkR4 (query: ContentQuery): Promise<void> {
|
|
114
|
-
console.log('── R4: query.list ──')
|
|
115
|
-
const all = await query.list()
|
|
116
|
-
if (all.length !== 2) ko('R4 list()', `len=${all.length}`)
|
|
117
|
-
else ok('R4 list() → 2 entries')
|
|
118
|
-
|
|
119
|
-
const legalOnly = await query.list('legal')
|
|
120
|
-
if (legalOnly.length !== 1) ko('R4 list(legal)', `len=${legalOnly.length}`)
|
|
121
|
-
else ok('R4 list(legal) → 1 entry')
|
|
122
|
-
|
|
123
|
-
const sorted = await query.list(undefined, { sort: (a, b) => a.id.localeCompare(b.id) })
|
|
124
|
-
if (sorted.length !== 2) ko('R4 list(sort)', `len=${sorted.length}`)
|
|
125
|
-
else ok('R4 list(sort) → 2 sorted')
|
|
126
|
-
|
|
127
|
-
const limited = await query.list(undefined, { limit: 1 })
|
|
128
|
-
if (limited.length !== 1) ko('R4 list(limit=1)', `len=${limited.length}`)
|
|
129
|
-
else ok('R4 list(limit=1) → 1 entry')
|
|
130
|
-
|
|
131
|
-
const filtered = await query.list(undefined, { where: e => e.collection === 'legal' })
|
|
132
|
-
if (filtered.length !== 1) ko('R4 list(where=legal)', `len=${filtered.length}`)
|
|
133
|
-
else ok('R4 list(where=legal) → 1 entry')
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// ── R5: no-index → throw (zero-parse invariant) ─────────
|
|
137
|
-
|
|
138
|
-
async function checkR5 (): Promise<void> {
|
|
139
|
-
console.log('── R5: no-index → throw (zero-parse invariant) ──')
|
|
140
|
-
const emptyStore = createMemoryArtifactStore()
|
|
141
|
-
const query = new ContentQuery(emptyStore)
|
|
142
|
-
try {
|
|
143
|
-
await query.byPath('/legal/privacy')
|
|
144
|
-
ko('R5 byPath no-index', 'should have thrown')
|
|
145
|
-
} catch (e) {
|
|
146
|
-
const msg = (e as Error).message
|
|
147
|
-
if (msg.includes('No index')) ok('R5 byPath no-index → throws')
|
|
148
|
-
else ko('R5 byPath no-index', `wrong message: ${msg}`)
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
await query.list()
|
|
152
|
-
ko('R5 list no-index', 'should have thrown')
|
|
153
|
-
} catch (e) {
|
|
154
|
-
const msg = (e as Error).message
|
|
155
|
-
if (msg.includes('No index')) ok('R5 list no-index → throws')
|
|
156
|
-
else ko('R5 list no-index', `wrong message: ${msg}`)
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// ── R6: deep-equal byPath vs byId (store round-trip) ────
|
|
161
|
-
|
|
162
|
-
async function checkR6 (query: ContentQuery): Promise<void> {
|
|
163
|
-
console.log('── R6: byPath vs byId deep-equal (round-trip) ──')
|
|
164
|
-
const byPath = await query.byPath('/legal/privacy')
|
|
165
|
-
const byId = await query.byId('legal/privacy')
|
|
166
|
-
if (byPath === null || byId === null) { ko('R6 deep-equal', 'one or both null'); return }
|
|
167
|
-
if (byPath.fixture !== byId.fixture) { ko('R6 deep-equal', 'fixture mismatch'); return }
|
|
168
|
-
if (byPath.domain !== byId.domain) { ko('R6 deep-equal', 'domain mismatch'); return }
|
|
169
|
-
if (byPath.content.blocks.length !== byId.content.blocks.length) { ko('R6 deep-equal', 'blocks length mismatch'); return }
|
|
170
|
-
ok('R6 byPath(/legal/privacy) deep-equal byId(legal/privacy)')
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// ── Main ─────────────────────────────────────────────────
|
|
174
|
-
|
|
175
|
-
async function main (): Promise<void> {
|
|
176
|
-
console.log('── runtime:check ──')
|
|
177
|
-
console.log(` output dir: ${OUTPUT_DIR}`)
|
|
178
|
-
|
|
179
|
-
const store: ArtifactStore = createFsArtifactStore(OUTPUT_DIR)
|
|
180
|
-
const query = new ContentQuery(store)
|
|
181
|
-
|
|
182
|
-
await checkR1(query)
|
|
183
|
-
await checkR2(query)
|
|
184
|
-
await checkR3(query)
|
|
185
|
-
await checkR4(query)
|
|
186
|
-
await checkR5()
|
|
187
|
-
await checkR6(query)
|
|
188
|
-
|
|
189
|
-
console.log(`\n──────── runtime:check summary ────────`)
|
|
190
|
-
console.log(`${pass} pass, ${fail} fail`)
|
|
191
|
-
if (fail > 0) {
|
|
192
|
-
console.log('\nFailures:')
|
|
193
|
-
for (const e of errors) console.log(` - ${e}`)
|
|
194
|
-
process.exit(1)
|
|
195
|
-
}
|
|
196
|
-
process.exit(0)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
void main()
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DocumentIdentity — dexinlabs-specific identity builder for document content.
|
|
3
|
-
*
|
|
4
|
-
* Maps page slugs to DocumentIdentity shape with collection='pages'.
|
|
5
|
-
*
|
|
6
|
-
* This keeps dexin-content generic while allowing dexinlabs to maintain
|
|
7
|
-
* its document/page organization.
|
|
8
|
-
*/
|
|
9
|
-
import type { DocumentIdentity } from '../../core/types/index'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Build a DocumentIdentity for a page file.
|
|
13
|
-
*
|
|
14
|
-
* @param slug - Page identifier (e.g., 'about', 'privacy')
|
|
15
|
-
* @param relPath - Relative path from content root (e.g., 'pages/about.md')
|
|
16
|
-
*/
|
|
17
|
-
export function buildDocumentIdentity(
|
|
18
|
-
slug: string,
|
|
19
|
-
relPath: string
|
|
20
|
-
): DocumentIdentity {
|
|
21
|
-
return {
|
|
22
|
-
id: slug,
|
|
23
|
-
path: `/${slug}`,
|
|
24
|
-
file: relPath,
|
|
25
|
-
collection: 'pages'
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// ─────────────────────────────────────────────────────────────
|
|
2
|
-
// domains/document/parser.ts
|
|
3
|
-
//
|
|
4
|
-
// Document domain parser. Identity behaviour defined in §6.3.15 frozen.
|
|
5
|
-
//
|
|
6
|
-
// Content behaviour:
|
|
7
|
-
// * Heading = straight docAST heading (h1→1, h2→2, h3→3, h4→4; no
|
|
8
|
-
// absorption, no downgrade).
|
|
9
|
-
// * md h5/h6 → DOC_HEADING_DEPTH_UNDEFINED (6.3.10 frozen: ERROR)。
|
|
10
|
-
// * No section grouping (6.3.11 frozen).
|
|
11
|
-
// * ContainerBlock (6.3.12) stays neutral (never re-mapped to domain blocks).
|
|
12
|
-
// * Plain blocks pass through unchanged from DocumentAST.
|
|
13
|
-
// * `mdDepth` 属 transient 辅助注解:clone/render 路径显式剔除。
|
|
14
|
-
// ─────────────────────────────────────────────────────────────
|
|
15
|
-
|
|
16
|
-
import type {
|
|
17
|
-
DomainParser,
|
|
18
|
-
ParseContext,
|
|
19
|
-
ParseError
|
|
20
|
-
} from '../../core/types/index'
|
|
21
|
-
import type { LessonContent, Block, HeadingBlock } from '../../core/types/lessonAST'
|
|
22
|
-
|
|
23
|
-
export const documentParser: DomainParser = {
|
|
24
|
-
domain: 'document',
|
|
25
|
-
parse (content: LessonContent, ctx: ParseContext): LessonContent {
|
|
26
|
-
// Pre-pass: document-domain heading depth fail-fast (6.3.10).
|
|
27
|
-
walkAndEnforceHeadingDepth(content.blocks, ctx)
|
|
28
|
-
|
|
29
|
-
// Deep-copy structural nodes so future mutations to docAST do not leak.
|
|
30
|
-
return { version: 1, blocks: cloneBlocks(content.blocks) }
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function walkAndEnforceHeadingDepth (blocks: Block[], ctx: ParseContext): void {
|
|
35
|
-
for (const b of blocks) {
|
|
36
|
-
if (b.type === 'heading') {
|
|
37
|
-
const mdDepth = (b as HeadingBlock).mdDepth ?? b.level
|
|
38
|
-
if (mdDepth > 4) {
|
|
39
|
-
const err = new Error(
|
|
40
|
-
`[DOC_HEADING_DEPTH_UNDEFINED] Document domain only supports h1–h4. ` +
|
|
41
|
-
`Encountered h${mdDepth} (heading level ${mdDepth}): no mapping defined per 6.3.10 frozen table. ` +
|
|
42
|
-
`Allowed heading markdown depths: 1–4. Silently capping to level 4 is forbidden (information loss). ` +
|
|
43
|
-
`file: ${ctx.file}`
|
|
44
|
-
) as ParseError & Error
|
|
45
|
-
err.code = 'DOC_HEADING_DEPTH_UNDEFINED'
|
|
46
|
-
err.file = ctx.file
|
|
47
|
-
err.loc = { headingDepth: mdDepth, allowed: 'h1-h4' }
|
|
48
|
-
throw err
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (b.type === 'quote') walkAndEnforceHeadingDepth(b.children, ctx)
|
|
52
|
-
if (b.type === 'container') walkAndEnforceHeadingDepth(b.children, ctx)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function cloneBlocks (xs: Block[]): Block[] {
|
|
57
|
-
return xs.map(b => {
|
|
58
|
-
switch (b.type) {
|
|
59
|
-
case 'paragraph': return { type:'paragraph', children: cloneInlineArr(b.children) }
|
|
60
|
-
case 'heading': {
|
|
61
|
-
// Drop transient mdDepth — canonical output never contains it.
|
|
62
|
-
return { type:'heading', level: b.level, children: cloneInlineArr(b.children) }
|
|
63
|
-
}
|
|
64
|
-
case 'quote': return { type:'quote', children: cloneBlocks(b.children) }
|
|
65
|
-
case 'divider': return { type:'divider' }
|
|
66
|
-
case 'list': return {
|
|
67
|
-
type:'list', ordered: b.ordered,
|
|
68
|
-
items: b.items.map(row => row.map(cloneInline))
|
|
69
|
-
}
|
|
70
|
-
case 'table': return {
|
|
71
|
-
type:'table',
|
|
72
|
-
headers: b.headers.map(c => c.map(cloneInline)),
|
|
73
|
-
rows: b.rows.map(r => r.map(c => c.map(cloneInline)))
|
|
74
|
-
}
|
|
75
|
-
case 'image': return { type:'image', src:b.src, alt:b.alt, caption:b.caption }
|
|
76
|
-
case 'code': return { type:'code', language: b.language, code: b.code }
|
|
77
|
-
case 'formula': return { type:'formula', latex: b.latex, display: b.display }
|
|
78
|
-
case 'container': return {
|
|
79
|
-
type:'container',
|
|
80
|
-
name: b.name,
|
|
81
|
-
attrs: { ...b.attrs },
|
|
82
|
-
children: cloneBlocks(b.children)
|
|
83
|
-
}
|
|
84
|
-
default:
|
|
85
|
-
// hint/definition/example/question/section — domain-specific, not expected here
|
|
86
|
-
return b
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function cloneInlineArr (xs: any[]): any[] {
|
|
92
|
-
return xs.map(cloneInline)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function cloneInline (i: any): any {
|
|
96
|
-
switch (i.type) {
|
|
97
|
-
case 'text': return { type:'text', value: i.value }
|
|
98
|
-
case 'code': return { type:'code', value: i.value }
|
|
99
|
-
case 'math': return { type:'math', latex: i.latex }
|
|
100
|
-
case 'bold': return { type:'bold', children: cloneInlineArr(i.children) }
|
|
101
|
-
case 'italic': return { type:'italic', children: cloneInlineArr(i.children) }
|
|
102
|
-
case 'link': return { type:'link', url: i.url, children: cloneInlineArr(i.children) }
|
|
103
|
-
default: return { ...i }
|
|
104
|
-
}
|
|
105
|
-
}
|