mdld-parse 0.9.1 → 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.
Files changed (59) hide show
  1. package/README.md +34 -1
  2. package/dist/.nojekyll +0 -0
  3. package/dist/CNAME +1 -0
  4. package/dist/README.md +425 -0
  5. package/dist/docs/API.md +397 -0
  6. package/dist/docs/Architecture.md +190 -0
  7. package/dist/docs/Guide.md +322 -0
  8. package/dist/docs/LLM-MDLD-Inducer.md +550 -0
  9. package/dist/docs/Parser.md +654 -0
  10. package/dist/docs/Performance.md +238 -0
  11. package/dist/docs/Spec-render.md +1439 -0
  12. package/dist/docs/Subject.md +180 -0
  13. package/dist/docs/Syntax.md +463 -0
  14. package/dist/docs/TodoList.md +215 -0
  15. package/dist/docs/Use-Cases.md +354 -0
  16. package/dist/docs/index.html +108 -0
  17. package/dist/docs/index.md +56 -0
  18. package/dist/docs/origin.md +459 -0
  19. package/dist/docs/polarity.md +586 -0
  20. package/dist/docs/primary-subject.md +199 -0
  21. package/dist/docs/statements.md +226 -0
  22. package/dist/docs/tokenization-performance-report.md +164 -0
  23. package/dist/examples/Cookbook.md +32 -0
  24. package/dist/examples/Status-SHACL.md +33 -0
  25. package/dist/examples/demo/index.md +35 -0
  26. package/dist/examples/demo/journal.md +18 -0
  27. package/dist/examples/demo/minimal.md +11 -0
  28. package/dist/examples/demo/project.md +15 -0
  29. package/dist/examples/demo/recipe.md +31 -0
  30. package/dist/examples/demo/research.md +46 -0
  31. package/dist/examples/demo/space-mission.md +48 -0
  32. package/dist/examples/dogfood.md +288 -0
  33. package/dist/examples/few-shot.md +309 -0
  34. package/dist/examples/graph/rdf-graph.html +473 -0
  35. package/dist/examples/graph/rdf_force_graph_web_component.html +506 -0
  36. package/dist/examples/index.md +45 -0
  37. package/dist/examples/llm-time-workflow.md +173 -0
  38. package/dist/examples/prov-o.md +2367 -0
  39. package/dist/examples/rdf.md +827 -0
  40. package/dist/examples/shacl.md +3399 -0
  41. package/dist/examples/statements.md +43 -0
  42. package/dist/examples/workflow.md +150 -0
  43. package/dist/index.html +2827 -0
  44. package/dist/index.js +1548 -0
  45. package/dist/logo.svg +7 -0
  46. package/dist/spec/Spec-compact.md +363 -0
  47. package/dist/spec/Spec-ultra.md +221 -0
  48. package/dist/spec/Spec.md +1068 -0
  49. package/dist/spec/index.md +46 -0
  50. package/package.json +13 -8
  51. package/src/constants.js +0 -31
  52. package/src/generate.js +0 -245
  53. package/src/index.js +0 -13
  54. package/src/locate.js +0 -23
  55. package/src/merge.js +0 -157
  56. package/src/parse.js +0 -765
  57. package/src/render.js +0 -576
  58. package/src/shared.js +0 -577
  59. package/src/utils.js +0 -430
package/src/parse.js DELETED
@@ -1,765 +0,0 @@
1
- import {
2
- DataFactory,
3
- expandIRI,
4
- quadIndexKey,
5
- createLiteral,
6
- hash
7
- } from './utils.js';
8
- import {
9
- DEFAULT_CONTEXT,
10
- URL_REGEX,
11
- FENCE_REGEX,
12
- PREFIX_REGEX,
13
- HEADING_REGEX,
14
- UNORDERED_LIST_REGEX,
15
- BLOCKQUOTE_REGEX,
16
- STANDALONE_SUBJECT_REGEX,
17
- CARRIER_PATTERN_ARRAY,
18
-
19
- } from './constants.js';
20
- import {
21
-
22
- getFenceClosePattern,
23
- calcRangeInfo,
24
- calcAttrsRange,
25
- createToken,
26
- createCarrier,
27
- createListToken,
28
- parseSemCached,
29
- parseLangAndAttrs,
30
- findMatchingBracket,
31
- extractUrlFromBrackets,
32
- extractAttributesFromText,
33
- determineCarrierType,
34
- calcCarrierRanges,
35
- extractCleanText,
36
- RDF_TYPE,
37
- RDF_STATEMENT,
38
- RDF_SUBJECT,
39
- RDF_PREDICATE,
40
- RDF_OBJECT,
41
- createLeanOriginEntry,
42
- resolveSubject,
43
- resolveObject,
44
- processTokenWithBlockTracking
45
- } from './shared.js';
46
-
47
-
48
- export function parse(firstArg, secondArg = {}) {
49
- // Dual-mode API: backward compatible with (text, options) and new ({ text, context, ... })
50
- const isNamedParams = typeof firstArg === 'object' && firstArg !== null && 'text' in firstArg;
51
-
52
- const text = isNamedParams ? firstArg.text : firstArg;
53
- const options = isNamedParams
54
- ? { context: firstArg.context, dataFactory: firstArg.dataFactory, graph: firstArg.graph }
55
- : secondArg;
56
-
57
- const state = {
58
- ctx: { ...DEFAULT_CONTEXT, ...(options.context || {}) },
59
- df: options.dataFactory || DataFactory,
60
- graph: options.graph ? DataFactory.namedNode(options.graph) : DataFactory.defaultGraph(),
61
- quads: [],
62
- quadBuffer: new Map(),
63
- removeSet: new Set(),
64
- origin: {
65
- quadIndex: new Map(),
66
- blocks: new Map(),
67
- documentStructure: []
68
- },
69
- currentSubject: null,
70
- primarySubject: null,
71
- tokens: null,
72
- currentTokenIndex: -1,
73
- statements: [],
74
- statementCandidates: new Map(),
75
- currentBlock: null,
76
- blockStack: []
77
- };
78
-
79
- const scanResult = scanTokens(text);
80
- state.tokens = scanResult.tokens;
81
-
82
- // Single-pass processing: resolve prefixes AND process tokens together
83
- for (let i = 0; i < state.tokens.length; i++) {
84
- const token = state.tokens[i];
85
- state.currentTokenIndex = i;
86
-
87
- // Handle prefix tokens immediately during main pass
88
- if (token.type === 'prefix') {
89
- let resolvedIri = token.iri;
90
- if (token.iri.includes(':')) {
91
- const colonIndex = token.iri.indexOf(':');
92
- const potentialPrefix = token.iri.substring(0, colonIndex);
93
- const reference = token.iri.substring(colonIndex + 1);
94
- if (state.ctx[potentialPrefix] && potentialPrefix !== '@vocab') {
95
- resolvedIri = state.ctx[potentialPrefix] + reference;
96
- }
97
- }
98
- state.ctx[token.prefix] = resolvedIri;
99
- continue; // Skip token processor for prefixes
100
- }
101
-
102
- // Process all other tokens
103
- TOKEN_PROCESSORS[token.type]?.(token, state);
104
- }
105
-
106
- // Optimized quad filtering - use Set.has() instead of array.includes()
107
- const quadKeys = new Set();
108
- for (const quad of state.quads) {
109
- quadKeys.add(quadIndexKey(quad.subject, quad.predicate, quad.object));
110
- }
111
-
112
- // Direct Set iteration - more efficient than filter()
113
- const filteredRemove = [];
114
- for (const quad of state.removeSet) {
115
- const key = quadIndexKey(quad.subject, quad.predicate, quad.object);
116
- if (!quadKeys.has(key)) {
117
- filteredRemove.push(quad);
118
- }
119
- }
120
-
121
- return {
122
- quads: state.quads,
123
- remove: filteredRemove,
124
- statements: state.statements,
125
- origin: state.origin,
126
- context: state.ctx,
127
- primarySubject: state.primarySubject,
128
- md: scanResult.md
129
- };
130
- }
131
-
132
-
133
- // Cache for fence regex patterns - using shared utility
134
-
135
- function getCarriers(token) {
136
- if (token.type === 'code') return [];
137
- return token._carriers || (token._carriers = extractInlineCarriers(token.text, token.range[0]));
138
- }
139
-
140
- function scanTokens(text) {
141
- const tokens = [];
142
- const mdLines = [];
143
- const lines = text.split('\n');
144
- let pos = 0;
145
- let codeBlock = null;
146
- const PROCESSORS = [
147
- { type: 'fence', test: line => FENCE_REGEX.test(line.trim()), process: handleFence },
148
- { type: 'content', test: () => codeBlock, process: line => codeBlock.content.push(line) },
149
- { type: 'prefix', test: line => PREFIX_REGEX.test(line), process: handlePrefix },
150
- { type: 'standalone', test: line => STANDALONE_SUBJECT_REGEX.test(line), process: handleStandaloneSubject },
151
- { type: 'heading', test: line => HEADING_REGEX.test(line), process: handleHeading },
152
- { type: 'list', test: line => UNORDERED_LIST_REGEX.test(line), process: handleList },
153
- { type: 'blockquote', test: line => BLOCKQUOTE_REGEX.test(line), process: handleBlockquote },
154
- { type: 'para', test: line => line.trim(), process: handlePara }
155
- ];
156
-
157
- function handleFence(line, lineStart, pos) {
158
- const trimmedLine = line.trim();
159
- if (!codeBlock) {
160
- const fenceMatch = trimmedLine.match(FENCE_REGEX);
161
- if (!fenceMatch) return false;
162
-
163
- const { lang, attrsText } = parseLangAndAttrs(fenceMatch[2]);
164
- const attrsStartInLine = attrsText ? line.indexOf(attrsText) : -1;
165
- const contentStart = lineStart + line.length + 1;
166
-
167
- codeBlock = {
168
- fence: fenceMatch[1],
169
- start: lineStart,
170
- content: [],
171
- lang,
172
- attrs: attrsText,
173
- attrsRange: attrsText && attrsStartInLine >= 0 ? [lineStart + attrsStartInLine, lineStart + attrsStartInLine + attrsText.length] : null,
174
- valueRangeStart: contentStart
175
- };
176
-
177
- // Add stripped fence line to mdLines
178
- const cleanFence = line.replace(/\s*\{[^}]+\}\s*$/, '');
179
- mdLines.push(cleanFence);
180
- } else {
181
- const fenceChar = codeBlock.fence[0];
182
- const expectedFence = fenceChar.repeat(codeBlock.fence.length);
183
- const fenceMatch = trimmedLine.match(getFenceClosePattern(fenceChar));
184
-
185
- if (fenceMatch && fenceMatch[1] === expectedFence) {
186
- const valueStart = codeBlock.valueRangeStart;
187
- const valueEnd = Math.max(valueStart, lineStart - 1);
188
- tokens.push({
189
- type: 'code',
190
- range: [codeBlock.start, lineStart],
191
- text: codeBlock.content.join('\n'),
192
- lang: codeBlock.lang,
193
- attrs: codeBlock.attrs,
194
- attrsRange: codeBlock.attrsRange,
195
- valueRange: [valueStart, valueEnd]
196
- });
197
-
198
- // Add all code content to mdLines before closing fence
199
- for (const contentLine of codeBlock.content) {
200
- mdLines.push(contentLine);
201
- }
202
-
203
- codeBlock = null;
204
-
205
- // Add closing fence to mdLines without trailing newline
206
- const closingFence = line.replace(/\r?\n.*$/, '');
207
- mdLines.push(closingFence);
208
- } else {
209
- // Code content - don't add to mdLines here, will be added when closing block
210
- // No need to add individual lines as they're processed when closing
211
- }
212
- }
213
- return true;
214
- }
215
-
216
- function handlePrefix(line, lineStart, pos) {
217
- const match = PREFIX_REGEX.exec(line);
218
- tokens.push({ type: 'prefix', prefix: match[1], iri: match[2].trim() });
219
- // Prefixes are typically stripped from clean MD
220
- return true;
221
- }
222
-
223
- function handleHeading(line, lineStart, pos) {
224
- const match = HEADING_REGEX.exec(line);
225
- const attrs = match[3] || null;
226
- const afterHashes = match[1].length;
227
- const rangeInfo = calcRangeInfo(line, attrs, lineStart, afterHashes, match[2].length);
228
- tokens.push(createToken('heading', [lineStart, pos - 1], match[2].trim(), attrs,
229
- rangeInfo.attrsRange, rangeInfo.valueRange, { depth: match[1].length }));
230
-
231
- // Add stripped heading to mdLines
232
- const cleanHeading = `${match[1]} ${match[2].trim()}`;
233
- mdLines.push(cleanHeading);
234
- return true;
235
- }
236
-
237
- function handleList(line, lineStart, pos) {
238
- const match = UNORDERED_LIST_REGEX.exec(line);
239
- tokens.push(createListToken('list', line, lineStart, pos, match));
240
-
241
- // Add stripped list item to mdLines
242
- const cleanList = `${match[1]}${match[2]} ${match[3].trim()}`;
243
- mdLines.push(cleanList);
244
- return true;
245
- }
246
-
247
- function handleBlockquote(line, lineStart, pos) {
248
- const match = BLOCKQUOTE_REGEX.exec(line);
249
- const attrs = match[2] || null;
250
- const valueStartInLine = line.startsWith('> ') ? 2 : line.indexOf('>') + 1;
251
- const valueEndInLine = valueStartInLine + match[1].length;
252
- tokens.push(createToken('blockquote', [lineStart, pos - 1], match[1].trim(), attrs,
253
- calcAttrsRange(line, attrs, lineStart),
254
- [lineStart + valueStartInLine, lineStart + valueEndInLine]));
255
-
256
- // Add stripped blockquote to mdLines
257
- const cleanBlockquote = `> ${match[1].trim()}`;
258
- mdLines.push(cleanBlockquote);
259
- return true;
260
- }
261
-
262
- function handlePara(line, lineStart, pos) {
263
- tokens.push(createToken('para', [lineStart, pos - 1], line.trim()));
264
-
265
- // Add stripped paragraph to mdLines
266
- let cleanPara = line;
267
-
268
- // Remove inline carrier annotations using existing patterns
269
- for (const [patternName, regex] of CARRIER_PATTERN_ARRAY) {
270
- const globalRegex = new RegExp(regex.source, 'gy');
271
- cleanPara = cleanPara.replace(globalRegex, (match, content, attrs) => {
272
- return content || '';
273
- });
274
- }
275
-
276
- // Remove bracket-style annotations [text] {annotation}
277
- cleanPara = cleanPara.replace(/\[([^\]]+)\]\s*\{[^}]+\}/g, '$1');
278
-
279
- // Remove any remaining standalone annotations
280
- cleanPara = cleanPara.replace(/\s*\{[^}]+\}\s*/g, ' ');
281
-
282
- // Clean up extra whitespace
283
- cleanPara = cleanPara.replace(/\s+/g, ' ').trim();
284
-
285
- mdLines.push(cleanPara);
286
- return true;
287
- }
288
-
289
- function handleStandaloneSubject(line, lineStart, pos) {
290
- tokens.push({ type: 'standalone', text: line.trim(), range: [lineStart, pos - 1] });
291
-
292
- // Standalone subjects are stripped from MD by default
293
- // Don't add anything to mdLines
294
- return true;
295
- }
296
-
297
- for (let i = 0; i < lines.length; i++) {
298
- const line = lines[i];
299
- const lineStart = pos;
300
- pos += line.length + 1;
301
-
302
- // Direct processor lookup - O(n) instead of O(n*m)
303
- for (const processor of PROCESSORS) {
304
- if (processor.test(line) && processor.process(line, lineStart, pos)) {
305
- break;
306
- }
307
- }
308
- }
309
-
310
- // Join without trailing newline to match expected output
311
- const mdContent = mdLines.join('\n');
312
- return { tokens, md: mdContent };
313
- }
314
-
315
- function extractInlineCarriers(text, baseOffset = 0) {
316
- const carriers = [];
317
- let pos = 0;
318
-
319
- const CARRIER_EXTRACTORS = {
320
- '<': (text, pos, baseOffset) => {
321
- const angleEnd = text.indexOf('>', pos);
322
- if (angleEnd === -1) return null;
323
- const url = text.slice(pos + 1, angleEnd);
324
- if (!URL_REGEX.test(url)) return null;
325
- const { attrs, attrsRange, finalSpanEnd } = extractAttributesFromText(text, angleEnd + 1, baseOffset);
326
- return createCarrier('link', url, attrs, attrsRange,
327
- [baseOffset + pos + 1, baseOffset + angleEnd],
328
- [baseOffset + pos, baseOffset + finalSpanEnd], finalSpanEnd, { url });
329
- },
330
- '[': (text, pos, baseOffset) => {
331
- const bracketEnd = findMatchingBracket(text, pos);
332
- if (!bracketEnd) return null;
333
- const carrierText = text.slice(pos + 1, bracketEnd - 1);
334
- const { url, spanEnd } = extractUrlFromBrackets(text, bracketEnd);
335
- const { attrs, attrsRange, finalSpanEnd } = extractAttributesFromText(text, spanEnd, baseOffset);
336
- const { carrierType, resourceIRI } = determineCarrierType(url);
337
- if (url?.startsWith('=')) return { skip: true, pos: finalSpanEnd };
338
- return createCarrier(carrierType, carrierText, attrs, attrsRange,
339
- [baseOffset + pos + 1, baseOffset + bracketEnd - 1],
340
- [baseOffset + pos, baseOffset + finalSpanEnd], finalSpanEnd, { url: resourceIRI });
341
- }
342
- };
343
-
344
- const extractCarrier = (text, pos, baseOffset) => {
345
- const extractor = CARRIER_EXTRACTORS[text[pos]];
346
- if (extractor) return extractor(text, pos, baseOffset);
347
-
348
- // Use pre-compiled patterns instead of Object.entries()
349
- for (const [type, pattern] of CARRIER_PATTERN_ARRAY) {
350
- pattern.lastIndex = pos;
351
- const match = pattern.exec(text);
352
- if (match) {
353
- const ranges = calcCarrierRanges(match, baseOffset, match.index);
354
- const carrierType = type === 'EMPHASIS' ? 'emphasis' : 'code';
355
- return createCarrier(carrierType, match[1], `{${match[2]}}`,
356
- ranges.attrsRange, ranges.valueRange, ranges.range, ranges.pos);
357
- }
358
- }
359
- return null;
360
- };
361
-
362
- while (pos < text.length) {
363
- const carrier = extractCarrier(text, pos, baseOffset);
364
- if (carrier) {
365
- if (carrier.skip) {
366
- pos = carrier.pos;
367
- } else {
368
- carriers.push(carrier);
369
- pos = carrier.pos;
370
- }
371
- } else {
372
- pos++;
373
- }
374
- }
375
-
376
- return carriers;
377
- }
378
-
379
-
380
- function createBlockEntry(token, state) {
381
- const blockId = token._blockId || hash(`${token.type}:${token.range?.[0]}:${token.range?.[1]}`);
382
- token._blockId = blockId; // Store for later reference
383
-
384
- // Extract inline carriers first to enable clean text extraction
385
- const carriers = getCarriers(token);
386
-
387
- const cleanText = extractCleanText(token);
388
-
389
- const blockEntry = {
390
- id: blockId,
391
- type: token.type,
392
- range: token.range,
393
- text: cleanText,
394
- subject: null,
395
- types: [],
396
- predicates: [],
397
- carriers: [],
398
- listLevel: token.indent || 0,
399
- parentBlockId: state.blockStack.length > 0 ? state.blockStack[state.blockStack.length - 1] : null,
400
- quadKeys: [] // Will be populated during quad emission
401
- };
402
-
403
- // Process carriers and add to block
404
- for (const carrier of carriers) {
405
- const carrierInfo = {
406
- type: carrier.type,
407
- range: carrier.range,
408
- text: carrier.text,
409
- subject: null,
410
- predicates: [],
411
- sem: null
412
- };
413
-
414
- // Extract carrier-specific semantics
415
- if (carrier.attrs) {
416
- const carrierSem = parseSemCached(carrier.attrs);
417
- carrierInfo.sem = carrierSem; // Store full semantics
418
- carrierInfo.predicates = carrierSem.predicates || [];
419
- carrierInfo.subject = carrierSem.subject;
420
- carrierInfo.types = carrierSem.types || [];
421
- }
422
-
423
- blockEntry.carriers.push(carrierInfo);
424
- }
425
-
426
- // Store block and add to document structure
427
- state.origin.blocks.set(blockId, blockEntry);
428
- state.origin.documentStructure.push(blockEntry);
429
-
430
- return blockEntry;
431
- }
432
-
433
- function enrichBlockFromAnnotation(blockEntry, sem, carrier, state) {
434
- // Update subject if available
435
- if (sem.subject && sem.subject !== 'RESET') {
436
- const resolvedSubject = resolveSubject(sem, state);
437
- if (resolvedSubject) {
438
- blockEntry.subject = resolvedSubject.value;
439
- }
440
- }
441
-
442
- // Add types
443
- if (sem.types && sem.types.length > 0) {
444
- sem.types.forEach(t => {
445
- const typeIRI = typeof t === 'string' ? t : t.iri;
446
- const expanded = expandIRI(typeIRI, state.ctx);
447
- if (!blockEntry.types.includes(expanded)) {
448
- blockEntry.types.push(expanded);
449
- }
450
- });
451
- }
452
-
453
- // Add predicates
454
- if (sem.predicates && sem.predicates.length > 0) {
455
- sem.predicates.forEach(pred => {
456
- const expandedPred = {
457
- iri: expandIRI(pred.iri, state.ctx),
458
- form: pred.form || '',
459
- object: null // Will be filled during quad emission
460
- };
461
- blockEntry.predicates.push(expandedPred);
462
- });
463
- }
464
-
465
- // Add carrier information
466
- if (carrier) {
467
- const carrierInfo = {
468
- type: carrier.type,
469
- range: carrier.range,
470
- text: carrier.text,
471
- subject: null,
472
- predicates: []
473
- };
474
-
475
- // Extract carrier-specific semantics
476
- if (carrier.attrs) {
477
- const carrierSem = parseSemCached(carrier.attrs);
478
- carrierInfo.sem = carrierSem; // Store full semantics
479
- carrierInfo.predicates = carrierSem.predicates || [];
480
- carrierInfo.subject = carrierSem.subject;
481
- carrierInfo.types = carrierSem.types || [];
482
- }
483
-
484
- blockEntry.carriers.push(carrierInfo);
485
- }
486
- }
487
-
488
- function processAnnotationWithBlockTracking(carrier, sem, state, options = {}) {
489
- const { preserveGlobalSubject = false, implicitSubject = null } = options;
490
-
491
- if (sem.subject === 'RESET') {
492
- state.currentSubject = null;
493
- return;
494
- }
495
-
496
- const previousSubject = state.currentSubject;
497
- const newSubject = resolveSubject(sem, state);
498
- const localObject = resolveObject(sem, state);
499
-
500
- // Track primary subject: first non-fragment subject declaration (fixed once detected)
501
- if (newSubject && !state.primarySubject && !sem.subject.startsWith('=#')) {
502
- state.primarySubject = newSubject.value; // Store as string IRI
503
- }
504
-
505
- const effectiveSubject = implicitSubject || (newSubject && !preserveGlobalSubject ? newSubject : previousSubject);
506
- if (newSubject && !preserveGlobalSubject && !implicitSubject) {
507
- state.currentSubject = newSubject;
508
- }
509
- const S = preserveGlobalSubject ? (newSubject || previousSubject) : (implicitSubject || state.currentSubject);
510
- if (!S) return;
511
-
512
- const block = createBlock(
513
- S.value, sem.types, sem.predicates,
514
- carrier.range, carrier.attrsRange || null, carrier.valueRange || null,
515
- carrier.type || null, state.ctx, carrier.text
516
- );
517
-
518
- const L = createLiteral(carrier.text, sem.datatype, sem.language, state.ctx, state.df);
519
- const carrierO = carrier.url ? state.df.namedNode(expandIRI(carrier.url, state.ctx)) : null;
520
- const newSubjectOrCarrierO = newSubject || carrierO;
521
-
522
- // Enrich current block with semantic information
523
- if (state.currentBlock) {
524
- enrichBlockFromAnnotation(state.currentBlock, sem, carrier, state);
525
- }
526
-
527
- processTypeAnnotations(sem, newSubject, localObject, carrierO, S, block, state, carrier);
528
- processPredicateAnnotations(sem, newSubject, previousSubject, localObject, newSubjectOrCarrierO, S, L, block, state, carrier);
529
- }
530
-
531
- function createBlock(subject, types, predicates, range, attrsRange, valueRange, carrierType, ctx, text) {
532
- const expanded = {
533
- subject,
534
- types: types.map(t => expandIRI(typeof t === 'string' ? t : t.iri, ctx)),
535
- predicates: predicates.map(p => ({ iri: expandIRI(p.iri, ctx), form: p.form }))
536
- };
537
-
538
- const signature = [subject, carrierType || 'unknown', expanded.types.join(','), expanded.predicates.map(p => `${p.form}${p.iri}`).join(',')].join('|');
539
- const blockId = hash(signature);
540
-
541
- return {
542
- id: blockId,
543
- range: { start: range[0], end: range[1] },
544
- carrierType: carrierType || null,
545
- subject,
546
- types: expanded.types,
547
- predicates: expanded.predicates,
548
- context: ctx,
549
- text: text || ''
550
- };
551
- }
552
-
553
- function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null, statements = null, statementCandidates = null, state = null) {
554
- if (!subject || !predicate || !object) return;
555
-
556
- const quad = dataFactory.quad(subject, predicate, object);
557
- const remove = meta?.remove || false;
558
-
559
- if (remove) {
560
- // Check if quad exists in current buffer
561
- const quadKey = quadIndexKey(quad.subject, quad.predicate, quad.object);
562
- if (quadBuffer.has(quadKey)) {
563
- // In current state → cancel, appears nowhere
564
- quadBuffer.delete(quadKey);
565
- // Also remove from quads array if present
566
- const index = quads.findIndex(q =>
567
- q.subject.value === quad.subject.value &&
568
- q.predicate.value === quad.predicate.value &&
569
- q.object.value === quad.object.value
570
- );
571
- if (index !== -1) {
572
- quads.splice(index, 1);
573
- }
574
- // Remove from quadIndex
575
- quadIndex.delete(quadKey);
576
- } else {
577
- // Not in current state → external retract
578
- removeSet.add(quad);
579
- }
580
- } else {
581
- // Add to buffer and quads
582
- const quadKey = quadIndexKey(quad.subject, quad.predicate, quad.object);
583
- quadBuffer.set(quadKey, quad);
584
- quads.push(quad);
585
-
586
- // Detect rdf:Statement pattern during single-pass parsing
587
- detectStatementPatternSinglePass(quad, dataFactory, meta, statements, statementCandidates);
588
-
589
- // Create lean origin entry using shared utility
590
- const originEntry = createLeanOriginEntry(block, subject, predicate, meta);
591
-
592
- quadIndex.set(quadKey, originEntry);
593
-
594
- // Link block to this quad for reverse lookup during rendering
595
- if (state.currentBlock && block.id === state.currentBlock.id) {
596
- if (!state.currentBlock.quadKeys) {
597
- state.currentBlock.quadKeys = [];
598
- }
599
- state.currentBlock.quadKeys.push(quadKey);
600
- }
601
- }
602
- }
603
-
604
- function detectStatementPatternSinglePass(quad, dataFactory, meta, statements = null, statementCandidates = null) {
605
- // Skip if not called from parse context (for testing compatibility)
606
- if (!statements || !statementCandidates) return;
607
-
608
- const predicate = quad.predicate.value;
609
-
610
- // Early filter: only process rdf:Statement related predicates
611
- if (predicate !== RDF_TYPE &&
612
- predicate !== RDF_SUBJECT &&
613
- predicate !== RDF_PREDICATE &&
614
- predicate !== RDF_OBJECT) {
615
- return;
616
- }
617
-
618
- // Check if this quad starts a new rdf:Statement pattern
619
- if (predicate === RDF_TYPE && quad.object.value === RDF_STATEMENT) {
620
- statementCandidates.set(quad.subject.value, { spo: {} });
621
- return;
622
- }
623
-
624
- // Check if this quad completes part of an existing rdf:Statement pattern
625
- const candidate = statementCandidates.get(quad.subject.value);
626
- if (!candidate) return;
627
-
628
- // Direct property assignment instead of switch for better performance
629
- if (predicate === RDF_SUBJECT) {
630
- candidate.spo.subject = quad.object;
631
- } else if (predicate === RDF_PREDICATE) {
632
- candidate.spo.predicate = quad.object;
633
- } else if (predicate === RDF_OBJECT) {
634
- candidate.spo.object = quad.object;
635
- // Store the original quad for potential literal extraction
636
- candidate.objectQuad = quad;
637
- }
638
-
639
- // Check if pattern is complete and create elevated SPO quad
640
- if (candidate.spo.subject && candidate.spo.predicate && candidate.spo.object) {
641
- // Use the object directly - literal detection happens at parse time
642
- const spoQuad = dataFactory.quad(
643
- candidate.spo.subject,
644
- candidate.spo.predicate,
645
- candidate.spo.object
646
- );
647
- statements.push(spoQuad);
648
- // Clean up candidate to avoid duplicate detection
649
- statementCandidates.delete(quad.subject.value);
650
- }
651
- }
652
-
653
-
654
- const createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
655
- const expandedType = expandIRI(typeIRI, state.ctx);
656
- const typeInfo = typeof entryIndex === 'object' ? entryIndex : { entryIndex, remove: false };
657
- emitQuad(
658
- state.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block,
659
- subject,
660
- state.df.namedNode(expandIRI('rdf:type', state.ctx)),
661
- state.df.namedNode(expandedType),
662
- state.df,
663
- { kind: 'type', token: `.${typeIRI}`, expandedType, entryIndex: typeInfo.entryIndex, remove: typeInfo.remove },
664
- state.statements, state.statementCandidates,
665
- state
666
- );
667
- };
668
-
669
- function processTypeAnnotations(sem, newSubject, localObject, carrierO, S, block, state, carrier) {
670
- sem.types.forEach(t => {
671
- const typeIRI = typeof t === 'string' ? t : t.iri;
672
- const typeInfo = typeof t === 'string' ? { entryIndex: null, remove: false } : t;
673
- // Type subject priority: explicit subject > soft object > carrier URL > current subject
674
- let typeSubject = newSubject || localObject || carrierO || S;
675
- createTypeQuad(typeIRI, typeSubject, state, block, typeInfo);
676
- });
677
- }
678
-
679
- const determinePredicateRole = (pred, carrier, newSubject, previousSubject, localObject, newSubjectOrCarrierO, S, L) => {
680
- if (pred.form === '' && carrier?.type === 'link' && carrier?.url && carrier.text === carrier.url) {
681
- return null;
682
- }
683
- switch (pred.form) {
684
- case '':
685
- // Literal predicates: explicit subject > current subject, URL only when no explicit subject
686
- return newSubject ? { subject: localObject || S, object: L }
687
- : (carrier?.type === 'link' && carrier?.url && carrier.text !== carrier.url)
688
- ? { subject: newSubjectOrCarrierO, object: L }
689
- : { subject: localObject || S, object: L };
690
- case '?':
691
- // Object predicates: use current subject → explicit object or URL
692
- return { subject: newSubject ? previousSubject : S, object: localObject || newSubjectOrCarrierO };
693
- case '!':
694
- // Reverse predicates: explicit object or URL → current subject
695
- return { subject: localObject || newSubjectOrCarrierO, object: newSubject ? previousSubject : S };
696
- default:
697
- return null;
698
- }
699
- };
700
-
701
- function processPredicateAnnotations(sem, newSubject, previousSubject, localObject, newSubjectOrCarrierO, S, L, block, state, carrier) {
702
- sem.predicates.forEach(pred => {
703
- const role = determinePredicateRole(pred, carrier, newSubject, previousSubject, localObject, newSubjectOrCarrierO, S, L);
704
- if (role) {
705
- const P = state.df.namedNode(expandIRI(pred.iri, state.ctx));
706
- emitQuad(state.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block,
707
- role.subject, P, role.object, state.df,
708
- { kind: 'pred', token: `${pred.form}${pred.iri}`, form: pred.form, expandedPredicate: P.value, entryIndex: pred.entryIndex, remove: pred.remove || false },
709
- state.statements, state.statementCandidates,
710
- state
711
- );
712
- }
713
- });
714
- }
715
-
716
- function processAnnotation(carrier, sem, state, options = {}) {
717
- // Use the enhanced block tracking version
718
- processAnnotationWithBlockTracking(carrier, sem, state, options);
719
- }
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
- function processTokenAnnotations(token, state, tokenType) {
729
- if (token.attrs) {
730
- const sem = parseSemCached(token.attrs);
731
- processAnnotation({
732
- type: tokenType, text: token.text, range: token.range,
733
- attrsRange: token.attrsRange || null, valueRange: token.valueRange || null
734
- }, sem, state);
735
- }
736
-
737
- getCarriers(token).forEach(carrier => {
738
- if (carrier.attrs) {
739
- const sem = parseSemCached(carrier.attrs);
740
- processAnnotation(carrier, sem, state);
741
- }
742
- });
743
- }
744
-
745
- function processStandaloneSubject(token, state) {
746
- const match = STANDALONE_SUBJECT_REGEX.exec(token.text);
747
- if (!match) return;
748
-
749
- const sem = parseSemCached(`{=${match[1]}}`);
750
- const attrsStart = token.range[0] + token.text.indexOf('{=');
751
- processAnnotation({
752
- type: 'standalone', text: '', range: token.range,
753
- attrsRange: [attrsStart, attrsStart + (match[1] ? match[1].length : 0)],
754
- valueRange: null
755
- }, sem, state);
756
- }
757
-
758
- const TOKEN_PROCESSORS = {
759
- heading: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
760
- code: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
761
- blockquote: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
762
- para: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry, [processStandaloneSubject]),
763
- list: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
764
- standalone: (token, state) => processStandaloneSubject(token, state),
765
- };