mdld-parse 0.8.3 → 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.3",
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
@@ -122,16 +122,29 @@ function buildDeterministicMDLD(subjectGroups, context, primarySubject = null) {
122
122
  // Separate types, literals, and objects using shared utility
123
123
  const { types, literals, objects } = filterQuadsByType(subjectQuads);
124
124
 
125
- // Generate heading
126
- const localSubjectName = extractLocalName(subjectIRI, context);
127
- const typeAnnotations = types.length > 0
128
- ? ' ' + 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(' ')
129
132
  : '';
133
+ if (hasLabel) {
134
+ annotations += (annotations ? ' ' : '') + 'label';
135
+ }
130
136
 
131
- text += `# ${localSubjectName} {=${shortSubject}${typeAnnotations}}\n\n`;
137
+ const annotationStr = annotations ? ' ' + annotations : '';
138
+ text += `# ${displayName} {=${shortSubject}${annotationStr}}\n\n`;
132
139
 
133
- // 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;
134
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
+ }
135
148
  text += generateLiteralText(quad, context);
136
149
  });
137
150
 
package/src/shared.js CHANGED
@@ -425,7 +425,34 @@ 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
458
  export const generateObjectText = (quad, context, labelLookup = null) => {
@@ -437,6 +464,7 @@ export const generateObjectText = (quad, context, labelLookup = null) => {
437
464
  ? labelLookup.get(quad.object.value)
438
465
  : objShort;
439
466
 
467
+ // Object links: italic
440
468
  return `[${displayText}] {+${objShort} ?${predShort}}\n`;
441
469
  };
442
470
 
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);