mdld-parse 0.9.2 → 0.9.4

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 (64) hide show
  1. package/dist/.nojekyll +0 -0
  2. package/dist/CNAME +1 -0
  3. package/dist/README.md +425 -0
  4. package/dist/docs/API.md +397 -0
  5. package/dist/docs/Architecture.md +190 -0
  6. package/dist/docs/Guide.md +816 -0
  7. package/dist/docs/LLM-MDLD-Inducer.md +550 -0
  8. package/dist/docs/Parser.md +654 -0
  9. package/dist/docs/Performance.md +238 -0
  10. package/dist/docs/Spec-render.md +1439 -0
  11. package/dist/docs/Subject.md +180 -0
  12. package/dist/docs/Syntax.md +463 -0
  13. package/dist/docs/TodoList.md +215 -0
  14. package/dist/docs/Use-Cases.md +354 -0
  15. package/dist/docs/guide-m.md +56 -0
  16. package/dist/docs/index.html +108 -0
  17. package/dist/docs/index.md +56 -0
  18. package/dist/docs/one-page.md +322 -0
  19. package/dist/docs/origin.md +459 -0
  20. package/dist/docs/polarity.md +586 -0
  21. package/dist/docs/primary-subject.md +199 -0
  22. package/dist/docs/statements.md +226 -0
  23. package/dist/docs/tokenization-performance-report.md +164 -0
  24. package/dist/examples/Cookbook.md +32 -0
  25. package/dist/examples/Status-SHACL.md +31 -0
  26. package/dist/examples/demo/index.md +35 -0
  27. package/dist/examples/demo/journal.md +119 -0
  28. package/dist/examples/demo/minimal.md +23 -0
  29. package/dist/examples/demo/project.md +98 -0
  30. package/dist/examples/demo/prov.md +107 -0
  31. package/dist/examples/demo/rdf.md +69 -0
  32. package/dist/examples/demo/recipe.md +95 -0
  33. package/dist/examples/demo/research.md +126 -0
  34. package/dist/examples/demo/shacl.md +161 -0
  35. package/dist/examples/demo/space-mission.md +129 -0
  36. package/dist/examples/demo/tasks.md +175 -0
  37. package/dist/examples/demo/xsd.md +91 -0
  38. package/dist/examples/dogfood.md +288 -0
  39. package/dist/examples/few-shot.md +309 -0
  40. package/dist/examples/graph/rdf-graph.html +473 -0
  41. package/dist/examples/graph/rdf_force_graph_web_component.html +506 -0
  42. package/dist/examples/index.md +45 -0
  43. package/dist/examples/llm-time-workflow.md +173 -0
  44. package/dist/examples/statements.md +43 -0
  45. package/dist/examples/workflow.md +150 -0
  46. package/dist/index.html +3253 -0
  47. package/dist/index.js +1770 -0
  48. package/dist/logo.svg +7 -0
  49. package/dist/spec/Spec-compact.md +363 -0
  50. package/dist/spec/Spec-ultra.md +221 -0
  51. package/dist/spec/Spec.md +1068 -0
  52. package/dist/spec/index.md +46 -0
  53. package/package.json +7 -8
  54. package/src/constants.js +0 -31
  55. package/src/generate.js +0 -245
  56. package/src/highlight.js +0 -362
  57. package/src/index.js +0 -13
  58. package/src/locate.js +0 -23
  59. package/src/merge.js +0 -157
  60. package/src/parse.js +0 -711
  61. package/src/render.js +0 -576
  62. package/src/shared.js +0 -577
  63. package/src/tokenizers.js +0 -594
  64. package/src/utils.js +0 -430
package/src/render.js DELETED
@@ -1,576 +0,0 @@
1
- import { parse } from './parse.js';
2
- import {
3
- DataFactory,
4
- expandIRI,
5
- shortenIRI
6
- } from './utils.js';
7
- import {
8
- escapeHtml,
9
- extractContentFromRange,
10
- getIndentLevel
11
- } from './shared.js';
12
- import { DEFAULT_CONTEXT } from './constants.js';
13
-
14
- /**
15
- * Render MD-LD to HTML+RDFa with round-trip safety using origin system
16
- *
17
- * This implementation leverages the origin tracking system to ensure that:
18
- * 1. Every quad from parsing is represented in the RDFa output
19
- * 2. RDFa attributes are placed exactly where the original annotations were
20
- * 3. The rendered HTML can be parsed back to identical quads
21
- *
22
- * @param {string} mdld - MD-LD input string
23
- * @param {Object} options - Rendering options
24
- * @param {Object} options.context - Additional context prefixes
25
- * @param {string} options.baseIRI - Base IRI for relative URLs
26
- * @param {boolean} options.validate - Enable round-trip validation
27
- * @param {boolean} options.debug - Enable debug output
28
- * @returns {Object} Render result with HTML, metadata, and validation
29
- */
30
- export function render(mdld, options = {}) {
31
- // Phase 1: Parse MD-LD with full origin tracking
32
- const parsed = parse({ text: mdld, context: options.context || {} });
33
-
34
- // Phase 2: Build render state using origin system
35
- const state = buildOriginBasedState(parsed, options, mdld);
36
-
37
- // Phase 3: Render HTML using origin-based positioning
38
- const html = renderWithOriginTracking(parsed.origin.blocks, state);
39
-
40
- // Phase 4: Wrap with RDFa context
41
- const wrapped = wrapWithRDFaContext(html, state.ctx);
42
-
43
- const result = {
44
- html: wrapped,
45
- context: state.ctx,
46
- metadata: {
47
- blockCount: parsed.origin.blocks.size,
48
- quadCount: parsed.quads.length,
49
- renderedRDFaCount: state.renderedRDFaCount,
50
- renderTime: Date.now()
51
- }
52
- };
53
-
54
- if (options.debug) {
55
- result.debug = {
56
- originBlocks: Array.from(parsed.origin.blocks.values()),
57
- quadIndex: Array.from(parsed.origin.quadIndex.entries()),
58
- renderMap: state.renderMap
59
- };
60
- }
61
-
62
- return result;
63
- }
64
-
65
- /**
66
- * Build render state using origin system for precise quad tracking
67
- */
68
- function buildOriginBasedState(parsed, options, mdld) {
69
- return {
70
- ctx: parsed.context || { ...DEFAULT_CONTEXT, ...(options.context || {}) },
71
- df: options.dataFactory || DataFactory,
72
- baseIRI: options.baseIRI || '',
73
- sourceText: mdld,
74
- output: [],
75
- currentSubject: null,
76
- // Origin-based tracking
77
- quadIndex: parsed.origin.quadIndex,
78
- blocks: parsed.origin.blocks,
79
- renderMap: new Map(), // Maps block ID to rendered RDFa attributes
80
- renderedRDFaCount: 0,
81
- validation: options.validate || false
82
- };
83
- }
84
-
85
- /**
86
- * Render blocks using origin tracking for precise RDFa placement
87
- */
88
- function renderWithOriginTracking(blocks, state) {
89
- // Sort blocks by document order
90
- const sortedBlocks = Array.from(blocks.values()).sort((a, b) => {
91
- return (a.range?.start || 0) - (b.range?.start || 0);
92
- });
93
-
94
- // Separate list blocks for special handling
95
- const listBlocks = sortedBlocks.filter(block => block.carrierType === 'list');
96
- const otherBlocks = sortedBlocks.filter(block => block.carrierType !== 'list');
97
-
98
- // Render non-list blocks
99
- otherBlocks.forEach(block => {
100
- renderBlockWithOrigin(block, state);
101
- });
102
-
103
- // Render list blocks with proper structure
104
- if (listBlocks.length > 0) {
105
- renderListsWithOrigin(listBlocks, state);
106
- }
107
-
108
- return state.output.join('');
109
- }
110
-
111
- /**
112
- * Render a single block using origin information
113
- */
114
- function renderBlockWithOrigin(block, state) {
115
- // Update subject context
116
- if (block.subject && block.subject !== 'RESET') {
117
- state.currentSubject = block.subject;
118
- }
119
-
120
- // Build RDFa attributes from origin data
121
- const rdfaAttrs = buildRDFaFromOrigin(block, state);
122
-
123
- // Store render mapping for validation
124
- if (rdfaAttrs) {
125
- state.renderMap.set(block.id || hashBlock(block), rdfaAttrs);
126
- }
127
-
128
- // Render based on block type
129
- switch (block.type || block.carrierType) {
130
- case 'heading':
131
- renderHeading(block, rdfaAttrs, state);
132
- break;
133
- case 'para':
134
- renderParagraph(block, rdfaAttrs, state);
135
- break;
136
- case 'list':
137
- // Handled separately
138
- break;
139
- case 'quote':
140
- renderBlockquote(block, rdfaAttrs, state);
141
- break;
142
- case 'code':
143
- renderCode(block, rdfaAttrs, state);
144
- break;
145
- default:
146
- renderDiv(block, rdfaAttrs, state);
147
- }
148
- }
149
-
150
- /**
151
- * Build RDFa attributes from origin tracking data
152
- */
153
- function buildRDFaFromOrigin(block, state) {
154
- const attrs = [];
155
-
156
- // Skip invalid subjects
157
- if (!block.subject || block.subject === 'RESET' ||
158
- block.subject.startsWith('=#') || block.subject.startsWith('+')) {
159
- return '';
160
- }
161
-
162
- // Subject resolution - use full IRI for round-trip compatibility
163
- const expandedSubject = expandIRI(block.subject, state.ctx);
164
- attrs.push(`about="${escapeHtml(expandedSubject)}"`);
165
-
166
- // Types from block annotations - use full IRIs
167
- if (block.types && block.types.length > 0) {
168
- const types = block.types.map(t => {
169
- const iri = typeof t === 'string' ? t : t.iri;
170
- return expandIRI(iri, state.ctx);
171
- }).join(' ');
172
- attrs.push(`typeof="${escapeHtml(types)}"`);
173
- }
174
-
175
- // Predicates from carriers and block
176
- const allPredicates = [];
177
-
178
- // Add predicates from block (for paragraph-level annotations)
179
- if (block.predicates && block.predicates.length > 0) {
180
- allPredicates.push(...block.predicates);
181
- }
182
-
183
- // Add predicates from carriers
184
- if (block.carriers && block.carriers.length > 0) {
185
- for (const carrier of block.carriers) {
186
- if (carrier.predicates && carrier.predicates.length > 0) {
187
- allPredicates.push(...carrier.predicates);
188
- }
189
- }
190
- }
191
-
192
- if (allPredicates.length > 0) {
193
- const { literalProps, objectProps, reverseProps } = processPredicates(allPredicates, state.ctx);
194
-
195
- if (literalProps.length > 0) {
196
- attrs.push(`property="${escapeHtml(literalProps.join(' '))}"`);
197
- }
198
- if (objectProps.length > 0) {
199
- attrs.push(`rel="${escapeHtml(objectProps.join(' '))}"`);
200
- }
201
- if (reverseProps.length > 0) {
202
- attrs.push(`rev="${escapeHtml(reverseProps.join(' '))}"`);
203
- }
204
- }
205
-
206
- // Count rendered RDFa attributes
207
- if (attrs.length > 1) { // More than just about
208
- state.renderedRDFaCount++;
209
- }
210
-
211
- return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
212
- }
213
-
214
- /**
215
- * Process predicates for RDFa attribute generation
216
- */
217
- function processPredicates(predicates, ctx) {
218
- const literalProps = [];
219
- const objectProps = [];
220
- const reverseProps = [];
221
-
222
- for (const pred of predicates) {
223
- const iri = typeof pred === 'string' ? pred : pred.iri;
224
- const expanded = expandIRI(iri, ctx);
225
-
226
- if (pred.polarity === '-') {
227
- reverseProps.push(expanded);
228
- } else {
229
- if (pred.object && pred.object.termType === 'NamedNode') {
230
- objectProps.push(expanded);
231
- } else {
232
- literalProps.push(expanded);
233
- }
234
- }
235
- }
236
-
237
- return { literalProps, objectProps, reverseProps };
238
- }
239
-
240
- /**
241
- * Render heading with RDFa
242
- */
243
- function renderHeading(block, rdfaAttrs, state) {
244
- const level = block.text ? block.text.match(/^#+/)?.[0]?.length || 1 : 1;
245
- const tag = `h${level}`;
246
-
247
- // Extract clean content by removing MDLD annotations
248
- let content = extractContentFromRange(state.sourceText, block.range);
249
- // Remove any remaining {...} patterns
250
- content = content.replace(/\s*\{[^}]+\}\s*$/g, '').trim();
251
-
252
- state.output.push(`<${tag}${rdfaAttrs}>${escapeHtml(content)}</${tag}>`);
253
- }
254
-
255
- /**
256
- * Render paragraph with inline carriers
257
- */
258
- function renderParagraph(block, rdfaAttrs, state) {
259
- const content = renderContentWithCarriers(block, state);
260
-
261
- state.output.push(`<p${rdfaAttrs}>${content}</p>`);
262
- }
263
-
264
- /**
265
- * Render blockquote with RDFa
266
- */
267
- function renderBlockquote(block, rdfaAttrs, state) {
268
- const content = renderContentWithCarriers(block, state);
269
-
270
- state.output.push(`<blockquote${rdfaAttrs}>${content}</blockquote>`);
271
- }
272
-
273
- /**
274
- * Render code block with RDFa
275
- */
276
- function renderCode(block, rdfaAttrs, state) {
277
- const language = block.info || '';
278
- const content = block.text || '';
279
-
280
- state.output.push(`<pre><code${rdfaAttrs}${language ? ` class="language-${escapeHtml(language)}"` : ''}>${escapeHtml(content)}</code></pre>`);
281
- }
282
-
283
- /**
284
- * Render generic div with RDFa
285
- */
286
- function renderDiv(block, rdfaAttrs, state) {
287
- const content = renderContentWithCarriers(block, state);
288
-
289
- state.output.push(`<div${rdfaAttrs}>${content}</div>`);
290
- }
291
-
292
- /**
293
- * Render content with inline carriers using origin positions
294
- */
295
- function renderContentWithCarriers(block, state) {
296
- if (!block.carriers || block.carriers.length === 0) {
297
- const content = extractCleanContent(state.sourceText, block.range);
298
- return escapeHtml(content);
299
- }
300
-
301
- // Build carrier position map using origin data
302
- const carrierMap = buildCarrierPositionMap(block);
303
-
304
- // Extract clean text without carriers (they'll be added separately)
305
- const cleanText = extractCleanContent(state.sourceText, block.range);
306
-
307
- // Render with carriers at exact positions
308
- return renderTextWithCarriers(cleanText, carrierMap, state);
309
- }
310
-
311
- /**
312
- * Extract clean content by removing all MDLD annotations
313
- */
314
- function extractCleanContent(sourceText, range) {
315
- if (!range || !sourceText) return '';
316
-
317
- let text = sourceText.substring(range[0], range[1]);
318
-
319
- // Remove MDLD annotation patterns
320
- // 1. Remove {...} at end of lines (including multiline)
321
- text = text.replace(/\s*\{[^}]*\}\s*$/gm, '');
322
- // 2. Remove {...} anywhere in content (more aggressive)
323
- text = text.replace(/\{[^}]*\}/g, '');
324
- // 3. Clean up extra whitespace and trailing characters
325
- text = text.replace(/\s+/g, ' ').trim();
326
- // 4. Remove any remaining annotation fragments like "]"
327
- text = text.replace(/\]$/, '');
328
-
329
- return text;
330
- }
331
-
332
- /**
333
- * Build carrier position map from origin data
334
- */
335
- function buildCarrierPositionMap(block) {
336
- const positionMap = [];
337
-
338
- if (!block.carriers) return positionMap;
339
-
340
- for (const carrier of block.carriers) {
341
- if (!carrier.text || !carrier.range) continue;
342
-
343
- positionMap.push({
344
- pos: carrier.range[0] - block.range[0], // Relative to block start
345
- carrier: carrier,
346
- length: carrier.text.length
347
- });
348
- }
349
-
350
- return positionMap.sort((a, b) => a.pos - b.pos);
351
- }
352
-
353
- /**
354
- * Render text with carriers at mapped positions
355
- */
356
- function renderTextWithCarriers(text, carrierMap, state) {
357
- if (carrierMap.length === 0) {
358
- return escapeHtml(text);
359
- }
360
-
361
- let html = '';
362
- let pos = 0;
363
-
364
- for (const mapping of carrierMap) {
365
- // Add text before carrier
366
- if (mapping.pos > pos) {
367
- html += escapeHtml(text.substring(pos, mapping.pos));
368
- }
369
-
370
- // Render carrier with RDFa
371
- html += renderCarrierWithOrigin(mapping.carrier, state);
372
-
373
- // Move position past carrier
374
- pos = mapping.pos + mapping.length;
375
- }
376
-
377
- // Add remaining text
378
- if (pos < text.length) {
379
- html += escapeHtml(text.substring(pos));
380
- }
381
-
382
- return html;
383
- }
384
-
385
- /*
386
- * Render carrier with origin - based RDFa
387
- */
388
- function renderCarrierWithOrigin(carrier, state) {
389
- const attrs = [];
390
-
391
- // Subject from carrier or current context
392
- const subject = carrier.subject || state.currentSubject;
393
- if (!subject || subject === 'RESET' || subject.startsWith('=#') || subject.startsWith('+')) {
394
- return escapeHtml(carrier.text || '');
395
- }
396
-
397
- // About attribute - use CURIE for consistency
398
- const shortenedSubject = shortenIRI(expandIRI(subject, state.ctx), state.ctx);
399
- attrs.push(`about="${escapeHtml(shortenedSubject)}"`);
400
-
401
- // Types - use CURIEs
402
- if (carrier.types && carrier.types.length > 0) {
403
- const types = carrier.types.map(t => {
404
- const iri = typeof t === 'string' ? t : t.iri;
405
- return shortenIRI(expandIRI(iri, state.ctx), state.ctx);
406
- }).join(' ');
407
- attrs.push(`typeof="${escapeHtml(types)}"`);
408
- }
409
-
410
- // Predicates - use CURIEs
411
- if (carrier.predicates && carrier.predicates.length > 0) {
412
- const { literalProps, objectProps, reverseProps } = processPredicates(carrier.predicates, state.ctx);
413
-
414
- if (literalProps.length > 0) {
415
- const shortProps = literalProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
416
- attrs.push(`property="${escapeHtml(shortProps)}"`);
417
- }
418
- if (objectProps.length > 0) {
419
- const shortProps = objectProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
420
- attrs.push(`rel="${escapeHtml(shortProps)}"`);
421
- }
422
- if (reverseProps.length > 0) {
423
- const shortProps = reverseProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
424
- attrs.push(`rev="${escapeHtml(shortProps)}"`);
425
- }
426
- }
427
-
428
- const rdfaAttrs = attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
429
-
430
- // Render based on carrier type
431
- switch (carrier.type) {
432
- case 'emphasis':
433
- return `<em${rdfaAttrs}>${escapeHtml(carrier.text || '')}</em>`;
434
- case 'strong':
435
- return `<strong${rdfaAttrs}>${escapeHtml(carrier.text || '')}</strong>`;
436
- case 'code':
437
- return `<code${rdfaAttrs}>${escapeHtml(carrier.text || '')}</code>`;
438
- case 'link':
439
- return `<a href="${escapeHtml(carrier.url || '')}"${rdfaAttrs}>${escapeHtml(carrier.text || '')}</a>`;
440
- default:
441
- return `<span${rdfaAttrs}>${escapeHtml(carrier.text || '')}</span>`;
442
- }
443
- }
444
-
445
- /**
446
- * Render lists using origin tracking
447
- */
448
- function renderListsWithOrigin(listBlocks, state) {
449
- // Group list blocks by hierarchy
450
- const listGroups = groupListBlocks(listBlocks, state.sourceText);
451
-
452
- listGroups.forEach(group => {
453
- renderListGroup(group, state);
454
- });
455
- }
456
-
457
- /**
458
- * Group list blocks by structural hierarchy
459
- */
460
- function groupListBlocks(listBlocks, sourceText) {
461
- const groups = [];
462
- let currentGroup = null;
463
-
464
- // Sort by position
465
- const sortedBlocks = listBlocks.sort((a, b) =>
466
- (a.range?.start || 0) - (b.range?.start || 0)
467
- );
468
-
469
- for (const block of sortedBlocks) {
470
- const indent = getIndentLevel(block, sourceText);
471
-
472
- if (indent === 0) {
473
- // Start new group
474
- if (currentGroup) {
475
- groups.push(currentGroup);
476
- }
477
- currentGroup = {
478
- contextName: 'Items',
479
- blocks: [block]
480
- };
481
- } else {
482
- // Add to current group
483
- if (currentGroup) {
484
- currentGroup.blocks.push(block);
485
- } else {
486
- currentGroup = {
487
- contextName: 'Items',
488
- blocks: [block]
489
- };
490
- }
491
- }
492
- }
493
-
494
- if (currentGroup) {
495
- groups.push(currentGroup);
496
- }
497
-
498
- return groups;
499
- }
500
-
501
- /**
502
- * Render a list group with proper structure
503
- */
504
- function renderListGroup(group, state) {
505
- state.output.push('<ul>');
506
-
507
- // Render list items preserving hierarchy
508
- for (const block of group.blocks) {
509
- renderListItem(block, state);
510
- }
511
-
512
- state.output.push('</ul>');
513
- }
514
-
515
- /**
516
- * Render individual list item
517
- */
518
- function renderListItem(block, state) {
519
- const rdfaAttrs = buildRDFaFromOrigin(block, state);
520
- const content = renderContentWithCarriers(block, state);
521
-
522
- state.output.push(`<li${rdfaAttrs}>${content}</li>`);
523
- }
524
-
525
- /**
526
- * Generate prefix declarations for RDFa
527
- */
528
- function generatePrefixDeclarations(ctx) {
529
- const prefixes = [];
530
-
531
- for (const [prefix, iri] of Object.entries(ctx)) {
532
- if (prefix !== '@vocab') {
533
- prefixes.push(`${prefix}: ${iri}`);
534
- }
535
- }
536
-
537
- return prefixes.length > 0 ? ` prefix="${prefixes.join(' ')}"` : '';
538
- }
539
-
540
- /**
541
- * Generate vocabulary declaration
542
- */
543
- function generateVocabDeclaration(ctx) {
544
- return ctx['@vocab'] ? ` vocab="${ctx['@vocab']}"` : '';
545
- }
546
-
547
- /**
548
- * Wrap HTML with RDFa context declarations
549
- */
550
- function wrapWithRDFaContext(html, ctx) {
551
- const prefixDecl = generatePrefixDeclarations(ctx);
552
- const vocabDecl = generateVocabDeclaration(ctx);
553
-
554
- return `<div${prefixDecl}${vocabDecl}>${html}</div>`;
555
- }
556
-
557
- /**
558
- * Generate hash for block identification
559
- */
560
- function hashBlock(block) {
561
- const content = `${block.type || block.carrierType}|${block.subject || ''}|${block.text || ''}`;
562
- return simpleHash(content);
563
- }
564
-
565
- /**
566
- * Simple hash function for block IDs
567
- */
568
- function simpleHash(str) {
569
- let hash = 0;
570
- for (let i = 0; i < str.length; i++) {
571
- const char = str.charCodeAt(i);
572
- hash = ((hash << 5) - hash) + char;
573
- hash = hash & hash; // Convert to 32-bit integer
574
- }
575
- return hash.toString(36);
576
- }