mdld-parse 0.7.8 → 0.8.0

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/README.md CHANGED
@@ -125,11 +125,11 @@ Each predicate form determines the graph edge:
125
125
 
126
126
  ## 📍 Elevated Statements
127
127
 
128
- MD-LD supports `rdf:Statement` patterns for elevating certain facts from the semantic data. Use the `extractStatements()` function to extract elevated SPO quads from any quad set.
128
+ MD-LD automatically detects `rdf:Statement` patterns during parsing and extracts elevated SPO quads for convenient consumption by applications.
129
129
 
130
130
  ### Pattern Detection
131
131
 
132
- When a document contains a complete `rdf:Statement` pattern with `rdf:subject`, `rdf:predicate`, and `rdf:object`, you can extract the corresponding SPO quad:
132
+ When the parser encounters a complete `rdf:Statement` pattern with `rdf:subject`, `rdf:predicate`, and `rdf:object`, it automatically adds the corresponding SPO quad to the `statements` array:
133
133
 
134
134
  ```markdown
135
135
  [ex] <http://example.org/>
@@ -137,18 +137,7 @@ When a document contains a complete `rdf:Statement` pattern with `rdf:subject`,
137
137
  ## Elevated statement {=ex:stmt1 .rdf:Statement}
138
138
  **Alice** {+ex:alice ?rdf:subject} *knows* {+ex:knows ?rdf:predicate} **Bob** {+ex:bob ?rdf:object}
139
139
 
140
- Direct statement:**Alice** {=ex:alice} knows **Bob** {?ex:knows +ex:bob}
141
- ```
142
-
143
- ### Extracting Statements
144
-
145
- ```javascript
146
- import { parse, extractStatements } from 'mdld-parse';
147
-
148
- const result = parse(markdown);
149
- const statements = extractStatements(result.quads);
150
-
151
- // statements contains elevated SPO quads from rdf:Statement patterns
140
+ Direct statement:**Alice** {=ex:alice} knows **Bob** {?ex:knows +ex:bob}
152
141
  ```
153
142
 
154
143
  ## 🎨 Syntax Quick Reference
@@ -209,10 +198,11 @@ Parse MD-LD markdown and return RDF quads with lean origin tracking.
209
198
  - `context` (object) — Prefix mappings (default: `{ '@vocab': 'http://www.w3.org/2000/01/rdf-schema#', rdf, rdfs, xsd, sh, prov }`)
210
199
  - `dataFactory` (object) — Custom RDF/JS DataFactory
211
200
 
212
- **Returns:** `{ quads, remove, origin, context, primarySubject }`
201
+ **Returns:** `{ quads, remove, statements, origin, context, primarySubject }`
213
202
 
214
203
  - `quads` — Array of RDF/JS Quads (final resolved graph state)
215
204
  - `remove` — Array of RDF/JS Quads (external retractions targeting prior state)
205
+ - `statements` — Array of elevated RDF/JS Quads extracted from rdf:Statement patterns
216
206
  - `origin` — Lean origin tracking object with quadIndex for UI navigation
217
207
  - `context` — Final context used (includes prefixes)
218
208
  - `primarySubject` — String IRI or null (first non-fragment subject declaration)
@@ -245,26 +235,6 @@ Generate deterministic MDLD from RDF quads.
245
235
 
246
236
  **Returns:** `{ text, context }`
247
237
 
248
- ### `extractStatements(quads, dataFactory)`
249
-
250
- Extract elevated statements from a quad set by detecting rdf:Statement patterns.
251
-
252
- **Parameters:**
253
- - `quads` (array) — Array of RDF/JS Quads to scan
254
- - `dataFactory` (object, optional) — Custom RDF/JS DataFactory (defaults to built-in)
255
-
256
- **Returns:** Array of elevated SPO quads extracted from rdf:Statement patterns
257
-
258
- **Example:**
259
- ```javascript
260
- import { parse, extractStatements } from 'mdld-parse';
261
-
262
- const result = parse(markdown);
263
- const statements = extractStatements(result.quads);
264
-
265
- // statements contains elevated SPO quads from rdf:Statement patterns
266
- ```
267
-
268
238
  ### `locate(quad, origin)`
269
239
 
270
240
  Locate the origin entry for a quad using the lean origin system.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdld-parse",
3
- "version": "0.7.8",
3
+ "version": "0.8.0",
4
4
  "description": "A standards-compliant parser for **MD-LD (Markdown-Linked Data)** — a human-friendly RDF authoring format that extends Markdown with semantic annotations.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/index.js CHANGED
@@ -3,7 +3,6 @@ export { merge } from './merge.js';
3
3
  export { generate } from './generate.js';
4
4
  export { locate } from './locate.js';
5
5
  export { render } from './render.js';
6
- export { extractStatements } from './extract-statements.js';
7
6
  export { DEFAULT_CONTEXT } from './constants.js';
8
7
  export {
9
8
  DataFactory,
package/src/merge.js CHANGED
@@ -31,15 +31,16 @@ function normalizeInput(input, options, docContext) {
31
31
 
32
32
  /**
33
33
  * Merges multiple MDLD documents with diff polarity resolution
34
- * @param {Array<string|ParseResult>} docs
34
+ * @param {Array<string|ParseResult>} docs
35
35
  * @param {Object} options
36
- * @returns {Object} Merge result with quads, remove, origin, and context
36
+ * @returns {Object} Merge result with quads, remove, statements, origin, and context
37
37
  */
38
38
  export function merge(docs, options = {}) {
39
39
  const sessionBuffer = new Map(); // Use Map instead of Set for proper quad storage
40
40
  const sessionRemoveSet = new Set();
41
41
  const allDocuments = [];
42
42
  const quadIndex = new Map();
43
+ const allStatements = []; // Collect statements from all documents
43
44
  const accumulatedContext = new Map(); // Track all unique prefixes across documents
44
45
  const primarySubjects = []; // Collect primary subjects from all documents
45
46
 
@@ -68,10 +69,16 @@ export function merge(docs, options = {}) {
68
69
  index: i,
69
70
  input: typeof input === 'string' ? 'string' : 'ParseResult',
70
71
  origin: doc.origin,
71
- context: doc.context
72
+ context: doc.context,
73
+ statementsCount: doc.statements?.length || 0 // Track statements count
72
74
  };
73
75
  allDocuments.push(documentOrigin);
74
76
 
77
+ // Collect statements from this document
78
+ if (doc.statements && doc.statements.length > 0) {
79
+ allStatements.push(...doc.statements);
80
+ }
81
+
75
82
  // Collect primary subject from this document (already a string IRI)
76
83
  if (doc.primarySubject) {
77
84
  primarySubjects.push(doc.primarySubject);
@@ -141,6 +148,7 @@ export function merge(docs, options = {}) {
141
148
  return {
142
149
  quads: filteredQuads,
143
150
  remove: filteredRemove,
151
+ statements: allStatements, // Include all collected statements
144
152
  origin: mergeOrigin,
145
153
  context: finalContext,
146
154
  primarySubjects: primarySubjects // Include all collected primary subjects
package/src/parse.js CHANGED
@@ -33,6 +33,11 @@ import {
33
33
  determineCarrierType,
34
34
  calcCarrierRanges,
35
35
  extractCleanText,
36
+ RDF_TYPE,
37
+ RDF_STATEMENT,
38
+ RDF_SUBJECT,
39
+ RDF_PREDICATE,
40
+ RDF_OBJECT,
36
41
  createLeanOriginEntry,
37
42
  resolveSubject,
38
43
  resolveObject,
@@ -57,6 +62,8 @@ export function parse(text, options = {}) {
57
62
  primarySubject: null,
58
63
  tokens: null,
59
64
  currentTokenIndex: -1,
65
+ statements: [],
66
+ statementCandidates: new Map(),
60
67
  currentBlock: null,
61
68
  blockStack: []
62
69
  };
@@ -102,7 +109,7 @@ export function parse(text, options = {}) {
102
109
  }
103
110
  }
104
111
 
105
- return { quads: state.quads, remove: filteredRemove, origin: state.origin, context: state.ctx, primarySubject: state.primarySubject };
112
+ return { quads: state.quads, remove: filteredRemove, statements: state.statements, origin: state.origin, context: state.ctx, primarySubject: state.primarySubject };
106
113
  }
107
114
 
108
115
 
@@ -435,7 +442,7 @@ function createBlock(subject, types, predicates, range, attrsRange, valueRange,
435
442
  };
436
443
  }
437
444
 
438
- function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null, state = null) {
445
+ function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null, statements = null, statementCandidates = null, state = null) {
439
446
  if (!subject || !predicate || !object) return;
440
447
 
441
448
  const quad = dataFactory.quad(subject, predicate, object);
@@ -468,6 +475,9 @@ function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predi
468
475
  quadBuffer.set(quadKey, quad);
469
476
  quads.push(quad);
470
477
 
478
+ // Detect rdf:Statement pattern during single-pass parsing
479
+ detectStatementPatternSinglePass(quad, dataFactory, meta, statements, statementCandidates);
480
+
471
481
  // Create lean origin entry using shared utility
472
482
  const originEntry = createLeanOriginEntry(block, subject, predicate, meta);
473
483
 
@@ -483,6 +493,55 @@ function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predi
483
493
  }
484
494
  }
485
495
 
496
+ function detectStatementPatternSinglePass(quad, dataFactory, meta, statements = null, statementCandidates = null) {
497
+ // Skip if not called from parse context (for testing compatibility)
498
+ if (!statements || !statementCandidates) return;
499
+
500
+ const predicate = quad.predicate.value;
501
+
502
+ // Early filter: only process rdf:Statement related predicates
503
+ if (predicate !== RDF_TYPE &&
504
+ predicate !== RDF_SUBJECT &&
505
+ predicate !== RDF_PREDICATE &&
506
+ predicate !== RDF_OBJECT) {
507
+ return;
508
+ }
509
+
510
+ // Check if this quad starts a new rdf:Statement pattern
511
+ if (predicate === RDF_TYPE && quad.object.value === RDF_STATEMENT) {
512
+ statementCandidates.set(quad.subject.value, { spo: {} });
513
+ return;
514
+ }
515
+
516
+ // Check if this quad completes part of an existing rdf:Statement pattern
517
+ const candidate = statementCandidates.get(quad.subject.value);
518
+ if (!candidate) return;
519
+
520
+ // Direct property assignment instead of switch for better performance
521
+ if (predicate === RDF_SUBJECT) {
522
+ candidate.spo.subject = quad.object;
523
+ } else if (predicate === RDF_PREDICATE) {
524
+ candidate.spo.predicate = quad.object;
525
+ } else if (predicate === RDF_OBJECT) {
526
+ candidate.spo.object = quad.object;
527
+ // Store the original quad for potential literal extraction
528
+ candidate.objectQuad = quad;
529
+ }
530
+
531
+ // Check if pattern is complete and create elevated SPO quad
532
+ if (candidate.spo.subject && candidate.spo.predicate && candidate.spo.object) {
533
+ // Use the object directly - literal detection happens at parse time
534
+ const spoQuad = dataFactory.quad(
535
+ candidate.spo.subject,
536
+ candidate.spo.predicate,
537
+ candidate.spo.object
538
+ );
539
+ statements.push(spoQuad);
540
+ // Clean up candidate to avoid duplicate detection
541
+ statementCandidates.delete(quad.subject.value);
542
+ }
543
+ }
544
+
486
545
 
487
546
  const createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
488
547
  const expandedType = expandIRI(typeIRI, state.ctx);
@@ -494,6 +553,7 @@ const createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
494
553
  state.df.namedNode(expandedType),
495
554
  state.df,
496
555
  { kind: 'type', token: `.${typeIRI}`, expandedType, entryIndex: typeInfo.entryIndex, remove: typeInfo.remove },
556
+ state.statements, state.statementCandidates,
497
557
  state
498
558
  );
499
559
  };
@@ -538,6 +598,7 @@ function processPredicateAnnotations(sem, newSubject, previousSubject, localObje
538
598
  emitQuad(state.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block,
539
599
  role.subject, P, role.object, state.df,
540
600
  { kind: 'pred', token: `${pred.form}${pred.iri}`, form: pred.form, expandedPredicate: P.value, entryIndex: pred.entryIndex, remove: pred.remove || false },
601
+ state.statements, state.statementCandidates,
541
602
  state
542
603
  );
543
604
  }
@@ -1,62 +0,0 @@
1
- import { DataFactory } from './utils.js';
2
- import { RDF_TYPE, RDF_SUBJECT, RDF_PREDICATE, RDF_OBJECT, RDF_STATEMENT } from './shared.js';
3
-
4
- /**
5
- * Extract elevated statements from a quad set by detecting rdf:Statement patterns.
6
- *
7
- * This function scans quads for complete rdf:Statement patterns (rdf:type rdf:Statement
8
- * with rdf:subject, rdf:predicate, and rdf:object) and extracts the corresponding SPO quads.
9
- *
10
- * @param {Array} quads - Array of RDF/JS Quads to scan
11
- * @param {Object} dataFactory - RDF/JS DataFactory instance (defaults to built-in)
12
- * @returns {Array} Array of elevated SPO quads extracted from rdf:Statement patterns
13
- */
14
- export function extractStatements(quads, dataFactory = DataFactory) {
15
- const statementCandidates = new Map();
16
- const statements = [];
17
-
18
- for (const quad of quads) {
19
- const predicate = quad.predicate.value;
20
-
21
- // Early filter: only process rdf:Statement related predicates
22
- if (predicate !== RDF_TYPE &&
23
- predicate !== RDF_SUBJECT &&
24
- predicate !== RDF_PREDICATE &&
25
- predicate !== RDF_OBJECT) {
26
- continue;
27
- }
28
-
29
- // Check if this quad starts a new rdf:Statement pattern
30
- if (predicate === RDF_TYPE && quad.object.value === RDF_STATEMENT) {
31
- statementCandidates.set(quad.subject.value, { spo: {} });
32
- continue;
33
- }
34
-
35
- // Check if this quad completes part of an existing rdf:Statement pattern
36
- const candidate = statementCandidates.get(quad.subject.value);
37
- if (!candidate) continue;
38
-
39
- // Direct property assignment for better performance
40
- if (predicate === RDF_SUBJECT) {
41
- candidate.spo.subject = quad.object;
42
- } else if (predicate === RDF_PREDICATE) {
43
- candidate.spo.predicate = quad.object;
44
- } else if (predicate === RDF_OBJECT) {
45
- candidate.spo.object = quad.object;
46
- }
47
-
48
- // Check if pattern is complete and create elevated SPO quad
49
- if (candidate.spo.subject && candidate.spo.predicate && candidate.spo.object) {
50
- const spoQuad = dataFactory.quad(
51
- candidate.spo.subject,
52
- candidate.spo.predicate,
53
- candidate.spo.object
54
- );
55
- statements.push(spoQuad);
56
- // Clean up candidate to avoid duplicate detection
57
- statementCandidates.delete(quad.subject.value);
58
- }
59
- }
60
-
61
- return statements;
62
- }