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/shared.js DELETED
@@ -1,577 +0,0 @@
1
- import { DEFAULT_CONTEXT, STANDALONE_SUBJECT_REGEX, FENCE_REGEX, PREFIX_REGEX, HEADING_REGEX, UNORDERED_LIST_REGEX, BLOCKQUOTE_REGEX } from './constants.js';
2
- import { parseSemanticBlock, expandIRI, shortenIRI } from './utils.js';
3
-
4
- // Cache for fence regex patterns
5
- export const FENCE_CLOSE_PATTERNS = new Map();
6
-
7
- export function getFenceClosePattern(fenceChar) {
8
- if (!FENCE_CLOSE_PATTERNS.has(fenceChar)) {
9
- FENCE_CLOSE_PATTERNS.set(fenceChar, new RegExp(`^(${fenceChar}{3,})`));
10
- }
11
- return FENCE_CLOSE_PATTERNS.get(fenceChar);
12
- }
13
-
14
- // Range calculation utilities - shared between parser and renderer
15
- export function calcRangeInfo(line, attrs, lineStart, prefixLength, valueLength) {
16
- const wsLength = prefixLength < line.length && line[prefixLength] === ' ' ? 1 :
17
- line.slice(prefixLength).match(/^\s+/)?.[0]?.length || 0;
18
- const valueStartInLine = prefixLength + wsLength;
19
- return {
20
- valueRange: [lineStart + valueStartInLine, lineStart + valueStartInLine + valueLength],
21
- attrsRange: calcAttrsRange(line, attrs, lineStart)
22
- };
23
- }
24
-
25
- export function calcAttrsRange(line, attrs, lineStart) {
26
- if (!attrs) return null;
27
- const attrsStartInLine = line.lastIndexOf(attrs);
28
- return attrsStartInLine >= 0 ? [lineStart + attrsStartInLine, lineStart + attrsStartInLine + attrs.length] : null;
29
- }
30
-
31
- // Token creation utilities - shared structure
32
- export function createToken(type, range, text, attrs = null, attrsRange = null, valueRange = null, extra = {}) {
33
- const token = { type, range, text, attrs, attrsRange, valueRange, ...extra };
34
- Object.defineProperty(token, '_carriers', {
35
- enumerable: false, writable: true, value: null
36
- });
37
- return token;
38
- }
39
-
40
- export function createCarrier(type, text, attrs, attrsRange, valueRange, range, pos, extra = {}) {
41
- return { type, text, attrs, attrsRange, valueRange, range, pos, ...extra };
42
- }
43
-
44
- // List token creation - shared logic
45
- export function createListToken(type, line, lineStart, pos, match) {
46
- const attrs = match[4] || null;
47
- const prefix = match[1].length + (match[2] ? match[2].length : 0);
48
- const rangeInfo = calcRangeInfo(line, attrs, lineStart, prefix, match[3].length);
49
- return createToken(type, [lineStart, pos - 1], match[3].trim(), attrs,
50
- rangeInfo.attrsRange, rangeInfo.valueRange, { indent: match[1].length });
51
- }
52
-
53
- // Semantic block parsing - shared between parser and renderer
54
- export const semCache = {};
55
- export const EMPTY_SEM = Object.freeze({ predicates: [], types: [], subject: null });
56
-
57
- export function parseSemCached(attrs) {
58
- if (!attrs) return EMPTY_SEM;
59
- let sem = semCache[attrs];
60
- if (!sem) {
61
- sem = Object.freeze(parseSemanticBlock(attrs));
62
- semCache[attrs] = sem;
63
- }
64
- return sem;
65
- }
66
-
67
- // Indentation utilities - shared for list processing
68
- export function getIndentLevel(block, sourceText) {
69
- if (!block.range || !sourceText) return 0;
70
-
71
- const text = sourceText.substring(block.range.start, block.range.end);
72
- const indentMatch = text.match(/^(\s*)/);
73
- const indentSpaces = indentMatch ? indentMatch[1].length : 0;
74
-
75
- // CommonMark: 4 spaces or 1 tab = one level
76
- // We'll use 2 spaces for better readability (configurable)
77
- return Math.floor(indentSpaces / 2);
78
- }
79
-
80
- // Content extraction utilities - shared between parser and renderer
81
- export function extractContentFromRange(sourceText, range, attrsRange = null) {
82
- if (!range || !sourceText) return '';
83
-
84
- let text = sourceText.substring(range[0], range[1]);
85
-
86
- // Remove MD-LD annotations, preserve content
87
- if (attrsRange) {
88
- const beforeAttrs = text.substring(0, attrsRange[0] - range[0]);
89
- const afterAttrs = text.substring(attrsRange[1] - range[0]);
90
- text = beforeAttrs + afterAttrs;
91
- }
92
-
93
- return text.trim();
94
- }
95
-
96
- // List marker utilities - shared for advanced list processing
97
- export function getListMarker(block, sourceText) {
98
- if (!block.range) return null;
99
-
100
- const text = sourceText.substring(block.range.start, block.range.end);
101
- const markerMatch = text.match(/^(\s*)([-*+]|\d+\[\.|\])\s+/);
102
-
103
- if (!markerMatch) return null;
104
-
105
- return {
106
- type: markerMatch[2].startsWith('-') ? 'dash' :
107
- markerMatch[2].startsWith('*') ? 'asterisk' :
108
- markerMatch[2].startsWith('+') ? 'plus' : 'ordered',
109
- marker: markerMatch[2],
110
- indent: markerMatch[1].length
111
- };
112
- }
113
-
114
- // CommonMark line processors - shared between parser and renderer
115
- export const PROCESSORS = [
116
- { test: line => line.startsWith('```'), process: null }, // Code blocks handled separately
117
- { test: line => line.startsWith('`'), process: null }, // Code spans handled separately
118
- { test: line => PREFIX_REGEX.test(line), process: null }, // Prefixes handled separately
119
- { test: line => HEADING_REGEX.test(line), process: null }, // Headings handled separately
120
- { test: line => UNORDERED_LIST_REGEX.test(line), process: null }, // Lists handled separately
121
- { test: line => BLOCKQUOTE_REGEX.test(line), process: null }, // Blockquotes handled separately
122
- { test: line => STANDALONE_SUBJECT_REGEX.test(line), process: null }, // Standalone subjects handled separately
123
- { test: line => line.trim() === '', process: null }, // Empty lines handled separately
124
- { test: line => true, process: null } // Default: paragraph
125
- ];
126
-
127
- // Token scanning processors - shared between parser and renderer
128
- export const TOKEN_PROCESSORS = [
129
- { type: 'fence', test: line => FENCE_REGEX.test(line.trim()), process: null }, // Will be overridden in parse.js
130
- { type: 'content', test: line => false, process: null }, // Will be overridden in parse.js
131
- { type: 'prefix', test: line => PREFIX_REGEX.test(line), process: null }, // Will be overridden in parse.js
132
- { type: 'heading', test: line => HEADING_REGEX.test(line), process: null }, // Will be overridden in parse.js
133
- { type: 'list', test: line => UNORDERED_LIST_REGEX.test(line), process: null }, // Will be overridden in parse.js
134
- { type: 'blockquote', test: line => BLOCKQUOTE_REGEX.test(line), process: null }, // Will be overridden in parse.js
135
- { type: 'para', test: line => line.trim(), process: null } // Will be overridden in parse.js
136
- ];
137
-
138
- // Language and attributes parsing
139
- export function parseLangAndAttrs(langAndAttrs) {
140
- const spaceIndex = langAndAttrs.indexOf(' ');
141
- const braceIndex = langAndAttrs.indexOf('{');
142
- const langEnd = Math.min(
143
- spaceIndex > -1 ? spaceIndex : Infinity,
144
- braceIndex > -1 ? braceIndex : Infinity
145
- );
146
- return {
147
- lang: langAndAttrs.substring(0, langEnd),
148
- attrsText: langAndAttrs.substring(langEnd).match(/\{[^{}]*\}/)?.[0] || null
149
- };
150
- }
151
-
152
- // Carrier extraction utilities
153
- export function findMatchingBracket(text, bracketStart) {
154
- let bracketDepth = 1;
155
- let bracketEnd = bracketStart + 1;
156
-
157
- while (bracketEnd < text.length && bracketDepth > 0) {
158
- if (text[bracketEnd] === '[') bracketDepth++;
159
- else if (text[bracketEnd] === ']') bracketDepth--;
160
- bracketEnd++;
161
- }
162
-
163
- return bracketDepth > 0 ? null : bracketEnd;
164
- }
165
-
166
- export function extractUrlFromBrackets(text, bracketEnd) {
167
- let url = null;
168
- let spanEnd = bracketEnd;
169
-
170
- if (text[spanEnd] === '(') {
171
- const parenEnd = text.indexOf(')', spanEnd);
172
- if (parenEnd !== -1) {
173
- url = text.substring(spanEnd + 1, parenEnd);
174
- spanEnd = parenEnd + 1;
175
- }
176
- }
177
-
178
- return { url, spanEnd };
179
- }
180
-
181
- export function extractAttributesFromText(text, spanEnd, baseOffset) {
182
- let attrs = null;
183
- let attrsRange = null;
184
- const remaining = text.substring(spanEnd);
185
-
186
- const wsMatch = remaining.match(/^\s+/);
187
- const attrsStart = wsMatch ? wsMatch[0].length : 0;
188
-
189
- if (remaining[attrsStart] === '{') {
190
- const braceEnd = remaining.indexOf('}', attrsStart);
191
- if (braceEnd !== -1) {
192
- attrs = remaining.substring(attrsStart, braceEnd + 1);
193
- const absStart = baseOffset + spanEnd + attrsStart;
194
- attrsRange = [absStart, absStart + attrs.length];
195
- spanEnd += braceEnd + 1;
196
- }
197
- }
198
-
199
- return { attrs, attrsRange, finalSpanEnd: spanEnd };
200
- }
201
-
202
- export function determineCarrierType(url) {
203
- if (url && !url.startsWith('=')) {
204
- return { carrierType: 'link', resourceIRI: url };
205
- }
206
- return { carrierType: 'span', resourceIRI: null };
207
- }
208
-
209
- export function calcCarrierRanges(match, baseOffset, matchStart) {
210
- const valueStart = baseOffset + matchStart + match[0].indexOf(match[1]);
211
- const valueEnd = valueStart + match[1].length;
212
- const attrsStart = baseOffset + matchStart + match[0].indexOf('{');
213
- const attrsEnd = attrsStart + match[2].length + 2; // +2 for { and }
214
- return {
215
- valueRange: [valueStart, valueEnd],
216
- attrsRange: [attrsStart, attrsEnd], // Include braces
217
- range: [baseOffset + matchStart, attrsEnd],
218
- pos: matchStart + match[0].length // pos should be relative to current text, not document
219
- };
220
- }
221
-
222
- // Clean text extraction utilities
223
- export function extractCleanText(token) {
224
- if (!token.text) return '';
225
-
226
- let text = token.text;
227
-
228
- // Remove block-level semantic annotations
229
- if (token.attrsRange) {
230
- const beforeAttrs = text.substring(0, token.attrsRange[0] - (token.range?.[0] || 0));
231
- const afterAttrs = text.substring(token.attrsRange[1] - (token.range?.[0] || 0));
232
- text = beforeAttrs + afterAttrs;
233
- }
234
-
235
- // Remove inline carrier annotations
236
- const carriers = token._carriers || [];
237
- const inlineRanges = carriers
238
- .filter(carrier => carrier.attrsRange)
239
- .map(carrier => carrier.attrsRange.map(pos => pos - (token.range?.[0] || 0)))
240
- .filter(([start, end]) => start >= 0 && end <= text.length)
241
- .sort((a, b) => b[0] - a[0]);
242
-
243
- for (const [relativeStart, relativeEnd] of inlineRanges) {
244
- const before = text.substring(0, relativeStart);
245
- const after = text.substring(relativeEnd);
246
- text = before + after;
247
- }
248
-
249
- // Clean based on token type
250
- switch (token.type) {
251
- case 'heading':
252
- return text.replace(/^#+\s*/, '').trim();
253
- case 'list':
254
- return text.replace(/^[-*+]\s*/, '').trim();
255
- case 'blockquote':
256
- return text.replace(/^>\s*/, '').trim();
257
- default:
258
- return text.trim();
259
- }
260
- }
261
-
262
- // Quad emission utilities
263
- export const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
264
- export const RDF_STATEMENT = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement';
265
- export const RDF_SUBJECT = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject';
266
- export const RDF_PREDICATE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate';
267
- export const RDF_OBJECT = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object';
268
-
269
- export function createLeanOriginEntry(block, subject, predicate, meta = null) {
270
- return {
271
- blockId: block.id,
272
- range: block.range,
273
- carrierType: block.carrierType,
274
- subject: subject.value,
275
- predicate: predicate.value,
276
- context: block.context, // Direct reference instead of spread
277
- polarity: meta?.remove ? '-' : '+',
278
- value: block.text || ''
279
- };
280
- }
281
-
282
- // Fragment resolution utilities
283
- export function resolveFragment(fragment, currentSubject, dataFactory) {
284
- if (!currentSubject) return null;
285
- const subjectValue = currentSubject.value;
286
- const hashIndex = subjectValue.indexOf('#');
287
- const baseIRI = hashIndex > -1 ? subjectValue.slice(0, hashIndex) : subjectValue;
288
- return dataFactory.namedNode(baseIRI + '#' + fragment);
289
- }
290
-
291
- export function resolveSubject(sem, state) {
292
- if (!sem.subject) return null;
293
- if (sem.subject === 'RESET') {
294
- state.currentSubject = null;
295
- return null;
296
- }
297
- if (sem.subject.startsWith('=#')) return resolveFragment(sem.subject.substring(2), state.currentSubject, state.df);
298
- return state.df.namedNode(expandIRI(sem.subject, state.ctx));
299
- }
300
-
301
- export function resolveObject(sem, state) {
302
- if (!sem.object) return null;
303
- if (sem.object.startsWith('#')) return resolveFragment(sem.object.substring(1), state.currentSubject, state.df);
304
- return state.df.namedNode(expandIRI(sem.object, state.ctx));
305
- }
306
-
307
- // HTML escaping - shared utility
308
- export function escapeHtml(text) {
309
- if (!text) return '';
310
- return text
311
- .replace(/&/g, '&amp;')
312
- .replace(/</g, '&lt;')
313
- .replace(/>/g, '&gt;')
314
- .replace(/"/g, '&quot;')
315
- .replace(/'/g, '&#x27;');
316
- }
317
-
318
- // RDF term type checking utilities - shared across modules
319
- export function isLiteral(term) {
320
- return term?.termType === 'Literal';
321
- }
322
-
323
- export function isNamedNode(term) {
324
- return term?.termType === 'NamedNode';
325
- }
326
-
327
- export function isRdfType(term) {
328
- return term?.value === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
329
- }
330
-
331
- // IRI prefix extraction utility
332
- export function getPrefixFromIRI(iri, context) {
333
- if (!iri) return null;
334
- const shortened = shortenIRI(iri, context);
335
- if (shortened.includes(':')) {
336
- return shortened.split(':')[0];
337
- }
338
- return null;
339
- }
340
-
341
- // Prefix collection utility - used by generate.js
342
- export function collectUsedPrefixes(subjectGroups, context) {
343
- const usedPrefixes = new Set();
344
-
345
- for (const subjectQuads of subjectGroups.values()) {
346
- for (const quad of subjectQuads) {
347
- // Check subject prefix
348
- const subjectPrefix = getPrefixFromIRI(quad.subject.value, context);
349
- if (subjectPrefix) usedPrefixes.add(subjectPrefix);
350
-
351
- // Check predicate prefix
352
- const predicatePrefix = getPrefixFromIRI(quad.predicate.value, context);
353
- if (predicatePrefix) usedPrefixes.add(predicatePrefix);
354
-
355
- // Check object prefix if it's a named node
356
- if (isNamedNode(quad.object)) {
357
- const objectPrefix = getPrefixFromIRI(quad.object.value, context);
358
- if (objectPrefix) usedPrefixes.add(objectPrefix);
359
- }
360
-
361
- // Check datatype prefix if present
362
- if (quad.object.datatype && quad.object.datatype.value) {
363
- const datatypePrefix = getPrefixFromIRI(quad.object.datatype.value, context);
364
- if (datatypePrefix) usedPrefixes.add(datatypePrefix);
365
- }
366
- }
367
- }
368
-
369
- return usedPrefixes;
370
- }
371
-
372
- // Token processing utility - eliminates duplication in TOKEN_PROCESSORS
373
- export function processTokenWithBlockTracking(token, state, processAnnotations, createBlockEntry, additionalProcessors = []) {
374
- const blockEntry = createBlockEntry(token, state);
375
- state.currentBlock = blockEntry;
376
- state.blockStack.push(blockEntry.id);
377
-
378
- // Run any additional processors first
379
- additionalProcessors.forEach(processor => processor(token, state));
380
-
381
- // Process annotations
382
- processAnnotations(token, state, token.type);
383
-
384
- state.blockStack.pop();
385
- state.currentBlock = state.blockStack.length > 0 ?
386
- state.origin.blocks.get(state.blockStack[state.blockStack.length - 1]) : null;
387
- }
388
-
389
- // Quad key generation - shared between parser and renderer
390
- export function quadIndexKey(subject, predicate, object) {
391
- const datatype = object.datatype?.value || '';
392
- const language = object.language || '';
393
- return `${subject.value}|${predicate.value}|${object.value}|${datatype}|${language}`;
394
- }
395
-
396
- // IRI expansion and shortening - shared utilities
397
- export function expandAndShortenIRI(iri, ctx) {
398
- const expanded = expandIRI(iri, ctx);
399
- return shortenIRI(expanded, ctx);
400
- }
401
-
402
- // Subject resolution utilities - shared between parser and renderer
403
- export function resolveSubjectType(subjectDecl) {
404
- if (!subjectDecl) return 'none';
405
-
406
- if (subjectDecl.startsWith('=#')) {
407
- return 'fragment';
408
- }
409
-
410
- if (subjectDecl.startsWith('+')) {
411
- return 'soft-object';
412
- }
413
-
414
- if (subjectDecl === 'RESET') {
415
- return 'reset';
416
- }
417
-
418
- return 'full-iri';
419
- }
420
-
421
- // Constants - shared across modules (bundle-size optimized)
422
- export const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
423
-
424
- // Optimized sorting utilities - inline for better minification
425
- export function sortQuadsByPredicate(quads) {
426
- return quads.sort((a, b) => a.predicate.value.localeCompare(b.predicate.value));
427
- }
428
-
429
- // Optimized text generation - template literals for smaller bundle
430
- export const generatePrefixDeclaration = (prefix, namespace) => `[${prefix}] <${namespace}>\n`;
431
-
432
- export function generateLiteralText(quad, context) {
433
- const predShort = shortenIRI(quad.predicate.value, context);
434
- let annotation = predShort;
435
-
436
- if (quad.object.language) {
437
- annotation += ` @${quad.object.language}`;
438
- } else if (quad.object.datatype.value !== XSD_STRING) {
439
- annotation += ` ^^${shortenIRI(quad.object.datatype.value, context)}`;
440
- }
441
-
442
- // Get clean value (handle both RDF/JS Literal objects and plain strings)
443
- const rawValue = quad.object.value || quad.object;
444
- const value = typeof rawValue === 'string' ? rawValue : String(rawValue);
445
-
446
- // Get datatype for styling decisions
447
- const datatype = quad.object.datatype?.value || '';
448
-
449
- // Multiline: fenced block (safe for arbitrary content)
450
- if (value.includes('\n')) {
451
- return `~~~ {${annotation}}\n${value}\n~~~\n\n`;
452
- }
453
-
454
- // Numbers (integer, decimal, double, float): code spans
455
- if (datatype.includes('integer') || datatype.includes('decimal') || datatype.includes('double') || datatype.includes('float')) {
456
- return `\`${value}\` {${annotation}}\n`;
457
- }
458
-
459
- if (datatype.includes('date') || datatype.includes('time')) {
460
- return `*${value}* {${annotation}}\n`;
461
- }
462
-
463
- // Booleans: bold (stands out visually)
464
- if (datatype.includes('boolean')) {
465
- return `**${value}** {${annotation}}\n`;
466
- }
467
-
468
- // Everything else (strings, dates, etc.): square brackets (simple, readable)
469
- return `[${value}] {${annotation}}\n`;
470
- }
471
-
472
- export const generateObjectText = (quad, context, labelLookup = null) => {
473
- const objShort = shortenIRI(quad.object.value, context);
474
- const predShort = shortenIRI(quad.predicate.value, context);
475
-
476
- // Use rdfs:label if available, otherwise use shortened IRI
477
- const displayText = labelLookup && labelLookup.has(quad.object.value)
478
- ? labelLookup.get(quad.object.value)
479
- : objShort;
480
-
481
- // Object links: italic
482
- return `[${displayText}] {+${objShort} ?${predShort}}\n`;
483
- };
484
-
485
- // Optimized quad filtering - destructuring for smaller minified output
486
- export function filterQuadsByType(subjectQuads) {
487
- const types = [], literals = [], objects = [];
488
- for (const q of subjectQuads) {
489
- if (isRdfType(q.predicate)) {
490
- types.push(q);
491
- } else if (isLiteral(q.object)) {
492
- literals.push(q);
493
- } else if (isNamedNode(q.object)) {
494
- objects.push(q);
495
- }
496
- }
497
- return { types, literals, objects };
498
- }
499
-
500
- // Predicate processing utilities - common RDFa patterns
501
- export function processPredicates(predicates, ctx) {
502
- const literalProps = [];
503
- const objectProps = [];
504
- const reverseProps = [];
505
-
506
- predicates.forEach(pred => {
507
- const iri = typeof pred === 'string' ? pred : pred.iri;
508
- const expanded = expandIRI(iri, ctx);
509
- const shortened = shortenIRI(expanded, ctx);
510
- const form = typeof pred === 'string' ? '' : (pred.form || '');
511
-
512
- if (form === '!') {
513
- reverseProps.push(shortened);
514
- } else if (form === '?') {
515
- objectProps.push(shortened);
516
- } else {
517
- literalProps.push(shortened);
518
- }
519
- });
520
-
521
- return { literalProps, objectProps, reverseProps };
522
- }
523
-
524
- // Deterministic sorting utilities - ensure consistent output
525
- export function sortDeterministic(array, keyFn) {
526
- return array.sort((a, b) => {
527
- const keyA = keyFn(a);
528
- const keyB = keyFn(b);
529
- return keyA.localeCompare(keyB);
530
- });
531
- }
532
-
533
- export function sortQuadsDeterministically(quads) {
534
- return quads.sort((a, b) => {
535
- // Deterministic sorting: subject -> predicate -> object
536
- const sComp = a.subject.value.localeCompare(b.subject.value);
537
- if (sComp !== 0) return sComp;
538
- const pComp = a.predicate.value.localeCompare(b.predicate.value);
539
- if (pComp !== 0) return pComp;
540
- const oA = isLiteral(a.object) ? a.object.value : a.object.value;
541
- const oB = isLiteral(b.object) ? b.object.value : b.object.value;
542
- return oA.localeCompare(oB);
543
- });
544
- }
545
-
546
- // Optimized deterministic prefix generation
547
- export function generateDeterministicPrefixes(context, usedPrefixes) {
548
- const sortedEntries = Object.entries(context).sort(([a], [b]) => a.localeCompare(b));
549
- let text = '';
550
-
551
- for (const [prefix, namespace] of sortedEntries) {
552
- if (prefix !== '@vocab' && !prefix.startsWith('@') && !DEFAULT_CONTEXT[prefix] && usedPrefixes.has(prefix)) {
553
- text += generatePrefixDeclaration(prefix, namespace);
554
- }
555
- }
556
-
557
- return text;
558
- }
559
-
560
- // Memory-efficient block creation
561
- export function createOptimizedBlockEntry(token, state) {
562
- const id = hash(`${token.range[0]}-${token.range[1]}-${token.text.slice(0, 50)}`);
563
- const block = {
564
- id,
565
- type: token.type,
566
- carrierType: token.type,
567
- range: token.range,
568
- text: token.text,
569
- carriers: [],
570
- predicates: [],
571
- subject: state.currentSubject,
572
- context: { ...state.ctx }
573
- };
574
-
575
- state.origin.blocks.set(id, block);
576
- return block;
577
- }