mdld-parse 0.9.2 → 0.9.3
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/dist/.nojekyll +0 -0
- package/dist/CNAME +1 -0
- package/dist/README.md +425 -0
- package/dist/docs/API.md +397 -0
- package/dist/docs/Architecture.md +190 -0
- package/dist/docs/Guide.md +322 -0
- package/dist/docs/LLM-MDLD-Inducer.md +550 -0
- package/dist/docs/Parser.md +654 -0
- package/dist/docs/Performance.md +238 -0
- package/dist/docs/Spec-render.md +1439 -0
- package/dist/docs/Subject.md +180 -0
- package/dist/docs/Syntax.md +463 -0
- package/dist/docs/TodoList.md +215 -0
- package/dist/docs/Use-Cases.md +354 -0
- package/dist/docs/index.html +108 -0
- package/dist/docs/index.md +56 -0
- package/dist/docs/origin.md +459 -0
- package/dist/docs/polarity.md +586 -0
- package/dist/docs/primary-subject.md +199 -0
- package/dist/docs/statements.md +226 -0
- package/dist/docs/tokenization-performance-report.md +164 -0
- package/dist/examples/Cookbook.md +32 -0
- package/dist/examples/Status-SHACL.md +33 -0
- package/dist/examples/demo/index.md +35 -0
- package/dist/examples/demo/journal.md +18 -0
- package/dist/examples/demo/minimal.md +11 -0
- package/dist/examples/demo/project.md +15 -0
- package/dist/examples/demo/recipe.md +31 -0
- package/dist/examples/demo/research.md +46 -0
- package/dist/examples/demo/space-mission.md +48 -0
- package/dist/examples/dogfood.md +288 -0
- package/dist/examples/few-shot.md +309 -0
- package/dist/examples/graph/rdf-graph.html +473 -0
- package/dist/examples/graph/rdf_force_graph_web_component.html +506 -0
- package/dist/examples/index.md +45 -0
- package/dist/examples/llm-time-workflow.md +173 -0
- package/dist/examples/prov-o.md +2367 -0
- package/dist/examples/rdf.md +827 -0
- package/dist/examples/shacl.md +3399 -0
- package/dist/examples/statements.md +43 -0
- package/dist/examples/workflow.md +150 -0
- package/dist/index.html +2827 -0
- package/dist/index.js +1548 -0
- package/dist/logo.svg +7 -0
- package/dist/spec/Spec-compact.md +363 -0
- package/dist/spec/Spec-ultra.md +221 -0
- package/dist/spec/Spec.md +1068 -0
- package/dist/spec/index.md +46 -0
- package/package.json +7 -8
- package/src/constants.js +0 -31
- package/src/generate.js +0 -245
- package/src/highlight.js +0 -362
- package/src/index.js +0 -13
- package/src/locate.js +0 -23
- package/src/merge.js +0 -157
- package/src/parse.js +0 -711
- package/src/render.js +0 -576
- package/src/shared.js +0 -577
- package/src/tokenizers.js +0 -594
- package/src/utils.js +0 -430
package/src/highlight.js
DELETED
|
@@ -1,362 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MD-LD Syntax Highlighter
|
|
3
|
-
*
|
|
4
|
-
* A performant, token-based syntax highlighter for MD-LD (Markdown Linked Data).
|
|
5
|
-
* Uses simple string operations rather than complex grammars for reliability
|
|
6
|
-
* and maintainability.
|
|
7
|
-
*
|
|
8
|
-
* Features:
|
|
9
|
-
* - Token-based parsing with string operations
|
|
10
|
-
* - Markdown formatting support with semantic distinction
|
|
11
|
-
* - Lookahead detection for legal value carriers
|
|
12
|
-
* - Orange semantic markers vs default markdown spans
|
|
13
|
-
* - Annotation, value carrier, and heading highlighting
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
// HTML escaping function
|
|
17
|
-
export function escapeHtml(s) {
|
|
18
|
-
return String(s)
|
|
19
|
-
.replaceAll('&', '&')
|
|
20
|
-
.replaceAll('<', '<')
|
|
21
|
-
.replaceAll('>', '>')
|
|
22
|
-
.replaceAll('"', '"')
|
|
23
|
-
.replaceAll("'", ''');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Token color scheme
|
|
27
|
-
export const TOKEN_COLORS = {
|
|
28
|
-
text: '#6b7280', // Grayscale - main text
|
|
29
|
-
marker: '#f97316', // Orange - .?!^^@
|
|
30
|
-
retraction: '#dc2626', // Red - whole predicate with -
|
|
31
|
-
value: '#f97316', // Yellow - values in []
|
|
32
|
-
annotation: '#4b5563' // Darker gray - annotation braces
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// Check if markdown formatting has following annotation
|
|
36
|
-
function hasFollowingAnnotation(code, endPos) {
|
|
37
|
-
let i = endPos;
|
|
38
|
-
while (i < code.length && /\s/.test(code[i])) {
|
|
39
|
-
i++;
|
|
40
|
-
}
|
|
41
|
-
return i < code.length && code[i] === '{';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Process markdown formatting in text while preserving original syntax
|
|
45
|
-
export function processMarkdownFormatting(text, hasFollowingAnnotation = false) {
|
|
46
|
-
let result = escapeHtml(text);
|
|
47
|
-
|
|
48
|
-
if (hasFollowingAnnotation) {
|
|
49
|
-
// Orange for legal value carriers (with following annotation)
|
|
50
|
-
result = result.replace(/\*\*(.*?)\*\*/g, `<strong style="color: ${TOKEN_COLORS.marker}">$1</strong>`);
|
|
51
|
-
result = result.replace(/__(.*?)__/g, `<strong style="color: ${TOKEN_COLORS.marker}">$1</strong>`);
|
|
52
|
-
result = result.replace(/\*(.*?)\*/g, `<em style="color: ${TOKEN_COLORS.marker}">$1</em>`);
|
|
53
|
-
result = result.replace(/_(.*?)_/g, `<em style="color: ${TOKEN_COLORS.marker}">$1</em>`);
|
|
54
|
-
result = result.replace(/`(.*?)`/g, `<code style="color: ${TOKEN_COLORS.marker}">$1</code>`);
|
|
55
|
-
} else {
|
|
56
|
-
// Default browser styles for regular markdown spans
|
|
57
|
-
result = result.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
58
|
-
result = result.replace(/__(.*?)__/g, '<strong>$1</strong>');
|
|
59
|
-
result = result.replace(/\*(.*?)\*/g, '<em>$1</em>');
|
|
60
|
-
result = result.replace(/_(.*?)_/g, '<em>$1</em>');
|
|
61
|
-
result = result.replace(/`(.*?)`/g, '<code>$1</code>');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return result;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Process markdown formatting at specific position for non-bracket text
|
|
68
|
-
function processMarkdownFormattingAtPosition(code, startPos) {
|
|
69
|
-
const char = code[startPos];
|
|
70
|
-
|
|
71
|
-
// Check for **bold**
|
|
72
|
-
if (char === '*' && startPos + 1 < code.length && code[startPos + 1] === '*') {
|
|
73
|
-
const endPos = code.indexOf('**', startPos + 2);
|
|
74
|
-
if (endPos !== -1) {
|
|
75
|
-
const content = code.slice(startPos + 2, endPos);
|
|
76
|
-
const hasAnnotation = hasFollowingAnnotation(code, endPos + 2);
|
|
77
|
-
const color = hasAnnotation ? ` style="color: ${TOKEN_COLORS.marker}"` : '';
|
|
78
|
-
return {
|
|
79
|
-
html: `<strong${color}>**${escapeHtml(content)}**</strong>`,
|
|
80
|
-
nextIndex: endPos + 2
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Check for __bold__
|
|
86
|
-
if (char === '_' && startPos + 1 < code.length && code[startPos + 1] === '_') {
|
|
87
|
-
const endPos = code.indexOf('__', startPos + 2);
|
|
88
|
-
if (endPos !== -1) {
|
|
89
|
-
const content = code.slice(startPos + 2, endPos);
|
|
90
|
-
const hasAnnotation = hasFollowingAnnotation(code, endPos + 2);
|
|
91
|
-
const color = hasAnnotation ? ` style="color: ${TOKEN_COLORS.marker}"` : '';
|
|
92
|
-
return {
|
|
93
|
-
html: `<strong${color}>__${escapeHtml(content)}__</strong>`,
|
|
94
|
-
nextIndex: endPos + 2
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Check for *italic*
|
|
100
|
-
if (char === '*') {
|
|
101
|
-
const endPos = code.indexOf('*', startPos + 1);
|
|
102
|
-
if (endPos !== -1) {
|
|
103
|
-
const content = code.slice(startPos + 1, endPos);
|
|
104
|
-
const hasAnnotation = hasFollowingAnnotation(code, endPos + 1);
|
|
105
|
-
const color = hasAnnotation ? ` style="color: ${TOKEN_COLORS.marker}"` : '';
|
|
106
|
-
return {
|
|
107
|
-
html: `<em${color}>*${escapeHtml(content)}*</em>`,
|
|
108
|
-
nextIndex: endPos + 1
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Check for _italic_
|
|
114
|
-
if (char === '_') {
|
|
115
|
-
const endPos = code.indexOf('_', startPos + 1);
|
|
116
|
-
if (endPos !== -1) {
|
|
117
|
-
const content = code.slice(startPos + 1, endPos);
|
|
118
|
-
const hasAnnotation = hasFollowingAnnotation(code, endPos + 1);
|
|
119
|
-
const color = hasAnnotation ? ` style="color: ${TOKEN_COLORS.marker}"` : '';
|
|
120
|
-
return {
|
|
121
|
-
html: `<em${color}>_${escapeHtml(content)}_</em>`,
|
|
122
|
-
nextIndex: endPos + 1
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Check for `code`
|
|
128
|
-
if (char === '`') {
|
|
129
|
-
const endPos = code.indexOf('`', startPos + 1);
|
|
130
|
-
if (endPos !== -1) {
|
|
131
|
-
const content = code.slice(startPos + 1, endPos);
|
|
132
|
-
const hasAnnotation = hasFollowingAnnotation(code, endPos + 1);
|
|
133
|
-
const color = hasAnnotation ? ` style="color: ${TOKEN_COLORS.marker}"` : '';
|
|
134
|
-
return {
|
|
135
|
-
html: `<code${color}>\`${escapeHtml(content)}\`</code>`,
|
|
136
|
-
nextIndex: endPos + 1
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Parse an annotation token { ... }
|
|
145
|
-
function parseAnnotation(content) {
|
|
146
|
-
const parts = content.trim().split(/\s+/);
|
|
147
|
-
let result = '';
|
|
148
|
-
let nextTermIsRetracted = false;
|
|
149
|
-
|
|
150
|
-
for (let i = 0; i < parts.length; i++) {
|
|
151
|
-
const part = parts[i];
|
|
152
|
-
if (!part) continue;
|
|
153
|
-
|
|
154
|
-
// Check for subject markers (= or +)
|
|
155
|
-
if (part.startsWith('=') || part.startsWith('+')) {
|
|
156
|
-
const marker = part[0]; // = or +
|
|
157
|
-
const rest = part.slice(1); // the rest of the subject
|
|
158
|
-
result += `<span style="color: ${TOKEN_COLORS.marker}">${escapeHtml(marker)}</span><span style="color: ${TOKEN_COLORS.text}">${escapeHtml(rest)}</span> `;
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Check for retraction marker (-)
|
|
163
|
-
if (part === '-') {
|
|
164
|
-
nextTermIsRetracted = true;
|
|
165
|
-
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Handle case where - and predicate are combined (e.g., -predicate)
|
|
170
|
-
if (part.startsWith('-') && part.length > 1) {
|
|
171
|
-
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Apply retraction color to the immediate next term only
|
|
176
|
-
const isRetracted = nextTermIsRetracted;
|
|
177
|
-
nextTermIsRetracted = false; // Reset after one term
|
|
178
|
-
|
|
179
|
-
// Check for object markers (? or !)
|
|
180
|
-
if (part.startsWith('?') || part.startsWith('!')) {
|
|
181
|
-
const marker = part[0]; // ? or !
|
|
182
|
-
const rest = part.slice(1); // the CURIE or rest of the term
|
|
183
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
184
|
-
const textColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
185
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span><span style="color: ${textColor}">${escapeHtml(rest)}</span> `;
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Check for datatype markers (^^, @)
|
|
190
|
-
if (part.startsWith('^^')) {
|
|
191
|
-
const marker = '^^'; // the datatype marker
|
|
192
|
-
const rest = part.slice(2); // the datatype
|
|
193
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
194
|
-
const textColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
195
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span><span style="color: ${textColor}">${escapeHtml(rest)}</span> `;
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (part.startsWith('@')) {
|
|
200
|
-
const marker = '@'; // the language marker
|
|
201
|
-
const rest = part.slice(1); // the language code
|
|
202
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
203
|
-
const textColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
204
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span><span style="color: ${textColor}">${escapeHtml(rest)}</span> `;
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Check for type marker (.)
|
|
209
|
-
if (part.startsWith('.')) {
|
|
210
|
-
const marker = '.'; // the dot
|
|
211
|
-
const rest = part.slice(1); // the class name
|
|
212
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
213
|
-
const textColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
214
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span><span style="color: ${textColor}">${escapeHtml(rest)}</span> `;
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Regular CURIE or IRI
|
|
219
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
220
|
-
result += `<span style="color: ${color}">${escapeHtml(part)}</span> `;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return result.trim();
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// Main MDLD syntax highlighting function
|
|
227
|
-
export function highlightMDLD(code) {
|
|
228
|
-
let result = '';
|
|
229
|
-
let i = 0;
|
|
230
|
-
|
|
231
|
-
while (i < code.length) {
|
|
232
|
-
const char = code[i];
|
|
233
|
-
|
|
234
|
-
// Handle annotation blocks { ... }
|
|
235
|
-
if (char === '{') {
|
|
236
|
-
const endIdx = code.indexOf('}', i);
|
|
237
|
-
if (endIdx === -1) {
|
|
238
|
-
// No closing brace, treat as regular text
|
|
239
|
-
result += escapeHtml(char);
|
|
240
|
-
i++;
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const content = code.slice(i + 1, endIdx);
|
|
245
|
-
const parsed = parseAnnotation(content);
|
|
246
|
-
|
|
247
|
-
result += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">{</span>`;
|
|
248
|
-
result += parsed;
|
|
249
|
-
result += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">}</span>`;
|
|
250
|
-
|
|
251
|
-
i = endIdx + 1;
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Handle value carriers [ ... ] and markdown formatting
|
|
256
|
-
if (char === '[') {
|
|
257
|
-
const endIdx = code.indexOf(']', i);
|
|
258
|
-
if (endIdx === -1) {
|
|
259
|
-
// No closing bracket, treat as regular text
|
|
260
|
-
result += escapeHtml(char);
|
|
261
|
-
i++;
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const content = code.slice(i + 1, endIdx);
|
|
266
|
-
const hasAnnotation = hasFollowingAnnotation(code, endIdx + 1);
|
|
267
|
-
const processedContent = processMarkdownFormatting(content, hasAnnotation);
|
|
268
|
-
result += `<span style="color: ${TOKEN_COLORS.value}; opacity: 0.85">[${processedContent}]</span>`;
|
|
269
|
-
|
|
270
|
-
i = endIdx + 1;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// Handle markdown formatting outside brackets
|
|
275
|
-
if (char === '*' || char === '_' || char === '`') {
|
|
276
|
-
const processed = processMarkdownFormattingAtPosition(code, i);
|
|
277
|
-
if (processed) {
|
|
278
|
-
result += processed.html;
|
|
279
|
-
i = processed.nextIndex;
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// Handle prefix declarations < ... >
|
|
285
|
-
if (char === '<') {
|
|
286
|
-
const endIdx = code.indexOf('>', i);
|
|
287
|
-
if (endIdx === -1) {
|
|
288
|
-
result += escapeHtml(char);
|
|
289
|
-
i++;
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
const content = code.slice(i + 1, endIdx);
|
|
294
|
-
result += `<span style="color: ${TOKEN_COLORS.text}; opacity: 0.6"><${escapeHtml(content)}></span>`;
|
|
295
|
-
|
|
296
|
-
i = endIdx + 1;
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// Handle markdown headers
|
|
301
|
-
if (char === '#') {
|
|
302
|
-
let hashCount = 0;
|
|
303
|
-
let j = i;
|
|
304
|
-
while (j < code.length && code[j] === '#') {
|
|
305
|
-
hashCount++;
|
|
306
|
-
j++;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (hashCount <= 6 && (j >= code.length || code[j] === ' ' || code[j] === '\t')) {
|
|
310
|
-
// Find end of line
|
|
311
|
-
const lineEnd = code.indexOf('\n', j);
|
|
312
|
-
const endIdx = lineEnd === -1 ? code.length : lineEnd;
|
|
313
|
-
const headerText = code.slice(j, endIdx).trim();
|
|
314
|
-
|
|
315
|
-
// Process heading text for annotations
|
|
316
|
-
let processedHeaderText = '';
|
|
317
|
-
let lastIndex = 0;
|
|
318
|
-
|
|
319
|
-
// Find and process annotations in heading text
|
|
320
|
-
let annotationStart = headerText.indexOf('{');
|
|
321
|
-
while (annotationStart !== -1) {
|
|
322
|
-
const annotationEnd = headerText.indexOf('}', annotationStart);
|
|
323
|
-
if (annotationEnd === -1) break;
|
|
324
|
-
|
|
325
|
-
// Add text before annotation (escaped)
|
|
326
|
-
const beforeText = headerText.slice(lastIndex, annotationStart);
|
|
327
|
-
processedHeaderText += escapeHtml(beforeText);
|
|
328
|
-
|
|
329
|
-
// Process annotation content
|
|
330
|
-
const annotationContent = headerText.slice(annotationStart + 1, annotationEnd);
|
|
331
|
-
const parsedAnnotation = parseAnnotation(annotationContent);
|
|
332
|
-
|
|
333
|
-
// Add annotation HTML
|
|
334
|
-
processedHeaderText += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">{</span>` +
|
|
335
|
-
parsedAnnotation +
|
|
336
|
-
`<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">}</span>`;
|
|
337
|
-
|
|
338
|
-
lastIndex = annotationEnd + 1;
|
|
339
|
-
annotationStart = headerText.indexOf('{', lastIndex);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Add remaining text after last annotation (escaped)
|
|
343
|
-
processedHeaderText += escapeHtml(headerText.slice(lastIndex));
|
|
344
|
-
|
|
345
|
-
// Create HTML heading that preserves the original markdown syntax
|
|
346
|
-
const headingTag = `h${hashCount}`;
|
|
347
|
-
const headingStyle = `margin: 0.5em 0; font-weight: 600;`;
|
|
348
|
-
const hashes = '#'.repeat(hashCount);
|
|
349
|
-
result += `<${headingTag} style="${headingStyle}"><span style="color: ${TOKEN_COLORS.marker}; opacity: 0.8">${hashes}</span> ${processedHeaderText}</${headingTag}>`;
|
|
350
|
-
|
|
351
|
-
i = endIdx;
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Regular text
|
|
357
|
-
result += escapeHtml(char);
|
|
358
|
-
i++;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return result;
|
|
362
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export { parse } from './parse.js';
|
|
2
|
-
export { merge } from './merge.js';
|
|
3
|
-
export { generate, generateNode } from './generate.js';
|
|
4
|
-
export { locate } from './locate.js';
|
|
5
|
-
export { render } from './render.js';
|
|
6
|
-
export { DEFAULT_CONTEXT } from './constants.js';
|
|
7
|
-
export {
|
|
8
|
-
DataFactory,
|
|
9
|
-
hash,
|
|
10
|
-
expandIRI,
|
|
11
|
-
shortenIRI,
|
|
12
|
-
parseSemanticBlock
|
|
13
|
-
} from './utils.js';
|
package/src/locate.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { quadToKeyForOrigin } from './utils.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Locate the origin entry for a quad using the lean origin system
|
|
5
|
-
*
|
|
6
|
-
* @param {Object} quad - The quad to locate (subject, predicate, object)
|
|
7
|
-
* @param {Object} origin - Origin object containing quadIndex
|
|
8
|
-
* @returns {Object|null} Origin entry or null if not found
|
|
9
|
-
*/
|
|
10
|
-
export function locate(quad, origin) {
|
|
11
|
-
if (!quad || !origin || !origin.quadIndex) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Generate the quad key to lookup in quadIndex
|
|
16
|
-
const quadKey = quadToKeyForOrigin(quad);
|
|
17
|
-
if (!quadKey) {
|
|
18
|
-
return null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Return the origin entry directly - no need to create new object
|
|
22
|
-
return origin.quadIndex.get(quadKey) || null;
|
|
23
|
-
}
|
package/src/merge.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { parse } from './parse.js';
|
|
2
|
-
import { quadToKeyForOrigin } from './utils.js';
|
|
3
|
-
import { DEFAULT_CONTEXT } from './constants.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates a unique key for quad identity matching - using shared utility
|
|
7
|
-
* @param {Quad} quad
|
|
8
|
-
* @returns {string}
|
|
9
|
-
*/
|
|
10
|
-
function quadKey(quad) {
|
|
11
|
-
return quadToKeyForOrigin(quad);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Normalizes merge input to ParseResult format
|
|
16
|
-
* @param {string|ParseResult} input
|
|
17
|
-
* @param {Object} options
|
|
18
|
-
* @param {Object} docContext
|
|
19
|
-
* @returns {ParseResult}
|
|
20
|
-
*/
|
|
21
|
-
function normalizeInput(input, options, docContext) {
|
|
22
|
-
if (typeof input === 'string') {
|
|
23
|
-
return parse({
|
|
24
|
-
text: input,
|
|
25
|
-
...options,
|
|
26
|
-
context: { ...docContext, ...options.context }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
// ParseResult passthrough - no re-parse
|
|
30
|
-
return input;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Merges multiple MDLD documents with diff polarity resolution
|
|
35
|
-
* @param {Array<string|ParseResult>} docs
|
|
36
|
-
* @param {Object} options
|
|
37
|
-
* @returns {Object} Merge result with quads, remove, statements, origin, and context
|
|
38
|
-
*/
|
|
39
|
-
export function merge(docs, options = {}) {
|
|
40
|
-
const sessionBuffer = new Map(); // Use Map instead of Set for proper quad storage
|
|
41
|
-
const sessionRemoveSet = new Set();
|
|
42
|
-
const allDocuments = [];
|
|
43
|
-
const quadIndex = new Map();
|
|
44
|
-
const allStatements = []; // Collect statements from all documents
|
|
45
|
-
const accumulatedContext = new Map(); // Track all unique prefixes across documents
|
|
46
|
-
const primarySubjects = []; // Collect primary subjects from all documents
|
|
47
|
-
|
|
48
|
-
// Process each document in order
|
|
49
|
-
for (let i = 0; i < docs.length; i++) {
|
|
50
|
-
const input = docs[i];
|
|
51
|
-
|
|
52
|
-
// Each document gets the same context (no inheritance)
|
|
53
|
-
const docContext = { ...DEFAULT_CONTEXT, ...options.context };
|
|
54
|
-
|
|
55
|
-
// Normalize input to ParseResult
|
|
56
|
-
const doc = normalizeInput(input, options, docContext);
|
|
57
|
-
|
|
58
|
-
// Accumulate context from this document
|
|
59
|
-
if (doc.context) {
|
|
60
|
-
for (const [prefix, namespace] of Object.entries(doc.context)) {
|
|
61
|
-
// Don't override default context entries unless explicitly provided in options
|
|
62
|
-
if (!accumulatedContext.has(prefix) && !DEFAULT_CONTEXT[prefix]) {
|
|
63
|
-
accumulatedContext.set(prefix, namespace);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Create document origin
|
|
69
|
-
const documentOrigin = {
|
|
70
|
-
index: i,
|
|
71
|
-
input: typeof input === 'string' ? 'string' : 'ParseResult',
|
|
72
|
-
origin: doc.origin,
|
|
73
|
-
context: doc.context,
|
|
74
|
-
statementsCount: doc.statements?.length || 0 // Track statements count
|
|
75
|
-
};
|
|
76
|
-
allDocuments.push(documentOrigin);
|
|
77
|
-
|
|
78
|
-
// Collect statements from this document
|
|
79
|
-
if (doc.statements && doc.statements.length > 0) {
|
|
80
|
-
allStatements.push(...doc.statements);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Collect primary subject from this document (already a string IRI)
|
|
84
|
-
if (doc.primarySubject) {
|
|
85
|
-
primarySubjects.push(doc.primarySubject);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Fold assertions into session buffer
|
|
89
|
-
for (const quad of doc.quads) {
|
|
90
|
-
const key = quadKey(quad);
|
|
91
|
-
sessionBuffer.set(key, quad);
|
|
92
|
-
|
|
93
|
-
// Create quad origin with document index and polarity
|
|
94
|
-
const existingOrigin = doc.origin.quadIndex.get(key);
|
|
95
|
-
quadIndex.set(key, {
|
|
96
|
-
...(existingOrigin || {}),
|
|
97
|
-
documentIndex: i,
|
|
98
|
-
polarity: '+'
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Fold retractions
|
|
103
|
-
for (const quad of doc.remove) {
|
|
104
|
-
const key = quadKey(quad);
|
|
105
|
-
|
|
106
|
-
if (sessionBuffer.has(key)) {
|
|
107
|
-
// Inter-document cancel - remove from buffer
|
|
108
|
-
sessionBuffer.delete(key);
|
|
109
|
-
} else {
|
|
110
|
-
// External retract - add to remove set
|
|
111
|
-
sessionRemoveSet.add(quad);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Create quad origin for remove quads
|
|
115
|
-
const existingOrigin = doc.origin.quadIndex.get(key);
|
|
116
|
-
quadIndex.set(key, {
|
|
117
|
-
...(existingOrigin || {}),
|
|
118
|
-
documentIndex: i,
|
|
119
|
-
polarity: '-'
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Build final result
|
|
125
|
-
const finalQuads = Array.from(sessionBuffer.values());
|
|
126
|
-
const finalRemove = Array.from(sessionRemoveSet);
|
|
127
|
-
|
|
128
|
-
// Build merge origin
|
|
129
|
-
const mergeOrigin = {
|
|
130
|
-
documents: allDocuments,
|
|
131
|
-
quadIndex: quadIndex
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// Build final context (union of all contexts)
|
|
135
|
-
const finalContext = {
|
|
136
|
-
...DEFAULT_CONTEXT,
|
|
137
|
-
...options.context,
|
|
138
|
-
...Object.fromEntries(accumulatedContext)
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// Enforce hard invariant
|
|
142
|
-
const quadKeys = new Set(finalQuads.map(quadKey));
|
|
143
|
-
const removeKeys = new Set(finalRemove.map(quadKey));
|
|
144
|
-
|
|
145
|
-
// Filter out any overlaps (shouldn't happen with correct implementation)
|
|
146
|
-
const filteredQuads = finalQuads.filter(quad => !removeKeys.has(quadKey(quad)));
|
|
147
|
-
const filteredRemove = finalRemove.filter(quad => !quadKeys.has(quadKey(quad)));
|
|
148
|
-
|
|
149
|
-
return {
|
|
150
|
-
quads: filteredQuads,
|
|
151
|
-
remove: filteredRemove,
|
|
152
|
-
statements: allStatements, // Include all collected statements
|
|
153
|
-
origin: mergeOrigin,
|
|
154
|
-
context: finalContext,
|
|
155
|
-
primarySubjects: primarySubjects // Include all collected primary subjects
|
|
156
|
-
};
|
|
157
|
-
}
|