mdld-parse 1.1.1 → 1.1.2
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 +2 -0
- package/dist/crawl.js +1 -2
- package/dist/mdld-parse.js +922 -20
- package/dist/quad-graph.js +132 -23
- package/package.json +13 -13
package/dist/mdld-parse.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/constants.js
|
|
2
2
|
/**
|
|
3
3
|
* Shared utilities for MD-LD Parser and Renderer
|
|
4
4
|
* Ensures DRY code and consistent CommonMark processing
|
|
@@ -19,8 +19,7 @@ var XSD_BOOLEAN = "http://www.w3.org/2001/XMLSchema#boolean";
|
|
|
19
19
|
var XSD_INTEGER = "http://www.w3.org/2001/XMLSchema#integer";
|
|
20
20
|
var XSD_DOUBLE = "http://www.w3.org/2001/XMLSchema#double";
|
|
21
21
|
var URL_REGEX = /^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file|urn:uuid):/;
|
|
22
|
-
|
|
23
|
-
//#region src/utils.js
|
|
22
|
+
// src/utils.js
|
|
24
23
|
var VALID_URI_SCHEMES = /^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file):/;
|
|
25
24
|
var Term = class {
|
|
26
25
|
constructor(id) {
|
|
@@ -431,8 +430,7 @@ function createLiteral(value, datatype, language, context, dataFactory) {
|
|
|
431
430
|
if (language) return dataFactory.literal(value, language);
|
|
432
431
|
return dataFactory.literal(value);
|
|
433
432
|
}
|
|
434
|
-
|
|
435
|
-
//#region src/tokenizers.js
|
|
433
|
+
// src/tokenizers.js
|
|
436
434
|
/**
|
|
437
435
|
* Character-based Tokenizers for MDLD
|
|
438
436
|
*
|
|
@@ -594,6 +592,113 @@ function detectStandaloneSubject(line) {
|
|
|
594
592
|
if (pos < line.length) return null;
|
|
595
593
|
return { content };
|
|
596
594
|
}
|
|
595
|
+
var HTML_BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
596
|
+
"address",
|
|
597
|
+
"article",
|
|
598
|
+
"aside",
|
|
599
|
+
"base",
|
|
600
|
+
"basefont",
|
|
601
|
+
"blockquote",
|
|
602
|
+
"body",
|
|
603
|
+
"caption",
|
|
604
|
+
"center",
|
|
605
|
+
"col",
|
|
606
|
+
"colgroup",
|
|
607
|
+
"dd",
|
|
608
|
+
"details",
|
|
609
|
+
"dialog",
|
|
610
|
+
"dir",
|
|
611
|
+
"div",
|
|
612
|
+
"dl",
|
|
613
|
+
"dt",
|
|
614
|
+
"fieldset",
|
|
615
|
+
"figcaption",
|
|
616
|
+
"figure",
|
|
617
|
+
"footer",
|
|
618
|
+
"form",
|
|
619
|
+
"frame",
|
|
620
|
+
"frameset",
|
|
621
|
+
"h1",
|
|
622
|
+
"h2",
|
|
623
|
+
"h3",
|
|
624
|
+
"h4",
|
|
625
|
+
"h5",
|
|
626
|
+
"h6",
|
|
627
|
+
"head",
|
|
628
|
+
"header",
|
|
629
|
+
"hr",
|
|
630
|
+
"html",
|
|
631
|
+
"iframe",
|
|
632
|
+
"legend",
|
|
633
|
+
"li",
|
|
634
|
+
"link",
|
|
635
|
+
"main",
|
|
636
|
+
"menu",
|
|
637
|
+
"menuitem",
|
|
638
|
+
"nav",
|
|
639
|
+
"noframes",
|
|
640
|
+
"ol",
|
|
641
|
+
"optgroup",
|
|
642
|
+
"option",
|
|
643
|
+
"p",
|
|
644
|
+
"param",
|
|
645
|
+
"search",
|
|
646
|
+
"section",
|
|
647
|
+
"summary",
|
|
648
|
+
"table",
|
|
649
|
+
"tbody",
|
|
650
|
+
"td",
|
|
651
|
+
"tfoot",
|
|
652
|
+
"th",
|
|
653
|
+
"thead",
|
|
654
|
+
"title",
|
|
655
|
+
"tr",
|
|
656
|
+
"track",
|
|
657
|
+
"ul"
|
|
658
|
+
]);
|
|
659
|
+
var HTML_RAW_TEXT_TAGS = /* @__PURE__ */ new Set([
|
|
660
|
+
"script",
|
|
661
|
+
"pre",
|
|
662
|
+
"style",
|
|
663
|
+
"textarea"
|
|
664
|
+
]);
|
|
665
|
+
/**
|
|
666
|
+
* Detect indented code block line: 4+ spaces or a tab (CommonMark).
|
|
667
|
+
* Cannot interrupt a paragraph — callers must only check at block start.
|
|
668
|
+
*/
|
|
669
|
+
function detectIndentedCode(line) {
|
|
670
|
+
if (line[0] === " ") return { indent: " " };
|
|
671
|
+
if (line[0] === " " && line[1] === " " && line[2] === " " && line[3] === " ") return { indent: " " };
|
|
672
|
+
return null;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Detect an HTML block start (CommonMark HTML blocks).
|
|
676
|
+
* Returns { tag, closeTag, interrupt } or null.
|
|
677
|
+
* - interrupt: whether the block may interrupt a paragraph (type 7 cannot).
|
|
678
|
+
* - closeTag: if set, the block ends on the line containing this string;
|
|
679
|
+
* otherwise it ends at the first blank line.
|
|
680
|
+
*/
|
|
681
|
+
function detectHtmlBlock(line, interruptOnly = false) {
|
|
682
|
+
const trimmed = line.trim();
|
|
683
|
+
if (trimmed[0] !== "<") return null;
|
|
684
|
+
const m = trimmed.match(/^<(\/?)([A-Za-z][A-Za-z0-9-]*)((?:"[^"]*"|'[^']*'|[^>"'])*)>/);
|
|
685
|
+
if (!m) return null;
|
|
686
|
+
const tag = m[2].toLowerCase();
|
|
687
|
+
const closing = m[1] === "/";
|
|
688
|
+
let interrupt = true;
|
|
689
|
+
let closeTag = null;
|
|
690
|
+
if (!closing && HTML_RAW_TEXT_TAGS.has(tag)) closeTag = `</${tag}`;
|
|
691
|
+
else if (!HTML_BLOCK_TAGS.has(tag)) {
|
|
692
|
+
if (closing || m[0] !== trimmed) return null;
|
|
693
|
+
interrupt = false;
|
|
694
|
+
}
|
|
695
|
+
if (interruptOnly && !interrupt) return null;
|
|
696
|
+
return {
|
|
697
|
+
tag,
|
|
698
|
+
closeTag,
|
|
699
|
+
interrupt
|
|
700
|
+
};
|
|
701
|
+
}
|
|
597
702
|
/**
|
|
598
703
|
* Find matching closing bracket, accounting for nesting
|
|
599
704
|
*/
|
|
@@ -913,8 +1018,7 @@ function scanInlineCarriers(text, baseOffset = 0) {
|
|
|
913
1018
|
}
|
|
914
1019
|
return carriers;
|
|
915
1020
|
}
|
|
916
|
-
|
|
917
|
-
//#region src/shared.js
|
|
1021
|
+
// src/shared.js
|
|
918
1022
|
var FENCE_CLOSE_PATTERNS = /* @__PURE__ */ new Map();
|
|
919
1023
|
function getFenceClosePattern(fenceChar) {
|
|
920
1024
|
if (!FENCE_CLOSE_PATTERNS.has(fenceChar)) FENCE_CLOSE_PATTERNS.set(fenceChar, new RegExp(`^(${fenceChar}{3,})`));
|
|
@@ -1016,6 +1120,10 @@ function resolveObject(sem, state) {
|
|
|
1016
1120
|
if (sem.object.startsWith("#")) return resolveFragment(sem.object.substring(1), state.currentSubject, state.df);
|
|
1017
1121
|
return state.df.namedNode(expandIRI(sem.object, state.ctx));
|
|
1018
1122
|
}
|
|
1123
|
+
function escapeHtml(text) {
|
|
1124
|
+
if (!text) return "";
|
|
1125
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1126
|
+
}
|
|
1019
1127
|
function isLiteral(term) {
|
|
1020
1128
|
return term?.termType === "Literal";
|
|
1021
1129
|
}
|
|
@@ -1136,8 +1244,7 @@ function generateRetractionText(quad, context) {
|
|
|
1136
1244
|
return `[${value}] {${annotation}}\n`;
|
|
1137
1245
|
}
|
|
1138
1246
|
}
|
|
1139
|
-
|
|
1140
|
-
//#region src/parse.js
|
|
1247
|
+
// src/parse.js
|
|
1141
1248
|
function parse(firstArg, secondArg = {}) {
|
|
1142
1249
|
const isNamedParams = typeof firstArg === "object" && firstArg !== null && "text" in firstArg;
|
|
1143
1250
|
const text = isNamedParams ? firstArg.text : firstArg;
|
|
@@ -1841,8 +1948,7 @@ var TOKEN_PROCESSORS = {
|
|
|
1841
1948
|
list: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
|
|
1842
1949
|
standalone: (token, state) => processStandaloneSubject(token, state)
|
|
1843
1950
|
};
|
|
1844
|
-
|
|
1845
|
-
//#region src/merge.js
|
|
1951
|
+
// src/merge.js
|
|
1846
1952
|
/**
|
|
1847
1953
|
* Creates a unique key for quad identity matching - using shared utility
|
|
1848
1954
|
* @param {Quad} quad
|
|
@@ -1949,8 +2055,7 @@ function merge(docs, options = {}) {
|
|
|
1949
2055
|
primary: primaryObjects
|
|
1950
2056
|
};
|
|
1951
2057
|
}
|
|
1952
|
-
|
|
1953
|
-
//#region src/generate.js
|
|
2058
|
+
// src/generate.js
|
|
1954
2059
|
var iriCache = /* @__PURE__ */ new Map();
|
|
1955
2060
|
var CACHE_SIZE_LIMIT = 1e3;
|
|
1956
2061
|
function getCachedShortIRI(iri, context) {
|
|
@@ -2034,9 +2139,15 @@ function normalizeAndSortQuads(quads) {
|
|
|
2034
2139
|
if (sComp !== 0) return sComp;
|
|
2035
2140
|
const pComp = a.predicate.value.localeCompare(b.predicate.value);
|
|
2036
2141
|
if (pComp !== 0) return pComp;
|
|
2037
|
-
const
|
|
2038
|
-
|
|
2039
|
-
|
|
2142
|
+
const oComp = a.object.value.localeCompare(b.object.value);
|
|
2143
|
+
if (oComp !== 0) return oComp;
|
|
2144
|
+
const tComp = String(a.object.termType).localeCompare(String(b.object.termType));
|
|
2145
|
+
if (tComp !== 0) return tComp;
|
|
2146
|
+
const dtA = a.object.datatype?.value ?? a.object.datatype ?? "";
|
|
2147
|
+
const dtB = b.object.datatype?.value ?? b.object.datatype ?? "";
|
|
2148
|
+
const dtComp = String(dtA).localeCompare(String(dtB));
|
|
2149
|
+
if (dtComp !== 0) return dtComp;
|
|
2150
|
+
return String(a.object.language ?? "").localeCompare(String(b.object.language ?? ""));
|
|
2040
2151
|
});
|
|
2041
2152
|
}
|
|
2042
2153
|
function groupQuadsBySubject(quads) {
|
|
@@ -2191,8 +2302,800 @@ function buildLabelLookup(subjectGroups, lang = null) {
|
|
|
2191
2302
|
}
|
|
2192
2303
|
return labelLookup;
|
|
2193
2304
|
}
|
|
2194
|
-
|
|
2195
|
-
|
|
2305
|
+
// src/render.js
|
|
2306
|
+
function detectHr(line) {
|
|
2307
|
+
return /^(\s*[-*_]\s*){3,}$/.test(line);
|
|
2308
|
+
}
|
|
2309
|
+
function isExternalHref(href) {
|
|
2310
|
+
return /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(href);
|
|
2311
|
+
}
|
|
2312
|
+
function isBlockStart(line) {
|
|
2313
|
+
return !!(detectFence(line.trim()) || detectPrefix(line) || detectHeading(line) || detectList(line) || detectBlockquote(line) || detectHtmlBlock(line) || detectHr(line) || detectStandaloneSubject(line) || detectIndentedCode(line));
|
|
2314
|
+
}
|
|
2315
|
+
function isLazyParagraphContent(content) {
|
|
2316
|
+
const t = content.trim();
|
|
2317
|
+
if (t === "") return false;
|
|
2318
|
+
return !(detectFence(t) || detectHeading(t) || detectHtmlBlock(t) || detectHr(t) || detectStandaloneSubject(t) || detectPrefix(t));
|
|
2319
|
+
}
|
|
2320
|
+
var INLINE_HTML_RE = /<\/?[A-Za-z][A-Za-z0-9-]*(?:\s[^<>]*)?\/?>/g;
|
|
2321
|
+
var ASCII_PUNCT = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
2322
|
+
var NAMED_ENTITIES = Object.assign(Object.create(null), {
|
|
2323
|
+
amp: "&",
|
|
2324
|
+
lt: "<",
|
|
2325
|
+
gt: ">",
|
|
2326
|
+
quot: "\"",
|
|
2327
|
+
apos: "'",
|
|
2328
|
+
nbsp: "\xA0",
|
|
2329
|
+
copy: "©",
|
|
2330
|
+
reg: "®",
|
|
2331
|
+
trade: "™",
|
|
2332
|
+
hellip: "…",
|
|
2333
|
+
mdash: "—",
|
|
2334
|
+
ndash: "–",
|
|
2335
|
+
lsquo: "‘",
|
|
2336
|
+
rsquo: "’",
|
|
2337
|
+
ldquo: "“",
|
|
2338
|
+
rdquo: "”",
|
|
2339
|
+
laquo: "«",
|
|
2340
|
+
raquo: "»",
|
|
2341
|
+
times: "×",
|
|
2342
|
+
divide: "÷",
|
|
2343
|
+
plusmn: "±",
|
|
2344
|
+
deg: "°",
|
|
2345
|
+
micro: "µ",
|
|
2346
|
+
para: "¶",
|
|
2347
|
+
sect: "§",
|
|
2348
|
+
middot: "·",
|
|
2349
|
+
bull: "•",
|
|
2350
|
+
dagger: "†",
|
|
2351
|
+
Dagger: "‡",
|
|
2352
|
+
permil: "‰",
|
|
2353
|
+
prime: "′",
|
|
2354
|
+
Prime: "″",
|
|
2355
|
+
euro: "€",
|
|
2356
|
+
pound: "£",
|
|
2357
|
+
yen: "¥",
|
|
2358
|
+
cent: "¢",
|
|
2359
|
+
curren: "¤",
|
|
2360
|
+
brvbar: "¦",
|
|
2361
|
+
uml: "¨",
|
|
2362
|
+
ordf: "ª",
|
|
2363
|
+
ordm: "º",
|
|
2364
|
+
iexcl: "¡",
|
|
2365
|
+
iquest: "¿",
|
|
2366
|
+
szlig: "ß",
|
|
2367
|
+
shy: "",
|
|
2368
|
+
ensp: " ",
|
|
2369
|
+
emsp: " ",
|
|
2370
|
+
thinsp: " ",
|
|
2371
|
+
zwnj: "",
|
|
2372
|
+
zwj: "",
|
|
2373
|
+
lrm: "",
|
|
2374
|
+
rlm: "",
|
|
2375
|
+
oline: "‾",
|
|
2376
|
+
frasl: "⁄",
|
|
2377
|
+
weierp: "℘",
|
|
2378
|
+
image: "ℑ",
|
|
2379
|
+
real: "ℜ",
|
|
2380
|
+
alefsym: "ℵ",
|
|
2381
|
+
larr: "←",
|
|
2382
|
+
uarr: "↑",
|
|
2383
|
+
rarr: "→",
|
|
2384
|
+
darr: "↓",
|
|
2385
|
+
harr: "↔",
|
|
2386
|
+
crarr: "↵",
|
|
2387
|
+
lArr: "⇐",
|
|
2388
|
+
uArr: "⇑",
|
|
2389
|
+
rArr: "⇒",
|
|
2390
|
+
dArr: "⇓",
|
|
2391
|
+
hArr: "⇔",
|
|
2392
|
+
forall: "∀",
|
|
2393
|
+
part: "∂",
|
|
2394
|
+
exist: "∃",
|
|
2395
|
+
empty: "∅",
|
|
2396
|
+
nabla: "∇",
|
|
2397
|
+
isin: "∈",
|
|
2398
|
+
notin: "∉",
|
|
2399
|
+
ni: "∋",
|
|
2400
|
+
prod: "∏",
|
|
2401
|
+
sum: "∑",
|
|
2402
|
+
minus: "−",
|
|
2403
|
+
lowast: "∗",
|
|
2404
|
+
radic: "√",
|
|
2405
|
+
prop: "∝",
|
|
2406
|
+
infin: "∞",
|
|
2407
|
+
ang: "∠",
|
|
2408
|
+
and: "∧",
|
|
2409
|
+
or: "∨",
|
|
2410
|
+
cap: "∩",
|
|
2411
|
+
cup: "∪",
|
|
2412
|
+
int: "∫",
|
|
2413
|
+
there4: "∴",
|
|
2414
|
+
sim: "∼",
|
|
2415
|
+
cong: "≅",
|
|
2416
|
+
asymp: "≈",
|
|
2417
|
+
ne: "≠",
|
|
2418
|
+
equiv: "≡",
|
|
2419
|
+
le: "≤",
|
|
2420
|
+
ge: "≥",
|
|
2421
|
+
sub: "⊂",
|
|
2422
|
+
sup: "⊃",
|
|
2423
|
+
nsub: "⊄",
|
|
2424
|
+
sube: "⊆",
|
|
2425
|
+
supe: "⊇",
|
|
2426
|
+
oplus: "⊕",
|
|
2427
|
+
otimes: "⊗",
|
|
2428
|
+
perp: "⊥",
|
|
2429
|
+
sdot: "⋅",
|
|
2430
|
+
lceil: "⌈",
|
|
2431
|
+
rceil: "⌉",
|
|
2432
|
+
lfloor: "⌊",
|
|
2433
|
+
rfloor: "⌋",
|
|
2434
|
+
lang: "〈",
|
|
2435
|
+
rang: "〉",
|
|
2436
|
+
loz: "◊",
|
|
2437
|
+
spades: "♠",
|
|
2438
|
+
clubs: "♣",
|
|
2439
|
+
hearts: "♥",
|
|
2440
|
+
diams: "♦"
|
|
2441
|
+
});
|
|
2442
|
+
function decodeNumericEntity(body) {
|
|
2443
|
+
let digits = body.slice(1);
|
|
2444
|
+
let radix = 10;
|
|
2445
|
+
if (digits[0] === "x" || digits[0] === "X") {
|
|
2446
|
+
radix = 16;
|
|
2447
|
+
digits = digits.slice(1);
|
|
2448
|
+
}
|
|
2449
|
+
if (digits === "") return null;
|
|
2450
|
+
for (let k = 0; k < digits.length; k++) {
|
|
2451
|
+
const c = digits[k];
|
|
2452
|
+
if (!(radix === 10 ? c >= "0" && c <= "9" : c >= "0" && c <= "9" || c >= "a" && c <= "f" || c >= "A" && c <= "F")) return null;
|
|
2453
|
+
}
|
|
2454
|
+
const cp = parseInt(digits, radix);
|
|
2455
|
+
if (cp === 0 || cp > 1114111 || cp >= 55296 && cp <= 57343) return "�";
|
|
2456
|
+
try {
|
|
2457
|
+
return String.fromCodePoint(cp);
|
|
2458
|
+
} catch {
|
|
2459
|
+
return "�";
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
function decodeInlineText(s) {
|
|
2463
|
+
let out = "";
|
|
2464
|
+
let i = 0;
|
|
2465
|
+
while (i < s.length) {
|
|
2466
|
+
const ch = s[i];
|
|
2467
|
+
if (ch === "\\" && i + 1 < s.length && ASCII_PUNCT.indexOf(s[i + 1]) !== -1) {
|
|
2468
|
+
out += s[i + 1];
|
|
2469
|
+
i += 2;
|
|
2470
|
+
continue;
|
|
2471
|
+
}
|
|
2472
|
+
if (ch === "&") {
|
|
2473
|
+
const semi = s.indexOf(";", i + 1);
|
|
2474
|
+
if (semi !== -1 && semi - i <= 33) {
|
|
2475
|
+
const body = s.slice(i + 1, semi);
|
|
2476
|
+
const decoded = body[0] === "#" ? decodeNumericEntity(body) : Object.prototype.hasOwnProperty.call(NAMED_ENTITIES, body) ? NAMED_ENTITIES[body] : null;
|
|
2477
|
+
if (decoded !== null) {
|
|
2478
|
+
out += decoded;
|
|
2479
|
+
i = semi + 1;
|
|
2480
|
+
continue;
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
out += "&";
|
|
2484
|
+
i++;
|
|
2485
|
+
continue;
|
|
2486
|
+
}
|
|
2487
|
+
out += ch;
|
|
2488
|
+
i++;
|
|
2489
|
+
}
|
|
2490
|
+
return out;
|
|
2491
|
+
}
|
|
2492
|
+
function isEscapedAt(s, pos) {
|
|
2493
|
+
let n = 0;
|
|
2494
|
+
let j = pos - 1;
|
|
2495
|
+
while (j >= 0 && s[j] === "\\") {
|
|
2496
|
+
n++;
|
|
2497
|
+
j--;
|
|
2498
|
+
}
|
|
2499
|
+
return n % 2 === 1;
|
|
2500
|
+
}
|
|
2501
|
+
function applyHardBreaks(escaped) {
|
|
2502
|
+
let out = "";
|
|
2503
|
+
let i = 0;
|
|
2504
|
+
while (i < escaped.length) {
|
|
2505
|
+
const nl = escaped.indexOf("\n", i);
|
|
2506
|
+
if (nl === -1) {
|
|
2507
|
+
out += escaped.slice(i);
|
|
2508
|
+
break;
|
|
2509
|
+
}
|
|
2510
|
+
let contentEnd = nl;
|
|
2511
|
+
let hard = false;
|
|
2512
|
+
if (escaped[nl - 1] === "\\" && escaped[nl - 2] !== "\\") {
|
|
2513
|
+
contentEnd = nl - 1;
|
|
2514
|
+
hard = true;
|
|
2515
|
+
} else {
|
|
2516
|
+
let j = nl;
|
|
2517
|
+
while (j > i && escaped[j - 1] === " ") j--;
|
|
2518
|
+
if (nl - j >= 2) {
|
|
2519
|
+
contentEnd = j;
|
|
2520
|
+
hard = true;
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
out += escaped.slice(i, contentEnd) + (hard ? "<br />\n" : "\n");
|
|
2524
|
+
i = nl + 1;
|
|
2525
|
+
}
|
|
2526
|
+
return out;
|
|
2527
|
+
}
|
|
2528
|
+
function escapeTextPreserveHtml(s) {
|
|
2529
|
+
let out = "";
|
|
2530
|
+
let last = 0;
|
|
2531
|
+
let m;
|
|
2532
|
+
INLINE_HTML_RE.lastIndex = 0;
|
|
2533
|
+
while (m = INLINE_HTML_RE.exec(s)) {
|
|
2534
|
+
if (isEscapedAt(s, m.index)) {
|
|
2535
|
+
INLINE_HTML_RE.lastIndex = m.index + 1;
|
|
2536
|
+
continue;
|
|
2537
|
+
}
|
|
2538
|
+
out += applyHardBreaks(escapeHtml(decodeInlineText(s.slice(last, m.index)))) + m[0];
|
|
2539
|
+
last = m.index + m[0].length;
|
|
2540
|
+
}
|
|
2541
|
+
return out + applyHardBreaks(escapeHtml(decodeInlineText(s.slice(last))));
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* MD-LD UI Renderer
|
|
2545
|
+
*
|
|
2546
|
+
* Generates clean, semantic HTML for presentation.
|
|
2547
|
+
* Preserves the raw MD-LD annotation in `data-annotation` for client-side
|
|
2548
|
+
* JS/CSS targeting, and resolves the primary IRI into `data-iri`.
|
|
2549
|
+
*/
|
|
2550
|
+
function render(src, options = {}) {
|
|
2551
|
+
const lines = src.split("\n");
|
|
2552
|
+
const context = buildRenderContext(lines, options.context);
|
|
2553
|
+
function safeExpand(term) {
|
|
2554
|
+
if (!term || term === "RESET") return null;
|
|
2555
|
+
if (term.startsWith("=#") || term.startsWith("#")) return term;
|
|
2556
|
+
return expandIRI(term, context);
|
|
2557
|
+
}
|
|
2558
|
+
function getAttrs(rawAnnotation, parsedSem, isLink = false, fallbackHref = null) {
|
|
2559
|
+
const attrs = [];
|
|
2560
|
+
const classes = [];
|
|
2561
|
+
if (rawAnnotation) attrs.push(`data-annotation="${escapeHtml("{" + rawAnnotation + "}")}"`);
|
|
2562
|
+
let iri = null;
|
|
2563
|
+
if (parsedSem?.subject && parsedSem.subject !== "RESET") iri = safeExpand(parsedSem.subject);
|
|
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)}"`);
|
|
2567
|
+
if (parsedSem?.types?.some((t) => !t.remove)) classes.push("typed");
|
|
2568
|
+
if (parsedSem?.predicates?.some((p) => p.remove) || parsedSem?.types?.some((t) => t.remove)) classes.push("retracted");
|
|
2569
|
+
if (classes.length > 0) attrs.unshift(`class="${classes.join(" ")}"`);
|
|
2570
|
+
return attrs.join(" ");
|
|
2571
|
+
}
|
|
2572
|
+
function mergeClasses(baseClass, customAttrs) {
|
|
2573
|
+
const classMatch = customAttrs.match(/class="([^"]*)"/);
|
|
2574
|
+
let finalClass = baseClass;
|
|
2575
|
+
let remainingAttrs = customAttrs;
|
|
2576
|
+
if (classMatch) {
|
|
2577
|
+
finalClass += " " + classMatch[1];
|
|
2578
|
+
remainingAttrs = customAttrs.replace(/class="[^"]*"/, "").trim();
|
|
2579
|
+
}
|
|
2580
|
+
return `class="${finalClass}"${remainingAttrs ? " " + remainingAttrs : ""}`;
|
|
2581
|
+
}
|
|
2582
|
+
function renderInline(text) {
|
|
2583
|
+
text = text.replace(/<!--[\s\S]*?-->/g, "");
|
|
2584
|
+
const carriers = scanInlineCarriers(text, 0);
|
|
2585
|
+
if (carriers.length === 0) return escapeTextPreserveHtml(text);
|
|
2586
|
+
let out = "";
|
|
2587
|
+
let lastPos = 0;
|
|
2588
|
+
for (const c of carriers) {
|
|
2589
|
+
if (c.range[0] > lastPos) out += escapeTextPreserveHtml(text.slice(lastPos, c.range[0]));
|
|
2590
|
+
const sem = c.attrs ? parseSemanticBlock(c.attrs) : null;
|
|
2591
|
+
const raw = c.attrs || "";
|
|
2592
|
+
const isLink = c.type === "link";
|
|
2593
|
+
const fallbackHref = c.url || null;
|
|
2594
|
+
const titleAttr = c.title ? ` title="${escapeHtml(c.title)}"` : "";
|
|
2595
|
+
if (c.type === "image") {
|
|
2596
|
+
const alt = escapeHtml(c.text);
|
|
2597
|
+
const src = escapeHtml(c.url);
|
|
2598
|
+
const attrString = mergeClasses("mdld-image", getAttrs(raw, sem, false, null));
|
|
2599
|
+
out += `<img src="${src}" alt="${alt}" ${attrString}${titleAttr} />`;
|
|
2600
|
+
} else {
|
|
2601
|
+
let tag = "span";
|
|
2602
|
+
let baseClass = "mdld-bracket";
|
|
2603
|
+
if (c.type === "link") {
|
|
2604
|
+
tag = "a";
|
|
2605
|
+
baseClass = "mdld-link";
|
|
2606
|
+
} else if (c.type === "code") {
|
|
2607
|
+
tag = "code";
|
|
2608
|
+
baseClass = "mdld-code";
|
|
2609
|
+
} else if (c.type === "strong") {
|
|
2610
|
+
tag = "strong";
|
|
2611
|
+
baseClass = "mdld-bold";
|
|
2612
|
+
} else if (c.type === "emphasis") {
|
|
2613
|
+
tag = "em";
|
|
2614
|
+
baseClass = "mdld-italic";
|
|
2615
|
+
}
|
|
2616
|
+
const customAttrs = getAttrs(raw, sem, isLink, fallbackHref);
|
|
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);
|
|
2619
|
+
out += `<${tag} ${attrString}${titleAttr}>${inner}</${tag}>`;
|
|
2620
|
+
}
|
|
2621
|
+
lastPos = c.range[1];
|
|
2622
|
+
}
|
|
2623
|
+
if (lastPos < text.length) out += escapeTextPreserveHtml(text.slice(lastPos));
|
|
2624
|
+
return out;
|
|
2625
|
+
}
|
|
2626
|
+
const out = [];
|
|
2627
|
+
let i = 0;
|
|
2628
|
+
let codeBlock = null;
|
|
2629
|
+
let inComment = false;
|
|
2630
|
+
while (i < lines.length) {
|
|
2631
|
+
let line = lines[i];
|
|
2632
|
+
if (codeBlock) {
|
|
2633
|
+
if (new RegExp(`^${codeBlock.fenceChar}{${codeBlock.fenceLength},}\\s*$`).test(line.trim())) {
|
|
2634
|
+
const sem = codeBlock.attrs ? parseSemanticBlock(codeBlock.attrs) : null;
|
|
2635
|
+
const carrierText = codeBlock.content.join("\n");
|
|
2636
|
+
const customAttrs = getAttrs(codeBlock.attrs, sem);
|
|
2637
|
+
const attrString = mergeClasses(`mdld-codeblock language-${codeBlock.lang || "text"}`, customAttrs);
|
|
2638
|
+
out.push(`<pre><code ${attrString}>${escapeHtml(carrierText)}</code></pre>`);
|
|
2639
|
+
codeBlock = null;
|
|
2640
|
+
} else codeBlock.content.push(line);
|
|
2641
|
+
i++;
|
|
2642
|
+
continue;
|
|
2643
|
+
}
|
|
2644
|
+
if (inComment) {
|
|
2645
|
+
const endIdx = line.indexOf("-->");
|
|
2646
|
+
if (endIdx !== -1) {
|
|
2647
|
+
inComment = false;
|
|
2648
|
+
line = line.slice(endIdx + 3);
|
|
2649
|
+
} else {
|
|
2650
|
+
i++;
|
|
2651
|
+
continue;
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
let processedLine = "";
|
|
2655
|
+
let current = line;
|
|
2656
|
+
while (true) {
|
|
2657
|
+
const startIdx = current.indexOf("<!--");
|
|
2658
|
+
if (startIdx === -1) {
|
|
2659
|
+
processedLine += current;
|
|
2660
|
+
break;
|
|
2661
|
+
}
|
|
2662
|
+
const endIdx = current.indexOf("-->", startIdx);
|
|
2663
|
+
if (endIdx === -1) {
|
|
2664
|
+
inComment = true;
|
|
2665
|
+
processedLine += current.slice(0, startIdx);
|
|
2666
|
+
break;
|
|
2667
|
+
}
|
|
2668
|
+
processedLine += current.slice(0, startIdx);
|
|
2669
|
+
current = current.slice(endIdx + 3);
|
|
2670
|
+
}
|
|
2671
|
+
line = processedLine;
|
|
2672
|
+
if (line.trim() === "") {
|
|
2673
|
+
i++;
|
|
2674
|
+
continue;
|
|
2675
|
+
}
|
|
2676
|
+
const fence = detectFence(line.trim());
|
|
2677
|
+
if (fence) {
|
|
2678
|
+
codeBlock = {
|
|
2679
|
+
fenceChar: fence.fenceChar,
|
|
2680
|
+
fenceLength: fence.fenceLength,
|
|
2681
|
+
lang: fence.lang,
|
|
2682
|
+
attrs: fence.attrs,
|
|
2683
|
+
content: []
|
|
2684
|
+
};
|
|
2685
|
+
i++;
|
|
2686
|
+
continue;
|
|
2687
|
+
}
|
|
2688
|
+
const prefix = detectPrefix(line);
|
|
2689
|
+
if (prefix) {
|
|
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>`);
|
|
2692
|
+
i++;
|
|
2693
|
+
continue;
|
|
2694
|
+
}
|
|
2695
|
+
const heading = detectHeading(line);
|
|
2696
|
+
if (heading) {
|
|
2697
|
+
const sem = heading.attrs ? parseSemanticBlock(heading.attrs) : null;
|
|
2698
|
+
const attrString = mergeClasses("mdld-heading", getAttrs(heading.attrs, sem));
|
|
2699
|
+
out.push(`<h${heading.depth} ${attrString}>${renderInline(heading.content)}</h${heading.depth}>`);
|
|
2700
|
+
i++;
|
|
2701
|
+
continue;
|
|
2702
|
+
}
|
|
2703
|
+
const list = detectList(line);
|
|
2704
|
+
if (list) {
|
|
2705
|
+
const isOrdered = /^\s*\d+[.)]\s/.test(line);
|
|
2706
|
+
const listTag = isOrdered ? "ol" : "ul";
|
|
2707
|
+
const sameMarker = (m) => isOrdered ? m.marker[m.marker.length - 1] === list.marker[list.marker.length - 1] : m.marker === list.marker;
|
|
2708
|
+
const startNum = isOrdered ? parseInt(list.marker, 10) : 1;
|
|
2709
|
+
const startAttr = isOrdered && startNum !== 1 ? ` start="${startNum}"` : "";
|
|
2710
|
+
out.push(`<${listTag} class="mdld-list"${startAttr}>`);
|
|
2711
|
+
let currentListItemLines = [];
|
|
2712
|
+
let currentAttrs = null;
|
|
2713
|
+
const processListItem = (content, attrs) => {
|
|
2714
|
+
const attrString = mergeClasses("mdld-item", getAttrs(attrs, attrs ? parseSemanticBlock(attrs) : null));
|
|
2715
|
+
out.push(`<li ${attrString}>${renderInline(content.join("\n"))}</li>`);
|
|
2716
|
+
};
|
|
2717
|
+
while (i < lines.length) {
|
|
2718
|
+
const l = detectList(lines[i]);
|
|
2719
|
+
if (l) {
|
|
2720
|
+
const nextIsOrdered = /^\s*\d+[.)]\s/.test(lines[i]);
|
|
2721
|
+
if (currentListItemLines.length > 0 && (nextIsOrdered !== isOrdered || !sameMarker(l))) break;
|
|
2722
|
+
if (currentListItemLines.length > 0) {
|
|
2723
|
+
processListItem(currentListItemLines, currentAttrs);
|
|
2724
|
+
currentListItemLines = [];
|
|
2725
|
+
}
|
|
2726
|
+
currentListItemLines.push(l.content);
|
|
2727
|
+
currentAttrs = l.attrs;
|
|
2728
|
+
i++;
|
|
2729
|
+
} else if (lines[i].trim() === "") {
|
|
2730
|
+
let peek = i + 1;
|
|
2731
|
+
while (peek < lines.length && lines[peek].trim() === "") peek++;
|
|
2732
|
+
if (peek < lines.length) {
|
|
2733
|
+
const nextList = detectList(lines[peek]);
|
|
2734
|
+
if (nextList) {
|
|
2735
|
+
if (/^\s*\d+[.)]\s/.test(lines[peek]) === isOrdered && sameMarker(nextList)) {
|
|
2736
|
+
currentListItemLines.push("");
|
|
2737
|
+
i = peek;
|
|
2738
|
+
continue;
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
2741
|
+
if (/^\s{2,}/.test(lines[peek]) || /^\t/.test(lines[peek])) {
|
|
2742
|
+
currentListItemLines.push("");
|
|
2743
|
+
i = peek;
|
|
2744
|
+
continue;
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
break;
|
|
2748
|
+
} else if (/^\s{2,}/.test(lines[i]) || /^\t/.test(lines[i])) {
|
|
2749
|
+
currentListItemLines.push(lines[i].replace(/^(\s{2,}|\t)/, ""));
|
|
2750
|
+
i++;
|
|
2751
|
+
} else break;
|
|
2752
|
+
}
|
|
2753
|
+
if (currentListItemLines.length > 0) processListItem(currentListItemLines, currentAttrs);
|
|
2754
|
+
out.push(`</${listTag}>`);
|
|
2755
|
+
continue;
|
|
2756
|
+
}
|
|
2757
|
+
const blockquote = detectBlockquote(line);
|
|
2758
|
+
if (blockquote) {
|
|
2759
|
+
let quoteLines = [blockquote.content];
|
|
2760
|
+
i++;
|
|
2761
|
+
let lazyOk = isLazyParagraphContent(quoteLines[quoteLines.length - 1]);
|
|
2762
|
+
while (i < lines.length) {
|
|
2763
|
+
const bq = detectBlockquote(lines[i]);
|
|
2764
|
+
if (bq) {
|
|
2765
|
+
quoteLines.push(bq.attrs ? `${bq.content} {${bq.attrs}}` : bq.content);
|
|
2766
|
+
lazyOk = isLazyParagraphContent(bq.content);
|
|
2767
|
+
i++;
|
|
2768
|
+
continue;
|
|
2769
|
+
}
|
|
2770
|
+
if (!lazyOk || lines[i].trim() === "" || isBlockStart(lines[i])) break;
|
|
2771
|
+
quoteLines.push(lines[i]);
|
|
2772
|
+
lazyOk = true;
|
|
2773
|
+
i++;
|
|
2774
|
+
}
|
|
2775
|
+
const sem = blockquote.attrs ? parseSemanticBlock(blockquote.attrs) : null;
|
|
2776
|
+
const attrString = mergeClasses("mdld-quote", getAttrs(blockquote.attrs, sem));
|
|
2777
|
+
const innerHtml = render(quoteLines.join("\n"), { context });
|
|
2778
|
+
out.push(`<blockquote ${attrString}>${innerHtml}</blockquote>`);
|
|
2779
|
+
continue;
|
|
2780
|
+
}
|
|
2781
|
+
const standalone = detectStandaloneSubject(line);
|
|
2782
|
+
if (standalone) {
|
|
2783
|
+
const fakeSem = parseSemanticBlock(`=${standalone.content}`);
|
|
2784
|
+
const attrString = mergeClasses("mdld-standalone", getAttrs(`=${standalone.content}`, fakeSem));
|
|
2785
|
+
out.push(`<div ${attrString} style="display:none"></div>`);
|
|
2786
|
+
i++;
|
|
2787
|
+
continue;
|
|
2788
|
+
}
|
|
2789
|
+
if (detectHr(line)) {
|
|
2790
|
+
out.push(`<hr class="mdld-hr" />`);
|
|
2791
|
+
i++;
|
|
2792
|
+
continue;
|
|
2793
|
+
}
|
|
2794
|
+
const htmlBlock = detectHtmlBlock(line);
|
|
2795
|
+
if (htmlBlock) {
|
|
2796
|
+
const blockLines = [line];
|
|
2797
|
+
i++;
|
|
2798
|
+
if (htmlBlock.closeTag) {
|
|
2799
|
+
const needle = htmlBlock.closeTag.toLowerCase();
|
|
2800
|
+
while (i < lines.length) {
|
|
2801
|
+
blockLines.push(lines[i]);
|
|
2802
|
+
const done = lines[i].toLowerCase().includes(needle);
|
|
2803
|
+
i++;
|
|
2804
|
+
if (done) break;
|
|
2805
|
+
}
|
|
2806
|
+
} else while (i < lines.length && lines[i].trim() !== "") {
|
|
2807
|
+
blockLines.push(lines[i]);
|
|
2808
|
+
i++;
|
|
2809
|
+
}
|
|
2810
|
+
out.push(blockLines.join("\n"));
|
|
2811
|
+
continue;
|
|
2812
|
+
}
|
|
2813
|
+
if (detectIndentedCode(line)) {
|
|
2814
|
+
const codeLines = [];
|
|
2815
|
+
while (i < lines.length) {
|
|
2816
|
+
const l = lines[i];
|
|
2817
|
+
if (l.trim() === "") {
|
|
2818
|
+
codeLines.push("");
|
|
2819
|
+
i++;
|
|
2820
|
+
continue;
|
|
2821
|
+
}
|
|
2822
|
+
const ind = detectIndentedCode(l);
|
|
2823
|
+
if (!ind) break;
|
|
2824
|
+
codeLines.push(l.slice(ind.indent.length));
|
|
2825
|
+
i++;
|
|
2826
|
+
}
|
|
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>`);
|
|
2829
|
+
continue;
|
|
2830
|
+
}
|
|
2831
|
+
let paraLines = [line];
|
|
2832
|
+
i++;
|
|
2833
|
+
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]) && !detectHr(lines[i]) && !detectHtmlBlock(lines[i], true)) {
|
|
2834
|
+
paraLines.push(lines[i]);
|
|
2835
|
+
i++;
|
|
2836
|
+
}
|
|
2837
|
+
const paraText = paraLines.join("\n");
|
|
2838
|
+
out.push(`<p class="mdld-paragraph">${renderInline(paraText)}</p>`);
|
|
2839
|
+
}
|
|
2840
|
+
return out.join("\n");
|
|
2841
|
+
}
|
|
2842
|
+
function buildRenderContext(lines, userContext = {}) {
|
|
2843
|
+
const context = {
|
|
2844
|
+
...DEFAULT_CONTEXT,
|
|
2845
|
+
...userContext
|
|
2846
|
+
};
|
|
2847
|
+
for (const line of lines) {
|
|
2848
|
+
const prefix = detectPrefix(line);
|
|
2849
|
+
if (prefix) {
|
|
2850
|
+
let iri = prefix.iri;
|
|
2851
|
+
if (iri.includes(":")) {
|
|
2852
|
+
const colonIndex = iri.indexOf(":");
|
|
2853
|
+
const p = iri.substring(0, colonIndex);
|
|
2854
|
+
const ref = iri.substring(colonIndex + 1);
|
|
2855
|
+
if (context[p] && p !== "@vocab") iri = context[p] + ref;
|
|
2856
|
+
}
|
|
2857
|
+
context[prefix.prefix] = iri;
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
return context;
|
|
2861
|
+
}
|
|
2862
|
+
/**
|
|
2863
|
+
* Reconstruct MD-LD from rendered HTML.
|
|
2864
|
+
* Pure string scanning over our constrained output format.
|
|
2865
|
+
* Platform-agnostic: no DOMParser, no browser APIs required.
|
|
2866
|
+
*/
|
|
2867
|
+
var MDLD_BLOCK_START_RE = /^<(?:p class="mdld-paragraph"|div class="mdld-prefix"|div class="mdld-standalone"|pre><code class="mdld-codeblock|h[1-6] class="mdld-heading"|blockquote class="mdld-quote"|ul class="mdld-list"|ol class="mdld-list"|hr class="mdld-hr")/;
|
|
2868
|
+
function deconstruct(html) {
|
|
2869
|
+
const blocks = [];
|
|
2870
|
+
let pos = 0;
|
|
2871
|
+
while (pos < html.length) {
|
|
2872
|
+
while (pos < html.length && /\s/.test(html[pos])) pos++;
|
|
2873
|
+
if (pos >= html.length) break;
|
|
2874
|
+
const nextTagStart = html.indexOf("<", pos);
|
|
2875
|
+
if (nextTagStart === -1) break;
|
|
2876
|
+
const tagEnd = html.indexOf(">", nextTagStart);
|
|
2877
|
+
if (tagEnd === -1) break;
|
|
2878
|
+
const openTag = html.slice(nextTagStart, tagEnd + 1);
|
|
2879
|
+
if (openTag.startsWith("<p") && extractAttr(openTag, "class")?.includes("mdld-paragraph")) {
|
|
2880
|
+
const close = html.indexOf("</p>", pos);
|
|
2881
|
+
if (close === -1) break;
|
|
2882
|
+
const openEnd = html.indexOf(">", pos) + 1;
|
|
2883
|
+
const content = html.slice(openEnd, close);
|
|
2884
|
+
blocks.push(deconstructInline(content));
|
|
2885
|
+
pos = close + 4;
|
|
2886
|
+
} else if (openTag.startsWith("<div") && extractAttr(openTag, "class")?.includes("mdld-prefix")) {
|
|
2887
|
+
const end = html.indexOf("></div>", pos);
|
|
2888
|
+
if (end === -1) break;
|
|
2889
|
+
const tag = html.slice(pos, end + 7);
|
|
2890
|
+
const prefix = extractAttr(tag, "data-prefix");
|
|
2891
|
+
const raw = extractAttr(tag, "data-raw") || extractAttr(tag, "data-iri");
|
|
2892
|
+
blocks.push(`[${prefix}] <${raw}>`);
|
|
2893
|
+
pos = end + 7;
|
|
2894
|
+
} else if (openTag.startsWith("<div") && extractAttr(openTag, "class")?.includes("mdld-standalone")) {
|
|
2895
|
+
const end = html.indexOf("</div>", pos);
|
|
2896
|
+
if (end === -1) break;
|
|
2897
|
+
const ann = extractAttr(html.slice(pos, end + 6), "data-annotation");
|
|
2898
|
+
if (ann) blocks.push(ann);
|
|
2899
|
+
pos = end + 6;
|
|
2900
|
+
} else if (html.startsWith("<pre><code", pos)) {
|
|
2901
|
+
const preClose = html.indexOf(">", pos) + 1;
|
|
2902
|
+
const codeOpenEnd = html.indexOf(">", preClose) + 1;
|
|
2903
|
+
const codeTag = html.slice(preClose, codeOpenEnd);
|
|
2904
|
+
if (!extractAttr(codeTag, "class")?.includes("mdld-codeblock")) {
|
|
2905
|
+
pos = nextTagStart + 1;
|
|
2906
|
+
continue;
|
|
2907
|
+
}
|
|
2908
|
+
const closePre = html.indexOf("</code></pre>", pos);
|
|
2909
|
+
if (closePre === -1) break;
|
|
2910
|
+
const full = html.slice(pos, closePre + 13);
|
|
2911
|
+
const content = full.slice(codeOpenEnd - pos, full.length - 13);
|
|
2912
|
+
const langMatch = (extractAttr(codeTag, "class") || "").match(/language-(\w+)/);
|
|
2913
|
+
const lang = langMatch ? langMatch[1] : "";
|
|
2914
|
+
const ann = extractAttr(codeTag, "data-annotation");
|
|
2915
|
+
blocks.push(`\`\`\`${lang}${ann ? " " + ann : ""}\n${unescapeHtml(content)}\n\`\`\``);
|
|
2916
|
+
pos = closePre + 13;
|
|
2917
|
+
} else if (html[pos] === "<" && /^<h[1-6]/.test(html.slice(pos, pos + 4)) && extractAttr(openTag, "class")?.includes("mdld-heading")) {
|
|
2918
|
+
const level = html[pos + 2];
|
|
2919
|
+
const closeTag = `</h${level}>`;
|
|
2920
|
+
const close = html.indexOf(closeTag, pos);
|
|
2921
|
+
if (close === -1) break;
|
|
2922
|
+
const full = html.slice(pos, close + closeTag.length);
|
|
2923
|
+
const openEnd = full.indexOf(">") + 1;
|
|
2924
|
+
const openTag = full.slice(0, openEnd);
|
|
2925
|
+
const content = full.slice(openEnd, full.length - closeTag.length);
|
|
2926
|
+
const hashes = "#".repeat(parseInt(level));
|
|
2927
|
+
const ann = extractAttr(openTag, "data-annotation");
|
|
2928
|
+
blocks.push(`${hashes} ${deconstructInline(content)}${ann ? " " + ann : ""}`);
|
|
2929
|
+
pos = close + closeTag.length;
|
|
2930
|
+
} else if (html.startsWith("<blockquote", pos) && extractAttr(openTag, "class")?.includes("mdld-quote")) {
|
|
2931
|
+
const close = html.indexOf("</blockquote>", pos);
|
|
2932
|
+
if (close === -1) break;
|
|
2933
|
+
const full = html.slice(pos, close + 13);
|
|
2934
|
+
const openEnd = full.indexOf(">") + 1;
|
|
2935
|
+
const openTag = full.slice(0, openEnd);
|
|
2936
|
+
const content = full.slice(openEnd, full.length - 13);
|
|
2937
|
+
const ann = extractAttr(openTag, "data-annotation");
|
|
2938
|
+
const quoted = deconstruct(content).split("\n").map((l) => l.trim() ? `> ${l}` : ">").join("\n");
|
|
2939
|
+
blocks.push(ann ? `${quoted} ${ann}` : quoted);
|
|
2940
|
+
pos = close + 13;
|
|
2941
|
+
} else if ((openTag.startsWith("<ul") || openTag.startsWith("<ol")) && extractAttr(openTag, "class")?.includes("mdld-list")) {
|
|
2942
|
+
const isOrdered = openTag.startsWith("<ol");
|
|
2943
|
+
const closeTag = isOrdered ? "</ol>" : "</ul>";
|
|
2944
|
+
const close = html.indexOf(closeTag, pos);
|
|
2945
|
+
if (close === -1) break;
|
|
2946
|
+
const openEnd = html.indexOf(">", pos) + 1;
|
|
2947
|
+
const inner = html.slice(openEnd, close);
|
|
2948
|
+
const items = [];
|
|
2949
|
+
let itemPos = 0;
|
|
2950
|
+
let counter = isOrdered ? parseInt(extractAttr(openTag, "start") || "1", 10) || 1 : 1;
|
|
2951
|
+
while (itemPos < inner.length) {
|
|
2952
|
+
const liStart = inner.indexOf("<li", itemPos);
|
|
2953
|
+
if (liStart === -1) break;
|
|
2954
|
+
const liClose = inner.indexOf("</li>", liStart);
|
|
2955
|
+
if (liClose === -1) break;
|
|
2956
|
+
const liFull = inner.slice(liStart, liClose + 5);
|
|
2957
|
+
const openEnd = liFull.indexOf(">") + 1;
|
|
2958
|
+
const openTag = liFull.slice(0, openEnd);
|
|
2959
|
+
const liContent = liFull.slice(openEnd, liFull.length - 5);
|
|
2960
|
+
const ann = extractAttr(openTag, "data-annotation");
|
|
2961
|
+
const lines = deconstructInline(liContent).split("\n");
|
|
2962
|
+
const indent = isOrdered ? " " : " ";
|
|
2963
|
+
const indented = lines.map((l, idx) => idx === 0 ? l : `${indent}${l}`).join("\n");
|
|
2964
|
+
const bullet = isOrdered ? `${counter}. ` : "- ";
|
|
2965
|
+
items.push(`${bullet}${indented}${ann ? " " + ann : ""}`);
|
|
2966
|
+
counter++;
|
|
2967
|
+
itemPos = liClose + 5;
|
|
2968
|
+
}
|
|
2969
|
+
blocks.push(items.join("\n"));
|
|
2970
|
+
pos = close + closeTag.length;
|
|
2971
|
+
} else if (openTag.startsWith("<hr") && extractAttr(openTag, "class")?.includes("mdld-hr")) {
|
|
2972
|
+
blocks.push("---");
|
|
2973
|
+
pos = tagEnd + 1;
|
|
2974
|
+
} else if (html[pos] === "<" && /^<\/?[A-Za-z]/.test(html.slice(pos, pos + 3))) {
|
|
2975
|
+
let end = pos;
|
|
2976
|
+
while (end < html.length) {
|
|
2977
|
+
const nl = html.indexOf("\n", end);
|
|
2978
|
+
const lineEnd = nl === -1 ? html.length : nl;
|
|
2979
|
+
const line = html.slice(end, lineEnd);
|
|
2980
|
+
if (end !== pos && (line.trim() === "" || MDLD_BLOCK_START_RE.test(line.trimStart()))) break;
|
|
2981
|
+
end = nl === -1 ? html.length : nl + 1;
|
|
2982
|
+
}
|
|
2983
|
+
blocks.push(html.slice(pos, end).trim());
|
|
2984
|
+
pos = end;
|
|
2985
|
+
} else pos++;
|
|
2986
|
+
}
|
|
2987
|
+
return blocks.join("\n\n");
|
|
2988
|
+
}
|
|
2989
|
+
function mdEscapeText(s) {
|
|
2990
|
+
let out = "";
|
|
2991
|
+
for (let i = 0; i < s.length; i++) {
|
|
2992
|
+
const ch = s[i];
|
|
2993
|
+
if (ch === "&") out += "&";
|
|
2994
|
+
else if (ch === "<") out += "<";
|
|
2995
|
+
else if (ch === "\\" || ch === "`" || ch === "*" || ch === "_" || ch === "[" || ch === "]") out += "\\" + ch;
|
|
2996
|
+
else out += ch;
|
|
2997
|
+
}
|
|
2998
|
+
return out;
|
|
2999
|
+
}
|
|
3000
|
+
function deconstructInline(html, inCode = false) {
|
|
3001
|
+
let out = "";
|
|
3002
|
+
let i = 0;
|
|
3003
|
+
while (i < html.length) if (html[i] === "<") {
|
|
3004
|
+
const closeGt = html.indexOf(">", i);
|
|
3005
|
+
if (closeGt === -1) {
|
|
3006
|
+
out += html.slice(i);
|
|
3007
|
+
break;
|
|
3008
|
+
}
|
|
3009
|
+
const tagContent = html.slice(i + 1, closeGt);
|
|
3010
|
+
if (tagContent.startsWith("/")) {
|
|
3011
|
+
i = closeGt + 1;
|
|
3012
|
+
continue;
|
|
3013
|
+
}
|
|
3014
|
+
const spaceOrEnd = tagContent.search(/[\s/]/);
|
|
3015
|
+
const tagName = (spaceOrEnd === -1 ? tagContent : tagContent.slice(0, spaceOrEnd)).toLowerCase();
|
|
3016
|
+
const attrs = spaceOrEnd === -1 ? "" : tagContent.slice(spaceOrEnd);
|
|
3017
|
+
if (tagContent.endsWith("/")) {
|
|
3018
|
+
out += deconstructInlineElement(tagName, attrs, "", inCode);
|
|
3019
|
+
i = closeGt + 1;
|
|
3020
|
+
continue;
|
|
3021
|
+
}
|
|
3022
|
+
const closeTag = `</${tagName}>`;
|
|
3023
|
+
let depth = 1;
|
|
3024
|
+
let j = closeGt + 1;
|
|
3025
|
+
let found = false;
|
|
3026
|
+
while (j < html.length && depth > 0) {
|
|
3027
|
+
let nextOpen = -1;
|
|
3028
|
+
let searchIdx = j;
|
|
3029
|
+
while (searchIdx < html.length) {
|
|
3030
|
+
const idx = html.indexOf(`<${tagName}`, searchIdx);
|
|
3031
|
+
if (idx === -1) break;
|
|
3032
|
+
const nextChar = html[idx + tagName.length + 1];
|
|
3033
|
+
if (nextChar === " " || nextChar === ">" || nextChar === "/" || nextChar === void 0) {
|
|
3034
|
+
nextOpen = idx;
|
|
3035
|
+
break;
|
|
3036
|
+
}
|
|
3037
|
+
searchIdx = idx + 1;
|
|
3038
|
+
}
|
|
3039
|
+
const nextClose = html.indexOf(closeTag, j);
|
|
3040
|
+
if (nextClose === -1) break;
|
|
3041
|
+
if (nextOpen !== -1 && nextOpen < nextClose) {
|
|
3042
|
+
depth++;
|
|
3043
|
+
j = nextOpen + 1;
|
|
3044
|
+
} else {
|
|
3045
|
+
depth--;
|
|
3046
|
+
if (depth === 0) {
|
|
3047
|
+
const inner = html.slice(closeGt + 1, nextClose);
|
|
3048
|
+
out += deconstructInlineElement(tagName, attrs, inner, inCode);
|
|
3049
|
+
i = nextClose + closeTag.length;
|
|
3050
|
+
found = true;
|
|
3051
|
+
break;
|
|
3052
|
+
}
|
|
3053
|
+
j = nextClose + 1;
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
if (!found) {
|
|
3057
|
+
out += html[i];
|
|
3058
|
+
i++;
|
|
3059
|
+
}
|
|
3060
|
+
} else {
|
|
3061
|
+
const nextTag = html.indexOf("<", i);
|
|
3062
|
+
const textEnd = nextTag === -1 ? html.length : nextTag;
|
|
3063
|
+
const text = unescapeHtml(html.slice(i, textEnd));
|
|
3064
|
+
out += inCode ? text : mdEscapeText(text);
|
|
3065
|
+
i = textEnd;
|
|
3066
|
+
}
|
|
3067
|
+
return out;
|
|
3068
|
+
}
|
|
3069
|
+
function deconstructInlineElement(tagName, attrs, inner, inCode = false) {
|
|
3070
|
+
const ann = extractAttr(attrs, "data-annotation");
|
|
3071
|
+
const content = deconstructInline(inner, inCode || tagName === "code");
|
|
3072
|
+
const suffix = ann ? " " + ann : "";
|
|
3073
|
+
const title = extractAttr(attrs, "title");
|
|
3074
|
+
const titleStr = title ? ` "${title}"` : "";
|
|
3075
|
+
switch (tagName) {
|
|
3076
|
+
case "a": return `[${content}](${extractAttr(attrs, "href") || ""}${titleStr})${suffix}`;
|
|
3077
|
+
case "img": {
|
|
3078
|
+
const src = extractAttr(attrs, "src") || "";
|
|
3079
|
+
return `${suffix}`;
|
|
3080
|
+
}
|
|
3081
|
+
case "span": return `[${content}]${suffix}`;
|
|
3082
|
+
case "code": return `\`${content}\`${suffix}`;
|
|
3083
|
+
case "strong": return `**${content}**${suffix}`;
|
|
3084
|
+
case "em": return `*${content}*${suffix}`;
|
|
3085
|
+
case "br": return " " + suffix;
|
|
3086
|
+
default: return content + suffix;
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
function extractAttr(tag, name) {
|
|
3090
|
+
const re = new RegExp(`(?:\\s|^)${name}=(["'])(.*?)\\1`, "is");
|
|
3091
|
+
const m = tag.match(re);
|
|
3092
|
+
if (!m) return null;
|
|
3093
|
+
return unescapeHtml(m[2]);
|
|
3094
|
+
}
|
|
3095
|
+
function unescapeHtml(s) {
|
|
3096
|
+
return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'");
|
|
3097
|
+
}
|
|
3098
|
+
// src/index.js
|
|
2196
3099
|
/**
|
|
2197
3100
|
* Update the value of a quad in MDLD text and return the updated text
|
|
2198
3101
|
*
|
|
@@ -2208,5 +3111,4 @@ function updateValue({ text, quad, value, origin }) {
|
|
|
2208
3111
|
if (!location || !location.valueRange) return text;
|
|
2209
3112
|
return text.substring(0, location.valueRange.start) + value + text.substring(location.valueRange.end);
|
|
2210
3113
|
}
|
|
2211
|
-
|
|
2212
|
-
export { DEFAULT_CONTEXT, DataFactory, expandIRI, generate, generateNode, hash, locate, merge, parse, parseSemanticBlock, shortenIRI, updateValue };
|
|
3114
|
+
export { DEFAULT_CONTEXT, DataFactory, deconstruct, expandIRI, generate, generateNode, hash, locate, merge, parse, parseSemanticBlock, render, shortenIRI, updateValue };
|