jmd-format 0.1.2 → 0.2.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/LICENSE +202 -21
- package/README.md +3 -1
- package/package.json +2 -2
- package/src/index.js +3 -2
- package/src/parser.js +413 -51
- package/src/serializer.js +59 -13
- package/src/value.js +13 -3
package/LICENSE
CHANGED
|
@@ -1,21 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -127,4 +127,6 @@ parser side, following §22.1 of the specification.
|
|
|
127
127
|
|
|
128
128
|
## License
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
Apache 2.0 — see [LICENSE](./LICENSE).
|
|
131
|
+
|
|
132
|
+
The JMD format specification is licensed separately under [CC BY 4.0](https://github.com/ostermeyer/jmd-spec/blob/main/LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jmd-format",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "JMD (JSON Markdown) — structured data format for LLM-driven infrastructure. JavaScript reference implementation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"name": "Andreas Ostermeyer",
|
|
12
12
|
"email": "andreas@ostermeyer.de"
|
|
13
13
|
},
|
|
14
|
-
"license": "
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
15
|
"homepage": "https://github.com/ostermeyer/jmd-spec",
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
1
2
|
// jmd-format — JavaScript reference implementation.
|
|
2
3
|
//
|
|
3
4
|
// Public surface: minimal on purpose. Batch API for the common case,
|
|
4
5
|
// streaming API for large or incremental workloads.
|
|
5
6
|
|
|
6
|
-
export { parse, createParser, toLines } from './parser.js'
|
|
7
|
-
export { serialize, serializeLines } from './serializer.js'
|
|
7
|
+
export { parse, createParser, toLines, JMDParseError } from './parser.js'
|
|
8
|
+
export { serialize, serializeLines, validateLabel } from './serializer.js'
|
package/src/parser.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
1
2
|
// JMD parser.
|
|
2
3
|
//
|
|
3
4
|
// The parser processes a JMD document line by line, maintaining a scope
|
|
@@ -8,11 +9,37 @@
|
|
|
8
9
|
//
|
|
9
10
|
// Both share the same line-processing core. Events follow the sequence
|
|
10
11
|
// defined in JMD spec §18.2. Parser-tolerant per §22.1.
|
|
12
|
+
//
|
|
13
|
+
// Spec coverage: v0.3.3 — including §7.4 repeated-heading promotion with
|
|
14
|
+
// three structured errors (sigil_conflict, repeated_explicit_array,
|
|
15
|
+
// repeated_scalar_key), §3.5.1 frontmatter `---` marker tolerance, and
|
|
16
|
+
// §5.2 multi-line block scalars (`|` literal, `>` folded).
|
|
11
17
|
|
|
12
18
|
import { parseScalar, parseKey, parseField } from './value.js'
|
|
13
19
|
|
|
14
20
|
const HEADING = /^(#+)([!?-])?(?:\s+(.*))?$/
|
|
15
21
|
|
|
22
|
+
// §7.4 — kinds tracked per object scope to detect repeated-heading
|
|
23
|
+
// conflicts and to drive implicit-array promotion.
|
|
24
|
+
const K_OBJECT_HEADING = 1 // `## foo` opened an object scope
|
|
25
|
+
const K_ARRAY_SIGIL = 2 // `## foo[]` opened an explicit array
|
|
26
|
+
const K_ARRAY_PROMOTED = 3 // two `## foo` collapsed into an array
|
|
27
|
+
const K_SCALAR_BARE = 4 // `foo: 1` at column 0
|
|
28
|
+
const K_SCALAR_HEADING = 5 // `## foo: 1`
|
|
29
|
+
|
|
30
|
+
// Structured parse error — `kind` lets callers distinguish the §7.4
|
|
31
|
+
// conditions (and any other tagged failures we add later) from generic
|
|
32
|
+
// malformed-input errors. The line number is appended to the message
|
|
33
|
+
// for legibility but also stored separately on the error.
|
|
34
|
+
export class JMDParseError extends Error {
|
|
35
|
+
constructor(kind, message, line) {
|
|
36
|
+
super(message + ' (line ' + line + ')')
|
|
37
|
+
this.name = 'JMDParseError'
|
|
38
|
+
this.kind = kind
|
|
39
|
+
this.line = line
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
16
43
|
export function createParser() {
|
|
17
44
|
let lineNo = 0
|
|
18
45
|
|
|
@@ -21,19 +48,28 @@ export function createParser() {
|
|
|
21
48
|
let label = null
|
|
22
49
|
const frontmatter = {}
|
|
23
50
|
let inFrontmatter = true
|
|
51
|
+
let frontmatterStarted = false // §3.5.1: have we seen any frontmatter field?
|
|
24
52
|
let seenRoot = false
|
|
25
53
|
let root = null
|
|
26
54
|
|
|
27
55
|
// Scope stack. Each entry:
|
|
28
|
-
// { kind: 'object' | 'array', container, depth,
|
|
29
|
-
//
|
|
30
|
-
//
|
|
56
|
+
// { kind: 'object' | 'array', container, depth,
|
|
57
|
+
// seen: Map<key, K_*>, // object scopes only
|
|
58
|
+
// currentItem?, itemSeen? } // array scopes only
|
|
59
|
+
// currentItem lives on array scopes and holds the object built by the
|
|
60
|
+
// most recent `- ` line so indented continuations attach to it.
|
|
61
|
+
// itemSeen mirrors `seen` for that per-item object.
|
|
31
62
|
let stack = []
|
|
32
63
|
|
|
33
64
|
// Pending blockquote state.
|
|
34
65
|
// { container, key, lines }
|
|
35
66
|
let bq = null
|
|
36
67
|
|
|
68
|
+
// Pending block-scalar state (§5.2).
|
|
69
|
+
// { container, key, kind: '|' | '>', lines, baseIndent }
|
|
70
|
+
// baseIndent is null until the first content line establishes it.
|
|
71
|
+
let block = null
|
|
72
|
+
|
|
37
73
|
// A blank line may or may not terminate the current scope — it depends on
|
|
38
74
|
// what comes after. We defer the decision: flag the blank, then let the
|
|
39
75
|
// next real line resolve it. A deeper heading re-enters (no reset); a bare
|
|
@@ -58,6 +94,11 @@ export function createParser() {
|
|
|
58
94
|
lineNo++
|
|
59
95
|
const line = rawLine.replace(/\r$/, '')
|
|
60
96
|
|
|
97
|
+
if (block !== null) {
|
|
98
|
+
if (processBlockLine(line)) return drain()
|
|
99
|
+
// Block ended; fall through to normal line handling.
|
|
100
|
+
}
|
|
101
|
+
|
|
61
102
|
if (bq !== null) {
|
|
62
103
|
if (line === '>' || line.startsWith('> ')) {
|
|
63
104
|
const content = line === '>' ? '' : line.slice(2)
|
|
@@ -148,23 +189,118 @@ export function createParser() {
|
|
|
148
189
|
bq = null
|
|
149
190
|
}
|
|
150
191
|
|
|
192
|
+
// --- Block scalars (§5.2) ------------------------------------------------
|
|
193
|
+
|
|
194
|
+
function startBlock(container, key, kind) {
|
|
195
|
+
block = { container, key, kind, lines: [], baseIndent: null }
|
|
196
|
+
emit('field_start', { key })
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Returns true if the line was consumed by the block scalar, false if it
|
|
200
|
+
// signals the block has ended and the line must be re-dispatched normally.
|
|
201
|
+
function processBlockLine(line) {
|
|
202
|
+
if (line === '') {
|
|
203
|
+
block.lines.push('')
|
|
204
|
+
return true
|
|
205
|
+
}
|
|
206
|
+
const m = /^(\s*)/.exec(line)
|
|
207
|
+
const indent = m[0].length
|
|
208
|
+
if (block.baseIndent === null) {
|
|
209
|
+
if (indent === 0) {
|
|
210
|
+
commitBlock()
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
block.baseIndent = indent
|
|
214
|
+
block.lines.push(line.slice(indent))
|
|
215
|
+
return true
|
|
216
|
+
}
|
|
217
|
+
if (indent < block.baseIndent && /\S/.test(line)) {
|
|
218
|
+
commitBlock()
|
|
219
|
+
return false
|
|
220
|
+
}
|
|
221
|
+
block.lines.push(line.slice(block.baseIndent))
|
|
222
|
+
return true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function commitBlock() {
|
|
226
|
+
const lines = block.lines.slice()
|
|
227
|
+
// Drop trailing blank lines for both kinds — spec §5.2 treats the
|
|
228
|
+
// trailing newline as a line-terminator, not part of the value.
|
|
229
|
+
while (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
|
|
230
|
+
const value = block.kind === '|' ? lines.join('\n') : foldLines(lines)
|
|
231
|
+
block.container[block.key] = value
|
|
232
|
+
emit('field', { key: block.key, value })
|
|
233
|
+
block = null
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Folded block scalar fold (§5.2):
|
|
237
|
+
// consecutive non-blank lines → joined with single space
|
|
238
|
+
// one blank between groups → one newline
|
|
239
|
+
// N+1 blank lines → N newlines
|
|
240
|
+
function foldLines(lines) {
|
|
241
|
+
let out = ''
|
|
242
|
+
let group = []
|
|
243
|
+
let blanks = 0
|
|
244
|
+
function flushGroup() {
|
|
245
|
+
if (group.length > 0) {
|
|
246
|
+
out += group.join(' ')
|
|
247
|
+
group = []
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
for (const ln of lines) {
|
|
251
|
+
if (ln === '') {
|
|
252
|
+
flushGroup()
|
|
253
|
+
blanks++
|
|
254
|
+
} else {
|
|
255
|
+
if (blanks > 0) {
|
|
256
|
+
out += '\n'.repeat(blanks)
|
|
257
|
+
blanks = 0
|
|
258
|
+
}
|
|
259
|
+
group.push(ln)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
flushGroup()
|
|
263
|
+
return out
|
|
264
|
+
}
|
|
265
|
+
|
|
151
266
|
// --- Root / frontmatter --------------------------------------------------
|
|
152
267
|
|
|
153
268
|
function onFrontmatter(line) {
|
|
269
|
+
// §3.5.1: tolerate `---` (or more) marker lines bracketing the
|
|
270
|
+
// frontmatter block. A marker before any field opens it; a marker
|
|
271
|
+
// after the last field separates it from the root heading. Both
|
|
272
|
+
// forms are consumed without emitting a frontmatter event.
|
|
273
|
+
if (/^-{3,}$/.test(line)) {
|
|
274
|
+
// Marker is structural-only — no state change beyond ignoring it.
|
|
275
|
+
return
|
|
276
|
+
}
|
|
154
277
|
const f = parseField(line)
|
|
155
278
|
if (f) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
279
|
+
if (f.empty) {
|
|
280
|
+
// D12: multi-line frontmatter value enters a blockquote that
|
|
281
|
+
// collects subsequent `> ...` lines; commit assigns the joined
|
|
282
|
+
// string to frontmatter[key] (handled by the bq state machine).
|
|
283
|
+
frontmatterStarted = true
|
|
284
|
+
startBlockquote(frontmatter, f.key)
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
frontmatter[f.key] = f.value
|
|
288
|
+
frontmatterStarted = true
|
|
289
|
+
emit('frontmatter', { key: f.key, value: f.value })
|
|
159
290
|
return
|
|
160
291
|
}
|
|
161
292
|
const pk = parseKey(line)
|
|
162
293
|
if (pk && pk.rest === '') {
|
|
163
294
|
frontmatter[pk.key] = true
|
|
295
|
+
frontmatterStarted = true
|
|
164
296
|
emit('frontmatter', { key: pk.key, value: true })
|
|
165
297
|
return
|
|
166
298
|
}
|
|
167
|
-
throw
|
|
299
|
+
throw new JMDParseError(
|
|
300
|
+
'malformed_frontmatter',
|
|
301
|
+
'Unexpected line before root heading',
|
|
302
|
+
lineNo,
|
|
303
|
+
)
|
|
168
304
|
}
|
|
169
305
|
|
|
170
306
|
function openRoot(modeMark, text) {
|
|
@@ -179,31 +315,50 @@ export function createParser() {
|
|
|
179
315
|
if (text === '[]') {
|
|
180
316
|
label = '[]'
|
|
181
317
|
root = []
|
|
182
|
-
stack = [
|
|
318
|
+
stack = [arrayScope(root, 1)]
|
|
183
319
|
} else if (text.endsWith('[]')) {
|
|
184
320
|
label = text.slice(0, -2)
|
|
185
321
|
root = []
|
|
186
|
-
stack = [
|
|
322
|
+
stack = [arrayScope(root, 1)]
|
|
187
323
|
} else {
|
|
188
324
|
label = text
|
|
189
325
|
root = {}
|
|
190
|
-
stack = [
|
|
326
|
+
stack = [objectScope(root, 1)]
|
|
191
327
|
}
|
|
192
328
|
emit('document_start', { mode, label })
|
|
193
329
|
}
|
|
194
330
|
|
|
331
|
+
function objectScope(container, depth) {
|
|
332
|
+
return { kind: 'object', container, depth, seen: new Map() }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function arrayScope(container, depth) {
|
|
336
|
+
return {
|
|
337
|
+
kind: 'array', container, depth,
|
|
338
|
+
currentItem: null, itemSeen: null,
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
195
342
|
// --- Headings ------------------------------------------------------------
|
|
196
343
|
|
|
197
344
|
function onHeading(depth, modeMark, text) {
|
|
198
345
|
if (!seenRoot) {
|
|
199
346
|
if (depth !== 1) {
|
|
200
|
-
throw
|
|
347
|
+
throw new JMDParseError(
|
|
348
|
+
'malformed_root',
|
|
349
|
+
'Document must begin with a depth-1 heading',
|
|
350
|
+
lineNo,
|
|
351
|
+
)
|
|
201
352
|
}
|
|
202
353
|
openRoot(modeMark, text)
|
|
203
354
|
return
|
|
204
355
|
}
|
|
205
356
|
if (modeMark) {
|
|
206
|
-
throw
|
|
357
|
+
throw new JMDParseError(
|
|
358
|
+
'malformed_heading',
|
|
359
|
+
'Mode markers (!, ?, -) are only valid on the root heading',
|
|
360
|
+
lineNo,
|
|
361
|
+
)
|
|
207
362
|
}
|
|
208
363
|
|
|
209
364
|
// Depth-qualified array item (§8.6a) or depth+1 item (§8.6b):
|
|
@@ -231,22 +386,37 @@ export function createParser() {
|
|
|
231
386
|
if (text.endsWith('[]')) {
|
|
232
387
|
const keyText = text.slice(0, -2)
|
|
233
388
|
const pk = parseKey(keyText)
|
|
234
|
-
if (!pk || pk.rest !== '')
|
|
389
|
+
if (!pk || pk.rest !== '') {
|
|
390
|
+
throw new JMDParseError(
|
|
391
|
+
'malformed_heading',
|
|
392
|
+
'Malformed array heading key',
|
|
393
|
+
lineNo,
|
|
394
|
+
)
|
|
395
|
+
}
|
|
235
396
|
openArrayScope(depth, pk.key)
|
|
236
397
|
return
|
|
237
398
|
}
|
|
238
399
|
|
|
239
400
|
const field = parseField(text)
|
|
240
|
-
if (field &&
|
|
241
|
-
const
|
|
242
|
-
|
|
401
|
+
if (field && field.value !== undefined) {
|
|
402
|
+
const { container, seen } = scalarParentAt(depth)
|
|
403
|
+
checkScalar(seen, field.key, K_SCALAR_HEADING)
|
|
404
|
+
container[field.key] = field.value
|
|
243
405
|
emit('field', { key: field.key, value: field.value })
|
|
244
406
|
return
|
|
245
407
|
}
|
|
246
408
|
|
|
247
409
|
if (field && field.empty) {
|
|
248
|
-
const
|
|
249
|
-
|
|
410
|
+
const { container, seen } = scalarParentAt(depth)
|
|
411
|
+
checkScalar(seen, field.key, K_SCALAR_HEADING)
|
|
412
|
+
startBlockquote(container, field.key)
|
|
413
|
+
return
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if (field && field.block) {
|
|
417
|
+
const { container, seen } = scalarParentAt(depth)
|
|
418
|
+
checkScalar(seen, field.key, K_SCALAR_HEADING)
|
|
419
|
+
startBlock(container, field.key, field.block)
|
|
250
420
|
return
|
|
251
421
|
}
|
|
252
422
|
|
|
@@ -256,22 +426,90 @@ export function createParser() {
|
|
|
256
426
|
return
|
|
257
427
|
}
|
|
258
428
|
|
|
259
|
-
throw
|
|
429
|
+
throw new JMDParseError(
|
|
430
|
+
'malformed_heading',
|
|
431
|
+
'Malformed heading',
|
|
432
|
+
lineNo,
|
|
433
|
+
)
|
|
260
434
|
}
|
|
261
435
|
|
|
262
436
|
function openObjectScope(depth, key) {
|
|
263
|
-
const parent =
|
|
437
|
+
const parent = parentScopeAt(depth)
|
|
438
|
+
const { container, seen } = parentContainerAndSeen(parent)
|
|
439
|
+
const prior = seen ? seen.get(key) : undefined
|
|
440
|
+
|
|
441
|
+
if (prior === K_ARRAY_SIGIL) {
|
|
442
|
+
throw new JMDParseError(
|
|
443
|
+
'sigil_conflict',
|
|
444
|
+
'Repeated heading "' + key + '" mixes [] sigil with bare form',
|
|
445
|
+
lineNo,
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
if (prior === K_SCALAR_BARE || prior === K_SCALAR_HEADING) {
|
|
449
|
+
throw new JMDParseError(
|
|
450
|
+
'repeated_scalar_key',
|
|
451
|
+
'Key "' + key + '" first seen as scalar, then as object heading',
|
|
452
|
+
lineNo,
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
if (prior === K_ARRAY_PROMOTED) {
|
|
456
|
+
// Third+ occurrence: append a fresh object to the existing array.
|
|
457
|
+
const arr = container[key]
|
|
458
|
+
const obj = {}
|
|
459
|
+
arr.push(obj)
|
|
460
|
+
stack.push(objectScope(obj, depth))
|
|
461
|
+
emit('object_start', { key })
|
|
462
|
+
return
|
|
463
|
+
}
|
|
464
|
+
if (prior === K_OBJECT_HEADING) {
|
|
465
|
+
// §7.4: second occurrence promotes the scalar object to an array.
|
|
466
|
+
const existing = container[key]
|
|
467
|
+
const obj = {}
|
|
468
|
+
const arr = [existing, obj]
|
|
469
|
+
container[key] = arr
|
|
470
|
+
if (seen) seen.set(key, K_ARRAY_PROMOTED)
|
|
471
|
+
stack.push(objectScope(obj, depth))
|
|
472
|
+
emit('object_start', { key })
|
|
473
|
+
return
|
|
474
|
+
}
|
|
475
|
+
// First occurrence — plain object heading.
|
|
264
476
|
const obj = {}
|
|
265
|
-
|
|
266
|
-
|
|
477
|
+
container[key] = obj
|
|
478
|
+
if (seen) seen.set(key, K_OBJECT_HEADING)
|
|
479
|
+
stack.push(objectScope(obj, depth))
|
|
267
480
|
emit('object_start', { key })
|
|
268
481
|
}
|
|
269
482
|
|
|
270
483
|
function openArrayScope(depth, key) {
|
|
271
|
-
const parent =
|
|
484
|
+
const parent = parentScopeAt(depth)
|
|
485
|
+
const { container, seen } = parentContainerAndSeen(parent)
|
|
486
|
+
const prior = seen ? seen.get(key) : undefined
|
|
487
|
+
|
|
488
|
+
if (prior === K_ARRAY_SIGIL) {
|
|
489
|
+
throw new JMDParseError(
|
|
490
|
+
'repeated_explicit_array',
|
|
491
|
+
'Repeated explicit-array heading "' + key + '"',
|
|
492
|
+
lineNo,
|
|
493
|
+
)
|
|
494
|
+
}
|
|
495
|
+
if (prior === K_OBJECT_HEADING || prior === K_ARRAY_PROMOTED) {
|
|
496
|
+
throw new JMDParseError(
|
|
497
|
+
'sigil_conflict',
|
|
498
|
+
'Heading "' + key + '" first appeared without [], then with []',
|
|
499
|
+
lineNo,
|
|
500
|
+
)
|
|
501
|
+
}
|
|
502
|
+
if (prior === K_SCALAR_BARE || prior === K_SCALAR_HEADING) {
|
|
503
|
+
throw new JMDParseError(
|
|
504
|
+
'repeated_scalar_key',
|
|
505
|
+
'Key "' + key + '" first seen as scalar, then as array heading',
|
|
506
|
+
lineNo,
|
|
507
|
+
)
|
|
508
|
+
}
|
|
272
509
|
const arr = []
|
|
273
|
-
|
|
274
|
-
|
|
510
|
+
container[key] = arr
|
|
511
|
+
if (seen) seen.set(key, K_ARRAY_SIGIL)
|
|
512
|
+
stack.push(arrayScope(arr, depth))
|
|
275
513
|
emit('array_start', { key })
|
|
276
514
|
}
|
|
277
515
|
|
|
@@ -280,25 +518,84 @@ export function createParser() {
|
|
|
280
518
|
// enclosing array scope.
|
|
281
519
|
const top = stack[stack.length - 1]
|
|
282
520
|
if (!top || top.kind !== 'array') {
|
|
283
|
-
throw
|
|
521
|
+
throw new JMDParseError(
|
|
522
|
+
'malformed_heading',
|
|
523
|
+
'Anonymous sub-array outside array scope',
|
|
524
|
+
lineNo,
|
|
525
|
+
)
|
|
284
526
|
}
|
|
285
527
|
closeItem(top)
|
|
286
528
|
const inner = []
|
|
287
529
|
top.container.push(inner)
|
|
288
|
-
stack.push(
|
|
530
|
+
stack.push(arrayScope(inner, depth))
|
|
289
531
|
emit('array_start', {})
|
|
290
532
|
}
|
|
291
533
|
|
|
292
|
-
|
|
534
|
+
// Find the scope that should receive a scalar field at the given heading
|
|
535
|
+
// depth, returning its container plus the `seen` map for §7.4 tracking.
|
|
536
|
+
function scalarParentAt(depth) {
|
|
293
537
|
for (let i = stack.length - 1; i >= 0; i--) {
|
|
294
538
|
const s = stack[i]
|
|
295
|
-
if (s.depth
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
throw parseError('Field has no enclosing object scope')
|
|
539
|
+
if (s.depth >= depth) continue
|
|
540
|
+
if (s.kind === 'object') {
|
|
541
|
+
return { container: s.container, seen: s.seen }
|
|
299
542
|
}
|
|
543
|
+
if (s.kind === 'array' && s.currentItem) {
|
|
544
|
+
// Per-item object: track its own seen-keys so repeated keys on
|
|
545
|
+
// one item still surface as repeated_scalar_key.
|
|
546
|
+
if (s.itemSeen === null) s.itemSeen = new Map()
|
|
547
|
+
return { container: s.currentItem, seen: s.itemSeen }
|
|
548
|
+
}
|
|
549
|
+
throw new JMDParseError(
|
|
550
|
+
'malformed_field',
|
|
551
|
+
'Field has no enclosing object scope',
|
|
552
|
+
lineNo,
|
|
553
|
+
)
|
|
554
|
+
}
|
|
555
|
+
throw new JMDParseError(
|
|
556
|
+
'malformed_field',
|
|
557
|
+
'No enclosing scope for depth ' + depth,
|
|
558
|
+
lineNo,
|
|
559
|
+
)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function parentScopeAt(depth) {
|
|
563
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
564
|
+
if (stack[i].depth < depth) return stack[i]
|
|
565
|
+
}
|
|
566
|
+
throw new JMDParseError(
|
|
567
|
+
'malformed_heading',
|
|
568
|
+
'No enclosing scope for depth ' + depth,
|
|
569
|
+
lineNo,
|
|
570
|
+
)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function parentContainerAndSeen(scope) {
|
|
574
|
+
if (scope.kind === 'object') {
|
|
575
|
+
return { container: scope.container, seen: scope.seen }
|
|
576
|
+
}
|
|
577
|
+
if (scope.kind === 'array' && scope.currentItem) {
|
|
578
|
+
if (scope.itemSeen === null) scope.itemSeen = new Map()
|
|
579
|
+
return { container: scope.currentItem, seen: scope.itemSeen }
|
|
300
580
|
}
|
|
301
|
-
throw
|
|
581
|
+
throw new JMDParseError(
|
|
582
|
+
'malformed_heading',
|
|
583
|
+
'Heading inside an array scope needs an active item',
|
|
584
|
+
lineNo,
|
|
585
|
+
)
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function checkScalar(seen, key, kind) {
|
|
589
|
+
if (!seen) return
|
|
590
|
+
const prior = seen.get(key)
|
|
591
|
+
if (prior !== undefined) {
|
|
592
|
+
throw new JMDParseError(
|
|
593
|
+
'repeated_scalar_key',
|
|
594
|
+
'Key "' + key + '" repeated',
|
|
595
|
+
lineNo,
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
seen.set(key, kind)
|
|
302
599
|
}
|
|
303
600
|
|
|
304
601
|
function popToDepth(targetDepth) {
|
|
@@ -321,6 +618,7 @@ export function createParser() {
|
|
|
321
618
|
if (arrayScope.currentItem !== null) {
|
|
322
619
|
emit('item_end')
|
|
323
620
|
arrayScope.currentItem = null
|
|
621
|
+
arrayScope.itemSeen = null
|
|
324
622
|
}
|
|
325
623
|
}
|
|
326
624
|
|
|
@@ -334,8 +632,18 @@ export function createParser() {
|
|
|
334
632
|
const bag = parent.kind === 'array' && parent.currentItem
|
|
335
633
|
? parent.currentItem
|
|
336
634
|
: parent.container
|
|
635
|
+
if (Array.isArray(bag)) {
|
|
636
|
+
// Promoted array: search every parent dict in the bag for the
|
|
637
|
+
// container reference (rare path; small N).
|
|
638
|
+
continue
|
|
639
|
+
}
|
|
337
640
|
for (const k of Object.keys(bag)) {
|
|
338
641
|
if (bag[k] === scope.container) return k
|
|
642
|
+
// §7.4 promoted: parent[k] may be an array containing scope.container.
|
|
643
|
+
if (Array.isArray(bag[k])
|
|
644
|
+
&& bag[k][bag[k].length - 1] === scope.container) {
|
|
645
|
+
return k
|
|
646
|
+
}
|
|
339
647
|
}
|
|
340
648
|
}
|
|
341
649
|
return undefined
|
|
@@ -345,15 +653,39 @@ export function createParser() {
|
|
|
345
653
|
|
|
346
654
|
function onField(line) {
|
|
347
655
|
const f = parseField(line)
|
|
348
|
-
if (!f)
|
|
656
|
+
if (!f) {
|
|
657
|
+
throw new JMDParseError(
|
|
658
|
+
'malformed_field',
|
|
659
|
+
'Malformed field line',
|
|
660
|
+
lineNo,
|
|
661
|
+
)
|
|
662
|
+
}
|
|
349
663
|
const top = stack[stack.length - 1]
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
664
|
+
let target, seen
|
|
665
|
+
if (top.kind === 'object') {
|
|
666
|
+
target = top.container
|
|
667
|
+
seen = top.seen
|
|
668
|
+
} else {
|
|
669
|
+
if (!top.currentItem) {
|
|
670
|
+
throw new JMDParseError(
|
|
671
|
+
'malformed_field',
|
|
672
|
+
'Bare field inside array scope without an item',
|
|
673
|
+
lineNo,
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
target = top.currentItem
|
|
677
|
+
if (top.itemSeen === null) top.itemSeen = new Map()
|
|
678
|
+
seen = top.itemSeen
|
|
679
|
+
}
|
|
680
|
+
checkScalar(seen, f.key, K_SCALAR_BARE)
|
|
353
681
|
if (f.empty) {
|
|
354
682
|
startBlockquote(target, f.key)
|
|
355
683
|
return
|
|
356
684
|
}
|
|
685
|
+
if (f.block) {
|
|
686
|
+
startBlock(target, f.key, f.block)
|
|
687
|
+
return
|
|
688
|
+
}
|
|
357
689
|
target[f.key] = f.value
|
|
358
690
|
emit('field', { key: f.key, value: f.value })
|
|
359
691
|
}
|
|
@@ -377,7 +709,11 @@ export function createParser() {
|
|
|
377
709
|
}
|
|
378
710
|
const targetIdx = sameDepthIdx !== -1 ? sameDepthIdx : parentDepthIdx
|
|
379
711
|
if (targetIdx === -1) {
|
|
380
|
-
throw
|
|
712
|
+
throw new JMDParseError(
|
|
713
|
+
'malformed_heading',
|
|
714
|
+
'Depth-qualified item has no matching array scope',
|
|
715
|
+
lineNo,
|
|
716
|
+
)
|
|
381
717
|
}
|
|
382
718
|
// Close any scopes nested inside the target array.
|
|
383
719
|
while (stack.length - 1 > targetIdx) popScope()
|
|
@@ -387,7 +723,13 @@ export function createParser() {
|
|
|
387
723
|
|
|
388
724
|
function onItem(line) {
|
|
389
725
|
const top = stack[stack.length - 1]
|
|
390
|
-
if (top.kind !== 'array')
|
|
726
|
+
if (top.kind !== 'array') {
|
|
727
|
+
throw new JMDParseError(
|
|
728
|
+
'malformed_item',
|
|
729
|
+
'Array item outside array scope',
|
|
730
|
+
lineNo,
|
|
731
|
+
)
|
|
732
|
+
}
|
|
391
733
|
|
|
392
734
|
closeItem(top)
|
|
393
735
|
const rest = line === '-' ? '' : line.slice(2)
|
|
@@ -396,6 +738,7 @@ export function createParser() {
|
|
|
396
738
|
const item = {}
|
|
397
739
|
top.container.push(item)
|
|
398
740
|
top.currentItem = item
|
|
741
|
+
top.itemSeen = new Map()
|
|
399
742
|
emit('item_start')
|
|
400
743
|
return
|
|
401
744
|
}
|
|
@@ -405,9 +748,13 @@ export function createParser() {
|
|
|
405
748
|
const item = {}
|
|
406
749
|
top.container.push(item)
|
|
407
750
|
top.currentItem = item
|
|
751
|
+
top.itemSeen = new Map()
|
|
752
|
+
top.itemSeen.set(f.key, K_SCALAR_BARE)
|
|
408
753
|
emit('item_start')
|
|
409
754
|
if (f.empty) {
|
|
410
755
|
startBlockquote(item, f.key)
|
|
756
|
+
} else if (f.block) {
|
|
757
|
+
startBlock(item, f.key, f.block)
|
|
411
758
|
} else {
|
|
412
759
|
item[f.key] = f.value
|
|
413
760
|
emit('field', { key: f.key, value: f.value })
|
|
@@ -418,6 +765,7 @@ export function createParser() {
|
|
|
418
765
|
const value = parseScalar(rest)
|
|
419
766
|
top.container.push(value)
|
|
420
767
|
top.currentItem = null
|
|
768
|
+
top.itemSeen = null
|
|
421
769
|
emit('item_value', { value })
|
|
422
770
|
}
|
|
423
771
|
|
|
@@ -425,14 +773,30 @@ export function createParser() {
|
|
|
425
773
|
const content = line.replace(/^\s+/, '')
|
|
426
774
|
const top = stack[stack.length - 1]
|
|
427
775
|
if (top.kind !== 'array' || !top.currentItem) {
|
|
428
|
-
throw
|
|
776
|
+
throw new JMDParseError(
|
|
777
|
+
'malformed_field',
|
|
778
|
+
'Indented continuation without an active array item',
|
|
779
|
+
lineNo,
|
|
780
|
+
)
|
|
429
781
|
}
|
|
430
782
|
const f = parseField(content)
|
|
431
|
-
if (!f)
|
|
783
|
+
if (!f) {
|
|
784
|
+
throw new JMDParseError(
|
|
785
|
+
'malformed_field',
|
|
786
|
+
'Malformed indented continuation',
|
|
787
|
+
lineNo,
|
|
788
|
+
)
|
|
789
|
+
}
|
|
790
|
+
if (top.itemSeen === null) top.itemSeen = new Map()
|
|
791
|
+
checkScalar(top.itemSeen, f.key, K_SCALAR_BARE)
|
|
432
792
|
if (f.empty) {
|
|
433
793
|
startBlockquote(top.currentItem, f.key)
|
|
434
794
|
return
|
|
435
795
|
}
|
|
796
|
+
if (f.block) {
|
|
797
|
+
startBlock(top.currentItem, f.key, f.block)
|
|
798
|
+
return
|
|
799
|
+
}
|
|
436
800
|
top.currentItem[f.key] = f.value
|
|
437
801
|
emit('field', { key: f.key, value: f.value })
|
|
438
802
|
}
|
|
@@ -440,24 +804,20 @@ export function createParser() {
|
|
|
440
804
|
// --- Finalization --------------------------------------------------------
|
|
441
805
|
|
|
442
806
|
function finish() {
|
|
807
|
+
if (block !== null) commitBlock()
|
|
443
808
|
if (bq !== null) commitBlockquote()
|
|
444
809
|
if (!seenRoot) {
|
|
445
|
-
throw
|
|
810
|
+
throw new JMDParseError(
|
|
811
|
+
'malformed_root',
|
|
812
|
+
'Document contained no root heading',
|
|
813
|
+
lineNo,
|
|
814
|
+
)
|
|
446
815
|
}
|
|
447
816
|
while (stack.length > 0) popScope()
|
|
448
817
|
emit('document_end')
|
|
449
818
|
return drain()
|
|
450
819
|
}
|
|
451
820
|
|
|
452
|
-
// --- Errors --------------------------------------------------------------
|
|
453
|
-
|
|
454
|
-
function parseError(msg) {
|
|
455
|
-
const err = new Error(msg + ' (line ' + lineNo + ')')
|
|
456
|
-
err.line = lineNo
|
|
457
|
-
return err
|
|
458
|
-
}
|
|
459
|
-
function throwHere(msg) { throw parseError(msg) }
|
|
460
|
-
|
|
461
821
|
// --- Public surface ------------------------------------------------------
|
|
462
822
|
|
|
463
823
|
function parse(text) {
|
|
@@ -467,6 +827,8 @@ export function createParser() {
|
|
|
467
827
|
if (lines.length && lines[lines.length - 1] === '') lines.pop()
|
|
468
828
|
for (const line of lines) processLine(line)
|
|
469
829
|
finish()
|
|
830
|
+
// frontmatterStarted is internal — keep the public shape unchanged.
|
|
831
|
+
void frontmatterStarted
|
|
470
832
|
return { mode, label, frontmatter, value: root }
|
|
471
833
|
}
|
|
472
834
|
|
package/src/serializer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
1
2
|
// JMD serializer.
|
|
2
3
|
//
|
|
3
4
|
// Two surfaces over one implementation:
|
|
@@ -19,16 +20,39 @@ import { serializeScalar, serializeKey } from './value.js'
|
|
|
19
20
|
|
|
20
21
|
export function serialize(value, label = 'Document', frontmatter = null) {
|
|
21
22
|
const lines = []
|
|
22
|
-
emitDocument(value, label, frontmatter, lines)
|
|
23
|
+
emitDocument(value, validateLabel(label), frontmatter, lines)
|
|
23
24
|
return lines.join('\n')
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
export function* serializeLines(value, label = 'Document', frontmatter = null) {
|
|
27
28
|
const lines = []
|
|
28
|
-
emitDocument(value, label, frontmatter, lines)
|
|
29
|
+
emitDocument(value, validateLabel(label), frontmatter, lines)
|
|
29
30
|
for (const ln of lines) yield ln + '\n'
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
// D11: validate and normalize a root-heading label. Newline characters
|
|
34
|
+
// would split the heading across lines and corrupt the document, so we
|
|
35
|
+
// reject them outright; surrounding whitespace is silently stripped, but
|
|
36
|
+
// a leading mode prefix (`- `, `? `, `! `) is preserved intact.
|
|
37
|
+
export function validateLabel(label) {
|
|
38
|
+
if (typeof label !== 'string') {
|
|
39
|
+
throw new TypeError('Label must be a string')
|
|
40
|
+
}
|
|
41
|
+
if (label.includes('\n') || label.includes('\r')) {
|
|
42
|
+
throw new RangeError(
|
|
43
|
+
'JMD root labels must not contain newline characters; got '
|
|
44
|
+
+ JSON.stringify(label),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
let s = label.replace(/^\s+/, '')
|
|
48
|
+
if (s.length >= 2
|
|
49
|
+
&& (s[0] === '-' || s[0] === '?' || s[0] === '!')
|
|
50
|
+
&& s[1] === ' ') {
|
|
51
|
+
return s.slice(0, 2) + s.slice(2).replace(/\s+$/, '')
|
|
52
|
+
}
|
|
53
|
+
return s.replace(/\s+$/, '')
|
|
54
|
+
}
|
|
55
|
+
|
|
32
56
|
// Mode markers attach directly to `#` in the root heading: `#- Order`,
|
|
33
57
|
// `#? Order`, `#! Order`. Callers pass the mark as a `- `, `? ` or `! `
|
|
34
58
|
// prefix on `label`; the serializer attaches it to `#` without a space
|
|
@@ -45,8 +69,18 @@ function splitLabel(label) {
|
|
|
45
69
|
function emitDocument(value, label, frontmatter, lines) {
|
|
46
70
|
if (frontmatter && Object.keys(frontmatter).length > 0) {
|
|
47
71
|
for (const [k, v] of Object.entries(frontmatter)) {
|
|
48
|
-
|
|
49
|
-
|
|
72
|
+
const qk = serializeKey(k)
|
|
73
|
+
if (v === true) {
|
|
74
|
+
lines.push(qk)
|
|
75
|
+
} else if (typeof v === 'string' && v.includes('\n')) {
|
|
76
|
+
// D12: multi-line frontmatter values go in the blockquote form,
|
|
77
|
+
// matching the body serializer's handling of multi-line scalars
|
|
78
|
+
// (§9.1) so the value round-trips through frontmatter parsing.
|
|
79
|
+
lines.push(qk + ':')
|
|
80
|
+
writeMultiline(v, lines)
|
|
81
|
+
} else {
|
|
82
|
+
lines.push(qk + ': ' + serializeScalar(v))
|
|
83
|
+
}
|
|
50
84
|
}
|
|
51
85
|
lines.push('')
|
|
52
86
|
}
|
|
@@ -133,24 +167,36 @@ function writeArrayItems(lst, lines, depth) {
|
|
|
133
167
|
return
|
|
134
168
|
}
|
|
135
169
|
|
|
136
|
-
// Heterogeneous array.
|
|
170
|
+
// Heterogeneous array — items mixing scalars, dicts, sub-arrays.
|
|
137
171
|
//
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
//
|
|
172
|
+
// After any item that opens a sub-scope (a nested list, or a dict
|
|
173
|
+
// with nested fields), the NEXT item needs an explicit depth-qualified
|
|
174
|
+
// heading `## - ...` (§8.6a same-depth form) so the parser pops out
|
|
175
|
+
// of the sub-scope and attaches the item to *this* array. The
|
|
176
|
+
// qualifier uses the array's own scope depth — items[] opened by
|
|
177
|
+
// `## items[]` at depth 2 take a `## - ...` prefix.
|
|
178
|
+
const qualifier = heading(depth)
|
|
179
|
+
let needsQualifier = false
|
|
141
180
|
for (const item of lst) {
|
|
142
181
|
if (isPlainObject(item)) {
|
|
143
|
-
|
|
182
|
+
const hasNested = Object.values(item).some(isNested)
|
|
183
|
+
writeDictItem(item, lines, depth, false, needsQualifier ? qualifier : '')
|
|
184
|
+
needsQualifier = hasNested
|
|
144
185
|
} else if (Array.isArray(item)) {
|
|
186
|
+
// Anonymous sub-array still opens at depth+1; only the item-
|
|
187
|
+
// qualifier shrinks to same-depth.
|
|
145
188
|
lines.push(heading(depth + 1) + '[]')
|
|
146
189
|
writeArrayItems(item, lines, depth + 1)
|
|
190
|
+
needsQualifier = true
|
|
147
191
|
} else {
|
|
148
|
-
|
|
192
|
+
const pfx = needsQualifier ? qualifier : ''
|
|
193
|
+
lines.push(pfx + '- ' + serializeScalar(item))
|
|
194
|
+
needsQualifier = false
|
|
149
195
|
}
|
|
150
196
|
}
|
|
151
197
|
}
|
|
152
198
|
|
|
153
|
-
function writeDictItem(item, lines, depth, separatorNeeded) {
|
|
199
|
+
function writeDictItem(item, lines, depth, separatorNeeded, qualifierPrefix = '') {
|
|
154
200
|
const scalarFields = []
|
|
155
201
|
const nestedFields = []
|
|
156
202
|
for (const [k, v] of Object.entries(item)) {
|
|
@@ -167,14 +213,14 @@ function writeDictItem(item, lines, depth, separatorNeeded) {
|
|
|
167
213
|
}
|
|
168
214
|
|
|
169
215
|
if (scalarFields.length === 0) {
|
|
170
|
-
lines.push('-')
|
|
216
|
+
lines.push(qualifierPrefix + '-')
|
|
171
217
|
} else {
|
|
172
218
|
let first = true
|
|
173
219
|
for (const [k, v] of scalarFields) {
|
|
174
220
|
const sv = serializeScalar(v)
|
|
175
221
|
const qk = serializeKey(k)
|
|
176
222
|
if (first) {
|
|
177
|
-
lines.push('- ' + qk + ': ' + sv)
|
|
223
|
+
lines.push(qualifierPrefix + '- ' + qk + ': ' + sv)
|
|
178
224
|
first = false
|
|
179
225
|
} else {
|
|
180
226
|
lines.push(' ' + qk + ': ' + sv)
|
package/src/value.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
1
2
|
// Scalar values and keys.
|
|
2
3
|
//
|
|
3
4
|
// Parsing and serialization of the smallest JMD units: scalar values
|
|
@@ -89,8 +90,14 @@ export function serializeKey(key) {
|
|
|
89
90
|
return JSON.stringify(key)
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
// Parse `key: value
|
|
93
|
-
//
|
|
93
|
+
// Parse `key: value`, `key:` (empty / blockquote-opening), or
|
|
94
|
+
// `key: |` / `key: >` (block-scalar opening, §5.2).
|
|
95
|
+
// Returns one of:
|
|
96
|
+
// { key, value } — scalar field
|
|
97
|
+
// { key, empty: true } — empty value, opens a blockquote field
|
|
98
|
+
// { key, block: '|' } — literal block scalar
|
|
99
|
+
// { key, block: '>' } — folded block scalar
|
|
100
|
+
// or null if the line is not a field.
|
|
94
101
|
export function parseField(line) {
|
|
95
102
|
const pk = parseKey(line)
|
|
96
103
|
if (!pk) return null
|
|
@@ -102,5 +109,8 @@ export function parseField(line) {
|
|
|
102
109
|
return { key: pk.key, empty: true }
|
|
103
110
|
}
|
|
104
111
|
if (after.charCodeAt(0) !== 32 /* space */) return null
|
|
105
|
-
|
|
112
|
+
const value = after.slice(1)
|
|
113
|
+
if (value === '|') return { key: pk.key, block: '|' }
|
|
114
|
+
if (value === '>') return { key: pk.key, block: '>' }
|
|
115
|
+
return { key: pk.key, value: parseScalar(value) }
|
|
106
116
|
}
|