mdld-parse 0.8.2 → 0.8.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdld-parse",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
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
@@ -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) {
@@ -119,21 +122,34 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
119
122
  // Separate types, literals, and objects using shared utility
120
123
  const { types, literals, objects } = filterQuadsByType(subjectQuads);
121
124
 
122
- // Generate heading
123
- const localSubjectName = extractLocalName(subjectIRI, context);
124
- const typeAnnotations = types.length > 0
125
- ? ' ' + types.map(t => '.' + shortenIRI(t.object.value, context)).sort().join(' ')
125
+ // Check if this subject has a label
126
+ const hasLabel = labelLookup.has(subjectIRI);
127
+ const displayName = hasLabel ? labelLookup.get(subjectIRI) : extractLocalName(subjectIRI, context);
128
+
129
+ // Build annotations: types + label indicator if present
130
+ let annotations = types.length > 0
131
+ ? types.map(t => '.' + shortenIRI(t.object.value, context)).sort().join(' ')
126
132
  : '';
133
+ if (hasLabel) {
134
+ annotations += (annotations ? ' ' : '') + 'label';
135
+ }
127
136
 
128
- text += `# ${localSubjectName} {=${shortSubject}${typeAnnotations}}\n\n`;
137
+ const annotationStr = annotations ? ' ' + annotations : '';
138
+ text += `# ${displayName} {=${shortSubject}${annotationStr}}\n\n`;
129
139
 
130
- // Add literals and objects using shared utilities
140
+ // Add literals (excluding the label used in heading) and objects
141
+ const rdfsLabelIRI = 'http://www.w3.org/2000/01/rdf-schema#label';
142
+ const headingLabel = hasLabel ? labelLookup.get(subjectIRI) : null;
131
143
  sortQuadsByPredicate(literals).forEach(quad => {
144
+ // Skip only the label that matches the heading display, render additional labels
145
+ if (quad.predicate.value === rdfsLabelIRI && quad.object.value === headingLabel) {
146
+ return; // Skip the heading label
147
+ }
132
148
  text += generateLiteralText(quad, context);
133
149
  });
134
150
 
135
151
  sortQuadsByPredicate(objects).forEach(quad => {
136
- text += generateObjectText(quad, context);
152
+ text += generateObjectText(quad, context, labelLookup);
137
153
  });
138
154
 
139
155
  text += '\n';
@@ -141,3 +157,18 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
141
157
 
142
158
  return { text };
143
159
  }
160
+
161
+ function buildLabelLookup(subjectGroups) {
162
+ const labelLookup = new Map();
163
+ const rdfsLabelIRI = 'http://www.w3.org/2000/01/rdf-schema#label';
164
+
165
+ for (const subjectQuads of subjectGroups.values()) {
166
+ for (const quad of subjectQuads) {
167
+ if (quad.predicate.value === rdfsLabelIRI && quad.object.termType === 'Literal') {
168
+ labelLookup.set(quad.subject.value, quad.object.value);
169
+ }
170
+ }
171
+ }
172
+
173
+ return labelLookup;
174
+ }
package/src/shared.js CHANGED
@@ -425,13 +425,47 @@ export function generateLiteralText(quad, context) {
425
425
  annotation += ` ^^${shortenIRI(quad.object.datatype.value, context)}`;
426
426
  }
427
427
 
428
- return `[${quad.object.value}] {${annotation}}\n`;
428
+ // Get clean value (handle both RDF/JS Literal objects and plain strings)
429
+ const rawValue = quad.object.value || quad.object;
430
+ const value = typeof rawValue === 'string' ? rawValue : String(rawValue);
431
+
432
+ // Get datatype for styling decisions
433
+ const datatype = quad.object.datatype?.value || '';
434
+
435
+ // Multiline: fenced block (safe for arbitrary content)
436
+ if (value.includes('\n')) {
437
+ return `~~~ {${annotation}}\n${value}\n~~~\n\n`;
438
+ }
439
+
440
+ // Numbers (integer, decimal, double, float): code spans
441
+ if (datatype.includes('integer') || datatype.includes('decimal') || datatype.includes('double') || datatype.includes('float')) {
442
+ return `\`${value}\` {${annotation}}\n`;
443
+ }
444
+
445
+ if (datatype.includes('date') || datatype.includes('time')) {
446
+ return `*${value}* {${annotation}}\n`;
447
+ }
448
+
449
+ // Booleans: bold (stands out visually)
450
+ if (datatype.includes('boolean')) {
451
+ return `**${value}** {${annotation}}\n`;
452
+ }
453
+
454
+ // Everything else (strings, dates, etc.): square brackets (simple, readable)
455
+ return `[${value}] {${annotation}}\n`;
429
456
  }
430
457
 
431
- export const generateObjectText = (quad, context) => {
458
+ export const generateObjectText = (quad, context, labelLookup = null) => {
432
459
  const objShort = shortenIRI(quad.object.value, context);
433
460
  const predShort = shortenIRI(quad.predicate.value, context);
434
- return `[${objShort}] {+${objShort} ?${predShort}}\n`;
461
+
462
+ // Use rdfs:label if available, otherwise use shortened IRI
463
+ const displayText = labelLookup && labelLookup.has(quad.object.value)
464
+ ? labelLookup.get(quad.object.value)
465
+ : objShort;
466
+
467
+ // Object links: italic
468
+ return `[${displayText}] {+${objShort} ?${predShort}}\n`;
435
469
  };
436
470
 
437
471
  // Optimized quad filtering - destructuring for smaller minified output
package/src/utils.js CHANGED
@@ -197,12 +197,14 @@ export const DataFactory = {
197
197
  case 'DefaultGraph':
198
198
  return DEFAULTGRAPH;
199
199
  case 'Literal':
200
+ // Escape quotes in value to ensure proper parsing
201
+ const escapedValue = String(term.value).replace(/"/g, '\\"');
200
202
  if (term.language) {
201
- return new Literal(`"${term.value}"@${term.language}`);
203
+ return new Literal(`"${escapedValue}"@${term.language}`);
202
204
  } else if (term.datatype) {
203
- return new Literal(`"${term.value}"^^${term.datatype.value || term.datatype}`);
205
+ return new Literal(`"${escapedValue}"^^${term.datatype.value || term.datatype}`);
204
206
  } else {
205
- return new Literal(`"${term.value}"`);
207
+ return new Literal(`"${escapedValue}"`);
206
208
  }
207
209
  case 'Quad':
208
210
  return DataFactory.fromQuad(term);