@art-md/constructs 0.0.4 → 0.0.6
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/CHANGELOG.md +63 -0
- package/README.md +1 -1
- package/dist/factories/constructs/Document/factory/createDocument.d.ts +3 -0
- package/dist/factories/constructs/Document/factory/types.d.ts +4 -0
- package/dist/factories/constructs/Document/index.d.ts +2 -0
- package/dist/factories/constructs/FieldBlock/factory/createFieldBlock.d.ts +3 -0
- package/dist/factories/constructs/FieldBlock/factory/types.d.ts +7 -0
- package/dist/factories/constructs/FieldBlock/index.d.ts +3 -0
- package/dist/factories/constructs/FieldBlock/types.d.ts +9 -0
- package/dist/factories/constructs/FieldInline/factory/createFieldInline.d.ts +3 -0
- package/dist/factories/constructs/FieldInline/factory/types.d.ts +7 -0
- package/dist/factories/constructs/FieldInline/index.d.ts +3 -0
- package/dist/factories/constructs/FieldInline/types.d.ts +9 -0
- package/dist/factories/constructs/NaturalBlock/factory/createNaturalBlock.d.ts +3 -0
- package/dist/factories/constructs/NaturalBlock/factory/types.d.ts +11 -0
- package/dist/factories/constructs/NaturalBlock/index.d.ts +3 -0
- package/dist/factories/constructs/NaturalBlock/types.d.ts +12 -0
- package/dist/factories/constructs/NaturalExpression/factory/createNaturalExpression.d.ts +3 -0
- package/dist/factories/constructs/NaturalExpression/factory/types.d.ts +7 -0
- package/dist/factories/constructs/NaturalExpression/index.d.ts +3 -0
- package/dist/factories/constructs/NaturalExpression/types.d.ts +8 -0
- package/dist/factories/constructs/SectionBlock/factory/createSectionBlock.d.ts +3 -0
- package/dist/factories/constructs/SectionBlock/factory/types.d.ts +8 -0
- package/dist/factories/constructs/SectionBlock/index.d.ts +3 -0
- package/dist/factories/constructs/SectionBlock/types.d.ts +9 -0
- package/dist/factories/constructs/Tag/factory/createTag.d.ts +3 -0
- package/dist/factories/constructs/Tag/factory/types.d.ts +3 -0
- package/dist/factories/constructs/Tag/index.d.ts +3 -0
- package/dist/factories/constructs/Tag/types.d.ts +5 -0
- package/dist/factories/index.d.ts +8 -0
- package/dist/factories/public.d.ts +36 -0
- package/dist/factories/types.d.ts +31 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +627 -0
- package/dist/index.mjs.map +7 -0
- package/dist/parser/constructs/FieldBlock/createFieldBlockParser.d.ts +2 -0
- package/dist/parser/constructs/FieldBlock/index.d.ts +1 -0
- package/dist/parser/constructs/FieldBlock/private/helpers/createFieldBlockFromNode.d.ts +4 -0
- package/dist/parser/constructs/FieldBlock/private/helpers/onBeforeConstruct.d.ts +2 -0
- package/dist/parser/constructs/FieldBlock/private/index.d.ts +2 -0
- package/dist/parser/constructs/FieldBlock/private/integrator/createFieldBlockIntegrator.d.ts +2 -0
- package/dist/parser/constructs/FieldBlock/private/processor/createFieldBlockProcessor.d.ts +2 -0
- package/dist/parser/constructs/FieldInline/createFieldInlineParser.d.ts +2 -0
- package/dist/parser/constructs/FieldInline/index.d.ts +1 -0
- package/dist/parser/constructs/FieldInline/private/helpers/createFieldInlineFromNode.d.ts +4 -0
- package/dist/parser/constructs/FieldInline/private/helpers/trimFieldEdges.d.ts +2 -0
- package/dist/parser/constructs/FieldInline/private/index.d.ts +1 -0
- package/dist/parser/constructs/FieldInline/private/processor/createFieldInlineProcessor.d.ts +2 -0
- package/dist/parser/constructs/NaturalBlock/createNaturalBlockParser.d.ts +2 -0
- package/dist/parser/constructs/NaturalBlock/index.d.ts +1 -0
- package/dist/parser/constructs/NaturalBlock/private/helpers/createNaturalBlockFromNode.d.ts +4 -0
- package/dist/parser/constructs/NaturalBlock/private/index.d.ts +1 -0
- package/dist/parser/constructs/NaturalBlock/private/processor/createNaturalBlockProcessor.d.ts +2 -0
- package/dist/parser/constructs/SectionBlock/createSectionBlockParser.d.ts +2 -0
- package/dist/parser/constructs/SectionBlock/index.d.ts +1 -0
- package/dist/parser/constructs/SectionBlock/private/helpers/createSectionBlockFromNode.d.ts +4 -0
- package/dist/parser/constructs/SectionBlock/private/helpers/findParentSection.d.ts +3 -0
- package/dist/parser/constructs/SectionBlock/private/index.d.ts +2 -0
- package/dist/parser/constructs/SectionBlock/private/integrator/createSectionBlockIntegrator.d.ts +2 -0
- package/dist/parser/constructs/SectionBlock/private/processor/createSectionBlockProcessor.d.ts +2 -0
- package/dist/parser/document/createArtDocumentFromNode.d.ts +3 -0
- package/dist/parser/document/index.d.ts +1 -0
- package/dist/parser/fields/constants.d.ts +1 -0
- package/dist/parser/fields/index.d.ts +3 -0
- package/dist/parser/fields/isFieldStrong.d.ts +3 -0
- package/dist/parser/fields/stripStrong.d.ts +3 -0
- package/dist/parser/index.d.ts +6 -0
- package/dist/parser/mdast/index.d.ts +1 -0
- package/dist/parser/mdast/rawSlice.d.ts +2 -0
- package/dist/parser/naturalExpression/createNaturalExpressionFromNode.d.ts +3 -0
- package/dist/parser/naturalExpression/index.d.ts +1 -0
- package/dist/parser/public.d.ts +5 -0
- package/dist/parser/tags/extractTags.d.ts +5 -0
- package/dist/parser/tags/index.d.ts +1 -0
- package/dist/parser/types.d.ts +14 -0
- package/dist/serializer/constructs/Document/createDocumentToMdast.d.ts +2 -0
- package/dist/serializer/constructs/Document/index.d.ts +1 -0
- package/dist/serializer/constructs/FieldBlock/createFieldBlockToMdast.d.ts +2 -0
- package/dist/serializer/constructs/FieldBlock/index.d.ts +1 -0
- package/dist/serializer/constructs/FieldInline/createFieldInlineToMdast.d.ts +2 -0
- package/dist/serializer/constructs/FieldInline/index.d.ts +1 -0
- package/dist/serializer/constructs/NaturalBlock/createNaturalBlockToMdast.d.ts +2 -0
- package/dist/serializer/constructs/NaturalBlock/index.d.ts +1 -0
- package/dist/serializer/constructs/NaturalExpression/createNaturalExpressionToMdast.d.ts +2 -0
- package/dist/serializer/constructs/NaturalExpression/index.d.ts +1 -0
- package/dist/serializer/constructs/SectionBlock/createSectionBlockToMdast.d.ts +2 -0
- package/dist/serializer/constructs/SectionBlock/index.d.ts +1 -0
- package/dist/serializer/index.d.ts +7 -0
- package/dist/serializer/public.d.ts +3 -0
- package/dist/serializer/tags/index.d.ts +2 -0
- package/dist/serializer/tags/tagToMdast.d.ts +3 -0
- package/dist/serializer/tags/tagsToMdast.d.ts +3 -0
- package/dist/serializer/types.d.ts +7 -0
- package/dist/test/helpers/constructs/FieldBlock/createFieldBlockFromNodeMock.d.ts +7 -0
- package/dist/test/helpers/constructs/FieldBlock/createFieldBlockMock.d.ts +7 -0
- package/dist/test/helpers/constructs/FieldBlock/makeFieldBlockFixture.d.ts +9 -0
- package/dist/test/helpers/constructs/FieldBlock/makeFieldBlockMock.d.ts +9 -0
- package/dist/test/helpers/constructs/FieldInline/createFieldInlineFromNodeMock.d.ts +7 -0
- package/dist/test/helpers/constructs/FieldInline/createFieldInlineMock.d.ts +7 -0
- package/dist/test/helpers/constructs/FieldInline/makeFieldInlineFixture.d.ts +9 -0
- package/dist/test/helpers/constructs/FieldInline/makeFieldInlineMock.d.ts +9 -0
- package/dist/test/helpers/constructs/NaturalBlock/createNaturalBlockFromNodeMock.d.ts +7 -0
- package/dist/test/helpers/constructs/NaturalBlock/createNaturalBlockMock.d.ts +7 -0
- package/dist/test/helpers/constructs/NaturalBlock/makeNaturalBlockFixture.d.ts +12 -0
- package/dist/test/helpers/constructs/NaturalBlock/makeNaturalBlockMock.d.ts +12 -0
- package/dist/test/helpers/constructs/NaturalExpression/makeNaturalExpressionFixture.d.ts +10 -0
- package/dist/test/helpers/constructs/NaturalExpression/makeNaturalExpressionMock.d.ts +10 -0
- package/dist/test/helpers/constructs/SectionBlock/createSectionBlockFromNodeMock.d.ts +7 -0
- package/dist/test/helpers/constructs/SectionBlock/createSectionBlockMock.d.ts +7 -0
- package/dist/test/helpers/constructs/SectionBlock/makeSectionBlockFixture.d.ts +10 -0
- package/dist/test/helpers/constructs/Tag/makeTagFixture.d.ts +7 -0
- package/dist/test/helpers/constructs/index.d.ts +14 -0
- package/dist/test/helpers/index.d.ts +2 -0
- package/dist/test/helpers/parsers/fields/stripStrongMock.d.ts +7 -0
- package/dist/test/helpers/parsers/index.d.ts +5 -0
- package/dist/test/helpers/parsers/mdast/rawSliceMock.d.ts +7 -0
- package/dist/test/helpers/parsers/natural-expression/createNaturalExpressionFromNodeMock.d.ts +7 -0
- package/dist/test/helpers/parsers/tags/extractTagsMock.d.ts +7 -0
- package/dist/test/helpers/parsers/tags/tagToMdastMock.d.ts +7 -0
- package/package.json +34 -15
- package/src/parser/constructs/FieldBlock/private/helpers/createFieldBlockFromNode.test.ts +1 -1
- package/src/parser/constructs/FieldBlock/private/integrator/createFieldBlockIntegrator.test.ts +1 -1
- package/src/parser/constructs/FieldBlock/private/processor/createFieldBlockProcessor.test.ts +1 -1
- package/src/parser/constructs/FieldInline/private/helpers/createFieldInlineFromNode.test.ts +1 -1
- package/src/parser/constructs/FieldInline/private/processor/createFieldInlineProcessor.test.ts +1 -1
- package/src/parser/constructs/NaturalBlock/private/helpers/createNaturalBlockFromNode.test.ts +1 -1
- package/src/parser/constructs/NaturalBlock/private/processor/createNaturalBlockProcessor.test.ts +1 -1
- package/src/parser/constructs/SectionBlock/private/helpers/createSectionBlockFromNode.test.ts +1 -1
- package/src/parser/constructs/SectionBlock/private/helpers/findParentSection.test.ts +1 -1
- package/src/parser/constructs/SectionBlock/private/integrator/createSectionBlockIntegrator.test.ts +1 -1
- package/src/parser/constructs/SectionBlock/private/processor/createSectionBlockProcessor.test.ts +1 -1
- package/src/parser/document/createArtDocumentFromNode.test.ts +1 -1
- package/src/parser/naturalExpression/createNaturalExpressionFromNode.test.ts +1 -6
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
// ../primitives/dist/index.mjs
|
|
2
|
+
function createArtDocument(data) {
|
|
3
|
+
return {
|
|
4
|
+
construct: "Document",
|
|
5
|
+
children: data?.children ?? []
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
function nodePosition(node) {
|
|
9
|
+
if (!node.position) {
|
|
10
|
+
throw new Error(`Expected source position for ${node.type}`);
|
|
11
|
+
}
|
|
12
|
+
const raw = node.position;
|
|
13
|
+
if (!raw?.start || !raw.end) {
|
|
14
|
+
throw new Error(`Expected source position for ${node.type}`);
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
start: {
|
|
18
|
+
line: raw.start.line,
|
|
19
|
+
column: raw.start.column,
|
|
20
|
+
offset: raw.start.offset ?? 0
|
|
21
|
+
},
|
|
22
|
+
end: {
|
|
23
|
+
line: raw.end.line,
|
|
24
|
+
column: raw.end.column,
|
|
25
|
+
offset: raw.end.offset ?? 0
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function sectionDepth(section) {
|
|
30
|
+
return section.depth ?? 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/factories/constructs/Document/factory/createDocument.ts
|
|
34
|
+
function createDocument(data) {
|
|
35
|
+
const docData = {
|
|
36
|
+
...data,
|
|
37
|
+
children: data.children || []
|
|
38
|
+
};
|
|
39
|
+
return createArtDocument(docData);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/factories/constructs/FieldBlock/factory/createFieldBlock.ts
|
|
43
|
+
function createFieldBlock(data) {
|
|
44
|
+
const field = {
|
|
45
|
+
construct: "FieldBlock",
|
|
46
|
+
name: data.name,
|
|
47
|
+
children: data.children ?? []
|
|
48
|
+
};
|
|
49
|
+
if (data.tags?.length) {
|
|
50
|
+
field.tags = data.tags;
|
|
51
|
+
}
|
|
52
|
+
return field;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/factories/constructs/FieldInline/factory/createFieldInline.ts
|
|
56
|
+
function createFieldInline(data) {
|
|
57
|
+
const field = {
|
|
58
|
+
construct: "FieldInline",
|
|
59
|
+
name: data.name,
|
|
60
|
+
children: data.children ?? []
|
|
61
|
+
};
|
|
62
|
+
if (data.tags?.length) {
|
|
63
|
+
field.tags = data.tags;
|
|
64
|
+
}
|
|
65
|
+
return field;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/factories/constructs/NaturalBlock/factory/createNaturalBlock.ts
|
|
69
|
+
function createNaturalBlock(data) {
|
|
70
|
+
const block = {
|
|
71
|
+
...data.attributes,
|
|
72
|
+
construct: "NaturalBlock",
|
|
73
|
+
value: data.value,
|
|
74
|
+
children: data.children
|
|
75
|
+
};
|
|
76
|
+
if (data.type) {
|
|
77
|
+
block.type = data.type;
|
|
78
|
+
}
|
|
79
|
+
if (data.lang !== void 0) {
|
|
80
|
+
block.lang = data.lang;
|
|
81
|
+
}
|
|
82
|
+
if (data.meta !== void 0) {
|
|
83
|
+
block.meta = data.meta;
|
|
84
|
+
}
|
|
85
|
+
if (data.tags?.length) {
|
|
86
|
+
block.tags = data.tags;
|
|
87
|
+
}
|
|
88
|
+
return block;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/factories/constructs/NaturalExpression/factory/createNaturalExpression.ts
|
|
92
|
+
function createNaturalExpression(data) {
|
|
93
|
+
return {
|
|
94
|
+
construct: "NaturalExpression",
|
|
95
|
+
type: data.type,
|
|
96
|
+
value: data.value,
|
|
97
|
+
attributes: data.attributes,
|
|
98
|
+
children: data.children ?? []
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/factories/constructs/SectionBlock/factory/createSectionBlock.ts
|
|
103
|
+
function createSectionBlock(data) {
|
|
104
|
+
const section = {
|
|
105
|
+
construct: "SectionBlock",
|
|
106
|
+
name: data.name,
|
|
107
|
+
children: data.children ?? []
|
|
108
|
+
};
|
|
109
|
+
if (data.depth !== void 0) section.depth = data.depth;
|
|
110
|
+
if (data.tags?.length) section.tags = data.tags;
|
|
111
|
+
return section;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/factories/constructs/Tag/factory/createTag.ts
|
|
115
|
+
function createTag(tagData) {
|
|
116
|
+
return { construct: "Tag", name: tagData.name };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/factories/public.ts
|
|
120
|
+
var FACTORIES = [
|
|
121
|
+
{
|
|
122
|
+
name: "Document",
|
|
123
|
+
factory: createDocument
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: "FieldBlock",
|
|
127
|
+
factory: createFieldBlock
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: "FieldInline",
|
|
131
|
+
factory: createFieldInline
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "NaturalBlock",
|
|
135
|
+
factory: createNaturalBlock
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: "NaturalExpression",
|
|
139
|
+
factory: createNaturalExpression
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
name: "SectionBlock",
|
|
143
|
+
factory: createSectionBlock
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: "Tag",
|
|
147
|
+
factory: createTag
|
|
148
|
+
}
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
// src/parser/constructs/FieldBlock/private/helpers/onBeforeConstruct.ts
|
|
152
|
+
var FIELD_BLOCK_BOUNDARIES = /* @__PURE__ */ new Set(["FieldBlock", "FieldInline", "SectionBlock"]);
|
|
153
|
+
function onBeforeConstruct(construct, context) {
|
|
154
|
+
if (!FIELD_BLOCK_BOUNDARIES.has(construct.construct)) {
|
|
155
|
+
return context;
|
|
156
|
+
}
|
|
157
|
+
const parent = context.parent();
|
|
158
|
+
if (!parent) {
|
|
159
|
+
return context;
|
|
160
|
+
}
|
|
161
|
+
return parent;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/parser/constructs/FieldBlock/private/integrator/createFieldBlockIntegrator.ts
|
|
165
|
+
function createFieldBlockIntegrator() {
|
|
166
|
+
return {
|
|
167
|
+
integrate(context, _node, construct) {
|
|
168
|
+
const field = construct;
|
|
169
|
+
context.captureChildConstruct(field);
|
|
170
|
+
return context.childContext(field, onBeforeConstruct);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/parser/fields/constants.ts
|
|
176
|
+
var FIELD_TEXT_PATTERN = /^[A-Za-z][A-Za-z ]*:(?:\s|$)/;
|
|
177
|
+
|
|
178
|
+
// src/parser/mdast/rawSlice.ts
|
|
179
|
+
function rawSlice(node, context) {
|
|
180
|
+
if (!node.position?.start || !node.position?.end) {
|
|
181
|
+
return "";
|
|
182
|
+
}
|
|
183
|
+
return context.source.markdown.slice(node.position.start.offset, node.position.end.offset);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// src/parser/fields/stripStrong.ts
|
|
187
|
+
function stripStrong(node, context) {
|
|
188
|
+
const raw = rawSlice(node, context);
|
|
189
|
+
return raw.length >= 4 && (raw.startsWith("**") && raw.endsWith("**") || raw.startsWith("__") && raw.endsWith("__")) ? raw.slice(2, -2) : raw;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/parser/fields/isFieldStrong.ts
|
|
193
|
+
function isFieldStrong(node, context) {
|
|
194
|
+
return node.type === "strong" && FIELD_TEXT_PATTERN.test(stripStrong(node, context));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/parser/tags/extractTags.ts
|
|
198
|
+
var TRAILING_TAGS = /^(.*?)((?:\(#[\w-]+\)\s*)+)$/;
|
|
199
|
+
var TAG = /\(#([\w-]+)\)/g;
|
|
200
|
+
function extractTags(text) {
|
|
201
|
+
const trimmed = text.trimEnd();
|
|
202
|
+
const match = trimmed.match(TRAILING_TAGS);
|
|
203
|
+
if (!match || !match[1] || !match[2]) {
|
|
204
|
+
return {
|
|
205
|
+
tags: [],
|
|
206
|
+
stripped: text
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const tags = [...match[2].matchAll(TAG)].map(
|
|
210
|
+
(matchResult) => createTag({ name: matchResult[1] ?? "" })
|
|
211
|
+
);
|
|
212
|
+
return {
|
|
213
|
+
tags,
|
|
214
|
+
stripped: match[1].trimEnd()
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/parser/constructs/FieldBlock/private/helpers/createFieldBlockFromNode.ts
|
|
219
|
+
function createFieldBlockFromNode(paragraph, context) {
|
|
220
|
+
const strong = paragraph.children[0];
|
|
221
|
+
const paragraphRaw = rawSlice(paragraph, context);
|
|
222
|
+
const strongRaw = rawSlice(strong, context);
|
|
223
|
+
const afterStrong = paragraphRaw.slice(strongRaw.length);
|
|
224
|
+
const { tags, stripped } = extractTags(afterStrong);
|
|
225
|
+
if (stripped.trim().length > 0) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const inner = stripStrong(strong, context);
|
|
229
|
+
const colonIndex = inner.indexOf(":");
|
|
230
|
+
const field = createFieldBlock({
|
|
231
|
+
name: inner.slice(0, colonIndex).trim(),
|
|
232
|
+
tags
|
|
233
|
+
});
|
|
234
|
+
field.position = nodePosition(paragraph);
|
|
235
|
+
return field;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/parser/constructs/FieldBlock/private/processor/createFieldBlockProcessor.ts
|
|
239
|
+
function createFieldBlockProcessor() {
|
|
240
|
+
return {
|
|
241
|
+
captureNode(context, node) {
|
|
242
|
+
if (node.type !== "paragraph") {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const paragraph = node;
|
|
246
|
+
const first = paragraph.children[0];
|
|
247
|
+
if (first === void 0 || !isFieldStrong(first, context)) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return createFieldBlockFromNode(paragraph, context);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/parser/constructs/FieldBlock/createFieldBlockParser.ts
|
|
256
|
+
var createFieldBlockParser = () => ({
|
|
257
|
+
name: "FieldBlock",
|
|
258
|
+
processor: createFieldBlockProcessor(),
|
|
259
|
+
integrator: createFieldBlockIntegrator()
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// src/parser/naturalExpression/createNaturalExpressionFromNode.ts
|
|
263
|
+
function createNaturalExpressionFromNode(node) {
|
|
264
|
+
const { position: _, children: mdastChildren, type, value, ...attributes } = node;
|
|
265
|
+
let children = [];
|
|
266
|
+
if (Array.isArray(mdastChildren)) {
|
|
267
|
+
children = mdastChildren.map((child) => createNaturalExpressionFromNode(child));
|
|
268
|
+
}
|
|
269
|
+
const expression = {
|
|
270
|
+
construct: "NaturalExpression",
|
|
271
|
+
type,
|
|
272
|
+
attributes,
|
|
273
|
+
value,
|
|
274
|
+
position: nodePosition(node),
|
|
275
|
+
children
|
|
276
|
+
};
|
|
277
|
+
return expression;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/parser/constructs/FieldInline/private/helpers/trimFieldEdges.ts
|
|
281
|
+
function trimFieldEdges(value) {
|
|
282
|
+
const trimmed = [...value];
|
|
283
|
+
const first = trimmed[0];
|
|
284
|
+
const last = trimmed[trimmed.length - 1];
|
|
285
|
+
if (first?.type === "text" && typeof first?.value === "string")
|
|
286
|
+
first.value = first.value.replace(/^\s+/, "");
|
|
287
|
+
if (last?.type === "text" && typeof last?.value === "string")
|
|
288
|
+
last.value = last.value.replace(/\s+$/, "");
|
|
289
|
+
return trimmed.filter(
|
|
290
|
+
(expression) => expression.type !== "text" || typeof expression.value === "string" && expression.value.length > 0
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// src/parser/constructs/FieldInline/private/helpers/createFieldInlineFromNode.ts
|
|
295
|
+
function createFieldInlineFromNode(paragraph, context) {
|
|
296
|
+
const strong = paragraph.children[0];
|
|
297
|
+
const paragraphRaw = rawSlice(paragraph, context);
|
|
298
|
+
const strongRaw = rawSlice(strong, context);
|
|
299
|
+
const afterStrong = paragraphRaw.slice(strongRaw.length);
|
|
300
|
+
const { stripped } = extractTags(afterStrong);
|
|
301
|
+
if (stripped.trim().length === 0) {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
const inner = stripStrong(strong, context);
|
|
305
|
+
const colonIndex = inner.indexOf(":");
|
|
306
|
+
const expressions = paragraph.children.slice(1).map((child) => createNaturalExpressionFromNode(child));
|
|
307
|
+
const children = trimFieldEdges(expressions);
|
|
308
|
+
const last = children[children.length - 1];
|
|
309
|
+
let tags = [];
|
|
310
|
+
if (last?.type === "text" && typeof last.value === "string") {
|
|
311
|
+
const extracted = extractTags(last.value);
|
|
312
|
+
if (extracted.tags.length) {
|
|
313
|
+
tags = extracted.tags;
|
|
314
|
+
last.value = extracted.stripped;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const field = createFieldInline({
|
|
318
|
+
name: inner.slice(0, colonIndex).trim(),
|
|
319
|
+
children,
|
|
320
|
+
tags
|
|
321
|
+
});
|
|
322
|
+
field.position = nodePosition(paragraph);
|
|
323
|
+
return field;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/parser/constructs/FieldInline/private/processor/createFieldInlineProcessor.ts
|
|
327
|
+
function createFieldInlineProcessor() {
|
|
328
|
+
return {
|
|
329
|
+
captureNode(context, node) {
|
|
330
|
+
if (node.type !== "paragraph") {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
const paragraph = node;
|
|
334
|
+
const first = paragraph.children[0];
|
|
335
|
+
if (first === void 0 || !isFieldStrong(first, context)) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
return createFieldInlineFromNode(paragraph, context);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/parser/constructs/FieldInline/createFieldInlineParser.ts
|
|
344
|
+
var createFieldInlineParser = () => ({
|
|
345
|
+
name: "FieldInline",
|
|
346
|
+
processor: createFieldInlineProcessor()
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
// src/parser/constructs/NaturalBlock/private/helpers/createNaturalBlockFromNode.ts
|
|
350
|
+
import { phrasing } from "mdast-util-phrasing";
|
|
351
|
+
function createNaturalBlockFromNode(node, context) {
|
|
352
|
+
let children = [];
|
|
353
|
+
if (Array.isArray(node.children)) {
|
|
354
|
+
const nodeChildren = node.children ?? [];
|
|
355
|
+
const phrasingContainer = node.type === "paragraph" || node.type === "heading" || node.type === "tableCell";
|
|
356
|
+
children = nodeChildren.map(
|
|
357
|
+
(child) => phrasingContainer || phrasing(child) ? createNaturalExpressionFromNode(child) : createNaturalBlockFromNode(child, context)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
let tags = [];
|
|
361
|
+
if (node.type === "paragraph") {
|
|
362
|
+
const last = children[children.length - 1];
|
|
363
|
+
if (last?.type === "text" && typeof last.value === "string") {
|
|
364
|
+
const { tags: extractedTags, stripped } = extractTags(last.value);
|
|
365
|
+
if (extractedTags.length) {
|
|
366
|
+
tags = extractedTags;
|
|
367
|
+
last.value = stripped;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const { type, ...attributes } = node;
|
|
372
|
+
const block = createNaturalBlock({
|
|
373
|
+
value: rawSlice(node, context),
|
|
374
|
+
children,
|
|
375
|
+
type,
|
|
376
|
+
attributes,
|
|
377
|
+
tags
|
|
378
|
+
});
|
|
379
|
+
block.position = nodePosition(node);
|
|
380
|
+
return block;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// src/parser/constructs/NaturalBlock/private/processor/createNaturalBlockProcessor.ts
|
|
384
|
+
function createNaturalBlockProcessor() {
|
|
385
|
+
return {
|
|
386
|
+
captureNode(context, node) {
|
|
387
|
+
return createNaturalBlockFromNode(node, context);
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// src/parser/constructs/NaturalBlock/createNaturalBlockParser.ts
|
|
393
|
+
var createNaturalBlockParser = () => ({
|
|
394
|
+
name: "NaturalBlock",
|
|
395
|
+
processor: createNaturalBlockProcessor()
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// src/parser/constructs/SectionBlock/private/helpers/findParentSection.ts
|
|
399
|
+
function findParentSection(context) {
|
|
400
|
+
let current = context;
|
|
401
|
+
while (current) {
|
|
402
|
+
if (current.construct?.construct === "SectionBlock") {
|
|
403
|
+
return current.construct;
|
|
404
|
+
}
|
|
405
|
+
current = current.parent();
|
|
406
|
+
}
|
|
407
|
+
return void 0;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// src/parser/constructs/SectionBlock/private/integrator/createSectionBlockIntegrator.ts
|
|
411
|
+
function createSectionBlockIntegrator() {
|
|
412
|
+
return {
|
|
413
|
+
integrate(context, node, construct) {
|
|
414
|
+
const section = construct;
|
|
415
|
+
let currentContext = context;
|
|
416
|
+
const heading = node;
|
|
417
|
+
while (currentContext.construct.construct === "SectionBlock") {
|
|
418
|
+
const parentSection = findParentSection(currentContext);
|
|
419
|
+
if (parentSection && sectionDepth(parentSection) >= heading.depth) {
|
|
420
|
+
const parentContext = currentContext.parent();
|
|
421
|
+
if (parentContext) {
|
|
422
|
+
currentContext = parentContext;
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
currentContext.captureChildConstruct(section);
|
|
429
|
+
return currentContext.childContext(section);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// src/parser/constructs/SectionBlock/private/helpers/createSectionBlockFromNode.ts
|
|
435
|
+
function createSectionBlockFromNode(node, context) {
|
|
436
|
+
const text = rawSlice(node, context).replace(/^[ \t]*#+[ \t]*/, "").trim();
|
|
437
|
+
const { tags, stripped } = extractTags(text);
|
|
438
|
+
const section = createSectionBlock({
|
|
439
|
+
name: stripped,
|
|
440
|
+
depth: node.depth,
|
|
441
|
+
children: [],
|
|
442
|
+
tags
|
|
443
|
+
});
|
|
444
|
+
section.position = nodePosition(node);
|
|
445
|
+
return section;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/parser/constructs/SectionBlock/private/processor/createSectionBlockProcessor.ts
|
|
449
|
+
function createSectionBlockProcessor() {
|
|
450
|
+
return {
|
|
451
|
+
captureNode(context, node) {
|
|
452
|
+
if (node.type !== "heading") {
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
return createSectionBlockFromNode(node, context);
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/parser/constructs/SectionBlock/createSectionBlockParser.ts
|
|
461
|
+
var createSectionBlockParser = () => ({
|
|
462
|
+
name: "SectionBlock",
|
|
463
|
+
processor: createSectionBlockProcessor(),
|
|
464
|
+
integrator: createSectionBlockIntegrator()
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
// src/parser/document/createArtDocumentFromNode.ts
|
|
468
|
+
function createArtDocumentFromNode(root) {
|
|
469
|
+
const document = createDocument({});
|
|
470
|
+
document.position = nodePosition(root);
|
|
471
|
+
return document;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// src/parser/public.ts
|
|
475
|
+
var CONSTRUCT_PARSERS = [
|
|
476
|
+
createFieldBlockParser,
|
|
477
|
+
createFieldInlineParser,
|
|
478
|
+
createSectionBlockParser
|
|
479
|
+
];
|
|
480
|
+
var DEFAULT_CONSTRUCT_PARSER = createNaturalBlockParser;
|
|
481
|
+
|
|
482
|
+
// src/serializer/constructs/Document/createDocumentToMdast.ts
|
|
483
|
+
function createDocumentToMdast() {
|
|
484
|
+
return {
|
|
485
|
+
name: "Document",
|
|
486
|
+
toMdast(_node, children) {
|
|
487
|
+
return {
|
|
488
|
+
type: "root",
|
|
489
|
+
children
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/serializer/tags/tagToMdast.ts
|
|
496
|
+
function tagToMdast(tag) {
|
|
497
|
+
return { type: "text", value: `(#${tag.name})` };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// src/serializer/tags/tagsToMdast.ts
|
|
501
|
+
function tagsToMdast(tags) {
|
|
502
|
+
const value = tags.map((tag) => tagToMdast(tag).value).join(" ");
|
|
503
|
+
return { type: "text", value: ` ${value}` };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// src/serializer/constructs/FieldBlock/createFieldBlockToMdast.ts
|
|
507
|
+
function createFieldBlockToMdast() {
|
|
508
|
+
return {
|
|
509
|
+
name: "FieldBlock",
|
|
510
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
511
|
+
toMdast(node, _children) {
|
|
512
|
+
const field = node;
|
|
513
|
+
const tagNode = field.tags?.length ? tagsToMdast(field.tags) : null;
|
|
514
|
+
return {
|
|
515
|
+
type: "paragraph",
|
|
516
|
+
children: [
|
|
517
|
+
{
|
|
518
|
+
type: "strong",
|
|
519
|
+
children: [{ type: "text", value: `${field.name}:` }]
|
|
520
|
+
},
|
|
521
|
+
...tagNode ? [tagNode] : []
|
|
522
|
+
]
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// src/serializer/constructs/FieldInline/createFieldInlineToMdast.ts
|
|
529
|
+
function createFieldInlineToMdast() {
|
|
530
|
+
return {
|
|
531
|
+
name: "FieldInline",
|
|
532
|
+
toMdast(node, children) {
|
|
533
|
+
const field = node;
|
|
534
|
+
const tagNode = field.tags?.length ? tagsToMdast(field.tags) : null;
|
|
535
|
+
return {
|
|
536
|
+
type: "paragraph",
|
|
537
|
+
children: [
|
|
538
|
+
{
|
|
539
|
+
type: "strong",
|
|
540
|
+
children: [{ type: "text", value: `${field.name}:` }]
|
|
541
|
+
},
|
|
542
|
+
{ type: "text", value: " " },
|
|
543
|
+
...children,
|
|
544
|
+
...tagNode ? [tagNode] : []
|
|
545
|
+
]
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// src/serializer/constructs/NaturalBlock/createNaturalBlockToMdast.ts
|
|
552
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
553
|
+
function createNaturalBlockToMdast() {
|
|
554
|
+
return {
|
|
555
|
+
name: "NaturalBlock",
|
|
556
|
+
toMdast(node, children) {
|
|
557
|
+
const block = node;
|
|
558
|
+
const parsed = fromMarkdown(block.value);
|
|
559
|
+
if (block.type === "paragraph" && children.length > 0) {
|
|
560
|
+
const paragraph = parsed.children.find((child) => child.type === "paragraph");
|
|
561
|
+
if (paragraph && "children" in paragraph) {
|
|
562
|
+
const tagNode = block.tags?.length ? tagsToMdast(block.tags) : null;
|
|
563
|
+
paragraph.children = [
|
|
564
|
+
...children,
|
|
565
|
+
...tagNode ? [tagNode] : []
|
|
566
|
+
];
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return {
|
|
570
|
+
type: "root",
|
|
571
|
+
children: parsed.children
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// src/serializer/constructs/NaturalExpression/createNaturalExpressionToMdast.ts
|
|
578
|
+
function createNaturalExpressionToMdast() {
|
|
579
|
+
return {
|
|
580
|
+
name: "NaturalExpression",
|
|
581
|
+
toMdast(node, children) {
|
|
582
|
+
const expression = node;
|
|
583
|
+
const attributes = { ...expression.attributes };
|
|
584
|
+
return { type: expression.type, value: expression.value, children, ...attributes };
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/serializer/constructs/SectionBlock/createSectionBlockToMdast.ts
|
|
590
|
+
import { fromMarkdown as fromMarkdown2 } from "mdast-util-from-markdown";
|
|
591
|
+
function createSectionBlockToMdast() {
|
|
592
|
+
return {
|
|
593
|
+
name: "SectionBlock",
|
|
594
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
595
|
+
toMdast(node, _children) {
|
|
596
|
+
const section = node;
|
|
597
|
+
const depth = section.depth ?? 1;
|
|
598
|
+
const parsed = fromMarkdown2(`# ${section.name}`);
|
|
599
|
+
const heading = parsed.children.find((child) => child.type === "heading");
|
|
600
|
+
const tagNode = section.tags?.length ? tagsToMdast(section.tags) : null;
|
|
601
|
+
const children = heading && "children" in heading ? [...heading.children, ...tagNode ? [tagNode] : []] : [{ type: "text", value: section.name }, ...tagNode ? [tagNode] : []];
|
|
602
|
+
return {
|
|
603
|
+
type: "heading",
|
|
604
|
+
depth,
|
|
605
|
+
children
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// src/serializer/public.ts
|
|
612
|
+
var CONSTRUCT_SERIALIZERS = [
|
|
613
|
+
createDocumentToMdast,
|
|
614
|
+
createFieldBlockToMdast,
|
|
615
|
+
createFieldInlineToMdast,
|
|
616
|
+
createNaturalBlockToMdast,
|
|
617
|
+
createNaturalExpressionToMdast,
|
|
618
|
+
createSectionBlockToMdast
|
|
619
|
+
];
|
|
620
|
+
export {
|
|
621
|
+
CONSTRUCT_PARSERS,
|
|
622
|
+
CONSTRUCT_SERIALIZERS,
|
|
623
|
+
DEFAULT_CONSTRUCT_PARSER,
|
|
624
|
+
FACTORIES,
|
|
625
|
+
createArtDocumentFromNode
|
|
626
|
+
};
|
|
627
|
+
//# sourceMappingURL=index.mjs.map
|