mdld-parse 1.0.5 → 1.0.6
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/dist/index.js +67 -42
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1060,6 +1060,7 @@ function parse(firstArg, secondArg = {}) {
|
|
|
1060
1060
|
primarySubject: null,
|
|
1061
1061
|
primaryType: null,
|
|
1062
1062
|
primaryLabel: null,
|
|
1063
|
+
primaryComment: null,
|
|
1063
1064
|
tokens: null,
|
|
1064
1065
|
currentTokenIndex: -1,
|
|
1065
1066
|
statements: [],
|
|
@@ -1088,12 +1089,11 @@ function parse(firstArg, secondArg = {}) {
|
|
|
1088
1089
|
}
|
|
1089
1090
|
TOKEN_PROCESSORS[token.type]?.(token, state);
|
|
1090
1091
|
}
|
|
1091
|
-
|
|
1092
|
-
for (const quad of state.quads) quadKeys.add(quadIndexKey(quad.subject, quad.predicate, quad.object));
|
|
1092
|
+
state.quads = Array.from(state.quadBuffer.values());
|
|
1093
1093
|
const filteredRemove = [];
|
|
1094
1094
|
for (const quad of state.removeSet) {
|
|
1095
1095
|
const key = quadIndexKey(quad.subject, quad.predicate, quad.object);
|
|
1096
|
-
if (!
|
|
1096
|
+
if (!state.quadBuffer.has(key)) filteredRemove.push(quad);
|
|
1097
1097
|
}
|
|
1098
1098
|
const primary = {
|
|
1099
1099
|
subject: state.primarySubject,
|
|
@@ -1114,7 +1114,8 @@ function parse(firstArg, secondArg = {}) {
|
|
|
1114
1114
|
}
|
|
1115
1115
|
function getCarriers(token) {
|
|
1116
1116
|
if (token.type === "code") return [];
|
|
1117
|
-
|
|
1117
|
+
const baseOffset = token.valueRange ? token.valueRange[0] : token.range[0];
|
|
1118
|
+
return token._carriers || (token._carriers = extractInlineCarriers(token.text, baseOffset));
|
|
1118
1119
|
}
|
|
1119
1120
|
function scanTokens(text) {
|
|
1120
1121
|
const tokens = [];
|
|
@@ -1244,7 +1245,7 @@ function scanTokens(text) {
|
|
|
1244
1245
|
const valueEnd = Math.max(valueStart, lineStart - 1);
|
|
1245
1246
|
tokens.push({
|
|
1246
1247
|
type: "code",
|
|
1247
|
-
range: [codeBlock.start,
|
|
1248
|
+
range: [codeBlock.start, pos - 1],
|
|
1248
1249
|
text: codeBlock.content.join("\n"),
|
|
1249
1250
|
lang: codeBlock.lang,
|
|
1250
1251
|
attrs: codeBlock.attrs,
|
|
@@ -1304,17 +1305,38 @@ function scanTokens(text) {
|
|
|
1304
1305
|
return true;
|
|
1305
1306
|
}
|
|
1306
1307
|
function handlePara(line, lineStart, pos) {
|
|
1307
|
-
|
|
1308
|
+
const leadingWhitespace = line.search(/\S/);
|
|
1309
|
+
const valueStart = leadingWhitespace === -1 ? lineStart : lineStart + leadingWhitespace;
|
|
1310
|
+
const trimmed = line.trim();
|
|
1311
|
+
const valueEnd = valueStart + trimmed.length;
|
|
1312
|
+
tokens.push(createToken("para", [lineStart, pos - 1], trimmed, null, null, [valueStart, valueEnd]));
|
|
1308
1313
|
let cleanPara = line;
|
|
1314
|
+
const codeSpans = [];
|
|
1315
|
+
cleanPara = cleanPara.replace(/`[^`]+`/g, (match) => {
|
|
1316
|
+
codeSpans.push(match);
|
|
1317
|
+
return `__INLINE_CODE_${codeSpans.length - 1}__`;
|
|
1318
|
+
});
|
|
1319
|
+
const mustaches = [];
|
|
1320
|
+
cleanPara = cleanPara.replace(/\{\{(?:[^{}]|\{[^{}]*\})*\}\}/g, (match) => {
|
|
1321
|
+
mustaches.push(match);
|
|
1322
|
+
return `__VUE_INTERPOLATION_${mustaches.length - 1}__`;
|
|
1323
|
+
});
|
|
1309
1324
|
const carriers = scanInlineCarriers(cleanPara, 0);
|
|
1310
|
-
for (
|
|
1311
|
-
const
|
|
1312
|
-
|
|
1313
|
-
|
|
1325
|
+
for (let i = carriers.length - 1; i >= 0; i--) {
|
|
1326
|
+
const carrier = carriers[i];
|
|
1327
|
+
if (carrier.attrs && (carrier.type === "emphasis" || carrier.type === "code")) {
|
|
1328
|
+
const before = cleanPara.substring(0, carrier.range[0]);
|
|
1329
|
+
const after = cleanPara.substring(carrier.range[1]);
|
|
1330
|
+
cleanPara = before + (carrier.text || "") + after;
|
|
1331
|
+
}
|
|
1314
1332
|
}
|
|
1315
1333
|
cleanPara = cleanPara.replace(/\[([^\]]+)\]\s*\{[^}]+\}/g, "$1");
|
|
1316
|
-
cleanPara = cleanPara.replace(
|
|
1317
|
-
cleanPara = cleanPara.replace(
|
|
1334
|
+
cleanPara = cleanPara.replace(/([^{]|^)\{[^{}]+\}(?=[^}]|$)/g, "$1");
|
|
1335
|
+
cleanPara = cleanPara.replace(/__VUE_INTERPOLATION_(\d+)__/g, (_, idx) => mustaches[Number(idx)]);
|
|
1336
|
+
cleanPara = cleanPara.replace(/__INLINE_CODE_(\d+)__/g, (_, idx) => codeSpans[Number(idx)]);
|
|
1337
|
+
const trailingSpaces = cleanPara.match(/ {2,}$/);
|
|
1338
|
+
if (trailingSpaces) cleanPara = cleanPara.replace(/[ \t]+$/, trailingSpaces[0]);
|
|
1339
|
+
else cleanPara = cleanPara.replace(/[ \t]+$/, "");
|
|
1318
1340
|
mdLines.push(cleanPara);
|
|
1319
1341
|
return true;
|
|
1320
1342
|
}
|
|
@@ -1500,33 +1522,35 @@ function createBlock(subject, types, predicates, range, attrsRange, valueRange,
|
|
|
1500
1522
|
text: text || ""
|
|
1501
1523
|
};
|
|
1502
1524
|
}
|
|
1503
|
-
|
|
1525
|
+
/**
|
|
1526
|
+
* Hardened O(1) quad emitter.
|
|
1527
|
+
* Uses state.quadBuffer (Map) as single source of truth during parsing.
|
|
1528
|
+
*/
|
|
1529
|
+
function emitQuad(state, block, subject, predicate, object, meta = null) {
|
|
1504
1530
|
if (!subject || !predicate || !object) return;
|
|
1505
|
-
const
|
|
1506
|
-
if (meta?.remove
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
} else removeSet.add(quad);
|
|
1514
|
-
} else {
|
|
1515
|
-
const quadKey = quadIndexKey(quad.subject, quad.predicate, quad.object);
|
|
1516
|
-
quadBuffer.set(quadKey, quad);
|
|
1517
|
-
quads.push(quad);
|
|
1518
|
-
if (state) {
|
|
1519
|
-
if (!state.primaryType && predicate.value === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") state.primaryType = object.value;
|
|
1520
|
-
if (!state.primaryLabel && predicate.value === "http://www.w3.org/2000/01/rdf-schema#label" && object.termType === "Literal") state.primaryLabel = object.value;
|
|
1521
|
-
if (!state.primaryComment && predicate.value === "http://www.w3.org/2000/01/rdf-schema#comment" && object.termType === "Literal") state.primaryComment = object.value;
|
|
1522
|
-
}
|
|
1523
|
-
detectStatementPatternSinglePass(quad, dataFactory, meta, statements, statementCandidates);
|
|
1524
|
-
const originEntry = createLeanOriginEntry(block, subject, predicate, meta);
|
|
1525
|
-
quadIndex.set(quadKey, originEntry);
|
|
1526
|
-
if (state.currentBlock && block.id === state.currentBlock.id) {
|
|
1527
|
-
if (!state.currentBlock.quadKeys) state.currentBlock.quadKeys = [];
|
|
1528
|
-
state.currentBlock.quadKeys.push(quadKey);
|
|
1531
|
+
const quadKey = quadIndexKey(subject, predicate, object);
|
|
1532
|
+
if (Boolean(meta?.remove)) {
|
|
1533
|
+
if (state.quadBuffer.has(quadKey)) {
|
|
1534
|
+
state.quadBuffer.delete(quadKey);
|
|
1535
|
+
state.origin.quadIndex.delete(quadKey);
|
|
1536
|
+
} else {
|
|
1537
|
+
const retractQuad = state.df.quad(subject, predicate, object, state.graph);
|
|
1538
|
+
state.removeSet.add(retractQuad);
|
|
1529
1539
|
}
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
const quad = state.df.quad(subject, predicate, object, state.graph);
|
|
1543
|
+
state.quadBuffer.set(quadKey, quad);
|
|
1544
|
+
const predVal = predicate.value;
|
|
1545
|
+
if (!state.primaryType && predVal === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") state.primaryType = object.value;
|
|
1546
|
+
else if (!state.primaryLabel && predVal === "http://www.w3.org/2000/01/rdf-schema#label" && object.termType === "Literal") state.primaryLabel = object.value;
|
|
1547
|
+
else if (!state.primaryComment && predVal === "http://www.w3.org/2000/01/rdf-schema#comment" && object.termType === "Literal") state.primaryComment = object.value;
|
|
1548
|
+
if (state.statements && state.statementCandidates) detectStatementPatternSinglePass(quad, state.df, meta, state.statements, state.statementCandidates);
|
|
1549
|
+
const originEntry = createLeanOriginEntry(block, subject, predicate, meta);
|
|
1550
|
+
state.origin.quadIndex.set(quadKey, originEntry);
|
|
1551
|
+
if (block && state.currentBlock && block.id === state.currentBlock.id) {
|
|
1552
|
+
if (!state.currentBlock.quadKeys) state.currentBlock.quadKeys = [];
|
|
1553
|
+
state.currentBlock.quadKeys.push(quadKey);
|
|
1530
1554
|
}
|
|
1531
1555
|
}
|
|
1532
1556
|
function detectStatementPatternSinglePass(quad, dataFactory, meta, statements = null, statementCandidates = null) {
|
|
@@ -1557,13 +1581,13 @@ var createTypeQuad = (typeIRI, subject, state, block, entryIndex = null) => {
|
|
|
1557
1581
|
entryIndex,
|
|
1558
1582
|
remove: false
|
|
1559
1583
|
};
|
|
1560
|
-
emitQuad(state
|
|
1584
|
+
emitQuad(state, block, subject, state.df.namedNode(expandIRI("rdf:type", state.ctx)), state.df.namedNode(expandedType), {
|
|
1561
1585
|
kind: "type",
|
|
1562
1586
|
token: `.${typeIRI}`,
|
|
1563
1587
|
expandedType,
|
|
1564
1588
|
entryIndex: typeInfo.entryIndex,
|
|
1565
1589
|
remove: typeInfo.remove
|
|
1566
|
-
}
|
|
1590
|
+
});
|
|
1567
1591
|
};
|
|
1568
1592
|
function processTypeAnnotations(sem, newSubject, localObject, carrierO, S, block, state, carrier) {
|
|
1569
1593
|
sem.types.forEach((t) => {
|
|
@@ -1602,14 +1626,14 @@ function processPredicateAnnotations(sem, newSubject, previousSubject, localObje
|
|
|
1602
1626
|
const role = determinePredicateRole(pred, carrier, newSubject, previousSubject, localObject, newSubjectOrCarrierO, S, L);
|
|
1603
1627
|
if (role) {
|
|
1604
1628
|
const P = state.df.namedNode(expandIRI(pred.iri, state.ctx));
|
|
1605
|
-
emitQuad(state
|
|
1629
|
+
emitQuad(state, block, role.subject, P, role.object, {
|
|
1606
1630
|
kind: "pred",
|
|
1607
1631
|
token: `${pred.form}${pred.iri}`,
|
|
1608
1632
|
form: pred.form,
|
|
1609
1633
|
expandedPredicate: P.value,
|
|
1610
1634
|
entryIndex: pred.entryIndex,
|
|
1611
1635
|
remove: pred.remove || false
|
|
1612
|
-
}
|
|
1636
|
+
});
|
|
1613
1637
|
}
|
|
1614
1638
|
});
|
|
1615
1639
|
}
|
|
@@ -1636,11 +1660,12 @@ function processStandaloneSubject(token, state) {
|
|
|
1636
1660
|
if (!result) return;
|
|
1637
1661
|
const sem = parseSemCached(`{=${result.content}}`);
|
|
1638
1662
|
const attrsStart = token.range[0] + token.text.indexOf("{=");
|
|
1663
|
+
const fullAttrLength = 3 + (result.content ? result.content.length : 0);
|
|
1639
1664
|
processAnnotation({
|
|
1640
1665
|
type: "standalone",
|
|
1641
1666
|
text: "",
|
|
1642
1667
|
range: token.range,
|
|
1643
|
-
attrsRange: [attrsStart, attrsStart +
|
|
1668
|
+
attrsRange: [attrsStart, attrsStart + fullAttrLength],
|
|
1644
1669
|
valueRange: null
|
|
1645
1670
|
}, sem, state);
|
|
1646
1671
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdld-parse",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
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",
|
|
@@ -60,4 +60,4 @@
|
|
|
60
60
|
"vscode-textmate": "^9.3.2",
|
|
61
61
|
"vue": "^3.6.0-beta.17"
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
}
|