@8btc/mditor 0.0.2 → 0.0.4

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Vditor v0.0.1 - A markdown editor written in TypeScript.
2
+ * Vditor v0.0.3 - A markdown editor written in TypeScript.
3
3
  *
4
4
  * MIT License
5
5
  *
@@ -2791,7 +2791,7 @@ var Vditor = /** @class */ (function () {
2791
2791
  /* harmony export */ "H": () => (/* binding */ _VDITOR_VERSION),
2792
2792
  /* harmony export */ "g": () => (/* binding */ Constants)
2793
2793
  /* harmony export */ });
2794
- var _VDITOR_VERSION = "0.0.1";
2794
+ var _VDITOR_VERSION = "0.0.3";
2795
2795
 
2796
2796
  var Constants = /** @class */ (function () {
2797
2797
  function Constants() {
@@ -3093,7 +3093,7 @@ var Constants = /** @class */ (function () {
3093
3093
  "c#",
3094
3094
  "bat",
3095
3095
  ];
3096
- Constants.CDN = "https://webcdn.wujieai.com/vditor@".concat("0.0.1");
3096
+ Constants.CDN = "https://webcdn.wujieai.com/vditor@".concat("0.0.3");
3097
3097
  Constants.MARKDOWN_OPTIONS = {
3098
3098
  autoSpace: false,
3099
3099
  gfmAutoLink: true,
@@ -8676,9 +8676,486 @@ var highlightToolbarIR = function (vditor) {
8676
8676
  }, 200);
8677
8677
  };
8678
8678
 
8679
+ ;// CONCATENATED MODULE: ./src/ts/util/attachLineNumbers.ts
8680
+ /**
8681
+ * 预处理 Markdown:构建块、无序列表、有序列表的行号索引。
8682
+ */
8683
+ var prepareLineNumberIndex = function (sourceMarkdown) {
8684
+ var ZWSP = "\u200b";
8685
+ var normalize = function (text) {
8686
+ return text
8687
+ .replace(/\r\n|\r/g, "\n")
8688
+ .replace(new RegExp(ZWSP, "g"), "")
8689
+ .replace(/\u00a0/g, " ")
8690
+ .replace(/\u2006/g, "")
8691
+ .replace(/[\t\f\v ]+/g, " ");
8692
+ };
8693
+ var stripInlineMD = function (text) {
8694
+ return text
8695
+ .replace(/\\([*_`~])/g, "$1")
8696
+ .replace(/\*\*|__/g, "")
8697
+ .replace(/\*|_/g, "")
8698
+ .replace(/~~/g, "")
8699
+ .replace(/`+/g, "")
8700
+ .replace(/\\\(/g, "(")
8701
+ .replace(/\\\)/g, ")")
8702
+ .replace(/\\\[/g, "[")
8703
+ .replace(/\\\]/g, "]")
8704
+ .replace(/\$/g, "")
8705
+ .trim();
8706
+ };
8707
+ var stripInlineForList = function (text) {
8708
+ return stripInlineMD(text)
8709
+ .replace(/\$(?:\\.|[^$])*\$/g, "")
8710
+ .replace(/\\\([^)]*\\\)/g, "")
8711
+ .replace(/\\\[[^\]]*\\\]/g, "")
8712
+ .replace(/\\[a-zA-Z]+/g, "")
8713
+ .replace(/[{}]/g, "")
8714
+ .trim();
8715
+ };
8716
+ var srcLines = normalize(sourceMarkdown).split("\n");
8717
+ var strippedLines = srcLines.map(function (l) { return stripInlineMD(l); });
8718
+ var lineLookup = new Map();
8719
+ for (var i = 0; i < strippedLines.length; i++) {
8720
+ var key = strippedLines[i];
8721
+ if (!lineLookup.has(key)) {
8722
+ lineLookup.set(key, [i + 1]);
8723
+ }
8724
+ else {
8725
+ lineLookup.get(key).push(i + 1);
8726
+ }
8727
+ }
8728
+ var unorderedEntries = [];
8729
+ var unorderedLookup = new Map();
8730
+ for (var i = 0; i < srcLines.length; i++) {
8731
+ var raw = srcLines[i];
8732
+ var m = raw.match(/^\s*[*+-]\s+(?:\[(?: |x|X)\]\s+)?(.*)$/);
8733
+ if (m && typeof m[1] === "string") {
8734
+ var contentStripped = stripInlineForList(m[1]);
8735
+ unorderedEntries.push({ ln: i + 1, content: contentStripped });
8736
+ if (!unorderedLookup.has(contentStripped)) {
8737
+ unorderedLookup.set(contentStripped, [i + 1]);
8738
+ }
8739
+ else {
8740
+ unorderedLookup.get(contentStripped).push(i + 1);
8741
+ }
8742
+ }
8743
+ }
8744
+ var orderedEntries = [];
8745
+ var orderedLookup = new Map();
8746
+ var ORDERED_RE = /\s*\d+(?:[.)、.。]|[\uFF0E\uFF09\u3001])\s+(?:\[(?: |x|X)\]\s+)?(.*)/;
8747
+ for (var i = 0; i < srcLines.length; i++) {
8748
+ var raw = srcLines[i];
8749
+ var m = raw.match(new RegExp("^".concat(ORDERED_RE.source, "$")));
8750
+ if (m && typeof m[1] === "string") {
8751
+ var contentStripped = stripInlineForList(m[1]);
8752
+ orderedEntries.push({ ln: i + 1, content: contentStripped });
8753
+ if (!orderedLookup.has(contentStripped)) {
8754
+ orderedLookup.set(contentStripped, [i + 1]);
8755
+ }
8756
+ else {
8757
+ orderedLookup.get(contentStripped).push(i + 1);
8758
+ }
8759
+ }
8760
+ }
8761
+ var orderedGroups = [];
8762
+ for (var i = 0; i < srcLines.length;) {
8763
+ var raw = srcLines[i];
8764
+ if (new RegExp("^".concat(ORDERED_RE.source, "$")).test(raw)) {
8765
+ var group = [];
8766
+ while (i < srcLines.length &&
8767
+ new RegExp("^".concat(ORDERED_RE.source, "$")).test(srcLines[i])) {
8768
+ group.push(i + 1);
8769
+ i++;
8770
+ }
8771
+ if (group.length > 0) {
8772
+ orderedGroups.push(group);
8773
+ }
8774
+ }
8775
+ else {
8776
+ i++;
8777
+ }
8778
+ }
8779
+ return {
8780
+ srcLines: srcLines,
8781
+ strippedLines: strippedLines,
8782
+ lineLookup: lineLookup,
8783
+ unorderedEntries: unorderedEntries,
8784
+ unorderedLookup: unorderedLookup,
8785
+ orderedEntries: orderedEntries,
8786
+ orderedLookup: orderedLookup,
8787
+ orderedGroups: orderedGroups,
8788
+ };
8789
+ };
8790
+ var attachLineNumbersToBlocks = function (root, sourceMarkdownOrIndex) {
8791
+ if (!root || !sourceMarkdownOrIndex) {
8792
+ return;
8793
+ }
8794
+ var ZWSP = "\u200b";
8795
+ var normalize = function (text) {
8796
+ return text
8797
+ .replace(/\r\n|\r/g, "\n")
8798
+ .replace(new RegExp(ZWSP, "g"), "")
8799
+ .replace(/\u00a0/g, " ")
8800
+ .replace(/\u2006/g, "")
8801
+ .replace(/[\t\f\v ]+/g, " ");
8802
+ };
8803
+ var index = typeof sourceMarkdownOrIndex === "string"
8804
+ ? prepareLineNumberIndex(sourceMarkdownOrIndex)
8805
+ : sourceMarkdownOrIndex;
8806
+ var srcLines = index.srcLines;
8807
+ var stripInlineMD = function (text) {
8808
+ return (text
8809
+ .replace(/\\([*_`~])/g, "$1")
8810
+ .replace(/\*\*|__/g, "")
8811
+ .replace(/\*|_/g, "")
8812
+ .replace(/~~/g, "")
8813
+ .replace(/`+/g, "")
8814
+ // 数学行内分隔符:保留内容,仅移除分隔符
8815
+ .replace(/\\\(/g, "(")
8816
+ .replace(/\\\)/g, ")")
8817
+ .replace(/\\\[/g, "[")
8818
+ .replace(/\\\]/g, "]")
8819
+ .replace(/\$/g, "")
8820
+ .trim());
8821
+ };
8822
+ var stripInlineForList = function (text) {
8823
+ return (stripInlineMD(text)
8824
+ // 移除 $...$(行内公式整体)
8825
+ .replace(/\$(?:\\.|[^$])*\$/g, "")
8826
+ // 移除 \(...\)、\[...\](行内/行间公式整体)
8827
+ .replace(/\\\([^)]*\\\)/g, "")
8828
+ .replace(/\\\[[^\]]*\\\]/g, "")
8829
+ // 去除通用 TeX 命令,如 \alpha、\mathbf 等
8830
+ .replace(/\\[a-zA-Z]+/g, "")
8831
+ // 去除多余大括号
8832
+ .replace(/[{}]/g, "")
8833
+ .trim());
8834
+ };
8835
+ var strippedLines = index.strippedLines;
8836
+ var lineLookup = index.lineLookup;
8837
+ var usedLines = new Set();
8838
+ var pickFirstUnused = function (candidates) {
8839
+ if (!candidates || candidates.length === 0)
8840
+ return -1;
8841
+ for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) {
8842
+ var ln = candidates_1[_i];
8843
+ if (!usedLines.has(ln))
8844
+ return ln;
8845
+ }
8846
+ return -1;
8847
+ };
8848
+ var findLineNumberByText = function (text) {
8849
+ var stripped = stripInlineMD(text);
8850
+ var ln = pickFirstUnused(lineLookup.get(stripped));
8851
+ if (ln !== -1)
8852
+ return ln;
8853
+ for (var i = 0; i < strippedLines.length; i++) {
8854
+ if (!usedLines.has(i + 1) &&
8855
+ stripped &&
8856
+ strippedLines[i].indexOf(stripped) !== -1) {
8857
+ return i + 1;
8858
+ }
8859
+ }
8860
+ return -1;
8861
+ };
8862
+ /**
8863
+ * 无序列表行号索引
8864
+ */
8865
+ var usedUnorderedLines = new Set();
8866
+ var unorderedListEntries = index.unorderedEntries;
8867
+ var unorderedLookup = index.unorderedLookup;
8868
+ /**
8869
+ * 从无序列表候选行中为给定文本选择一个行号。
8870
+ * 优先精确匹配(去标记后完全相同),否则回退到包含匹配。
8871
+ */
8872
+ var pickFirstUnusedUnordered = function (candidates) {
8873
+ if (!candidates || candidates.length === 0)
8874
+ return -1;
8875
+ for (var _i = 0, candidates_2 = candidates; _i < candidates_2.length; _i++) {
8876
+ var ln = candidates_2[_i];
8877
+ if (!usedUnorderedLines.has(ln))
8878
+ return ln;
8879
+ }
8880
+ return -1;
8881
+ };
8882
+ var findUnorderedListLineNumber = function (text) {
8883
+ var stripped = stripInlineForList(text);
8884
+ // 先尝试精确匹配
8885
+ var exact = pickFirstUnusedUnordered(unorderedLookup.get(stripped));
8886
+ if (exact !== -1)
8887
+ return exact;
8888
+ // 回退到包含匹配
8889
+ for (var i = 0; i < unorderedListEntries.length; i++) {
8890
+ var entry = unorderedListEntries[i];
8891
+ if (usedUnorderedLines.has(entry.ln))
8892
+ continue;
8893
+ if (!stripped)
8894
+ continue;
8895
+ if (entry.content.indexOf(stripped) !== -1 ||
8896
+ stripped.indexOf(entry.content) !== -1) {
8897
+ return entry.ln;
8898
+ }
8899
+ }
8900
+ return -1;
8901
+ };
8902
+ /**
8903
+ * 有序列表行号索引
8904
+ */
8905
+ var usedOrderedLines = new Set();
8906
+ var orderedListEntries = index.orderedEntries;
8907
+ var orderedLookup = index.orderedLookup;
8908
+ var orderedGroups = index.orderedGroups;
8909
+ console.debug("[LineNumber][ol:index]", {
8910
+ total: orderedListEntries.length,
8911
+ });
8912
+ /**
8913
+ * 为给定文本在有序列表候选中选择行号,精确匹配优先,包含匹配回退。
8914
+ */
8915
+ var pickFirstUnusedOrdered = function (candidates) {
8916
+ if (!candidates || candidates.length === 0)
8917
+ return -1;
8918
+ for (var _i = 0, candidates_3 = candidates; _i < candidates_3.length; _i++) {
8919
+ var ln = candidates_3[_i];
8920
+ if (!usedOrderedLines.has(ln))
8921
+ return ln;
8922
+ }
8923
+ return -1;
8924
+ };
8925
+ var findOrderedListLineNumber = function (text) {
8926
+ var stripped = stripInlineForList(text);
8927
+ var exact = pickFirstUnusedOrdered(orderedLookup.get(stripped));
8928
+ if (exact !== -1)
8929
+ return exact;
8930
+ for (var i = 0; i < orderedListEntries.length; i++) {
8931
+ var entry = orderedListEntries[i];
8932
+ if (usedOrderedLines.has(entry.ln))
8933
+ continue;
8934
+ if (!stripped)
8935
+ continue;
8936
+ if (entry.content.indexOf(stripped) !== -1 ||
8937
+ stripped.indexOf(entry.content) !== -1) {
8938
+ return entry.ln;
8939
+ }
8940
+ }
8941
+ return -1;
8942
+ };
8943
+ /**
8944
+ * 获取 `li` 的直接内容文本:仅拼接其直系子节点中的文本,排除嵌套的 `UL/OL` 列表。
8945
+ * 以便在嵌套列表场景下准确定位每个条目对应的源行。
8946
+ */
8947
+ var getImmediateLiText = function (li) {
8948
+ var buf = "";
8949
+ li.childNodes.forEach(function (node) {
8950
+ var _a;
8951
+ if (node.nodeType === 3) {
8952
+ buf += node.data;
8953
+ }
8954
+ else if (node.tagName !== "UL" &&
8955
+ node.tagName !== "OL" &&
8956
+ !((_a = node.classList) === null || _a === void 0 ? void 0 : _a.contains("katex"))) {
8957
+ buf += node.textContent || "";
8958
+ }
8959
+ });
8960
+ return buf;
8961
+ };
8962
+ var blocks = root.querySelectorAll("[data-block]");
8963
+ blocks.forEach(function (el) {
8964
+ try {
8965
+ var container = el;
8966
+ var dataType = container.getAttribute("data-type") || "";
8967
+ if (dataType === "math-block" || dataType === "code-block") {
8968
+ // container.setAttribute("data-linenumber", "");
8969
+ return;
8970
+ }
8971
+ var text = container.textContent || "";
8972
+ var normText = normalize(text);
8973
+ // 跳过纯空白块
8974
+ if (!normText.trim()) {
8975
+ // container.setAttribute("data-linenumber", "");
8976
+ return;
8977
+ }
8978
+ var lineNumber = -1;
8979
+ var tag = container.tagName;
8980
+ if (tag === "BLOCKQUOTE") {
8981
+ var firstLine = normText.split("\n").find(function (l) { return l.trim().length > 0; }) ||
8982
+ normText;
8983
+ var stripped = stripInlineMD(firstLine);
8984
+ for (var i = 0; i < srcLines.length; i++) {
8985
+ if (usedLines.has(i + 1))
8986
+ continue;
8987
+ if (/^\s*>+\s/.test(srcLines[i])) {
8988
+ var content = stripInlineMD(srcLines[i].replace(/^\s*>+\s+/, ""));
8989
+ if (content.indexOf(stripped) !== -1) {
8990
+ lineNumber = i + 1;
8991
+ break;
8992
+ }
8993
+ }
8994
+ }
8995
+ }
8996
+ else {
8997
+ var firstLine = normText.split("\n").find(function (l) { return l.trim().length > 0; }) ||
8998
+ normText;
8999
+ lineNumber = findLineNumberByText(firstLine);
9000
+ }
9001
+ if (lineNumber !== -1) {
9002
+ usedLines.add(lineNumber);
9003
+ container.setAttribute("data-linenumber", String(lineNumber));
9004
+ }
9005
+ }
9006
+ catch (_a) {
9007
+ void 0;
9008
+ }
9009
+ });
9010
+ /**
9011
+ * 为所有 `ul` 下的每个直接 `li` 子元素追加 `data-linenumber`,不修改 `ul` 本身。
9012
+ * 支持多层嵌套与非文本起始节点(如加粗/行内代码)。
9013
+ */
9014
+ var ulElements = root.querySelectorAll("ul");
9015
+ ulElements.forEach(function (ul) {
9016
+ try {
9017
+ var children = Array.from(ul.children);
9018
+ for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
9019
+ var child = children_1[_i];
9020
+ if (child.tagName !== "LI")
9021
+ continue;
9022
+ var li = child;
9023
+ var rawText = getImmediateLiText(li);
9024
+ var norm = normalize(rawText);
9025
+ var firstLine = norm.split("\n").find(function (l) { return l.trim().length > 0; }) || norm;
9026
+ var ln = findUnorderedListLineNumber(firstLine);
9027
+ if (ln !== -1) {
9028
+ usedUnorderedLines.add(ln);
9029
+ li.setAttribute("data-linenumber", String(ln));
9030
+ }
9031
+ else {
9032
+ li.setAttribute("data-linenumber", "");
9033
+ }
9034
+ }
9035
+ }
9036
+ catch (_a) {
9037
+ void 0;
9038
+ }
9039
+ });
9040
+ /**
9041
+ * 为所有 `ol` 下的每个直接 `li` 子元素追加 `data-linenumber`,不修改 `ol` 本身。
9042
+ */
9043
+ var olElements = root.querySelectorAll("ol");
9044
+ olElements.forEach(function (ol) {
9045
+ try {
9046
+ var currentGroupIdx = -1;
9047
+ var lastAssigned_1 = -1;
9048
+ var children = Array.from(ol.children);
9049
+ for (var _i = 0, children_2 = children; _i < children_2.length; _i++) {
9050
+ var child = children_2[_i];
9051
+ if (child.tagName !== "LI")
9052
+ continue;
9053
+ var li = child;
9054
+ var rawText = getImmediateLiText(li);
9055
+ var norm = normalize(rawText);
9056
+ var firstLine = norm.split("\n").find(function (l) { return l.trim().length > 0; }) || norm;
9057
+ var stripped0 = stripInlineForList(firstLine);
9058
+ var stripped = stripped0 || stripInlineMD(firstLine);
9059
+ var candidates = orderedLookup.get(stripped) || [];
9060
+ var available = candidates.filter(function (n) { return !usedOrderedLines.has(n); });
9061
+ var ln = findOrderedListLineNumber(firstLine);
9062
+ if (ln !== -1) {
9063
+ usedOrderedLines.add(ln);
9064
+ li.setAttribute("data-linenumber", String(ln));
9065
+ // 成功映射日志
9066
+ console.debug("[LineNumber][ol>li]", {
9067
+ text: firstLine,
9068
+ stripped: stripped,
9069
+ line: ln,
9070
+ });
9071
+ // 记录当前组
9072
+ for (var gi = 0; gi < orderedGroups.length; gi++) {
9073
+ if (orderedGroups[gi].includes(ln)) {
9074
+ currentGroupIdx = gi;
9075
+ break;
9076
+ }
9077
+ }
9078
+ lastAssigned_1 = ln;
9079
+ }
9080
+ else {
9081
+ // 顺序回退:基于当前组或猜测组,按序分配下一未使用行
9082
+ var assigned = -1;
9083
+ var pickNextFromGroup = function (gi) {
9084
+ if (gi < 0 || gi >= orderedGroups.length)
9085
+ return -1;
9086
+ var lines = orderedGroups[gi];
9087
+ // 优先从上次分配后续查找
9088
+ var startIdx = 0;
9089
+ if (lastAssigned_1 !== -1) {
9090
+ var idx = lines.indexOf(lastAssigned_1);
9091
+ startIdx = idx >= 0 ? idx + 1 : 0;
9092
+ }
9093
+ for (var k = startIdx; k < lines.length; k++) {
9094
+ var cand = lines[k];
9095
+ if (!usedOrderedLines.has(cand))
9096
+ return cand;
9097
+ }
9098
+ for (var k = 0; k < startIdx; k++) {
9099
+ var cand = lines[k];
9100
+ if (!usedOrderedLines.has(cand))
9101
+ return cand;
9102
+ }
9103
+ return -1;
9104
+ };
9105
+ if (currentGroupIdx !== -1) {
9106
+ assigned = pickNextFromGroup(currentGroupIdx);
9107
+ }
9108
+ if (assigned === -1) {
9109
+ // 猜测组:选择拥有可用行的首个组
9110
+ for (var gi = 0; gi < orderedGroups.length && assigned === -1; gi++) {
9111
+ var cand = pickNextFromGroup(gi);
9112
+ if (cand !== -1) {
9113
+ currentGroupIdx = gi;
9114
+ assigned = cand;
9115
+ }
9116
+ }
9117
+ }
9118
+ if (assigned !== -1) {
9119
+ usedOrderedLines.add(assigned);
9120
+ li.setAttribute("data-linenumber", String(assigned));
9121
+ console.debug("[LineNumber][ol>li][fallback-seq]", {
9122
+ text: firstLine,
9123
+ stripped: stripped,
9124
+ line: assigned,
9125
+ group: currentGroupIdx,
9126
+ });
9127
+ lastAssigned_1 = assigned;
9128
+ }
9129
+ else {
9130
+ li.setAttribute("data-linenumber", "");
9131
+ // 失败诊断日志
9132
+ console.warn("[LineNumber][ol>li][missing]", {
9133
+ text: firstLine,
9134
+ stripped: stripped,
9135
+ candidates: candidates,
9136
+ available: available,
9137
+ reason: !stripped0
9138
+ ? "empty-after-strip"
9139
+ : candidates.length === 0
9140
+ ? "no-index-candidates"
9141
+ : available.length === 0
9142
+ ? "all-candidates-consumed"
9143
+ : "fallback-no-match",
9144
+ });
9145
+ }
9146
+ }
9147
+ }
9148
+ }
9149
+ catch (_a) {
9150
+ void 0;
9151
+ }
9152
+ });
9153
+ };
9154
+
8679
9155
  ;// CONCATENATED MODULE: ./src/ts/wysiwyg/afterRenderEvent.ts
8680
9156
 
8681
9157
 
9158
+
8682
9159
  var afterRenderEvent = function (vditor, options) {
8683
9160
  if (options === void 0) { options = {
8684
9161
  enableAddUndoStack: true,
@@ -8712,6 +9189,13 @@ var afterRenderEvent = function (vditor, options) {
8712
9189
  if (options.enableAddUndoStack) {
8713
9190
  vditor.undo.addToUndoStack(vditor);
8714
9191
  }
9192
+ try {
9193
+ var lnIndex = vditor.lineNumberIndex || prepareLineNumberIndex(text);
9194
+ attachLineNumbersToBlocks(vditor.wysiwyg.element, lnIndex);
9195
+ }
9196
+ catch (_a) {
9197
+ void 0;
9198
+ }
8715
9199
  }, vditor.options.undoDelay);
8716
9200
  };
8717
9201
 
@@ -11197,6 +11681,7 @@ var selectEvent = function (vditor, editorElement) {
11197
11681
 
11198
11682
 
11199
11683
 
11684
+
11200
11685
  var processPaste = function (vditor, text) {
11201
11686
  var range = (0,selection/* getEditorRange */.zh)(vditor);
11202
11687
  range.extractContents();
@@ -11207,9 +11692,10 @@ var processPaste = function (vditor, text) {
11207
11692
  blockElement = vditor.sv.element;
11208
11693
  }
11209
11694
  var spinHTML = vditor.lute.SpinVditorSVDOM(blockElement.textContent);
11210
- spinHTML = "<div data-block='0'>" +
11211
- spinHTML.replace(/<span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span><span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span></g, '<span data-type="newline"><br /><span style="display: none">\n</span></span><span data-type="newline"><br /><span style="display: none">\n</span></span></div><div data-block="0"><') +
11212
- "</div>";
11695
+ spinHTML =
11696
+ "<div data-block='0'>" +
11697
+ spinHTML.replace(/<span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span><span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span></g, '<span data-type="newline"><br /><span style="display: none">\n</span></span><span data-type="newline"><br /><span style="display: none">\n</span></span></div><div data-block="0"><') +
11698
+ "</div>";
11213
11699
  if (blockElement.isEqualNode(vditor.sv.element)) {
11214
11700
  blockElement.innerHTML = spinHTML;
11215
11701
  }
@@ -11242,9 +11728,10 @@ var getSideByType = function (spanNode, type, isPrevious) {
11242
11728
  var processSpinVditorSVDOM = function (html, vditor) {
11243
11729
  log("SpinVditorSVDOM", html, "argument", vditor.options.debugger);
11244
11730
  var spinHTML = vditor.lute.SpinVditorSVDOM(html);
11245
- html = "<div data-block='0'>" +
11246
- spinHTML.replace(/<span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span><span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span></g, '<span data-type="newline"><br /><span style="display: none">\n</span></span><span data-type="newline"><br /><span style="display: none">\n</span></span></div><div data-block="0"><') +
11247
- "</div>";
11731
+ html =
11732
+ "<div data-block='0'>" +
11733
+ spinHTML.replace(/<span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span><span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span></g, '<span data-type="newline"><br /><span style="display: none">\n</span></span><span data-type="newline"><br /><span style="display: none">\n</span></span></div><div data-block="0"><') +
11734
+ "</div>";
11248
11735
  log("SpinVditorSVDOM", html, "result", vditor.options.debugger);
11249
11736
  return html;
11250
11737
  };
@@ -11252,21 +11739,32 @@ var processPreviousMarkers = function (spanElement) {
11252
11739
  var spanType = spanElement.getAttribute("data-type");
11253
11740
  var previousElement = spanElement.previousElementSibling;
11254
11741
  // 有内容的子列表/标题,在其 marker 后换行
11255
- var markerText = (spanType && spanType !== "text" && spanType !== "table" && spanType !== "heading-marker" &&
11256
- spanType !== "newline" && spanType !== "yaml-front-matter-open-marker" && spanType !== "yaml-front-matter-close-marker"
11257
- && spanType !== "code-block-info" && spanType !== "code-block-close-marker" && spanType !== "code-block-open-marker") ?
11258
- spanElement.textContent : "";
11742
+ var markerText = spanType &&
11743
+ spanType !== "text" &&
11744
+ spanType !== "table" &&
11745
+ spanType !== "heading-marker" &&
11746
+ spanType !== "newline" &&
11747
+ spanType !== "yaml-front-matter-open-marker" &&
11748
+ spanType !== "yaml-front-matter-close-marker" &&
11749
+ spanType !== "code-block-info" &&
11750
+ spanType !== "code-block-close-marker" &&
11751
+ spanType !== "code-block-open-marker"
11752
+ ? spanElement.textContent
11753
+ : "";
11259
11754
  var hasNL = false;
11260
11755
  if (spanType === "newline") {
11261
11756
  hasNL = true;
11262
11757
  }
11263
11758
  while (previousElement && !hasNL) {
11264
11759
  var previousType = previousElement.getAttribute("data-type");
11265
- if (previousType === "li-marker" || previousType === "blockquote-marker" || previousType === "task-marker" ||
11760
+ if (previousType === "li-marker" ||
11761
+ previousType === "blockquote-marker" ||
11762
+ previousType === "task-marker" ||
11266
11763
  previousType === "padding") {
11267
11764
  var previousText = previousElement.textContent;
11268
11765
  if (previousType === "li-marker" &&
11269
- (spanType === "code-block-open-marker" || spanType === "code-block-info")) {
11766
+ (spanType === "code-block-open-marker" ||
11767
+ spanType === "code-block-info")) {
11270
11768
  // https://github.com/Vanessa219/vditor/issues/586
11271
11769
  markerText = previousText.replace(/\S/g, " ") + markerText;
11272
11770
  }
@@ -11321,6 +11819,13 @@ var processAfterRender = function (vditor, options) {
11321
11819
  if (options.enableAddUndoStack && !vditor.sv.composingLock) {
11322
11820
  vditor.undo.addToUndoStack(vditor);
11323
11821
  }
11822
+ try {
11823
+ var lnIndex = vditor.lineNumberIndex || prepareLineNumberIndex(text);
11824
+ attachLineNumbersToBlocks(vditor.sv.element, lnIndex);
11825
+ }
11826
+ catch (_a) {
11827
+ void 0;
11828
+ }
11324
11829
  }, vditor.options.undoDelay);
11325
11830
  };
11326
11831
  var processHeading = function (vditor, value) {
@@ -11356,8 +11861,13 @@ var processToolbar = function (vditor, actionBtn, prefix, suffix) {
11356
11861
  document.execCommand("insertHTML", false, html);
11357
11862
  return;
11358
11863
  }
11359
- else if (commandName === "italic" || commandName === "bold" || commandName === "strike" ||
11360
- commandName === "inline-code" || commandName === "code" || commandName === "table" || commandName === "line") {
11864
+ else if (commandName === "italic" ||
11865
+ commandName === "bold" ||
11866
+ commandName === "strike" ||
11867
+ commandName === "inline-code" ||
11868
+ commandName === "code" ||
11869
+ commandName === "table" ||
11870
+ commandName === "line") {
11361
11871
  var html = void 0;
11362
11872
  // https://github.com/Vanessa219/vditor/issues/563 代码块不需要后面的 ```
11363
11873
  if (range.toString() === "") {
@@ -11366,7 +11876,10 @@ var processToolbar = function (vditor, actionBtn, prefix, suffix) {
11366
11876
  else {
11367
11877
  html = "".concat(prefix).concat(range.toString()).concat(Lute.Caret).concat(commandName === "code" ? "" : suffix);
11368
11878
  }
11369
- if (commandName === "table" || (commandName === "code" && spanElement && spanElement.textContent !== "")) {
11879
+ if (commandName === "table" ||
11880
+ (commandName === "code" &&
11881
+ spanElement &&
11882
+ spanElement.textContent !== "")) {
11370
11883
  html = "\n\n" + html;
11371
11884
  }
11372
11885
  else if (commandName === "line") {
@@ -11375,7 +11888,9 @@ var processToolbar = function (vditor, actionBtn, prefix, suffix) {
11375
11888
  document.execCommand("insertHTML", false, html);
11376
11889
  return;
11377
11890
  }
11378
- else if (commandName === "check" || commandName === "list" || commandName === "ordered-list" ||
11891
+ else if (commandName === "check" ||
11892
+ commandName === "list" ||
11893
+ commandName === "ordered-list" ||
11379
11894
  commandName === "quote") {
11380
11895
  if (spanElement) {
11381
11896
  var marker = "* ";
@@ -12415,12 +12930,25 @@ var isHeadingMD = function (text) {
12415
12930
  }
12416
12931
  return false;
12417
12932
  };
12933
+
12934
+
12418
12935
  var execAfterRender = function (vditor, options) {
12419
12936
  if (options === void 0) { options = {
12420
12937
  enableAddUndoStack: true,
12421
12938
  enableHint: false,
12422
12939
  enableInput: true,
12423
12940
  }; }
12941
+ try {
12942
+ var md = getMarkdown(vditor);
12943
+ var cached = vditor.lineNumberIndex;
12944
+ var same = cached &&
12945
+ Array.isArray(cached.srcLines) &&
12946
+ cached.srcLines.join("\n") === md;
12947
+ vditor.lineNumberIndex = same ? cached : prepareLineNumberIndex(md);
12948
+ }
12949
+ catch (_a) {
12950
+ vditor.lineNumberIndex = undefined;
12951
+ }
12424
12952
  if (vditor.currentMode === "wysiwyg") {
12425
12953
  afterRenderEvent(vditor, options);
12426
12954
  }
@@ -13682,6 +14210,7 @@ var templateObject_1;
13682
14210
 
13683
14211
 
13684
14212
 
14213
+
13685
14214
  var processHint = function (vditor) {
13686
14215
  var _a, _b;
13687
14216
  vditor.hint.render(vditor);
@@ -13689,16 +14218,20 @@ var processHint = function (vditor) {
13689
14218
  // 代码块语言提示
13690
14219
  var preBeforeElement = (0,hasClosest/* hasClosestByAttribute */.a1)(startContainer, "data-type", "code-block-info");
13691
14220
  if (preBeforeElement) {
13692
- if (preBeforeElement.textContent.replace(constants/* Constants.ZWSP */.g.ZWSP, "") === "" && vditor.hint.recentLanguage) {
13693
- preBeforeElement.textContent = constants/* Constants.ZWSP */.g.ZWSP + vditor.hint.recentLanguage;
14221
+ if (preBeforeElement.textContent.replace(constants/* Constants.ZWSP */.g.ZWSP, "") === "" &&
14222
+ vditor.hint.recentLanguage) {
14223
+ preBeforeElement.textContent =
14224
+ constants/* Constants.ZWSP */.g.ZWSP + vditor.hint.recentLanguage;
13694
14225
  var range = (0,selection/* getEditorRange */.zh)(vditor);
13695
14226
  range.selectNodeContents(preBeforeElement);
13696
14227
  }
13697
14228
  else {
13698
14229
  var matchLangData_1 = [];
13699
- var key_1 = preBeforeElement.textContent.substring(0, (0,selection/* getSelectPosition */.im)(preBeforeElement, vditor.ir.element).start)
14230
+ var key_1 = preBeforeElement.textContent
14231
+ .substring(0, (0,selection/* getSelectPosition */.im)(preBeforeElement, vditor.ir.element).start)
13700
14232
  .replace(constants/* Constants.ZWSP */.g.ZWSP, "");
13701
- (vditor.options.preview.hljs.langs || constants/* Constants.ALIAS_CODE_LANGUAGES.concat */.g.ALIAS_CODE_LANGUAGES.concat(((_b = (_a = window.hljs) === null || _a === void 0 ? void 0 : _a.listLanguages()) !== null && _b !== void 0 ? _b : []).sort())).forEach(function (keyName) {
14233
+ (vditor.options.preview.hljs.langs ||
14234
+ constants/* Constants.ALIAS_CODE_LANGUAGES.concat */.g.ALIAS_CODE_LANGUAGES.concat(((_b = (_a = window.hljs) === null || _a === void 0 ? void 0 : _a.listLanguages()) !== null && _b !== void 0 ? _b : []).sort())).forEach(function (keyName) {
13702
14235
  if (keyName.indexOf(key_1.toLowerCase()) > -1) {
13703
14236
  matchLangData_1.push({
13704
14237
  html: keyName,
@@ -13743,11 +14276,19 @@ var process_processAfterRender = function (vditor, options) {
13743
14276
  if (options.enableAddUndoStack) {
13744
14277
  vditor.undo.addToUndoStack(vditor);
13745
14278
  }
14279
+ try {
14280
+ var lnIndex = vditor.lineNumberIndex || prepareLineNumberIndex(text);
14281
+ attachLineNumbersToBlocks(vditor.ir.element, lnIndex);
14282
+ }
14283
+ catch (_a) {
14284
+ void 0;
14285
+ }
13746
14286
  }, vditor.options.undoDelay);
13747
14287
  };
13748
14288
  var process_processHeading = function (vditor, value) {
13749
14289
  var range = (0,selection/* getEditorRange */.zh)(vditor);
13750
- var headingElement = (0,hasClosest/* hasClosestBlock */.F9)(range.startContainer) || range.startContainer;
14290
+ var headingElement = (0,hasClosest/* hasClosestBlock */.F9)(range.startContainer) ||
14291
+ range.startContainer;
13751
14292
  if (headingElement) {
13752
14293
  var headingMarkerElement = headingElement.querySelector(".vditor-ir__marker--heading");
13753
14294
  if (headingMarkerElement) {
@@ -13770,7 +14311,8 @@ var removeInline = function (range, vditor, type) {
13770
14311
  range.insertNode(document.createElement("wbr"));
13771
14312
  var tempElement = document.createElement("div");
13772
14313
  tempElement.innerHTML = vditor.lute.SpinVditorIRDOM(inlineElement.outerHTML);
13773
- inlineElement.outerHTML = tempElement.firstElementChild.innerHTML.trim();
14314
+ inlineElement.outerHTML =
14315
+ tempElement.firstElementChild.innerHTML.trim();
13774
14316
  }
13775
14317
  };
13776
14318
  var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
@@ -13787,8 +14329,10 @@ var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
13787
14329
  var quoteElement = (0,hasClosest/* hasClosestByMatchTag */.lG)(typeElement, "BLOCKQUOTE");
13788
14330
  if (quoteElement) {
13789
14331
  range.insertNode(document.createElement("wbr"));
13790
- quoteElement.outerHTML = quoteElement.innerHTML.trim() === "" ?
13791
- "<p data-block=\"0\">".concat(quoteElement.innerHTML, "</p>") : quoteElement.innerHTML;
14332
+ quoteElement.outerHTML =
14333
+ quoteElement.innerHTML.trim() === ""
14334
+ ? "<p data-block=\"0\">".concat(quoteElement.innerHTML, "</p>")
14335
+ : quoteElement.innerHTML;
13792
14336
  }
13793
14337
  }
13794
14338
  else if (commandName === "link") {
@@ -13800,7 +14344,9 @@ var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
13800
14344
  aElement.outerHTML = aTextElement.innerHTML;
13801
14345
  }
13802
14346
  else {
13803
- aElement.outerHTML = aElement.querySelector(".vditor-ir__link").innerHTML + "<wbr>";
14347
+ aElement.outerHTML =
14348
+ aElement.querySelector(".vditor-ir__link").innerHTML +
14349
+ "<wbr>";
13804
14350
  }
13805
14351
  }
13806
14352
  }
@@ -13816,7 +14362,9 @@ var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
13816
14362
  else if (commandName === "inline-code") {
13817
14363
  removeInline(range, vditor, "code");
13818
14364
  }
13819
- else if (commandName === "check" || commandName === "list" || commandName === "ordered-list") {
14365
+ else if (commandName === "check" ||
14366
+ commandName === "list" ||
14367
+ commandName === "ordered-list") {
13820
14368
  listToggle(vditor, range, commandName);
13821
14369
  useHighlight = false;
13822
14370
  actionBtn.classList.remove("vditor-menu--current");
@@ -13860,8 +14408,12 @@ var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
13860
14408
  useHighlight = false;
13861
14409
  actionBtn.classList.add("vditor-menu--current");
13862
14410
  }
13863
- else if (commandName === "italic" || commandName === "bold" || commandName === "strike"
13864
- || commandName === "inline-code" || commandName === "code" || commandName === "table") {
14411
+ else if (commandName === "italic" ||
14412
+ commandName === "bold" ||
14413
+ commandName === "strike" ||
14414
+ commandName === "inline-code" ||
14415
+ commandName === "code" ||
14416
+ commandName === "table") {
13865
14417
  var html = void 0;
13866
14418
  if (range.toString() === "") {
13867
14419
  html = "".concat(prefix, "<wbr>").concat(suffix);
@@ -13890,10 +14442,16 @@ var process_processToolbar = function (vditor, actionBtn, prefix, suffix) {
13890
14442
  (0,selection/* setSelectionFocus */.Hc)(range);
13891
14443
  }
13892
14444
  }
13893
- else if (commandName === "check" || commandName === "list" || commandName === "ordered-list") {
14445
+ else if (commandName === "check" ||
14446
+ commandName === "list" ||
14447
+ commandName === "ordered-list") {
13894
14448
  listToggle(vditor, range, commandName, false);
13895
14449
  useHighlight = false;
13896
- removeCurrentToolbar(vditor.toolbar.elements, ["check", "list", "ordered-list"]);
14450
+ removeCurrentToolbar(vditor.toolbar.elements, [
14451
+ "check",
14452
+ "list",
14453
+ "ordered-list",
14454
+ ]);
13897
14455
  actionBtn.classList.add("vditor-menu--current");
13898
14456
  }
13899
14457
  }
@@ -17732,6 +18290,7 @@ var src_extends = (undefined && undefined.__extends) || (function () {
17732
18290
 
17733
18291
 
17734
18292
 
18293
+
17735
18294
 
17736
18295
 
17737
18296
  /**
@@ -18025,6 +18584,12 @@ var Vditor = /** @class */ (function (_super) {
18025
18584
  Vditor.prototype.setValue = function (markdown, clearStack) {
18026
18585
  var _this = this;
18027
18586
  if (clearStack === void 0) { clearStack = false; }
18587
+ try {
18588
+ this.vditor.lineNumberIndex = prepareLineNumberIndex(markdown);
18589
+ }
18590
+ catch (_a) {
18591
+ this.vditor.lineNumberIndex = undefined;
18592
+ }
18028
18593
  if (this.vditor.currentMode === "sv") {
18029
18594
  this.vditor.sv.element.innerHTML = "<div data-block='0'>".concat(this.vditor.lute.SpinVditorSVDOM(markdown), "</div>");
18030
18595
  processAfterRender(this.vditor, {