mdld-parse 0.8.1 → 0.8.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.
- package/package.json +1 -1
- package/src/generate.js +20 -2
- package/src/shared.js +8 -2
- package/src/utils.js +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdld-parse",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
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/generate.js
CHANGED
|
@@ -46,7 +46,7 @@ export function generate(quads, context = {}, primarySubject = null) {
|
|
|
46
46
|
|
|
47
47
|
const { text } = buildDeterministicMDLD(subjectGroups, fullContext, primarySubject);
|
|
48
48
|
|
|
49
|
-
return text;
|
|
49
|
+
return { text, context: fullContext };
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function normalizeAndSortQuads(quads) {
|
|
@@ -90,6 +90,9 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
|
|
|
90
90
|
let text = '';
|
|
91
91
|
const usedPrefixes = collectUsedPrefixes(subjectGroups, context);
|
|
92
92
|
|
|
93
|
+
// Build label lookup map for all IRIs that have rdfs:label
|
|
94
|
+
const labelLookup = buildLabelLookup(subjectGroups);
|
|
95
|
+
|
|
93
96
|
// Add prefixes first (deterministic order), but exclude default context prefixes
|
|
94
97
|
const sortedPrefixes = Object.entries(context).sort(([a], [b]) => a.localeCompare(b));
|
|
95
98
|
for (const [prefix, namespace] of sortedPrefixes) {
|
|
@@ -133,7 +136,7 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
|
|
|
133
136
|
});
|
|
134
137
|
|
|
135
138
|
sortQuadsByPredicate(objects).forEach(quad => {
|
|
136
|
-
text += generateObjectText(quad, context);
|
|
139
|
+
text += generateObjectText(quad, context, labelLookup);
|
|
137
140
|
});
|
|
138
141
|
|
|
139
142
|
text += '\n';
|
|
@@ -141,3 +144,18 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
|
|
|
141
144
|
|
|
142
145
|
return { text };
|
|
143
146
|
}
|
|
147
|
+
|
|
148
|
+
function buildLabelLookup(subjectGroups) {
|
|
149
|
+
const labelLookup = new Map();
|
|
150
|
+
const rdfsLabelIRI = 'http://www.w3.org/2000/01/rdf-schema#label';
|
|
151
|
+
|
|
152
|
+
for (const subjectQuads of subjectGroups.values()) {
|
|
153
|
+
for (const quad of subjectQuads) {
|
|
154
|
+
if (quad.predicate.value === rdfsLabelIRI && quad.object.termType === 'Literal') {
|
|
155
|
+
labelLookup.set(quad.subject.value, quad.object.value);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return labelLookup;
|
|
161
|
+
}
|
package/src/shared.js
CHANGED
|
@@ -428,10 +428,16 @@ export function generateLiteralText(quad, context) {
|
|
|
428
428
|
return `[${quad.object.value}] {${annotation}}\n`;
|
|
429
429
|
}
|
|
430
430
|
|
|
431
|
-
export const generateObjectText = (quad, context) => {
|
|
431
|
+
export const generateObjectText = (quad, context, labelLookup = null) => {
|
|
432
432
|
const objShort = shortenIRI(quad.object.value, context);
|
|
433
433
|
const predShort = shortenIRI(quad.predicate.value, context);
|
|
434
|
-
|
|
434
|
+
|
|
435
|
+
// Use rdfs:label if available, otherwise use shortened IRI
|
|
436
|
+
const displayText = labelLookup && labelLookup.has(quad.object.value)
|
|
437
|
+
? labelLookup.get(quad.object.value)
|
|
438
|
+
: objShort;
|
|
439
|
+
|
|
440
|
+
return `[${displayText}] {+${objShort} ?${predShort}}\n`;
|
|
435
441
|
};
|
|
436
442
|
|
|
437
443
|
// Optimized quad filtering - destructuring for smaller minified output
|
package/src/utils.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { URL_REGEX, DEFAULT_CONTEXT } from './constants.js';
|
|
2
2
|
|
|
3
|
+
const VALID_URI_SCHEMES = /^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file):/;
|
|
4
|
+
|
|
3
5
|
// Base Term class for RDF/JS compatibility
|
|
4
6
|
export class Term {
|
|
5
7
|
constructor(id) {
|
|
@@ -268,7 +270,7 @@ export function expandIRI(term, ctx) {
|
|
|
268
270
|
}
|
|
269
271
|
|
|
270
272
|
export function shortenIRI(iri, ctx) {
|
|
271
|
-
if (!iri || !
|
|
273
|
+
if (!iri || !VALID_URI_SCHEMES.test(iri)) return iri;
|
|
272
274
|
if (ctx['@vocab'] && iri.startsWith(ctx['@vocab'])) return iri.substring(ctx['@vocab'].length);
|
|
273
275
|
|
|
274
276
|
// Find the best matching prefix - more precise matching
|