mdld-parse 1.1.0 → 1.1.1
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 -0
- package/dist/mdld-parse.js +193 -719
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -501,6 +501,10 @@ Convert MD-LD to semantic HTML with preserved annotations.
|
|
|
501
501
|
- Preserves complete MD-LD syntax in `data-annotation` for roundtrip reconstruction
|
|
502
502
|
- Resolves IRIs into `data-iri` attributes for easy querying
|
|
503
503
|
- Adds semantic CSS classes: `.mdld-heading`, `.mdld-link`, `.typed`, `.retracted`
|
|
504
|
+
- External links (`http(s)://`, `mailto:`, any scheme, protocol-relative) get `target="_blank" rel="noopener noreferrer"`; fragment (`#`) and relative links stay on-page
|
|
505
|
+
- Full CommonMark inline links: `[text](#fragment)`, `[text](/path)`, `[text](mailto:…)`, titles (`[t](url "title")`), and images ``
|
|
506
|
+
- Entity references (`&`, `©`, `©`, `©`) decode; backslash escapes (`\*`) suppress markup
|
|
507
|
+
- Standard Markdown/HTML passthrough: raw HTML blocks (`<table>`, `<div>`, …), inline HTML, hard breaks (`<br />`), and indented code blocks render per CommonMark; code stays escaped
|
|
504
508
|
- Platform-agnostic: works in Node.js, Deno, browsers, edge workers
|
|
505
509
|
|
|
506
510
|
**Example:**
|
|
@@ -540,6 +544,18 @@ const mdld = deconstruct(html);
|
|
|
540
544
|
- Client-side graph extraction: `parse(deconstruct(document.body.innerHTML))`
|
|
541
545
|
- Offline-first apps: cache HTML, reconstruct quads on demand
|
|
542
546
|
|
|
547
|
+
**Notes:**
|
|
548
|
+
- Raw HTML blocks pass through `deconstruct()` verbatim, so `<table>` and friends roundtrip losslessly
|
|
549
|
+
- `deconstruct()` only recognizes elements carrying `mdld-*` classes — hand-written HTML without them is treated as raw passthrough
|
|
550
|
+
|
|
551
|
+
**CommonMark compatibility — deliberate limitations:**
|
|
552
|
+
- **Setext headings** (`Title` + `===`/`---`) are not supported: they need lookahead that conflicts with per-line annotation parsing, and `---` already means `<hr>`. Use ATX `#` headings.
|
|
553
|
+
- **Reference-style links** (`[text][ref]`, `[ref]: url`) are not supported: definitions require document-level state that wouldn't survive `deconstruct()` round-trips anyway. Use inline `[text](url)`.
|
|
554
|
+
- **Shortcut references** (`[ref]` alone as a link) conflict with MD-LD's `[text]` bracket-span syntax.
|
|
555
|
+
- **GFM extensions** (pipe tables, `~~strikethrough~~`, task lists, `www.` autolinks) are out of scope — use raw HTML for tables.
|
|
556
|
+
- Combined emphasis `***text***` renders as `*<strong>text</strong>*`, not nested `<em><strong>`.
|
|
557
|
+
- Empty link destinations `[text]()` stay literal; indented code inside list items is not recognized; inline HTML tags are dropped by `deconstruct()` (inner text preserved).
|
|
558
|
+
|
|
543
559
|
## 🎨 Graph Visualization — `<quad-graph>`
|
|
544
560
|
|
|
545
561
|
Visualize your RDF quads as an interactive force-directed graph with the zero-dependency `<quad-graph>` web component.
|
package/dist/mdld-parse.js
CHANGED
|
@@ -504,6 +504,8 @@ function detectHeading(line) {
|
|
|
504
504
|
attrs = attrsMatch[1];
|
|
505
505
|
content = trimmed.slice(0, -attrsMatch[0].length).trim();
|
|
506
506
|
}
|
|
507
|
+
content = content.replace(/[ \t]+#+[ \t]*$/, "");
|
|
508
|
+
if (/^#+$/.test(content)) content = "";
|
|
507
509
|
return {
|
|
508
510
|
depth,
|
|
509
511
|
content,
|
|
@@ -526,7 +528,7 @@ function detectList(line) {
|
|
|
526
528
|
else if (char >= "0" && char <= "9") {
|
|
527
529
|
let numEnd = pos + 1;
|
|
528
530
|
while (numEnd < line.length && line[numEnd] >= "0" && line[numEnd] <= "9") numEnd++;
|
|
529
|
-
if (numEnd >= line.length || line[numEnd] !== ".") return null;
|
|
531
|
+
if (numEnd >= line.length || line[numEnd] !== "." && line[numEnd] !== ")") return null;
|
|
530
532
|
marker = line.slice(pos, numEnd + 1);
|
|
531
533
|
contentStart = numEnd + 1;
|
|
532
534
|
} else return null;
|
|
@@ -636,6 +638,28 @@ function detectAngleBracketUrl(text, startPos) {
|
|
|
636
638
|
contentEnd: closeAngle
|
|
637
639
|
};
|
|
638
640
|
}
|
|
641
|
+
var UNICODE_PUNCT_RE = /\p{P}/u;
|
|
642
|
+
var UNICODE_WS_RE = /\s/;
|
|
643
|
+
function isUnicodeWs(ch) {
|
|
644
|
+
return ch === void 0 || UNICODE_WS_RE.test(ch);
|
|
645
|
+
}
|
|
646
|
+
function isUnicodePunct(ch) {
|
|
647
|
+
return ch !== void 0 && UNICODE_PUNCT_RE.test(ch);
|
|
648
|
+
}
|
|
649
|
+
function delimiterFlags(text, start, len, char) {
|
|
650
|
+
const before = start > 0 ? text[start - 1] : void 0;
|
|
651
|
+
const after = start + len < text.length ? text[start + len] : void 0;
|
|
652
|
+
const leftFlanking = !isUnicodeWs(after) && (!isUnicodePunct(after) || isUnicodeWs(before) || isUnicodePunct(before));
|
|
653
|
+
const rightFlanking = !isUnicodeWs(before) && (!isUnicodePunct(before) || isUnicodeWs(after) || isUnicodePunct(after));
|
|
654
|
+
if (char === "_") return {
|
|
655
|
+
canOpen: leftFlanking && (!rightFlanking || isUnicodePunct(before)),
|
|
656
|
+
canClose: rightFlanking && (!leftFlanking || isUnicodePunct(after))
|
|
657
|
+
};
|
|
658
|
+
return {
|
|
659
|
+
canOpen: leftFlanking,
|
|
660
|
+
canClose: rightFlanking
|
|
661
|
+
};
|
|
662
|
+
}
|
|
639
663
|
/**
|
|
640
664
|
* Detect emphasis/strong spans: *text* or _text_
|
|
641
665
|
* EBNF: emphasisSpan = "*", text, "*" | "_", text, "_"
|
|
@@ -647,28 +671,35 @@ function detectEmphasis(text, startPos) {
|
|
|
647
671
|
let delimCount = 1;
|
|
648
672
|
while (startPos + delimCount < text.length && text[startPos + delimCount] === char) delimCount++;
|
|
649
673
|
if (delimCount > 2) return null;
|
|
674
|
+
if (!delimiterFlags(text, startPos, delimCount, char).canOpen) return null;
|
|
650
675
|
const type = delimCount === 1 ? "emphasis" : "strong";
|
|
651
|
-
const closer = char.repeat(delimCount);
|
|
652
676
|
const contentStart = startPos + delimCount;
|
|
653
677
|
let pos = contentStart;
|
|
678
|
+
let depth = 0;
|
|
654
679
|
while (pos < text.length) {
|
|
655
|
-
if (text
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
680
|
+
if (text[pos] === char) {
|
|
681
|
+
let runLen = 1;
|
|
682
|
+
while (pos + runLen < text.length && text[pos + runLen] === char) runLen++;
|
|
683
|
+
const flags = delimiterFlags(text, pos, runLen, char);
|
|
684
|
+
if (depth === 0 && flags.canClose && runLen >= delimCount) {
|
|
685
|
+
const content = text.slice(contentStart, pos);
|
|
686
|
+
if (content.length === 0) return null;
|
|
687
|
+
const endPos = pos + delimCount;
|
|
688
|
+
const attrsResult = extractAttributes(text, endPos);
|
|
689
|
+
const finalPos = attrsResult ? attrsResult.endPos : endPos;
|
|
690
|
+
return {
|
|
691
|
+
type,
|
|
692
|
+
content,
|
|
693
|
+
attrs: attrsResult?.attrs || null,
|
|
694
|
+
endPos: finalPos,
|
|
695
|
+
contentStart,
|
|
696
|
+
contentEnd: pos
|
|
697
|
+
};
|
|
659
698
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
return {
|
|
665
|
-
type,
|
|
666
|
-
content,
|
|
667
|
-
attrs: attrsResult?.attrs || null,
|
|
668
|
-
endPos: finalPos,
|
|
669
|
-
contentStart,
|
|
670
|
-
contentEnd: pos
|
|
671
|
-
};
|
|
699
|
+
if (flags.canClose) depth = Math.max(0, depth - 1);
|
|
700
|
+
else if (flags.canOpen) depth++;
|
|
701
|
+
pos += runLen;
|
|
702
|
+
continue;
|
|
672
703
|
}
|
|
673
704
|
pos++;
|
|
674
705
|
}
|
|
@@ -705,6 +736,48 @@ function detectCodeSpan(text, startPos) {
|
|
|
705
736
|
}
|
|
706
737
|
return null;
|
|
707
738
|
}
|
|
739
|
+
function parseLinkTitle(s) {
|
|
740
|
+
if (s.length < 2) return null;
|
|
741
|
+
const open = s[0];
|
|
742
|
+
const close = s[s.length - 1];
|
|
743
|
+
return open === "\"" && close === "\"" || open === "'" && close === "'" || open === "(" && close === ")" ? s.slice(1, -1) : null;
|
|
744
|
+
}
|
|
745
|
+
function parseLinkDestination(inner) {
|
|
746
|
+
const s = inner.trim();
|
|
747
|
+
if (s === "") return null;
|
|
748
|
+
let dest, rest;
|
|
749
|
+
if (s[0] === "<") {
|
|
750
|
+
const close = s.indexOf(">");
|
|
751
|
+
if (close === -1) return null;
|
|
752
|
+
dest = s.slice(1, close);
|
|
753
|
+
rest = s.slice(close + 1).trim();
|
|
754
|
+
} else {
|
|
755
|
+
let w = 0;
|
|
756
|
+
while (w < s.length && s[w] !== " " && s[w] !== " " && s[w] !== "\n") w++;
|
|
757
|
+
dest = s.slice(0, w);
|
|
758
|
+
rest = s.slice(w).trim();
|
|
759
|
+
let depth = 0;
|
|
760
|
+
for (let k = 0; k < dest.length; k++) {
|
|
761
|
+
if (dest[k] === "(") depth++;
|
|
762
|
+
else if (dest[k] === ")") depth--;
|
|
763
|
+
if (depth < 0) return null;
|
|
764
|
+
}
|
|
765
|
+
if (depth !== 0) return null;
|
|
766
|
+
}
|
|
767
|
+
if (dest === "") return null;
|
|
768
|
+
if (rest !== "") {
|
|
769
|
+
const title = parseLinkTitle(rest);
|
|
770
|
+
if (title === null) return null;
|
|
771
|
+
return {
|
|
772
|
+
dest,
|
|
773
|
+
title
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
return {
|
|
777
|
+
dest,
|
|
778
|
+
title: null
|
|
779
|
+
};
|
|
780
|
+
}
|
|
708
781
|
/**
|
|
709
782
|
* Detect bracket link: [text](url) or [text]{...} or [text] alone
|
|
710
783
|
* EBNF: linkSpan = "[", text, "](", text, ")" | "[", text, "]"
|
|
@@ -716,14 +789,20 @@ function detectBracketLink(text, startPos) {
|
|
|
716
789
|
const linkText = text.slice(startPos + 1, bracketEnd);
|
|
717
790
|
let pos = bracketEnd + 1;
|
|
718
791
|
let url = null;
|
|
792
|
+
let title = null;
|
|
719
793
|
if (pos < text.length && text[pos] === "(") {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
794
|
+
let parenEnd = text.indexOf(")", pos + 1);
|
|
795
|
+
while (parenEnd !== -1) {
|
|
796
|
+
const parsed = parseLinkDestination(text.slice(pos + 1, parenEnd));
|
|
797
|
+
if (parsed) {
|
|
798
|
+
url = parsed.dest;
|
|
799
|
+
title = parsed.title;
|
|
800
|
+
pos = parenEnd + 1;
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
parenEnd = text.indexOf(")", parenEnd + 1);
|
|
726
804
|
}
|
|
805
|
+
if (url === null) return null;
|
|
727
806
|
} else if (pos < text.length && text[pos] === "<") {
|
|
728
807
|
const closeAngle = text.indexOf(">", pos + 1);
|
|
729
808
|
if (closeAngle !== -1) {
|
|
@@ -740,6 +819,7 @@ function detectBracketLink(text, startPos) {
|
|
|
740
819
|
type: url ? "link" : "span",
|
|
741
820
|
text: linkText,
|
|
742
821
|
url,
|
|
822
|
+
title,
|
|
743
823
|
attrs: attrsResult?.attrs || null,
|
|
744
824
|
endPos: finalPos,
|
|
745
825
|
contentStart: startPos + 1,
|
|
@@ -750,6 +830,15 @@ function detectBracketLink(text, startPos) {
|
|
|
750
830
|
* Scan text for all inline carriers using character-based detection
|
|
751
831
|
* Memory-optimized version - minimizes object allocations
|
|
752
832
|
*/
|
|
833
|
+
function isEscaped(text, pos) {
|
|
834
|
+
let n = 0;
|
|
835
|
+
let j = pos - 1;
|
|
836
|
+
while (j >= 0 && text[j] === "\\") {
|
|
837
|
+
n++;
|
|
838
|
+
j--;
|
|
839
|
+
}
|
|
840
|
+
return n % 2 === 1;
|
|
841
|
+
}
|
|
753
842
|
function scanInlineCarriers(text, baseOffset = 0) {
|
|
754
843
|
const carriers = [];
|
|
755
844
|
const len = text.length;
|
|
@@ -757,7 +846,21 @@ function scanInlineCarriers(text, baseOffset = 0) {
|
|
|
757
846
|
while (pos < len) {
|
|
758
847
|
const char = text[pos];
|
|
759
848
|
let detected = null;
|
|
849
|
+
if (isEscaped(text, pos)) {
|
|
850
|
+
pos++;
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
760
853
|
switch (char) {
|
|
854
|
+
case "!":
|
|
855
|
+
if (text[pos + 1] === "[") {
|
|
856
|
+
const inner = detectBracketLink(text, pos + 1);
|
|
857
|
+
if (inner && inner.url) {
|
|
858
|
+
detected = inner;
|
|
859
|
+
detected.type = "image";
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
break;
|
|
863
|
+
case "<":
|
|
761
864
|
case "<":
|
|
762
865
|
detected = detectAngleBracketUrl(text, pos);
|
|
763
866
|
if (detected) {
|
|
@@ -801,6 +904,7 @@ function scanInlineCarriers(text, baseOffset = 0) {
|
|
|
801
904
|
valueRange,
|
|
802
905
|
attrs,
|
|
803
906
|
url,
|
|
907
|
+
title: detected.title || null,
|
|
804
908
|
pos: detected.endPos
|
|
805
909
|
};
|
|
806
910
|
if (attrs) carrier.attrsRange = [absContentEnd, absEnd];
|
|
@@ -912,10 +1016,6 @@ function resolveObject(sem, state) {
|
|
|
912
1016
|
if (sem.object.startsWith("#")) return resolveFragment(sem.object.substring(1), state.currentSubject, state.df);
|
|
913
1017
|
return state.df.namedNode(expandIRI(sem.object, state.ctx));
|
|
914
1018
|
}
|
|
915
|
-
function escapeHtml$1(text) {
|
|
916
|
-
if (!text) return "";
|
|
917
|
-
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
918
|
-
}
|
|
919
1019
|
function isLiteral(term) {
|
|
920
1020
|
return term?.termType === "Literal";
|
|
921
1021
|
}
|
|
@@ -1130,6 +1230,7 @@ function scanTokens(text) {
|
|
|
1130
1230
|
let pos = 0;
|
|
1131
1231
|
let codeBlock = null;
|
|
1132
1232
|
let sfcBlock = null;
|
|
1233
|
+
let quoteBlock = null;
|
|
1133
1234
|
function detectSfcStart(trimmed) {
|
|
1134
1235
|
if (trimmed.startsWith("<!--")) return true;
|
|
1135
1236
|
return /^<(script|style|template)\b/i.test(trimmed);
|
|
@@ -1172,6 +1273,11 @@ function scanTokens(text) {
|
|
|
1172
1273
|
return true;
|
|
1173
1274
|
}
|
|
1174
1275
|
const PROCESSORS = [
|
|
1276
|
+
{
|
|
1277
|
+
type: "quoteContent",
|
|
1278
|
+
test: () => quoteBlock !== null,
|
|
1279
|
+
process: handleQuoteContent
|
|
1280
|
+
},
|
|
1175
1281
|
{
|
|
1176
1282
|
type: "fence",
|
|
1177
1283
|
test: (line) => detectFence(line.trim()),
|
|
@@ -1300,16 +1406,67 @@ function scanTokens(text) {
|
|
|
1300
1406
|
mdLines.push(cleanList);
|
|
1301
1407
|
return true;
|
|
1302
1408
|
}
|
|
1409
|
+
function quoteValueStart(line) {
|
|
1410
|
+
return line.startsWith("> ") ? 2 : line.indexOf(">") + 1;
|
|
1411
|
+
}
|
|
1412
|
+
function emitQuoteSegment(qb) {
|
|
1413
|
+
if (!qb.parts.length) return;
|
|
1414
|
+
const last = qb.parts[qb.parts.length - 1];
|
|
1415
|
+
const text = qb.parts.map((p) => p.content).join("\n");
|
|
1416
|
+
tokens.push(createToken("blockquote", [qb.segStart, qb.end], text, last.attrs, calcAttrsRange(last.line, last.attrs, last.lineStart), [qb.segValueStart, qb.valueEnd]));
|
|
1417
|
+
qb.parts = [];
|
|
1418
|
+
qb.segStart = null;
|
|
1419
|
+
}
|
|
1303
1420
|
function handleBlockquote(line, lineStart, pos) {
|
|
1304
1421
|
const result = detectBlockquote(line);
|
|
1305
|
-
const
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1422
|
+
const valueStartInLine = quoteValueStart(line);
|
|
1423
|
+
quoteBlock = {
|
|
1424
|
+
segStart: lineStart,
|
|
1425
|
+
segValueStart: lineStart + valueStartInLine,
|
|
1426
|
+
end: pos - 1,
|
|
1427
|
+
valueEnd: lineStart + valueStartInLine + result.content.length,
|
|
1428
|
+
parts: [{
|
|
1429
|
+
content: result.content,
|
|
1430
|
+
attrs: result.attrs,
|
|
1431
|
+
line,
|
|
1432
|
+
lineStart,
|
|
1433
|
+
valueStartInLine
|
|
1434
|
+
}]
|
|
1435
|
+
};
|
|
1436
|
+
mdLines.push(`> ${result.content}`);
|
|
1437
|
+
if (result.attrs) emitQuoteSegment(quoteBlock);
|
|
1311
1438
|
return true;
|
|
1312
1439
|
}
|
|
1440
|
+
function flushQuoteBlock() {
|
|
1441
|
+
if (!quoteBlock) return;
|
|
1442
|
+
emitQuoteSegment(quoteBlock);
|
|
1443
|
+
quoteBlock = null;
|
|
1444
|
+
}
|
|
1445
|
+
function handleQuoteContent(line, lineStart, pos) {
|
|
1446
|
+
const bq = detectBlockquote(line);
|
|
1447
|
+
if (bq) {
|
|
1448
|
+
const valueStartInLine = quoteValueStart(line);
|
|
1449
|
+
const qb = quoteBlock;
|
|
1450
|
+
if (!qb.parts.length) {
|
|
1451
|
+
qb.segStart = lineStart;
|
|
1452
|
+
qb.segValueStart = lineStart + valueStartInLine;
|
|
1453
|
+
}
|
|
1454
|
+
qb.parts.push({
|
|
1455
|
+
content: bq.content,
|
|
1456
|
+
attrs: bq.attrs,
|
|
1457
|
+
line,
|
|
1458
|
+
lineStart,
|
|
1459
|
+
valueStartInLine
|
|
1460
|
+
});
|
|
1461
|
+
qb.end = pos - 1;
|
|
1462
|
+
qb.valueEnd = lineStart + valueStartInLine + bq.content.length;
|
|
1463
|
+
mdLines.push(`> ${bq.content}`);
|
|
1464
|
+
if (bq.attrs) emitQuoteSegment(qb);
|
|
1465
|
+
return true;
|
|
1466
|
+
}
|
|
1467
|
+
flushQuoteBlock();
|
|
1468
|
+
return false;
|
|
1469
|
+
}
|
|
1313
1470
|
function handlePara(line, lineStart, pos) {
|
|
1314
1471
|
const leadingWhitespace = line.search(/\S/);
|
|
1315
1472
|
const valueStart = leadingWhitespace === -1 ? lineStart : lineStart + leadingWhitespace;
|
|
@@ -1360,6 +1517,7 @@ function scanTokens(text) {
|
|
|
1360
1517
|
pos += line.length + 1;
|
|
1361
1518
|
for (const processor of PROCESSORS) if (processor.test(line) && processor.process(line, lineStart, pos)) break;
|
|
1362
1519
|
}
|
|
1520
|
+
flushQuoteBlock();
|
|
1363
1521
|
return {
|
|
1364
1522
|
tokens,
|
|
1365
1523
|
md: mdLines.join("\n")
|
|
@@ -2034,690 +2192,6 @@ function buildLabelLookup(subjectGroups, lang = null) {
|
|
|
2034
2192
|
return labelLookup;
|
|
2035
2193
|
}
|
|
2036
2194
|
//#endregion
|
|
2037
|
-
//#region src/render.js
|
|
2038
|
-
/**
|
|
2039
|
-
* MD-LD UI Renderer
|
|
2040
|
-
*
|
|
2041
|
-
* Generates clean, semantic HTML for presentation.
|
|
2042
|
-
* Preserves the raw MD-LD annotation in `data-annotation` for client-side
|
|
2043
|
-
* JS/CSS targeting, and resolves the primary IRI into `data-iri`.
|
|
2044
|
-
*/
|
|
2045
|
-
function render(src, options = {}) {
|
|
2046
|
-
const lines = src.split("\n");
|
|
2047
|
-
const context = buildRenderContext(lines, options.context);
|
|
2048
|
-
function safeExpand(term) {
|
|
2049
|
-
if (!term || term === "RESET") return null;
|
|
2050
|
-
if (term.startsWith("=#") || term.startsWith("#")) return term;
|
|
2051
|
-
return expandIRI(term, context);
|
|
2052
|
-
}
|
|
2053
|
-
function getAttrs(rawAnnotation, parsedSem, isLink = false, fallbackHref = null) {
|
|
2054
|
-
const attrs = [];
|
|
2055
|
-
const classes = [];
|
|
2056
|
-
if (rawAnnotation) attrs.push(`data-annotation="${escapeHtml$1("{" + rawAnnotation + "}")}"`);
|
|
2057
|
-
let iri = null;
|
|
2058
|
-
if (parsedSem?.subject && parsedSem.subject !== "RESET") iri = safeExpand(parsedSem.subject);
|
|
2059
|
-
else if (parsedSem?.object) iri = safeExpand(parsedSem.object);
|
|
2060
|
-
if (iri) attrs.push(`data-iri="${escapeHtml$1(iri)}"`);
|
|
2061
|
-
if (isLink && fallbackHref) attrs.push(`href="${escapeHtml$1(fallbackHref)}"`);
|
|
2062
|
-
if (parsedSem?.types?.some((t) => !t.remove)) classes.push("typed");
|
|
2063
|
-
if (parsedSem?.predicates?.some((p) => p.remove) || parsedSem?.types?.some((t) => t.remove)) classes.push("retracted");
|
|
2064
|
-
if (classes.length > 0) attrs.unshift(`class="${classes.join(" ")}"`);
|
|
2065
|
-
return attrs.join(" ");
|
|
2066
|
-
}
|
|
2067
|
-
function mergeClasses(baseClass, customAttrs) {
|
|
2068
|
-
const classMatch = customAttrs.match(/class="([^"]*)"/);
|
|
2069
|
-
let finalClass = baseClass;
|
|
2070
|
-
let remainingAttrs = customAttrs;
|
|
2071
|
-
if (classMatch) {
|
|
2072
|
-
finalClass += " " + classMatch[1];
|
|
2073
|
-
remainingAttrs = customAttrs.replace(/class="[^"]*"/, "").trim();
|
|
2074
|
-
}
|
|
2075
|
-
return `class="${finalClass}"${remainingAttrs ? " " + remainingAttrs : ""}`;
|
|
2076
|
-
}
|
|
2077
|
-
function renderInline(text) {
|
|
2078
|
-
const carriers = scanInlineCarriers(text, 0);
|
|
2079
|
-
if (carriers.length === 0) return escapeHtml$1(text);
|
|
2080
|
-
let out = "";
|
|
2081
|
-
let lastPos = 0;
|
|
2082
|
-
for (const c of carriers) {
|
|
2083
|
-
if (c.range[0] > lastPos) out += escapeHtml$1(text.slice(lastPos, c.range[0]));
|
|
2084
|
-
const sem = c.attrs ? parseSemanticBlock(c.attrs) : null;
|
|
2085
|
-
const raw = c.attrs || "";
|
|
2086
|
-
const isLink = c.type === "link";
|
|
2087
|
-
const fallbackHref = c.url || null;
|
|
2088
|
-
let tag = "span";
|
|
2089
|
-
let baseClass = "mdld-bracket";
|
|
2090
|
-
if (c.type === "link") {
|
|
2091
|
-
tag = "a";
|
|
2092
|
-
baseClass = "mdld-link";
|
|
2093
|
-
} else if (c.type === "code") {
|
|
2094
|
-
tag = "code";
|
|
2095
|
-
baseClass = "mdld-code";
|
|
2096
|
-
} else if (c.type === "strong") {
|
|
2097
|
-
tag = "strong";
|
|
2098
|
-
baseClass = "mdld-bold";
|
|
2099
|
-
} else if (c.type === "emphasis") {
|
|
2100
|
-
tag = "em";
|
|
2101
|
-
baseClass = "mdld-italic";
|
|
2102
|
-
}
|
|
2103
|
-
const customAttrs = getAttrs(raw, sem, isLink, fallbackHref);
|
|
2104
|
-
const attrString = mergeClasses(baseClass, customAttrs);
|
|
2105
|
-
out += `<${tag} ${attrString}>${renderInline(c.text)}</${tag}>`;
|
|
2106
|
-
lastPos = c.range[1];
|
|
2107
|
-
}
|
|
2108
|
-
if (lastPos < text.length) out += escapeHtml$1(text.slice(lastPos));
|
|
2109
|
-
return out;
|
|
2110
|
-
}
|
|
2111
|
-
const out = [];
|
|
2112
|
-
let i = 0;
|
|
2113
|
-
let codeBlock = null;
|
|
2114
|
-
while (i < lines.length) {
|
|
2115
|
-
const line = lines[i];
|
|
2116
|
-
if (codeBlock) {
|
|
2117
|
-
if (new RegExp(`^${codeBlock.fenceChar}{${codeBlock.fenceLength},}\\s*$`).test(line.trim())) {
|
|
2118
|
-
const sem = codeBlock.attrs ? parseSemanticBlock(codeBlock.attrs) : null;
|
|
2119
|
-
const carrierText = codeBlock.content.join("\n");
|
|
2120
|
-
const customAttrs = getAttrs(codeBlock.attrs, sem);
|
|
2121
|
-
const attrString = mergeClasses(`mdld-codeblock language-${codeBlock.lang || "text"}`, customAttrs);
|
|
2122
|
-
out.push(`<pre><code ${attrString}>${escapeHtml$1(carrierText)}</code></pre>`);
|
|
2123
|
-
codeBlock = null;
|
|
2124
|
-
} else codeBlock.content.push(line);
|
|
2125
|
-
i++;
|
|
2126
|
-
continue;
|
|
2127
|
-
}
|
|
2128
|
-
if (line.trim() === "") {
|
|
2129
|
-
i++;
|
|
2130
|
-
continue;
|
|
2131
|
-
}
|
|
2132
|
-
const fence = detectFence(line.trim());
|
|
2133
|
-
if (fence) {
|
|
2134
|
-
codeBlock = {
|
|
2135
|
-
fenceChar: fence.fenceChar,
|
|
2136
|
-
fenceLength: fence.fenceLength,
|
|
2137
|
-
lang: fence.lang,
|
|
2138
|
-
attrs: fence.attrs,
|
|
2139
|
-
content: []
|
|
2140
|
-
};
|
|
2141
|
-
i++;
|
|
2142
|
-
continue;
|
|
2143
|
-
}
|
|
2144
|
-
const prefix = detectPrefix(line);
|
|
2145
|
-
if (prefix) {
|
|
2146
|
-
const resolvedIri = context[prefix.prefix];
|
|
2147
|
-
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>`);
|
|
2148
|
-
i++;
|
|
2149
|
-
continue;
|
|
2150
|
-
}
|
|
2151
|
-
const heading = detectHeading(line);
|
|
2152
|
-
if (heading) {
|
|
2153
|
-
const sem = heading.attrs ? parseSemanticBlock(heading.attrs) : null;
|
|
2154
|
-
const attrString = mergeClasses("mdld-heading", getAttrs(heading.attrs, sem));
|
|
2155
|
-
out.push(`<h${heading.depth} ${attrString}>${renderInline(heading.content)}</h${heading.depth}>`);
|
|
2156
|
-
i++;
|
|
2157
|
-
continue;
|
|
2158
|
-
}
|
|
2159
|
-
if (detectList(line)) {
|
|
2160
|
-
out.push("<ul class=\"mdld-list\">");
|
|
2161
|
-
while (i < lines.length) {
|
|
2162
|
-
const l = detectList(lines[i]);
|
|
2163
|
-
if (!l) break;
|
|
2164
|
-
const sem = l.attrs ? parseSemanticBlock(l.attrs) : null;
|
|
2165
|
-
const attrString = mergeClasses("mdld-item", getAttrs(l.attrs, sem));
|
|
2166
|
-
out.push(`<li ${attrString}>${renderInline(l.content)}</li>`);
|
|
2167
|
-
i++;
|
|
2168
|
-
}
|
|
2169
|
-
out.push("</ul>");
|
|
2170
|
-
continue;
|
|
2171
|
-
}
|
|
2172
|
-
const blockquote = detectBlockquote(line);
|
|
2173
|
-
if (blockquote) {
|
|
2174
|
-
let quoteLines = [blockquote.content];
|
|
2175
|
-
i++;
|
|
2176
|
-
while (i < lines.length) {
|
|
2177
|
-
const bq = detectBlockquote(lines[i]);
|
|
2178
|
-
if (!bq) break;
|
|
2179
|
-
quoteLines.push(bq.content);
|
|
2180
|
-
i++;
|
|
2181
|
-
}
|
|
2182
|
-
const sem = blockquote.attrs ? parseSemanticBlock(blockquote.attrs) : null;
|
|
2183
|
-
const attrString = mergeClasses("mdld-quote", getAttrs(blockquote.attrs, sem));
|
|
2184
|
-
const carrierText = quoteLines.join("\n");
|
|
2185
|
-
out.push(`<blockquote ${attrString}>${renderInline(carrierText)}</blockquote>`);
|
|
2186
|
-
continue;
|
|
2187
|
-
}
|
|
2188
|
-
const standalone = detectStandaloneSubject(line);
|
|
2189
|
-
if (standalone) {
|
|
2190
|
-
const fakeSem = parseSemanticBlock(`=${standalone.content}`);
|
|
2191
|
-
const attrString = mergeClasses("mdld-standalone", getAttrs(`=${standalone.content}`, fakeSem));
|
|
2192
|
-
out.push(`<div ${attrString} style="display:none"></div>`);
|
|
2193
|
-
i++;
|
|
2194
|
-
continue;
|
|
2195
|
-
}
|
|
2196
|
-
let paraLines = [line];
|
|
2197
|
-
i++;
|
|
2198
|
-
while (i < lines.length && lines[i].trim() !== "" && !detectFence(lines[i].trim()) && !detectPrefix(lines[i]) && !detectHeading(lines[i]) && !detectList(lines[i]) && !detectBlockquote(lines[i]) && !detectStandaloneSubject(lines[i])) {
|
|
2199
|
-
paraLines.push(lines[i]);
|
|
2200
|
-
i++;
|
|
2201
|
-
}
|
|
2202
|
-
const paraText = paraLines.join("\n");
|
|
2203
|
-
out.push(`<p class="mdld-paragraph">${renderInline(paraText)}</p>`);
|
|
2204
|
-
}
|
|
2205
|
-
return out.join("\n");
|
|
2206
|
-
}
|
|
2207
|
-
function buildRenderContext(lines, userContext = {}) {
|
|
2208
|
-
const context = {
|
|
2209
|
-
...DEFAULT_CONTEXT,
|
|
2210
|
-
...userContext
|
|
2211
|
-
};
|
|
2212
|
-
for (const line of lines) {
|
|
2213
|
-
const prefix = detectPrefix(line);
|
|
2214
|
-
if (prefix) {
|
|
2215
|
-
let iri = prefix.iri;
|
|
2216
|
-
if (iri.includes(":")) {
|
|
2217
|
-
const colonIndex = iri.indexOf(":");
|
|
2218
|
-
const p = iri.substring(0, colonIndex);
|
|
2219
|
-
const ref = iri.substring(colonIndex + 1);
|
|
2220
|
-
if (context[p] && p !== "@vocab") iri = context[p] + ref;
|
|
2221
|
-
}
|
|
2222
|
-
context[prefix.prefix] = iri;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
return context;
|
|
2226
|
-
}
|
|
2227
|
-
/**
|
|
2228
|
-
* Reconstruct MD-LD from rendered HTML.
|
|
2229
|
-
* Pure string scanning over our constrained output format.
|
|
2230
|
-
* Platform-agnostic: no DOMParser, no browser APIs required.
|
|
2231
|
-
*/
|
|
2232
|
-
function deconstruct(html) {
|
|
2233
|
-
const blocks = [];
|
|
2234
|
-
let pos = 0;
|
|
2235
|
-
while (pos < html.length) {
|
|
2236
|
-
while (pos < html.length && /\s/.test(html[pos])) pos++;
|
|
2237
|
-
if (pos >= html.length) break;
|
|
2238
|
-
const nextTagStart = html.indexOf("<", pos);
|
|
2239
|
-
if (nextTagStart === -1) break;
|
|
2240
|
-
const tagEnd = html.indexOf(">", nextTagStart);
|
|
2241
|
-
if (tagEnd === -1) break;
|
|
2242
|
-
const openTag = html.slice(nextTagStart, tagEnd + 1);
|
|
2243
|
-
if (openTag.startsWith("<p") && extractAttr(openTag, "class")?.includes("mdld-paragraph")) {
|
|
2244
|
-
const close = html.indexOf("</p>", pos);
|
|
2245
|
-
if (close === -1) break;
|
|
2246
|
-
const openEnd = html.indexOf(">", pos) + 1;
|
|
2247
|
-
const content = html.slice(openEnd, close);
|
|
2248
|
-
blocks.push(deconstructInline(content));
|
|
2249
|
-
pos = close + 4;
|
|
2250
|
-
} else if (openTag.startsWith("<div") && extractAttr(openTag, "class")?.includes("mdld-prefix")) {
|
|
2251
|
-
const end = html.indexOf("></div>", pos);
|
|
2252
|
-
if (end === -1) break;
|
|
2253
|
-
const tag = html.slice(pos, end + 7);
|
|
2254
|
-
const prefix = extractAttr(tag, "data-prefix");
|
|
2255
|
-
const raw = extractAttr(tag, "data-raw") || extractAttr(tag, "data-iri");
|
|
2256
|
-
blocks.push(`[${prefix}] <${raw}>`);
|
|
2257
|
-
pos = end + 7;
|
|
2258
|
-
} else if (openTag.startsWith("<div") && extractAttr(openTag, "class")?.includes("mdld-standalone")) {
|
|
2259
|
-
const end = html.indexOf("</div>", pos);
|
|
2260
|
-
if (end === -1) break;
|
|
2261
|
-
const ann = extractAttr(html.slice(pos, end + 6), "data-annotation");
|
|
2262
|
-
if (ann) blocks.push(ann);
|
|
2263
|
-
pos = end + 6;
|
|
2264
|
-
} else if (html.startsWith("<pre><code", pos)) {
|
|
2265
|
-
const closePre = html.indexOf("</code></pre>", pos);
|
|
2266
|
-
if (closePre === -1) break;
|
|
2267
|
-
const full = html.slice(pos, closePre + 13);
|
|
2268
|
-
const preClose = full.indexOf(">") + 1;
|
|
2269
|
-
const codeOpenEnd = full.indexOf(">", preClose) + 1;
|
|
2270
|
-
const codeTag = full.slice(preClose, codeOpenEnd);
|
|
2271
|
-
const content = full.slice(codeOpenEnd, full.length - 13);
|
|
2272
|
-
const langMatch = (extractAttr(codeTag, "class") || "").match(/language-(\w+)/);
|
|
2273
|
-
const lang = langMatch ? langMatch[1] : "";
|
|
2274
|
-
const ann = extractAttr(codeTag, "data-annotation");
|
|
2275
|
-
blocks.push(`\`\`\`${lang}${ann ? " " + ann : ""}\n${unescapeHtml(content)}\n\`\`\``);
|
|
2276
|
-
pos = closePre + 13;
|
|
2277
|
-
} else if (html[pos] === "<" && /^<h[1-6]/.test(html.slice(pos, pos + 4))) {
|
|
2278
|
-
const level = html[pos + 2];
|
|
2279
|
-
const closeTag = `</h${level}>`;
|
|
2280
|
-
const close = html.indexOf(closeTag, pos);
|
|
2281
|
-
if (close === -1) break;
|
|
2282
|
-
const full = html.slice(pos, close + closeTag.length);
|
|
2283
|
-
const openEnd = full.indexOf(">") + 1;
|
|
2284
|
-
const openTag = full.slice(0, openEnd);
|
|
2285
|
-
const content = full.slice(openEnd, full.length - closeTag.length);
|
|
2286
|
-
const hashes = "#".repeat(parseInt(level));
|
|
2287
|
-
const ann = extractAttr(openTag, "data-annotation");
|
|
2288
|
-
blocks.push(`${hashes} ${deconstructInline(content)}${ann ? " " + ann : ""}`);
|
|
2289
|
-
pos = close + closeTag.length;
|
|
2290
|
-
} else if (html.startsWith("<blockquote", pos)) {
|
|
2291
|
-
const close = html.indexOf("</blockquote>", pos);
|
|
2292
|
-
if (close === -1) break;
|
|
2293
|
-
const full = html.slice(pos, close + 13);
|
|
2294
|
-
const openEnd = full.indexOf(">") + 1;
|
|
2295
|
-
const openTag = full.slice(0, openEnd);
|
|
2296
|
-
const content = full.slice(openEnd, full.length - 13);
|
|
2297
|
-
const ann = extractAttr(openTag, "data-annotation");
|
|
2298
|
-
const quoted = deconstructInline(content).split("\n").map((l) => l.trim() ? `> ${l}` : ">").join("\n");
|
|
2299
|
-
blocks.push(ann ? `${quoted} ${ann}` : quoted);
|
|
2300
|
-
pos = close + 13;
|
|
2301
|
-
} else if (html.startsWith("<ul class=\"mdld-list\">", pos)) {
|
|
2302
|
-
const close = html.indexOf("</ul>", pos);
|
|
2303
|
-
if (close === -1) break;
|
|
2304
|
-
const inner = html.slice(pos + 21, close);
|
|
2305
|
-
const items = [];
|
|
2306
|
-
let itemPos = 0;
|
|
2307
|
-
while (itemPos < inner.length) {
|
|
2308
|
-
const liStart = inner.indexOf("<li", itemPos);
|
|
2309
|
-
if (liStart === -1) break;
|
|
2310
|
-
const liClose = inner.indexOf("</li>", liStart);
|
|
2311
|
-
if (liClose === -1) break;
|
|
2312
|
-
const liFull = inner.slice(liStart, liClose + 5);
|
|
2313
|
-
const openEnd = liFull.indexOf(">") + 1;
|
|
2314
|
-
const openTag = liFull.slice(0, openEnd);
|
|
2315
|
-
const liContent = liFull.slice(openEnd, liFull.length - 5);
|
|
2316
|
-
const ann = extractAttr(openTag, "data-annotation");
|
|
2317
|
-
items.push(`- ${deconstructInline(liContent)}${ann ? " " + ann : ""}`);
|
|
2318
|
-
itemPos = liClose + 5;
|
|
2319
|
-
}
|
|
2320
|
-
blocks.push(items.join("\n"));
|
|
2321
|
-
pos = close + 5;
|
|
2322
|
-
} else pos++;
|
|
2323
|
-
}
|
|
2324
|
-
return blocks.join("\n\n");
|
|
2325
|
-
}
|
|
2326
|
-
function deconstructInline(html) {
|
|
2327
|
-
let out = "";
|
|
2328
|
-
let i = 0;
|
|
2329
|
-
while (i < html.length) if (html[i] === "<") {
|
|
2330
|
-
const closeGt = html.indexOf(">", i);
|
|
2331
|
-
if (closeGt === -1) {
|
|
2332
|
-
out += html.slice(i);
|
|
2333
|
-
break;
|
|
2334
|
-
}
|
|
2335
|
-
const tagContent = html.slice(i + 1, closeGt);
|
|
2336
|
-
if (tagContent.startsWith("/")) {
|
|
2337
|
-
i = closeGt + 1;
|
|
2338
|
-
continue;
|
|
2339
|
-
}
|
|
2340
|
-
const spaceOrEnd = tagContent.search(/[\s/]/);
|
|
2341
|
-
const tagName = (spaceOrEnd === -1 ? tagContent : tagContent.slice(0, spaceOrEnd)).toLowerCase();
|
|
2342
|
-
const attrs = spaceOrEnd === -1 ? "" : tagContent.slice(spaceOrEnd);
|
|
2343
|
-
const closeTag = `</${tagName}>`;
|
|
2344
|
-
let depth = 1;
|
|
2345
|
-
let j = closeGt + 1;
|
|
2346
|
-
let found = false;
|
|
2347
|
-
while (j < html.length && depth > 0) {
|
|
2348
|
-
const nextOpen = html.indexOf(`<${tagName}`, j);
|
|
2349
|
-
const nextClose = html.indexOf(closeTag, j);
|
|
2350
|
-
if (nextClose === -1) break;
|
|
2351
|
-
if (nextOpen !== -1 && nextOpen < nextClose) {
|
|
2352
|
-
depth++;
|
|
2353
|
-
j = nextOpen + 1;
|
|
2354
|
-
} else {
|
|
2355
|
-
depth--;
|
|
2356
|
-
if (depth === 0) {
|
|
2357
|
-
const inner = html.slice(closeGt + 1, nextClose);
|
|
2358
|
-
out += deconstructInlineElement(tagName, attrs, inner);
|
|
2359
|
-
i = nextClose + closeTag.length;
|
|
2360
|
-
found = true;
|
|
2361
|
-
break;
|
|
2362
|
-
}
|
|
2363
|
-
j = nextClose + 1;
|
|
2364
|
-
}
|
|
2365
|
-
}
|
|
2366
|
-
if (!found) {
|
|
2367
|
-
out += html[i];
|
|
2368
|
-
i++;
|
|
2369
|
-
}
|
|
2370
|
-
} else {
|
|
2371
|
-
const nextTag = html.indexOf("<", i);
|
|
2372
|
-
const textEnd = nextTag === -1 ? html.length : nextTag;
|
|
2373
|
-
out += unescapeHtml(html.slice(i, textEnd));
|
|
2374
|
-
i = textEnd;
|
|
2375
|
-
}
|
|
2376
|
-
return out;
|
|
2377
|
-
}
|
|
2378
|
-
function deconstructInlineElement(tagName, attrs, inner) {
|
|
2379
|
-
const ann = extractAttr(attrs, "data-annotation");
|
|
2380
|
-
const content = deconstructInline(inner);
|
|
2381
|
-
const suffix = ann ? " " + ann : "";
|
|
2382
|
-
switch (tagName) {
|
|
2383
|
-
case "a": return `[${content}](${extractAttr(attrs, "href") || ""})${suffix}`;
|
|
2384
|
-
case "span": return `[${content}]${suffix}`;
|
|
2385
|
-
case "code": return `\`${content}\`${suffix}`;
|
|
2386
|
-
case "strong": return `**${content}**${suffix}`;
|
|
2387
|
-
case "em": return `*${content}*${suffix}`;
|
|
2388
|
-
default: return content + suffix;
|
|
2389
|
-
}
|
|
2390
|
-
}
|
|
2391
|
-
function extractAttr(tag, name) {
|
|
2392
|
-
const re = new RegExp(`(?:\\s|^)${name}=(["'])(.*?)\\1`, "is");
|
|
2393
|
-
const m = tag.match(re);
|
|
2394
|
-
if (!m) return null;
|
|
2395
|
-
return unescapeHtml(m[2]);
|
|
2396
|
-
}
|
|
2397
|
-
function unescapeHtml(s) {
|
|
2398
|
-
return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'");
|
|
2399
|
-
}
|
|
2400
|
-
//#endregion
|
|
2401
|
-
//#region src/highlight.js
|
|
2402
|
-
/**
|
|
2403
|
-
* MD-LD Syntax Highlighter
|
|
2404
|
-
*
|
|
2405
|
-
* A performant, token-based syntax highlighter for MD-LD (Markdown Linked Data).
|
|
2406
|
-
* Uses simple string operations rather than complex grammars for reliability
|
|
2407
|
-
* and maintainability.
|
|
2408
|
-
*
|
|
2409
|
-
* Features:
|
|
2410
|
-
* - Token-based parsing with string operations
|
|
2411
|
-
* - Markdown formatting support with semantic distinction
|
|
2412
|
-
* - Lookahead detection for legal value carriers
|
|
2413
|
-
* - Orange semantic markers vs default markdown spans
|
|
2414
|
-
* - Annotation, value carrier, and heading highlighting
|
|
2415
|
-
*/
|
|
2416
|
-
function escapeHtml(s) {
|
|
2417
|
-
return String(s).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
2418
|
-
}
|
|
2419
|
-
var TOKEN_COLORS = {
|
|
2420
|
-
text: "#6b7280",
|
|
2421
|
-
marker: "#f97316",
|
|
2422
|
-
retraction: "#dc2626",
|
|
2423
|
-
value: "#eab308",
|
|
2424
|
-
annotation: "#4b5563"
|
|
2425
|
-
};
|
|
2426
|
-
function hasFollowingAnnotation(code, endPos) {
|
|
2427
|
-
let i = endPos;
|
|
2428
|
-
while (i < code.length && code[i] !== "\n" && code[i] !== "\r") {
|
|
2429
|
-
if (code[i] === "{") return true;
|
|
2430
|
-
i++;
|
|
2431
|
-
}
|
|
2432
|
-
return false;
|
|
2433
|
-
}
|
|
2434
|
-
function processMarkdownFormattingAtPosition(code, startPos) {
|
|
2435
|
-
const char = code[startPos];
|
|
2436
|
-
if (char === "*" && startPos + 1 < code.length && code[startPos + 1] === "*") {
|
|
2437
|
-
const endPos = code.indexOf("**", startPos + 2);
|
|
2438
|
-
if (endPos !== -1) {
|
|
2439
|
-
const content = code.slice(startPos + 2, endPos);
|
|
2440
|
-
const color = hasFollowingAnnotation(code, endPos + 2) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
2441
|
-
return {
|
|
2442
|
-
html: `<span${color}>**</span><strong>${escapeHtml(content)}</strong><span${color}>**</span>`,
|
|
2443
|
-
nextIndex: endPos + 2
|
|
2444
|
-
};
|
|
2445
|
-
}
|
|
2446
|
-
}
|
|
2447
|
-
if (char === "_" && startPos + 1 < code.length && code[startPos + 1] === "_") {
|
|
2448
|
-
const endPos = code.indexOf("__", startPos + 2);
|
|
2449
|
-
if (endPos !== -1) {
|
|
2450
|
-
const content = code.slice(startPos + 2, endPos);
|
|
2451
|
-
const color = hasFollowingAnnotation(code, endPos + 2) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
2452
|
-
return {
|
|
2453
|
-
html: `<span${color}>__</span><strong>${escapeHtml(content)}</strong><span${color}>__</span>`,
|
|
2454
|
-
nextIndex: endPos + 2
|
|
2455
|
-
};
|
|
2456
|
-
}
|
|
2457
|
-
}
|
|
2458
|
-
if (char === "*") {
|
|
2459
|
-
const endPos = code.indexOf("*", startPos + 1);
|
|
2460
|
-
if (endPos !== -1) {
|
|
2461
|
-
const content = code.slice(startPos + 1, endPos);
|
|
2462
|
-
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
2463
|
-
return {
|
|
2464
|
-
html: `<span${color}>*</span><em>${escapeHtml(content)}</em><span${color}>*</span>`,
|
|
2465
|
-
nextIndex: endPos + 1
|
|
2466
|
-
};
|
|
2467
|
-
}
|
|
2468
|
-
}
|
|
2469
|
-
if (char === "_") {
|
|
2470
|
-
const endPos = code.indexOf("_", startPos + 1);
|
|
2471
|
-
if (endPos !== -1) {
|
|
2472
|
-
const content = code.slice(startPos + 1, endPos);
|
|
2473
|
-
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
2474
|
-
return {
|
|
2475
|
-
html: `<span${color}>_</span><em>${escapeHtml(content)}</em><span${color}>_</span>`,
|
|
2476
|
-
nextIndex: endPos + 1
|
|
2477
|
-
};
|
|
2478
|
-
}
|
|
2479
|
-
}
|
|
2480
|
-
if (char === "`") {
|
|
2481
|
-
const endPos = code.indexOf("`", startPos + 1);
|
|
2482
|
-
if (endPos !== -1) {
|
|
2483
|
-
const content = code.slice(startPos + 1, endPos);
|
|
2484
|
-
const color = hasFollowingAnnotation(code, endPos + 1) ? ` style="color: ${TOKEN_COLORS.marker}"` : "";
|
|
2485
|
-
return {
|
|
2486
|
-
html: `<span${color}>\`</span><code style="background-color:#7773">${escapeHtml(content)}</code><span${color}>\`</span>`,
|
|
2487
|
-
nextIndex: endPos + 1
|
|
2488
|
-
};
|
|
2489
|
-
}
|
|
2490
|
-
}
|
|
2491
|
-
return null;
|
|
2492
|
-
}
|
|
2493
|
-
var colorCache = /* @__PURE__ */ new Map();
|
|
2494
|
-
function hashIRI(iri) {
|
|
2495
|
-
let hash = 0;
|
|
2496
|
-
for (let i = 0; i < iri.length; i++) {
|
|
2497
|
-
const char = iri.charCodeAt(i);
|
|
2498
|
-
hash = (hash << 5) - hash + char;
|
|
2499
|
-
hash = hash & hash;
|
|
2500
|
-
}
|
|
2501
|
-
return Math.abs(hash);
|
|
2502
|
-
}
|
|
2503
|
-
function getIRIColor(iri) {
|
|
2504
|
-
if (colorCache.has(iri)) return colorCache.get(iri);
|
|
2505
|
-
const hash = hashIRI(iri);
|
|
2506
|
-
const color = `hsl(${hash % 360}, ${15 + hash % 10}%, ${45 + hash % 10}%)`;
|
|
2507
|
-
colorCache.set(iri, color);
|
|
2508
|
-
return color;
|
|
2509
|
-
}
|
|
2510
|
-
function extractPrefixes(code) {
|
|
2511
|
-
const prefixes = /* @__PURE__ */ new Map();
|
|
2512
|
-
const prefixRegex = /^\[(\w+)\]\s*<([^>]*)>\s*$/gm;
|
|
2513
|
-
let match;
|
|
2514
|
-
while ((match = prefixRegex.exec(code)) !== null) {
|
|
2515
|
-
const prefixName = match[1];
|
|
2516
|
-
let iri = match[2];
|
|
2517
|
-
if (iri.includes(":")) {
|
|
2518
|
-
const colonIndex = iri.indexOf(":");
|
|
2519
|
-
if (colonIndex > 0) {
|
|
2520
|
-
const refPrefix = iri.slice(0, colonIndex);
|
|
2521
|
-
const refSuffix = iri.slice(colonIndex + 1);
|
|
2522
|
-
if (prefixes.has(refPrefix)) iri = prefixes.get(refPrefix) + refSuffix;
|
|
2523
|
-
}
|
|
2524
|
-
}
|
|
2525
|
-
prefixes.set(prefixName, iri);
|
|
2526
|
-
}
|
|
2527
|
-
for (const [key, iri] of Object.entries(DEFAULT_CONTEXT)) if (key !== "@vocab") prefixes.set(key, iri);
|
|
2528
|
-
return prefixes;
|
|
2529
|
-
}
|
|
2530
|
-
function renderTerm(part, isRetracted, prefixes) {
|
|
2531
|
-
const baseColor = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.text;
|
|
2532
|
-
const colonIndex = part.indexOf(":");
|
|
2533
|
-
if (colonIndex > 0) {
|
|
2534
|
-
const prefix = part.slice(0, colonIndex);
|
|
2535
|
-
const local = part.slice(colonIndex + 1);
|
|
2536
|
-
if (prefixes.has(prefix)) {
|
|
2537
|
-
const prefixIRI = prefixes.get(prefix);
|
|
2538
|
-
const prefixColor = getIRIColor(prefixIRI);
|
|
2539
|
-
const fullIRI = prefixIRI + local;
|
|
2540
|
-
const localColor = getIRIColor(fullIRI);
|
|
2541
|
-
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> `;
|
|
2542
|
-
}
|
|
2543
|
-
}
|
|
2544
|
-
if (part.startsWith("http:") || part.startsWith("https:") || part.startsWith("tag:") || part.startsWith("urn:")) return `<span style="color: ${getIRIColor(part)}">${escapeHtml(part)}</span> `;
|
|
2545
|
-
return `<span style="color: ${baseColor}">${escapeHtml(part)}</span> `;
|
|
2546
|
-
}
|
|
2547
|
-
function parseAnnotation(content, prefixes) {
|
|
2548
|
-
const parts = content.trim().split(/\s+/);
|
|
2549
|
-
let result = "";
|
|
2550
|
-
let nextTermIsRetracted = false;
|
|
2551
|
-
for (let i = 0; i < parts.length; i++) {
|
|
2552
|
-
const part = parts[i];
|
|
2553
|
-
if (!part) continue;
|
|
2554
|
-
if (part.startsWith("=") || part.startsWith("+")) {
|
|
2555
|
-
const marker = part[0];
|
|
2556
|
-
const rest = part.slice(1);
|
|
2557
|
-
result += `<span style="color: ${TOKEN_COLORS.marker}">${escapeHtml(marker)}</span>${renderTerm(rest, false, prefixes)}`;
|
|
2558
|
-
continue;
|
|
2559
|
-
}
|
|
2560
|
-
if (part === "-") {
|
|
2561
|
-
nextTermIsRetracted = true;
|
|
2562
|
-
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
2563
|
-
continue;
|
|
2564
|
-
}
|
|
2565
|
-
if (part.startsWith("-") && part.length > 1) {
|
|
2566
|
-
result += `<span style="color: ${TOKEN_COLORS.retraction}">${escapeHtml(part)}</span> `;
|
|
2567
|
-
continue;
|
|
2568
|
-
}
|
|
2569
|
-
const isRetracted = nextTermIsRetracted;
|
|
2570
|
-
nextTermIsRetracted = false;
|
|
2571
|
-
if (part.startsWith("?") || part.startsWith("!")) {
|
|
2572
|
-
const marker = part[0];
|
|
2573
|
-
const rest = part.slice(1);
|
|
2574
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
2575
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
2576
|
-
continue;
|
|
2577
|
-
}
|
|
2578
|
-
if (part.startsWith("^^")) {
|
|
2579
|
-
const marker = "^^";
|
|
2580
|
-
const rest = part.slice(2);
|
|
2581
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
2582
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
2583
|
-
continue;
|
|
2584
|
-
}
|
|
2585
|
-
if (part.startsWith("@")) {
|
|
2586
|
-
const marker = "@";
|
|
2587
|
-
const rest = part.slice(1);
|
|
2588
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
2589
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
2590
|
-
continue;
|
|
2591
|
-
}
|
|
2592
|
-
if (part.startsWith(".")) {
|
|
2593
|
-
const marker = ".";
|
|
2594
|
-
const rest = part.slice(1);
|
|
2595
|
-
const color = isRetracted ? TOKEN_COLORS.retraction : TOKEN_COLORS.marker;
|
|
2596
|
-
result += `<span style="color: ${color}">${escapeHtml(marker)}</span>${renderTerm(rest, isRetracted, prefixes)}`;
|
|
2597
|
-
continue;
|
|
2598
|
-
}
|
|
2599
|
-
result += renderTerm(part, isRetracted, prefixes);
|
|
2600
|
-
}
|
|
2601
|
-
return result.trim();
|
|
2602
|
-
}
|
|
2603
|
-
function highlightMDLD(code) {
|
|
2604
|
-
const prefixes = extractPrefixes(code);
|
|
2605
|
-
let result = "";
|
|
2606
|
-
let i = 0;
|
|
2607
|
-
while (i < code.length) {
|
|
2608
|
-
const char = code[i];
|
|
2609
|
-
if (char === "{") {
|
|
2610
|
-
const endIdx = code.indexOf("}", i);
|
|
2611
|
-
if (endIdx === -1) {
|
|
2612
|
-
result += escapeHtml(char);
|
|
2613
|
-
i++;
|
|
2614
|
-
continue;
|
|
2615
|
-
}
|
|
2616
|
-
const parsed = parseAnnotation(code.slice(i + 1, endIdx), prefixes);
|
|
2617
|
-
result += `<span class="mdld-annotation"><span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">{</span>`;
|
|
2618
|
-
result += parsed;
|
|
2619
|
-
result += `<span style="color: ${TOKEN_COLORS.annotation}; opacity: 0.75">}</span></span>`;
|
|
2620
|
-
i = endIdx + 1;
|
|
2621
|
-
continue;
|
|
2622
|
-
}
|
|
2623
|
-
if (char === "[") {
|
|
2624
|
-
const endBracket = code.indexOf("]", i);
|
|
2625
|
-
if (endBracket === -1) {
|
|
2626
|
-
result += escapeHtml(char);
|
|
2627
|
-
i++;
|
|
2628
|
-
continue;
|
|
2629
|
-
}
|
|
2630
|
-
const prefixName = code.slice(i + 1, endBracket);
|
|
2631
|
-
let j = endBracket + 1;
|
|
2632
|
-
while (j < code.length && /\s/.test(code[j])) j++;
|
|
2633
|
-
if (j < code.length && code[j] === "<") {
|
|
2634
|
-
const iriEnd = code.indexOf(">", j);
|
|
2635
|
-
if (iriEnd !== -1) {
|
|
2636
|
-
const iriContent = code.slice(j + 1, iriEnd);
|
|
2637
|
-
const prefixColor = prefixes.has(prefixName) ? getIRIColor(prefixes.get(prefixName)) : TOKEN_COLORS.text;
|
|
2638
|
-
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>`;
|
|
2639
|
-
i = iriEnd + 1;
|
|
2640
|
-
continue;
|
|
2641
|
-
}
|
|
2642
|
-
}
|
|
2643
|
-
const content = code.slice(i + 1, endBracket);
|
|
2644
|
-
const bracketColor = hasFollowingAnnotation(code, endBracket + 1) ? TOKEN_COLORS.marker : TOKEN_COLORS.value;
|
|
2645
|
-
const escapedContent = escapeHtml(content);
|
|
2646
|
-
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>`;
|
|
2647
|
-
i = endBracket + 1;
|
|
2648
|
-
continue;
|
|
2649
|
-
}
|
|
2650
|
-
if (char === "*" || char === "_" || char === "`") {
|
|
2651
|
-
const processed = processMarkdownFormattingAtPosition(code, i);
|
|
2652
|
-
if (processed) {
|
|
2653
|
-
result += processed.html;
|
|
2654
|
-
i = processed.nextIndex;
|
|
2655
|
-
continue;
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
if (char === "#") {
|
|
2659
|
-
let hashCount = 0;
|
|
2660
|
-
let j = i;
|
|
2661
|
-
while (j < code.length && code[j] === "#") {
|
|
2662
|
-
hashCount++;
|
|
2663
|
-
j++;
|
|
2664
|
-
}
|
|
2665
|
-
if (hashCount <= 6 && (j >= code.length || code[j] === " " || code[j] === " ")) {
|
|
2666
|
-
const lineEnd = code.indexOf("\n", j);
|
|
2667
|
-
const endIdx = lineEnd === -1 ? code.length : lineEnd;
|
|
2668
|
-
const headerText = code.slice(j, endIdx).trim();
|
|
2669
|
-
let processedHeaderText = "";
|
|
2670
|
-
let lastIndex = 0;
|
|
2671
|
-
let annotationStart = headerText.indexOf("{");
|
|
2672
|
-
while (annotationStart !== -1) {
|
|
2673
|
-
const annotationEnd = headerText.indexOf("}", annotationStart);
|
|
2674
|
-
if (annotationEnd === -1) break;
|
|
2675
|
-
const beforeText = headerText.slice(lastIndex, annotationStart);
|
|
2676
|
-
processedHeaderText += escapeHtml(beforeText);
|
|
2677
|
-
const parsedAnnotation = parseAnnotation(headerText.slice(annotationStart + 1, annotationEnd), prefixes);
|
|
2678
|
-
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>`;
|
|
2679
|
-
lastIndex = annotationEnd + 1;
|
|
2680
|
-
annotationStart = headerText.indexOf("{", lastIndex);
|
|
2681
|
-
}
|
|
2682
|
-
processedHeaderText += escapeHtml(headerText.slice(lastIndex));
|
|
2683
|
-
const hasAnnotation = headerText.includes("{") && headerText.includes("}");
|
|
2684
|
-
const headingTag = `h${hashCount}`;
|
|
2685
|
-
const headingStyle = `margin: 0; font-weight: 600;`;
|
|
2686
|
-
const hashes = "#".repeat(hashCount);
|
|
2687
|
-
const hashColor = hasAnnotation ? `color: ${TOKEN_COLORS.marker}; opacity: 0.8` : "";
|
|
2688
|
-
result += `<${headingTag} style="${headingStyle}"><span style="${hashColor}">${hashes}</span> ${processedHeaderText}</${headingTag}>`;
|
|
2689
|
-
i = endIdx;
|
|
2690
|
-
continue;
|
|
2691
|
-
}
|
|
2692
|
-
}
|
|
2693
|
-
if (char === "-" || char === "*") {
|
|
2694
|
-
const prevChar = i > 0 ? code[i - 1] : "\n";
|
|
2695
|
-
if (prevChar === "\n" || prevChar === "\r" || prevChar === " " || prevChar === " ") {
|
|
2696
|
-
if (code[i + 1] === " ") {
|
|
2697
|
-
const markerColor = hasFollowingAnnotation(code, i + 2) ? `color: ${TOKEN_COLORS.marker}; opacity: 0.85` : "";
|
|
2698
|
-
result += `<span style="${markerColor}">${escapeHtml(char)}</span>`;
|
|
2699
|
-
i++;
|
|
2700
|
-
continue;
|
|
2701
|
-
}
|
|
2702
|
-
}
|
|
2703
|
-
}
|
|
2704
|
-
if (char === ">") {
|
|
2705
|
-
const prevChar = i > 0 ? code[i - 1] : "\n";
|
|
2706
|
-
if (prevChar === "\n" || prevChar === "\r" || prevChar === " " || prevChar === " ") {
|
|
2707
|
-
if (code[i + 1] === " ") {
|
|
2708
|
-
const markerColor = hasFollowingAnnotation(code, i + 2) ? `color: ${TOKEN_COLORS.marker}; opacity: 0.85` : "";
|
|
2709
|
-
result += `<span style="${markerColor}">${escapeHtml(char)}</span>`;
|
|
2710
|
-
i++;
|
|
2711
|
-
continue;
|
|
2712
|
-
}
|
|
2713
|
-
}
|
|
2714
|
-
}
|
|
2715
|
-
result += escapeHtml(char);
|
|
2716
|
-
i++;
|
|
2717
|
-
}
|
|
2718
|
-
return result;
|
|
2719
|
-
}
|
|
2720
|
-
//#endregion
|
|
2721
2195
|
//#region src/index.js
|
|
2722
2196
|
/**
|
|
2723
2197
|
* Update the value of a quad in MDLD text and return the updated text
|
|
@@ -2735,4 +2209,4 @@ function updateValue({ text, quad, value, origin }) {
|
|
|
2735
2209
|
return text.substring(0, location.valueRange.start) + value + text.substring(location.valueRange.end);
|
|
2736
2210
|
}
|
|
2737
2211
|
//#endregion
|
|
2738
|
-
export { DEFAULT_CONTEXT, DataFactory,
|
|
2212
|
+
export { DEFAULT_CONTEXT, DataFactory, expandIRI, generate, generateNode, hash, locate, merge, parse, parseSemanticBlock, 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.1",
|
|
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 && 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/"
|
|
41
|
+
"build": "vite build && vite build --config vite.config.lib.js && vite build --config vite.config.full.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": {
|