mdld-parse 0.2.1 → 0.2.2
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 +18 -1
- package/index.js +31 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,7 +171,8 @@ Serialize RDF changes back to markdown with proper positioning.
|
|
|
171
171
|
- `add` — Array of quads to add
|
|
172
172
|
- `delete` — Array of quads to remove
|
|
173
173
|
- `origin` (object) — Origin object from parse result
|
|
174
|
-
- `options` (object, optional) — Additional options
|
|
174
|
+
- `options` (object, optional) — Additional options:
|
|
175
|
+
- `context` (object) — Context for IRI shortening (default: empty object)
|
|
175
176
|
|
|
176
177
|
**Returns:** Object containing:
|
|
177
178
|
- `text` — Updated markdown text
|
|
@@ -198,6 +199,22 @@ Written by [Alice](ex:alice) {ex:author}
|
|
|
198
199
|
// object: { termType: 'NamedNode', value: 'http://schema.org/Article' },
|
|
199
200
|
// graph: { termType: 'DefaultGraph' }
|
|
200
201
|
// }
|
|
202
|
+
|
|
203
|
+
// Add a new quad with proper IRI shortening
|
|
204
|
+
const newQuad = {
|
|
205
|
+
subject: { termType: 'NamedNode', value: 'http://example.org/doc#article' },
|
|
206
|
+
predicate: { termType: 'NamedNode', value: 'http://schema.org/dateCreated' },
|
|
207
|
+
object: { termType: 'Literal', value: '2024-01-01' }
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const serialized = serialize({
|
|
211
|
+
text: originalText,
|
|
212
|
+
diff: { add: [newQuad] },
|
|
213
|
+
origin: result.origin,
|
|
214
|
+
options: { context: result.context } // Important: pass context for IRI shortening
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Result: [2024-01-01] {dateCreated} // Properly shortened!
|
|
201
218
|
```
|
|
202
219
|
|
|
203
220
|
## Implementation Details
|
package/index.js
CHANGED
|
@@ -313,7 +313,7 @@ function processAnnotation(token, state, textContent = null) {
|
|
|
313
313
|
state.df.namedNode(expandIRI(token.url, state.ctx)) : targetSubject;
|
|
314
314
|
emitQuad(state.quads, state.origin.quadIndex, block.id,
|
|
315
315
|
typeSubject, state.df.namedNode(expandIRI('rdf:type', state.ctx)),
|
|
316
|
-
state.df.namedNode(e.classIRI), state.df);
|
|
316
|
+
state.df.namedNode(expandIRI(e.classIRI, state.ctx)), state.df);
|
|
317
317
|
} else if (e.kind === 'property' && e.predicate) {
|
|
318
318
|
const predicate = state.df.namedNode(expandIRI(e.predicate, state.ctx));
|
|
319
319
|
let object;
|
|
@@ -384,7 +384,7 @@ function processListItem(token, state) {
|
|
|
384
384
|
|
|
385
385
|
// Process list context relationship
|
|
386
386
|
if (state.listContext?.predicate && originalSubject) {
|
|
387
|
-
const predicate = state.df.namedNode(state.listContext.predicate);
|
|
387
|
+
const predicate = state.df.namedNode(expandIRI(state.listContext.predicate, state.ctx));
|
|
388
388
|
if (state.listContext.reverse) {
|
|
389
389
|
emitQuad(state.quads, state.origin.quadIndex, 'list-context',
|
|
390
390
|
state.currentSubject, predicate, originalSubject, state.df);
|
|
@@ -469,11 +469,31 @@ export function parse(text, options = {}) {
|
|
|
469
469
|
return { quads: state.quads, origin: state.origin, context: state.ctx };
|
|
470
470
|
}
|
|
471
471
|
|
|
472
|
+
function shortenIRI(iri, ctx) {
|
|
473
|
+
if (!iri || !iri.startsWith('http')) return iri;
|
|
474
|
+
|
|
475
|
+
// Check @vocab first
|
|
476
|
+
if (ctx['@vocab'] && iri.startsWith(ctx['@vocab'])) {
|
|
477
|
+
return iri.substring(ctx['@vocab'].length);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// Check prefixes
|
|
481
|
+
for (const [prefix, namespace] of Object.entries(ctx)) {
|
|
482
|
+
if (prefix !== '@vocab' && iri.startsWith(namespace)) {
|
|
483
|
+
return prefix + ':' + iri.substring(namespace.length);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// No prefix found, return full IRI
|
|
488
|
+
return iri;
|
|
489
|
+
}
|
|
490
|
+
|
|
472
491
|
export function serialize({ text, diff, origin, options = {} }) {
|
|
473
492
|
if (!diff || (!diff.add?.length && !diff.delete?.length)) return { text, origin };
|
|
474
493
|
|
|
475
494
|
let result = text;
|
|
476
495
|
const edits = [];
|
|
496
|
+
const ctx = options.context || {};
|
|
477
497
|
|
|
478
498
|
if (diff.delete) {
|
|
479
499
|
diff.delete.forEach(quad => {
|
|
@@ -506,8 +526,15 @@ export function serialize({ text, diff, origin, options = {} }) {
|
|
|
506
526
|
}
|
|
507
527
|
}
|
|
508
528
|
|
|
509
|
-
const pred = quad.predicate.value
|
|
510
|
-
|
|
529
|
+
const pred = shortenIRI(quad.predicate.value, ctx);
|
|
530
|
+
let objText;
|
|
531
|
+
|
|
532
|
+
if (quad.object.termType === 'Literal') {
|
|
533
|
+
objText = quad.object.value;
|
|
534
|
+
} else {
|
|
535
|
+
objText = shortenIRI(quad.object.value, ctx);
|
|
536
|
+
}
|
|
537
|
+
|
|
511
538
|
const newLine = `\n[${objText}] {${pred}}`;
|
|
512
539
|
|
|
513
540
|
edits.push({ start: insertPos, end: insertPos, text: newLine });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdld-parse",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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",
|