mdld-parse 0.7.7 → 0.7.8
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 +38 -8
- package/package.json +1 -1
- package/src/extract-statements.js +62 -0
- package/src/index.js +1 -0
- package/src/merge.js +2 -10
- package/src/parse.js +2 -62
package/README.md
CHANGED
|
@@ -123,13 +123,13 @@ Each predicate form determines the graph edge:
|
|
|
123
123
|
| `?p` | S → O | `[NASA] {=ex:nasa ?org}` | object property |
|
|
124
124
|
| `!p` | O → S | `[Parent] {=ex:p !hasPart}` | reverse object |
|
|
125
125
|
|
|
126
|
-
##
|
|
126
|
+
## 📍 Elevated Statements
|
|
127
127
|
|
|
128
|
-
MD-LD
|
|
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.
|
|
129
129
|
|
|
130
130
|
### Pattern Detection
|
|
131
131
|
|
|
132
|
-
When
|
|
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:
|
|
133
133
|
|
|
134
134
|
```markdown
|
|
135
135
|
[ex] <http://example.org/>
|
|
@@ -137,10 +137,21 @@ When the parser encounters 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
|
-
|
|
140
|
+
Direct statement:**Alice** {=ex:alice} knows **Bob** {?ex:knows +ex:bob}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Extracting Statements
|
|
142
144
|
|
|
143
|
-
|
|
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
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 🎨 Syntax Quick Reference
|
|
144
155
|
|
|
145
156
|
### Subject Declaration
|
|
146
157
|
Set current subject (emits no quads):
|
|
@@ -198,11 +209,10 @@ Parse MD-LD markdown and return RDF quads with lean origin tracking.
|
|
|
198
209
|
- `context` (object) — Prefix mappings (default: `{ '@vocab': 'http://www.w3.org/2000/01/rdf-schema#', rdf, rdfs, xsd, sh, prov }`)
|
|
199
210
|
- `dataFactory` (object) — Custom RDF/JS DataFactory
|
|
200
211
|
|
|
201
|
-
**Returns:** `{ quads, remove,
|
|
212
|
+
**Returns:** `{ quads, remove, origin, context, primarySubject }`
|
|
202
213
|
|
|
203
214
|
- `quads` — Array of RDF/JS Quads (final resolved graph state)
|
|
204
215
|
- `remove` — Array of RDF/JS Quads (external retractions targeting prior state)
|
|
205
|
-
- `statements` — Array of elevated RDF/JS Quads extracted from rdf:Statement patterns
|
|
206
216
|
- `origin` — Lean origin tracking object with quadIndex for UI navigation
|
|
207
217
|
- `context` — Final context used (includes prefixes)
|
|
208
218
|
- `primarySubject` — String IRI or null (first non-fragment subject declaration)
|
|
@@ -235,6 +245,26 @@ Generate deterministic MDLD from RDF quads.
|
|
|
235
245
|
|
|
236
246
|
**Returns:** `{ text, context }`
|
|
237
247
|
|
|
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
|
+
|
|
238
268
|
### `locate(quad, origin)`
|
|
239
269
|
|
|
240
270
|
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.
|
|
3
|
+
"version": "0.7.8",
|
|
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",
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
}
|
package/src/index.js
CHANGED
|
@@ -3,6 +3,7 @@ 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';
|
|
6
7
|
export { DEFAULT_CONTEXT } from './constants.js';
|
|
7
8
|
export {
|
|
8
9
|
DataFactory,
|
package/src/merge.js
CHANGED
|
@@ -33,14 +33,13 @@ function normalizeInput(input, options, docContext) {
|
|
|
33
33
|
* Merges multiple MDLD documents with diff polarity resolution
|
|
34
34
|
* @param {Array<string|ParseResult>} docs
|
|
35
35
|
* @param {Object} options
|
|
36
|
-
* @returns {Object} Merge result with quads, remove,
|
|
36
|
+
* @returns {Object} Merge result with quads, remove, 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
|
|
44
43
|
const accumulatedContext = new Map(); // Track all unique prefixes across documents
|
|
45
44
|
const primarySubjects = []; // Collect primary subjects from all documents
|
|
46
45
|
|
|
@@ -69,16 +68,10 @@ export function merge(docs, options = {}) {
|
|
|
69
68
|
index: i,
|
|
70
69
|
input: typeof input === 'string' ? 'string' : 'ParseResult',
|
|
71
70
|
origin: doc.origin,
|
|
72
|
-
context: doc.context
|
|
73
|
-
statementsCount: doc.statements?.length || 0 // Track statements count
|
|
71
|
+
context: doc.context
|
|
74
72
|
};
|
|
75
73
|
allDocuments.push(documentOrigin);
|
|
76
74
|
|
|
77
|
-
// Collect statements from this document
|
|
78
|
-
if (doc.statements && doc.statements.length > 0) {
|
|
79
|
-
allStatements.push(...doc.statements);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
75
|
// Collect primary subject from this document (already a string IRI)
|
|
83
76
|
if (doc.primarySubject) {
|
|
84
77
|
primarySubjects.push(doc.primarySubject);
|
|
@@ -148,7 +141,6 @@ export function merge(docs, options = {}) {
|
|
|
148
141
|
return {
|
|
149
142
|
quads: filteredQuads,
|
|
150
143
|
remove: filteredRemove,
|
|
151
|
-
statements: allStatements, // Include all collected statements
|
|
152
144
|
origin: mergeOrigin,
|
|
153
145
|
context: finalContext,
|
|
154
146
|
primarySubjects: primarySubjects // Include all collected primary subjects
|
package/src/parse.js
CHANGED
|
@@ -33,11 +33,6 @@ 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,
|
|
41
36
|
createLeanOriginEntry,
|
|
42
37
|
resolveSubject,
|
|
43
38
|
resolveObject,
|
|
@@ -62,8 +57,6 @@ export function parse(text, options = {}) {
|
|
|
62
57
|
primarySubject: null,
|
|
63
58
|
tokens: null,
|
|
64
59
|
currentTokenIndex: -1,
|
|
65
|
-
statements: [],
|
|
66
|
-
statementCandidates: new Map(),
|
|
67
60
|
currentBlock: null,
|
|
68
61
|
blockStack: []
|
|
69
62
|
};
|
|
@@ -109,7 +102,7 @@ export function parse(text, options = {}) {
|
|
|
109
102
|
}
|
|
110
103
|
}
|
|
111
104
|
|
|
112
|
-
return { quads: state.quads, remove: filteredRemove,
|
|
105
|
+
return { quads: state.quads, remove: filteredRemove, origin: state.origin, context: state.ctx, primarySubject: state.primarySubject };
|
|
113
106
|
}
|
|
114
107
|
|
|
115
108
|
|
|
@@ -442,7 +435,7 @@ function createBlock(subject, types, predicates, range, attrsRange, valueRange,
|
|
|
442
435
|
};
|
|
443
436
|
}
|
|
444
437
|
|
|
445
|
-
function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null,
|
|
438
|
+
function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null, state = null) {
|
|
446
439
|
if (!subject || !predicate || !object) return;
|
|
447
440
|
|
|
448
441
|
const quad = dataFactory.quad(subject, predicate, object);
|
|
@@ -475,9 +468,6 @@ function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predi
|
|
|
475
468
|
quadBuffer.set(quadKey, quad);
|
|
476
469
|
quads.push(quad);
|
|
477
470
|
|
|
478
|
-
// Detect rdf:Statement pattern during single-pass parsing
|
|
479
|
-
detectStatementPatternSinglePass(quad, dataFactory, meta, statements, statementCandidates);
|
|
480
|
-
|
|
481
471
|
// Create lean origin entry using shared utility
|
|
482
472
|
const originEntry = createLeanOriginEntry(block, subject, predicate, meta);
|
|
483
473
|
|
|
@@ -493,54 +483,6 @@ function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predi
|
|
|
493
483
|
}
|
|
494
484
|
}
|
|
495
485
|
|
|
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
|
const createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
|
|
546
488
|
const expandedType = expandIRI(typeIRI, state.ctx);
|
|
@@ -552,7 +494,6 @@ const createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
|
|
|
552
494
|
state.df.namedNode(expandedType),
|
|
553
495
|
state.df,
|
|
554
496
|
{ kind: 'type', token: `.${typeIRI}`, expandedType, entryIndex: typeInfo.entryIndex, remove: typeInfo.remove },
|
|
555
|
-
state.statements, state.statementCandidates,
|
|
556
497
|
state
|
|
557
498
|
);
|
|
558
499
|
};
|
|
@@ -597,7 +538,6 @@ function processPredicateAnnotations(sem, newSubject, previousSubject, localObje
|
|
|
597
538
|
emitQuad(state.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block,
|
|
598
539
|
role.subject, P, role.object, state.df,
|
|
599
540
|
{ kind: 'pred', token: `${pred.form}${pred.iri}`, form: pred.form, expandedPredicate: P.value, entryIndex: pred.entryIndex, remove: pred.remove || false },
|
|
600
|
-
state.statements, state.statementCandidates,
|
|
601
541
|
state
|
|
602
542
|
);
|
|
603
543
|
}
|