mdld-parse 1.1.2 → 1.1.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/README.md +16 -9
- package/dist/mdld-parse.js +333 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -58,24 +58,31 @@ Or use `importmap` in the browser:
|
|
|
58
58
|
|
|
59
59
|
|
|
60
60
|
```javascript
|
|
61
|
-
import { parse, generate, merge, render, deconstruct } from 'mdld-parse';
|
|
61
|
+
import { parse, generate, merge, render, deconstruct, highlight } from 'mdld-parse';
|
|
62
62
|
|
|
63
63
|
// Parse MDLD to RDF quads
|
|
64
|
-
const
|
|
65
|
-
console.log(
|
|
66
|
-
console.log(
|
|
67
|
-
console.log(
|
|
68
|
-
console.log(
|
|
64
|
+
const { quads, primary, statements, origin } = parse({ text: mdldString });
|
|
65
|
+
console.log(quads); // RDF/JS quads
|
|
66
|
+
console.log(primary); // Primary metadata (subject, type, label, comment)
|
|
67
|
+
console.log(statements); // Elevated statements
|
|
68
|
+
console.log(origin); // Provenance tracking
|
|
69
69
|
|
|
70
70
|
// Generate MDLD from quads
|
|
71
|
-
const { text } = generate({ quads
|
|
71
|
+
const { text } = generate({ quads });
|
|
72
|
+
|
|
73
|
+
// Highlight MDLD text with syntax coloring
|
|
74
|
+
const highlighted = highlight(text);
|
|
72
75
|
|
|
73
76
|
// Render to semantic HTML with preserved annotations
|
|
74
|
-
const html = render(
|
|
77
|
+
const html = render(text);
|
|
75
78
|
|
|
76
79
|
// Reconstruct MDLD from HTML (lossless roundtrip)
|
|
77
80
|
const reconstructed = deconstruct(html);
|
|
78
|
-
const
|
|
81
|
+
const reconstructedQuads = parse(reconstructed).quads; // Same quads as original!
|
|
82
|
+
|
|
83
|
+
// Verify roundtrip integrity by using generate() as canonicalization
|
|
84
|
+
const canonical = generate({ quads: reconstructedQuads });
|
|
85
|
+
console.log('Roundtrip preserved:', text === canonical.text);
|
|
79
86
|
|
|
80
87
|
// Lossy rendering for privacy-preserving publishing
|
|
81
88
|
const cleanHtml = render(parse(mdldString).md);
|
package/dist/mdld-parse.js
CHANGED
|
@@ -1120,7 +1120,7 @@ function resolveObject(sem, state) {
|
|
|
1120
1120
|
if (sem.object.startsWith("#")) return resolveFragment(sem.object.substring(1), state.currentSubject, state.df);
|
|
1121
1121
|
return state.df.namedNode(expandIRI(sem.object, state.ctx));
|
|
1122
1122
|
}
|
|
1123
|
-
function escapeHtml(text) {
|
|
1123
|
+
function escapeHtml$1(text) {
|
|
1124
1124
|
if (!text) return "";
|
|
1125
1125
|
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1126
1126
|
}
|
|
@@ -2535,10 +2535,10 @@ function escapeTextPreserveHtml(s) {
|
|
|
2535
2535
|
INLINE_HTML_RE.lastIndex = m.index + 1;
|
|
2536
2536
|
continue;
|
|
2537
2537
|
}
|
|
2538
|
-
out += applyHardBreaks(escapeHtml(decodeInlineText(s.slice(last, m.index)))) + m[0];
|
|
2538
|
+
out += applyHardBreaks(escapeHtml$1(decodeInlineText(s.slice(last, m.index)))) + m[0];
|
|
2539
2539
|
last = m.index + m[0].length;
|
|
2540
2540
|
}
|
|
2541
|
-
return out + applyHardBreaks(escapeHtml(decodeInlineText(s.slice(last))));
|
|
2541
|
+
return out + applyHardBreaks(escapeHtml$1(decodeInlineText(s.slice(last))));
|
|
2542
2542
|
}
|
|
2543
2543
|
/**
|
|
2544
2544
|
* MD-LD UI Renderer
|
|
@@ -2558,12 +2558,12 @@ function render(src, options = {}) {
|
|
|
2558
2558
|
function getAttrs(rawAnnotation, parsedSem, isLink = false, fallbackHref = null) {
|
|
2559
2559
|
const attrs = [];
|
|
2560
2560
|
const classes = [];
|
|
2561
|
-
if (rawAnnotation) attrs.push(`data-annotation="${escapeHtml("{" + rawAnnotation + "}")}"`);
|
|
2561
|
+
if (rawAnnotation) attrs.push(`data-annotation="${escapeHtml$1("{" + rawAnnotation + "}")}"`);
|
|
2562
2562
|
let iri = null;
|
|
2563
2563
|
if (parsedSem?.subject && parsedSem.subject !== "RESET") iri = safeExpand(parsedSem.subject);
|
|
2564
2564
|
else if (parsedSem?.object) iri = safeExpand(parsedSem.object);
|
|
2565
|
-
if (iri) attrs.push(`data-iri="${escapeHtml(iri)}"`);
|
|
2566
|
-
if (isLink && fallbackHref) attrs.push(`href="${escapeHtml(fallbackHref)}"`);
|
|
2565
|
+
if (iri) attrs.push(`data-iri="${escapeHtml$1(iri)}"`);
|
|
2566
|
+
if (isLink && fallbackHref) attrs.push(`href="${escapeHtml$1(fallbackHref)}"`);
|
|
2567
2567
|
if (parsedSem?.types?.some((t) => !t.remove)) classes.push("typed");
|
|
2568
2568
|
if (parsedSem?.predicates?.some((p) => p.remove) || parsedSem?.types?.some((t) => t.remove)) classes.push("retracted");
|
|
2569
2569
|
if (classes.length > 0) attrs.unshift(`class="${classes.join(" ")}"`);
|
|
@@ -2591,10 +2591,10 @@ function render(src, options = {}) {
|
|
|
2591
2591
|
const raw = c.attrs || "";
|
|
2592
2592
|
const isLink = c.type === "link";
|
|
2593
2593
|
const fallbackHref = c.url || null;
|
|
2594
|
-
const titleAttr = c.title ? ` title="${escapeHtml(c.title)}"` : "";
|
|
2594
|
+
const titleAttr = c.title ? ` title="${escapeHtml$1(c.title)}"` : "";
|
|
2595
2595
|
if (c.type === "image") {
|
|
2596
|
-
const alt = escapeHtml(c.text);
|
|
2597
|
-
const src = escapeHtml(c.url);
|
|
2596
|
+
const alt = escapeHtml$1(c.text);
|
|
2597
|
+
const src = escapeHtml$1(c.url);
|
|
2598
2598
|
const attrString = mergeClasses("mdld-image", getAttrs(raw, sem, false, null));
|
|
2599
2599
|
out += `<img src="${src}" alt="${alt}" ${attrString}${titleAttr} />`;
|
|
2600
2600
|
} else {
|
|
@@ -2615,7 +2615,7 @@ function render(src, options = {}) {
|
|
|
2615
2615
|
}
|
|
2616
2616
|
const customAttrs = getAttrs(raw, sem, isLink, fallbackHref);
|
|
2617
2617
|
const attrString = mergeClasses(baseClass, isLink && fallbackHref && isExternalHref(fallbackHref) ? customAttrs + " target=\"_blank\" rel=\"noopener noreferrer\"" : customAttrs);
|
|
2618
|
-
const inner = c.type === "code" ? escapeHtml(c.text) : renderInline(c.text);
|
|
2618
|
+
const inner = c.type === "code" ? escapeHtml$1(c.text) : renderInline(c.text);
|
|
2619
2619
|
out += `<${tag} ${attrString}${titleAttr}>${inner}</${tag}>`;
|
|
2620
2620
|
}
|
|
2621
2621
|
lastPos = c.range[1];
|
|
@@ -2635,7 +2635,7 @@ function render(src, options = {}) {
|
|
|
2635
2635
|
const carrierText = codeBlock.content.join("\n");
|
|
2636
2636
|
const customAttrs = getAttrs(codeBlock.attrs, sem);
|
|
2637
2637
|
const attrString = mergeClasses(`mdld-codeblock language-${codeBlock.lang || "text"}`, customAttrs);
|
|
2638
|
-
out.push(`<pre><code ${attrString}>${escapeHtml(carrierText)}</code></pre>`);
|
|
2638
|
+
out.push(`<pre><code ${attrString}>${escapeHtml$1(carrierText)}</code></pre>`);
|
|
2639
2639
|
codeBlock = null;
|
|
2640
2640
|
} else codeBlock.content.push(line);
|
|
2641
2641
|
i++;
|
|
@@ -2688,7 +2688,7 @@ function render(src, options = {}) {
|
|
|
2688
2688
|
const prefix = detectPrefix(line);
|
|
2689
2689
|
if (prefix) {
|
|
2690
2690
|
const resolvedIri = context[prefix.prefix];
|
|
2691
|
-
out.push(`<div class="mdld-prefix" data-prefix="${prefix.prefix}" data-raw="${escapeHtml(prefix.iri)}" data-iri="${escapeHtml(resolvedIri)}" style="display:none"></div>`);
|
|
2691
|
+
out.push(`<div class="mdld-prefix" data-prefix="${prefix.prefix}" data-raw="${escapeHtml$1(prefix.iri)}" data-iri="${escapeHtml$1(resolvedIri)}" style="display:none"></div>`);
|
|
2692
2692
|
i++;
|
|
2693
2693
|
continue;
|
|
2694
2694
|
}
|
|
@@ -2825,7 +2825,7 @@ function render(src, options = {}) {
|
|
|
2825
2825
|
i++;
|
|
2826
2826
|
}
|
|
2827
2827
|
while (codeLines.length > 0 && codeLines[codeLines.length - 1] === "") codeLines.pop();
|
|
2828
|
-
out.push(`<pre><code class="mdld-codeblock language-text">${escapeHtml(codeLines.join("\n"))}</code></pre>`);
|
|
2828
|
+
out.push(`<pre><code class="mdld-codeblock language-text">${escapeHtml$1(codeLines.join("\n"))}</code></pre>`);
|
|
2829
2829
|
continue;
|
|
2830
2830
|
}
|
|
2831
2831
|
let paraLines = [line];
|
|
@@ -3095,6 +3095,325 @@ function extractAttr(tag, name) {
|
|
|
3095
3095
|
function unescapeHtml(s) {
|
|
3096
3096
|
return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'");
|
|
3097
3097
|
}
|
|
3098
|
+
// src/highlight.js
|
|
3099
|
+
/**
|
|
3100
|
+
* MD-LD Syntax Highlighter
|
|
3101
|
+
*
|
|
3102
|
+
* A performant, token-based syntax highlighter for MD-LD (Markdown Linked Data).
|
|
3103
|
+
* Uses simple string operations rather than complex grammars for reliability
|
|
3104
|
+
* and maintainability.
|
|
3105
|
+
*
|
|
3106
|
+
* Features:
|
|
3107
|
+
* - Token-based parsing with string operations
|
|
3108
|
+
* - Markdown formatting support with semantic distinction
|
|
3109
|
+
* - Lookahead detection for legal value carriers
|
|
3110
|
+
* - Orange semantic markers vs default markdown spans
|
|
3111
|
+
* - Annotation, value carrier, and heading highlighting
|
|
3112
|
+
*/
|
|
3113
|
+
function escapeHtml(s) {
|
|
3114
|
+
return String(s).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
3115
|
+
}
|
|
3116
|
+
var TOKEN_COLORS = {
|
|
3117
|
+
text: "#6b7280",
|
|
3118
|
+
marker: "#f97316",
|
|
3119
|
+
retraction: "#dc2626",
|
|
3120
|
+
value: "#eab308",
|
|
3121
|
+
annotation: "#4b5563"
|
|
3122
|
+
};
|
|
3123
|
+
function hasFollowingAnnotation(code, endPos) {
|
|
3124
|
+
let i = endPos;
|
|
3125
|
+
while (i < code.length && code[i] !== "\n" && code[i] !== "\r") {
|
|
3126
|
+
if (code[i] === "{") return true;
|
|
3127
|
+
i++;
|
|
3128
|
+
}
|
|
3129
|
+
return false;
|
|
3130
|
+
}
|
|
3131
|
+
function processMarkdownFormattingAtPosition(code, startPos) {
|
|
3132
|
+
const char = code[startPos];
|
|
3133
|
+
if (char === "*" && startPos + 1 < code.length && code[startPos + 1] === "*") {
|
|
3134
|
+
const endPos = code.indexOf("**", startPos + 2);
|
|
3135
|
+
if (endPos !== -1) {
|
|
3136
|
+
const content = code.slice(startPos + 2, endPos);
|
|
3137
|
+
const color = hasFollowingAnnotation(code, endPos + 2) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
3138
|
+
return {
|
|
3139
|
+
html: `<span${color}>**</span><strong>${escapeHtml(content)}</strong><span${color}>**</span>`,
|
|
3140
|
+
nextIndex: endPos + 2
|
|
3141
|
+
};
|
|
3142
|
+
}
|
|
3143
|
+
}
|
|
3144
|
+
if (char === "_" && startPos + 1 < code.length && code[startPos + 1] === "_") {
|
|
3145
|
+
const endPos = code.indexOf("__", startPos + 2);
|
|
3146
|
+
if (endPos !== -1) {
|
|
3147
|
+
const content = code.slice(startPos + 2, endPos);
|
|
3148
|
+
const color = hasFollowingAnnotation(code, endPos + 2) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
3149
|
+
return {
|
|
3150
|
+
html: `<span${color}>__</span><strong>${escapeHtml(content)}</strong><span${color}>__</span>`,
|
|
3151
|
+
nextIndex: endPos + 2
|
|
3152
|
+
};
|
|
3153
|
+
}
|
|
3154
|
+
}
|
|
3155
|
+
if (char === "*") {
|
|
3156
|
+
const endPos = code.indexOf("*", startPos + 1);
|
|
3157
|
+
if (endPos !== -1) {
|
|
3158
|
+
const content = code.slice(startPos + 1, endPos);
|
|
3159
|
+
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
3160
|
+
return {
|
|
3161
|
+
html: `<span${color}>*</span><em>${escapeHtml(content)}</em><span${color}>*</span>`,
|
|
3162
|
+
nextIndex: endPos + 1
|
|
3163
|
+
};
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
if (char === "_") {
|
|
3167
|
+
const endPos = code.indexOf("_", startPos + 1);
|
|
3168
|
+
if (endPos !== -1) {
|
|
3169
|
+
const content = code.slice(startPos + 1, endPos);
|
|
3170
|
+
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
3171
|
+
return {
|
|
3172
|
+
html: `<span${color}>_</span><em>${escapeHtml(content)}</em><span${color}>_</span>`,
|
|
3173
|
+
nextIndex: endPos + 1
|
|
3174
|
+
};
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
if (char === "`") {
|
|
3178
|
+
const endPos = code.indexOf("`", startPos + 1);
|
|
3179
|
+
if (endPos !== -1) {
|
|
3180
|
+
const content = code.slice(startPos + 1, endPos);
|
|
3181
|
+
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
3182
|
+
return {
|
|
3183
|
+
html: `<span${color}>\`</span><code style="background-color:#7773">${escapeHtml(content)}</code><span${color}>\`</span>`,
|
|
3184
|
+
nextIndex: endPos + 1
|
|
3185
|
+
};
|
|
3186
|
+
}
|
|
3187
|
+
}
|
|
3188
|
+
return null;
|
|
3189
|
+
}
|
|
3190
|
+
var colorCache = /* @__PURE__ */ new Map();
|
|
3191
|
+
function hashIRI(iri) {
|
|
3192
|
+
let hash = 0;
|
|
3193
|
+
for (let i = 0; i < iri.length; i++) {
|
|
3194
|
+
const char = iri.charCodeAt(i);
|
|
3195
|
+
hash = (hash << 5) - hash + char;
|
|
3196
|
+
hash = hash & hash;
|
|
3197
|
+
}
|
|
3198
|
+
return Math.abs(hash);
|
|
3199
|
+
}
|
|
3200
|
+
function getIRIColor(iri) {
|
|
3201
|
+
if (colorCache.has(iri)) return colorCache.get(iri);
|
|
3202
|
+
const hash = hashIRI(iri);
|
|
3203
|
+
const color = `hsl(${hash % 360}, ${15 + hash % 10}%, ${45 + hash % 10}%)`;
|
|
3204
|
+
colorCache.set(iri, color);
|
|
3205
|
+
return color;
|
|
3206
|
+
}
|
|
3207
|
+
function extractPrefixes(code) {
|
|
3208
|
+
const prefixes = /* @__PURE__ */ new Map();
|
|
3209
|
+
const prefixRegex = /^\[(\w+)\]\s*<([^>]*)>\s*$/gm;
|
|
3210
|
+
let match;
|
|
3211
|
+
while ((match = prefixRegex.exec(code)) !== null) {
|
|
3212
|
+
const prefixName = match[1];
|
|
3213
|
+
let iri = match[2];
|
|
3214
|
+
if (iri.includes(":")) {
|
|
3215
|
+
const colonIndex = iri.indexOf(":");
|
|
3216
|
+
if (colonIndex > 0) {
|
|
3217
|
+
const refPrefix = iri.slice(0, colonIndex);
|
|
3218
|
+
const refSuffix = iri.slice(colonIndex + 1);
|
|
3219
|
+
if (prefixes.has(refPrefix)) iri = prefixes.get(refPrefix) + refSuffix;
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
prefixes.set(prefixName, iri);
|
|
3223
|
+
}
|
|
3224
|
+
for (const [key, iri] of Object.entries(DEFAULT_CONTEXT)) if (key !== "@vocab") prefixes.set(key, iri);
|
|
3225
|
+
return prefixes;
|
|
3226
|
+
}
|
|
3227
|
+
function renderTerm(part, isRetracted, prefixes) {
|
|
3228
|
+
const baseColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
3229
|
+
const colonIndex = part.indexOf(":");
|
|
3230
|
+
if (colonIndex > 0) {
|
|
3231
|
+
const prefix = part.slice(0, colonIndex);
|
|
3232
|
+
const local = part.slice(colonIndex + 1);
|
|
3233
|
+
if (prefixes.has(prefix)) {
|
|
3234
|
+
const prefixIRI = prefixes.get(prefix);
|
|
3235
|
+
const prefixColor = getIRIColor(prefixIRI);
|
|
3236
|
+
const fullIRI = prefixIRI + local;
|
|
3237
|
+
const localColor = getIRIColor(fullIRI);
|
|
3238
|
+
return `<span data-iri="${fullIRI}"><span style="color: ${prefixColor}">${escapeHtml(prefix)}</span><span style="color: ${TOKEN_COLORS.marker}">:</span><span style="color: ${localColor}">${escapeHtml(local)}</span></span> `;
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3241
|
+
if (part.startsWith("http:") || part.startsWith("https:") || part.startsWith("tag:") || part.startsWith("urn:")) return `<span style="color: ${getIRIColor(part)}">${escapeHtml(part)}</span> `;
|
|
3242
|
+
return `<span style="color: ${baseColor}">${escapeHtml(part)}</span> `;
|
|
3243
|
+
}
|
|
3244
|
+
function parseAnnotation(content, prefixes) {
|
|
3245
|
+
const parts = content.trim().split(/\s+/);
|
|
3246
|
+
let result = "";
|
|
3247
|
+
let nextTermIsRetracted = false;
|
|
3248
|
+
for (let i = 0; i < parts.length; i++) {
|
|
3249
|
+
const part = parts[i];
|
|
3250
|
+
if (!part) continue;
|
|
3251
|
+
if (part.startsWith("=") || part.startsWith("+")) {
|
|
3252
|
+
const marker = part[0];
|
|
3253
|
+
const rest = part.slice(1);
|
|
3254
|
+
result += `<span style="color: ${TOKEN_COLORS.marker}">${escapeHtml(marker)}</span>${renderTerm(rest, false, prefixes)}`;
|
|
3255
|
+
continue;
|
|
3256
|
+
}
|
|
3257
|
+
if (part === "-") {
|
|
3258
|
+
nextTermIsRetracted = true;
|
|
3259
|
+
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
3260
|
+
continue;
|
|
3261
|
+
}
|
|
3262
|
+
if (part.startsWith("-") && part.length > 1) {
|
|
3263
|
+
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
3264
|
+
continue;
|
|
3265
|
+
}
|
|
3266
|
+
const isRetracted = nextTermIsRetracted;
|
|
3267
|
+
nextTermIsRetracted = false;
|
|
3268
|
+
if (part.startsWith("?") || part.startsWith("!")) {
|
|
3269
|
+
const marker = part[0];
|
|
3270
|
+
const rest = part.slice(1);
|
|
3271
|
+
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
3272
|
+
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
3273
|
+
continue;
|
|
3274
|
+
}
|
|
3275
|
+
if (part.startsWith("^^")) {
|
|
3276
|
+
const marker = "^^";
|
|
3277
|
+
const rest = part.slice(2);
|
|
3278
|
+
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
3279
|
+
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
3280
|
+
continue;
|
|
3281
|
+
}
|
|
3282
|
+
if (part.startsWith("@")) {
|
|
3283
|
+
const marker = "@";
|
|
3284
|
+
const rest = part.slice(1);
|
|
3285
|
+
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
3286
|
+
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
3287
|
+
continue;
|
|
3288
|
+
}
|
|
3289
|
+
if (part.startsWith(".")) {
|
|
3290
|
+
const marker = ".";
|
|
3291
|
+
const rest = part.slice(1);
|
|
3292
|
+
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
3293
|
+
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
3294
|
+
continue;
|
|
3295
|
+
}
|
|
3296
|
+
result += renderTerm(part, isRetracted, prefixes);
|
|
3297
|
+
}
|
|
3298
|
+
return result.trim();
|
|
3299
|
+
}
|
|
3300
|
+
function highlight(code) {
|
|
3301
|
+
const prefixes = extractPrefixes(code);
|
|
3302
|
+
let result = "";
|
|
3303
|
+
let i = 0;
|
|
3304
|
+
while (i < code.length) {
|
|
3305
|
+
const char = code[i];
|
|
3306
|
+
if (char === "{") {
|
|
3307
|
+
const endIdx = code.indexOf("}", i);
|
|
3308
|
+
if (endIdx === -1) {
|
|
3309
|
+
result += escapeHtml(char);
|
|
3310
|
+
i++;
|
|
3311
|
+
continue;
|
|
3312
|
+
}
|
|
3313
|
+
const parsed = parseAnnotation(code.slice(i + 1, endIdx), prefixes);
|
|
3314
|
+
result += `<span class="mdld-annotation"><span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">{</span>`;
|
|
3315
|
+
result += parsed;
|
|
3316
|
+
result += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">}</span></span>`;
|
|
3317
|
+
i = endIdx + 1;
|
|
3318
|
+
continue;
|
|
3319
|
+
}
|
|
3320
|
+
if (char === "[") {
|
|
3321
|
+
const endBracket = code.indexOf("]", i);
|
|
3322
|
+
if (endBracket === -1) {
|
|
3323
|
+
result += escapeHtml(char);
|
|
3324
|
+
i++;
|
|
3325
|
+
continue;
|
|
3326
|
+
}
|
|
3327
|
+
const prefixName = code.slice(i + 1, endBracket);
|
|
3328
|
+
let j = endBracket + 1;
|
|
3329
|
+
while (j < code.length && /\s/.test(code[j])) j++;
|
|
3330
|
+
if (j < code.length && code[j] === "<") {
|
|
3331
|
+
const iriEnd = code.indexOf(">", j);
|
|
3332
|
+
if (iriEnd !== -1) {
|
|
3333
|
+
const iriContent = code.slice(j + 1, iriEnd);
|
|
3334
|
+
const prefixColor = prefixes.has(prefixName) ? getIRIColor(prefixes.get(prefixName)) : TOKEN_COLORS.text;
|
|
3335
|
+
result += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">[</span><span style="color: ${prefixColor}">${escapeHtml(prefixName)}</span><span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">]</span> <span style="color: ${TOKEN_COLORS.text}; opacity: 0.6"><${escapeHtml(iriContent)}></span>`;
|
|
3336
|
+
i = iriEnd + 1;
|
|
3337
|
+
continue;
|
|
3338
|
+
}
|
|
3339
|
+
}
|
|
3340
|
+
const content = code.slice(i + 1, endBracket);
|
|
3341
|
+
const bracketColor = hasFollowingAnnotation(code, endBracket + 1) ? TOKEN_COLORS.marker : TOKEN_COLORS.value;
|
|
3342
|
+
const escapedContent = escapeHtml(content);
|
|
3343
|
+
result += `<span style="color: ${bracketColor}; opacity: 0.85">[</span><span style="background-color: ${bracketColor}15">${escapedContent}</span><span style="color: ${bracketColor}; opacity: 0.85">]</span>`;
|
|
3344
|
+
i = endBracket + 1;
|
|
3345
|
+
continue;
|
|
3346
|
+
}
|
|
3347
|
+
if (char === "*" || char === "_" || char === "`") {
|
|
3348
|
+
const processed = processMarkdownFormattingAtPosition(code, i);
|
|
3349
|
+
if (processed) {
|
|
3350
|
+
result += processed.html;
|
|
3351
|
+
i = processed.nextIndex;
|
|
3352
|
+
continue;
|
|
3353
|
+
}
|
|
3354
|
+
}
|
|
3355
|
+
if (char === "#") {
|
|
3356
|
+
let hashCount = 0;
|
|
3357
|
+
let j = i;
|
|
3358
|
+
while (j < code.length && code[j] === "#") {
|
|
3359
|
+
hashCount++;
|
|
3360
|
+
j++;
|
|
3361
|
+
}
|
|
3362
|
+
if (hashCount <= 6 && (j >= code.length || code[j] === " " || code[j] === " ")) {
|
|
3363
|
+
const lineEnd = code.indexOf("\n", j);
|
|
3364
|
+
const endIdx = lineEnd === -1 ? code.length : lineEnd;
|
|
3365
|
+
const headerText = code.slice(j, endIdx).trim();
|
|
3366
|
+
let processedHeaderText = "";
|
|
3367
|
+
let lastIndex = 0;
|
|
3368
|
+
let annotationStart = headerText.indexOf("{");
|
|
3369
|
+
while (annotationStart !== -1) {
|
|
3370
|
+
const annotationEnd = headerText.indexOf("}", annotationStart);
|
|
3371
|
+
if (annotationEnd === -1) break;
|
|
3372
|
+
const beforeText = headerText.slice(lastIndex, annotationStart);
|
|
3373
|
+
processedHeaderText += escapeHtml(beforeText);
|
|
3374
|
+
const parsedAnnotation = parseAnnotation(headerText.slice(annotationStart + 1, annotationEnd), prefixes);
|
|
3375
|
+
processedHeaderText += `<span class="mdld-annotation"><span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">{</span>` + parsedAnnotation + `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">}</span></span>`;
|
|
3376
|
+
lastIndex = annotationEnd + 1;
|
|
3377
|
+
annotationStart = headerText.indexOf("{", lastIndex);
|
|
3378
|
+
}
|
|
3379
|
+
processedHeaderText += escapeHtml(headerText.slice(lastIndex));
|
|
3380
|
+
const hasAnnotation = headerText.includes("{") && headerText.includes("}");
|
|
3381
|
+
const headingTag = `h${hashCount}`;
|
|
3382
|
+
const headingStyle = `margin: 0; font-weight: 600;`;
|
|
3383
|
+
const hashes = "#".repeat(hashCount);
|
|
3384
|
+
const hashColor = hasAnnotation ? `color: ${TOKEN_COLORS.marker}; opacity: 0.8` : "";
|
|
3385
|
+
result += `<${headingTag} style="${headingStyle}"><span style="${hashColor}">${hashes}</span> ${processedHeaderText}</${headingTag}>`;
|
|
3386
|
+
i = endIdx;
|
|
3387
|
+
continue;
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
if (char === "-" || char === "*") {
|
|
3391
|
+
const prevChar = i > 0 ? code[i - 1] : "\n";
|
|
3392
|
+
if (prevChar === "\n" || prevChar === "\r" || prevChar === " " || prevChar === " ") {
|
|
3393
|
+
if (code[i + 1] === " ") {
|
|
3394
|
+
const markerColor = hasFollowingAnnotation(code, i + 2) ? `color: ${TOKEN_COLORS.marker}; opacity: 0.85` : "";
|
|
3395
|
+
result += `<span style="${markerColor}">${escapeHtml(char)}</span>`;
|
|
3396
|
+
i++;
|
|
3397
|
+
continue;
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
if (char === ">") {
|
|
3402
|
+
const prevChar = i > 0 ? code[i - 1] : "\n";
|
|
3403
|
+
if (prevChar === "\n" || prevChar === "\r" || prevChar === " " || prevChar === " ") {
|
|
3404
|
+
if (code[i + 1] === " ") {
|
|
3405
|
+
const markerColor = hasFollowingAnnotation(code, i + 2) ? `color: ${TOKEN_COLORS.marker}; opacity: 0.85` : "";
|
|
3406
|
+
result += `<span style="${markerColor}">${escapeHtml(char)}</span>`;
|
|
3407
|
+
i++;
|
|
3408
|
+
continue;
|
|
3409
|
+
}
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
result += escapeHtml(char);
|
|
3413
|
+
i++;
|
|
3414
|
+
}
|
|
3415
|
+
return result;
|
|
3416
|
+
}
|
|
3098
3417
|
// src/index.js
|
|
3099
3418
|
/**
|
|
3100
3419
|
* Update the value of a quad in MDLD text and return the updated text
|
|
@@ -3111,4 +3430,4 @@ function updateValue({ text, quad, value, origin }) {
|
|
|
3111
3430
|
if (!location || !location.valueRange) return text;
|
|
3112
3431
|
return text.substring(0, location.valueRange.start) + value + text.substring(location.valueRange.end);
|
|
3113
3432
|
}
|
|
3114
|
-
export { DEFAULT_CONTEXT, DataFactory, deconstruct, expandIRI, generate, generateNode, hash, locate, merge, parse, parseSemanticBlock, render, shortenIRI, updateValue };
|
|
3433
|
+
export { DEFAULT_CONTEXT, DataFactory, deconstruct, expandIRI, generate, generateNode, hash, highlight, locate, merge, parse, parseSemanticBlock, render, shortenIRI, updateValue };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdld-parse",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Write RDF knowledge graphs as plain Markdown. Parse to quads, generate back, merge documents. The human-friendly semantic web format.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rdf",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"scripts": {
|
|
39
39
|
"test": "node tests/index.js",
|
|
40
40
|
"dev": "vite dev",
|
|
41
|
-
"build": "vite build && vite build --config vite.config.lib.js &&
|
|
41
|
+
"build": "vite build && vite build --config vite.config.lib.js && cp -r docs/ dist/docs/ && cp -r server/ dist/server/ && cp -r examples/ dist/examples/ && cp -r grammar/ dist/grammar/ && cp -r ontologies/ dist/ontologies/ && cp -r spec/ dist/spec/ && cp -r README.md dist/README.md && cp -r docs.html dist/docs.html && cp -r demo/ dist/demo/ && cp -r src/ dist/src/"
|
|
42
42
|
},
|
|
43
43
|
"author": "davay42",
|
|
44
44
|
"repository": {
|