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.
Files changed (2) hide show
  1. package/dist/index.js +67 -42
  2. 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
- const quadKeys = /* @__PURE__ */ new Set();
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 (!quadKeys.has(key)) filteredRemove.push(quad);
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
- return token._carriers || (token._carriers = extractInlineCarriers(token.text, token.range[0]));
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, lineStart],
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
- tokens.push(createToken("para", [lineStart, pos - 1], line.trim()));
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 (const carrier of carriers) if (carrier.attrs && (carrier.type === "emphasis" || carrier.type === "code")) {
1311
- const before = cleanPara.substring(0, carrier.range[0]);
1312
- const after = cleanPara.substring(carrier.range[1]);
1313
- cleanPara = before + (carrier.text || "") + after;
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(/\s*\{[^}]+\}\s*/g, " ");
1317
- cleanPara = cleanPara.replace(/\s+/g, " ").trim();
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
- function emitQuad(quads, quadBuffer, removeSet, quadIndex, block, subject, predicate, object, dataFactory, meta = null, statements = null, statementCandidates = null, state = null) {
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 quad = dataFactory.quad(subject, predicate, object);
1506
- if (meta?.remove || false) {
1507
- const quadKey = quadIndexKey(quad.subject, quad.predicate, quad.object);
1508
- if (quadBuffer.has(quadKey)) {
1509
- quadBuffer.delete(quadKey);
1510
- const index = quads.findIndex((q) => q.subject.value === quad.subject.value && q.predicate.value === quad.predicate.value && q.object.value === quad.object.value);
1511
- if (index !== -1) quads.splice(index, 1);
1512
- quadIndex.delete(quadKey);
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.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block, subject, state.df.namedNode(expandIRI("rdf:type", state.ctx)), state.df.namedNode(expandedType), state.df, {
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
- }, state.statements, state.statementCandidates, state);
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.quads, state.quadBuffer, state.removeSet, state.origin.quadIndex, block, role.subject, P, role.object, state.df, {
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
- }, state.statements, state.statementCandidates, state);
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 + (result.content ? result.content.length : 0)],
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.5",
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
+ }