markdown-codec 0.0.0 → 1.0.0
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/LICENSE +21 -0
- package/README.md +215 -0
- package/dist/ast/ast.cjs +25 -0
- package/dist/ast/ast.d.cts +2 -0
- package/dist/ast/ast.d.ts +2 -0
- package/dist/ast/ast.js +23 -0
- package/dist/ast-BXYwy08e.d.cts +160 -0
- package/dist/ast-BXYwy08e.d.ts +160 -0
- package/dist/block/block.cjs +530 -0
- package/dist/block/block.d.cts +18 -0
- package/dist/block/block.d.ts +18 -0
- package/dist/block/block.js +529 -0
- package/dist/block/definitions.cjs +59 -0
- package/dist/block/definitions.d.cts +6 -0
- package/dist/block/definitions.d.ts +6 -0
- package/dist/block/definitions.js +58 -0
- package/dist/block/line.cjs +76 -0
- package/dist/block/line.d.cts +28 -0
- package/dist/block/line.d.ts +28 -0
- package/dist/block/line.js +74 -0
- package/dist/block/list.cjs +110 -0
- package/dist/block/list.d.cts +8 -0
- package/dist/block/list.d.ts +8 -0
- package/dist/block/list.js +107 -0
- package/dist/block/node.cjs +68 -0
- package/dist/block/node.d.cts +45 -0
- package/dist/block/node.d.ts +45 -0
- package/dist/block/node.js +65 -0
- package/dist/block/table.cjs +64 -0
- package/dist/block/table.d.cts +7 -0
- package/dist/block/table.d.ts +7 -0
- package/dist/block/table.js +61 -0
- package/dist/codec.cjs +22 -0
- package/dist/codec.d.cts +361 -0
- package/dist/codec.d.ts +361 -0
- package/dist/codec.js +20 -0
- package/dist/defaults/defaults.cjs +31 -0
- package/dist/defaults/defaults.d.cts +16 -0
- package/dist/defaults/defaults.d.ts +16 -0
- package/dist/defaults/defaults.js +20 -0
- package/dist/diagnostics/diagnostics.cjs +85 -0
- package/dist/diagnostics/diagnostics.d.cts +2 -0
- package/dist/diagnostics/diagnostics.d.ts +2 -0
- package/dist/diagnostics/diagnostics.js +77 -0
- package/dist/diagnostics-DmuWc7d8.d.cts +61 -0
- package/dist/diagnostics-DmuWc7d8.d.ts +61 -0
- package/dist/emit/emit.cjs +228 -0
- package/dist/emit/emit.d.cts +6 -0
- package/dist/emit/emit.d.ts +6 -0
- package/dist/emit/emit.js +227 -0
- package/dist/emit/front-matter.cjs +24 -0
- package/dist/emit/front-matter.d.cts +5 -0
- package/dist/emit/front-matter.d.ts +5 -0
- package/dist/emit/front-matter.js +23 -0
- package/dist/emit/image.cjs +10 -0
- package/dist/emit/image.d.cts +5 -0
- package/dist/emit/image.d.ts +5 -0
- package/dist/emit/image.js +9 -0
- package/dist/emit/inline.cjs +183 -0
- package/dist/emit/inline.d.cts +12 -0
- package/dist/emit/inline.d.ts +12 -0
- package/dist/emit/inline.js +180 -0
- package/dist/emit/table.cjs +73 -0
- package/dist/emit/table.d.cts +10 -0
- package/dist/emit/table.d.ts +10 -0
- package/dist/emit/table.js +72 -0
- package/dist/html/html.cjs +116 -0
- package/dist/html/html.d.cts +2 -0
- package/dist/html/html.d.ts +2 -0
- package/dist/html/html.js +113 -0
- package/dist/html/render.cjs +193 -0
- package/dist/html/render.d.cts +8 -0
- package/dist/html/render.d.ts +8 -0
- package/dist/html/render.js +189 -0
- package/dist/html-bkz2QTuq.d.cts +7 -0
- package/dist/html-bkz2QTuq.d.ts +7 -0
- package/dist/image/image.cjs +129 -0
- package/dist/image/image.d.cts +2 -0
- package/dist/image/image.d.ts +2 -0
- package/dist/image/image.js +125 -0
- package/dist/image-C4KYmz_L.d.cts +19 -0
- package/dist/image-Cm3hT5PS.d.ts +19 -0
- package/dist/image-DescdRgq.d.cts +12 -0
- package/dist/image-DescdRgq.d.ts +12 -0
- package/dist/index.cjs +17 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/inline/chars.cjs +50 -0
- package/dist/inline/chars.d.cts +11 -0
- package/dist/inline/chars.d.ts +11 -0
- package/dist/inline/chars.js +42 -0
- package/dist/inline/delimiter.cjs +136 -0
- package/dist/inline/delimiter.d.cts +29 -0
- package/dist/inline/delimiter.d.ts +29 -0
- package/dist/inline/delimiter.js +132 -0
- package/dist/inline/entity.cjs +67 -0
- package/dist/inline/entity.d.cts +9 -0
- package/dist/inline/entity.d.ts +9 -0
- package/dist/inline/entity.js +65 -0
- package/dist/inline/gfm-autolink.cjs +156 -0
- package/dist/inline/gfm-autolink.d.cts +5 -0
- package/dist/inline/gfm-autolink.d.ts +5 -0
- package/dist/inline/gfm-autolink.js +155 -0
- package/dist/inline/inline.cjs +457 -0
- package/dist/inline/inline.d.cts +2 -0
- package/dist/inline/inline.d.ts +2 -0
- package/dist/inline/inline.js +456 -0
- package/dist/inline/link.cjs +130 -0
- package/dist/inline/link.d.cts +2 -0
- package/dist/inline/link.d.ts +2 -0
- package/dist/inline/link.js +124 -0
- package/dist/inline/node.cjs +57 -0
- package/dist/inline/node.d.cts +23 -0
- package/dist/inline/node.d.ts +23 -0
- package/dist/inline/node.js +55 -0
- package/dist/inline-B50cM6cn.d.ts +10 -0
- package/dist/inline-C9YEGDV5.d.cts +10 -0
- package/dist/link-Dv4kxVjk.d.cts +18 -0
- package/dist/link-Dv4kxVjk.d.ts +18 -0
- package/dist/lower/front-matter.cjs +75 -0
- package/dist/lower/front-matter.d.cts +10 -0
- package/dist/lower/front-matter.d.ts +10 -0
- package/dist/lower/front-matter.js +74 -0
- package/dist/lower/image.cjs +29 -0
- package/dist/lower/image.d.cts +2 -0
- package/dist/lower/image.d.ts +2 -0
- package/dist/lower/image.js +28 -0
- package/dist/lower/inline.cjs +96 -0
- package/dist/lower/inline.d.cts +19 -0
- package/dist/lower/inline.d.ts +19 -0
- package/dist/lower/inline.js +93 -0
- package/dist/lower/lower.cjs +293 -0
- package/dist/lower/lower.d.cts +8 -0
- package/dist/lower/lower.d.ts +8 -0
- package/dist/lower/lower.js +291 -0
- package/dist/lower/table.cjs +27 -0
- package/dist/lower/table.d.cts +7 -0
- package/dist/lower/table.d.ts +7 -0
- package/dist/lower/table.js +26 -0
- package/dist/options/options.cjs +0 -0
- package/dist/options/options.d.cts +43 -0
- package/dist/options/options.d.ts +43 -0
- package/dist/options/options.js +0 -0
- package/dist/read.cjs +20 -0
- package/dist/read.d.cts +11 -0
- package/dist/read.d.ts +11 -0
- package/dist/read.js +19 -0
- package/dist/scan/entity-table.cjs +2131 -0
- package/dist/scan/entity-table.d.cts +4 -0
- package/dist/scan/entity-table.d.ts +4 -0
- package/dist/scan/entity-table.js +2130 -0
- package/dist/scan/scan.cjs +80 -0
- package/dist/scan/scan.d.cts +2 -0
- package/dist/scan/scan.d.ts +2 -0
- package/dist/scan/scan.js +78 -0
- package/dist/scan-DeBxgG-r.d.cts +30 -0
- package/dist/scan-DeBxgG-r.d.ts +30 -0
- package/dist/shared/list-id.cjs +36 -0
- package/dist/shared/list-id.d.cts +22 -0
- package/dist/shared/list-id.d.ts +22 -0
- package/dist/shared/list-id.js +32 -0
- package/dist/shared/style-constants.cjs +34 -0
- package/dist/shared/style-constants.d.cts +14 -0
- package/dist/shared/style-constants.d.ts +14 -0
- package/dist/shared/style-constants.js +23 -0
- package/dist/write.cjs +9 -0
- package/dist/write.d.cts +6 -0
- package/dist/write.d.ts +6 -0
- package/dist/write.js +8 -0
- package/package.json +92 -2
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { matchHtmlTag } from "../html/html.js";
|
|
2
|
+
import { containsAsciiControlOrSpace, isAsciiPunctuation } from "./chars.js";
|
|
3
|
+
import { matchEntity } from "./entity.js";
|
|
4
|
+
import { DelimiterStack, isDelimiterChar, processEmphasis, scanDelimiterRun } from "./delimiter.js";
|
|
5
|
+
import { InlineNode, createTextNode } from "./node.js";
|
|
6
|
+
import { applyGfmAutolinks } from "./gfm-autolink.js";
|
|
7
|
+
import { matchLinkLabel, normalizeLinkLabel, parseLinkDestination, parseLinkTitle, skipInlineWhitespace } from "./link.js";
|
|
8
|
+
//#region src/inline/inline.ts
|
|
9
|
+
const PLAIN_TEXT_PATTERN = /[^\n`[\]\\!<&*_~]+/y;
|
|
10
|
+
const URI_AUTOLINK_PATTERN = /<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>]*>/y;
|
|
11
|
+
const EMAIL_AUTOLINK_PATTERN = /<([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/y;
|
|
12
|
+
const HARD_BREAK_SPACES = " ";
|
|
13
|
+
const EMPTY_LABEL_LENGTH = 2;
|
|
14
|
+
function stripOneSurroundingSpace(content) {
|
|
15
|
+
if (content.startsWith(" ") && content.endsWith(" ") && /[^ ]/.test(content)) return content.slice(1, content.length - 1);
|
|
16
|
+
return content;
|
|
17
|
+
}
|
|
18
|
+
function createWrapper(kind, marker) {
|
|
19
|
+
const node = new InlineNode(kind);
|
|
20
|
+
if (marker !== "~") node.marker = marker;
|
|
21
|
+
return node;
|
|
22
|
+
}
|
|
23
|
+
var InlineParser = class {
|
|
24
|
+
text;
|
|
25
|
+
references;
|
|
26
|
+
gfmStrikethrough;
|
|
27
|
+
container = new InlineNode("container");
|
|
28
|
+
delimiters = new DelimiterStack();
|
|
29
|
+
brackets;
|
|
30
|
+
pos = 0;
|
|
31
|
+
constructor(text, references, options) {
|
|
32
|
+
this.text = text;
|
|
33
|
+
this.references = references;
|
|
34
|
+
this.gfmStrikethrough = options.gfmStrikethrough ?? true;
|
|
35
|
+
}
|
|
36
|
+
parse() {
|
|
37
|
+
while (this.pos < this.text.length) this.step();
|
|
38
|
+
processEmphasis(this.delimiters, void 0, createWrapper);
|
|
39
|
+
mergeAdjacentText(this.container);
|
|
40
|
+
return this.container;
|
|
41
|
+
}
|
|
42
|
+
step() {
|
|
43
|
+
const char = this.text.charAt(this.pos);
|
|
44
|
+
switch (char) {
|
|
45
|
+
case "\n":
|
|
46
|
+
this.parseLineBreak();
|
|
47
|
+
return;
|
|
48
|
+
case "\\":
|
|
49
|
+
this.parseBackslash();
|
|
50
|
+
return;
|
|
51
|
+
case "`":
|
|
52
|
+
this.parseCodeSpan();
|
|
53
|
+
return;
|
|
54
|
+
case "<":
|
|
55
|
+
this.parseAngleBracket();
|
|
56
|
+
return;
|
|
57
|
+
case "&":
|
|
58
|
+
this.parseEntity();
|
|
59
|
+
return;
|
|
60
|
+
case "[":
|
|
61
|
+
this.parseOpenBracket();
|
|
62
|
+
return;
|
|
63
|
+
case "!":
|
|
64
|
+
this.parseBang();
|
|
65
|
+
return;
|
|
66
|
+
case "]":
|
|
67
|
+
this.parseCloseBracket();
|
|
68
|
+
return;
|
|
69
|
+
default:
|
|
70
|
+
if (isDelimiterChar(char)) {
|
|
71
|
+
this.parseDelimiterRun(char);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.parsePlainText();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
appendText(literal) {
|
|
78
|
+
const node = createTextNode(literal);
|
|
79
|
+
this.container.appendChild(node);
|
|
80
|
+
return node;
|
|
81
|
+
}
|
|
82
|
+
parsePlainText() {
|
|
83
|
+
PLAIN_TEXT_PATTERN.lastIndex = this.pos;
|
|
84
|
+
const match = PLAIN_TEXT_PATTERN.exec(this.text);
|
|
85
|
+
if (match === null) {
|
|
86
|
+
this.appendText(this.text.charAt(this.pos));
|
|
87
|
+
this.pos += 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.appendText(match[0]);
|
|
91
|
+
this.pos += match[0].length;
|
|
92
|
+
}
|
|
93
|
+
parseLineBreak() {
|
|
94
|
+
this.pos += 1;
|
|
95
|
+
const last = this.container.lastChild;
|
|
96
|
+
if (last?.kind === "text" && last.literal.endsWith(" ")) {
|
|
97
|
+
const hard = last.literal.endsWith(HARD_BREAK_SPACES);
|
|
98
|
+
last.literal = last.literal.replace(/ +$/, "");
|
|
99
|
+
this.container.appendChild(new InlineNode(hard ? "hardBreak" : "softBreak"));
|
|
100
|
+
} else this.container.appendChild(new InlineNode("softBreak"));
|
|
101
|
+
while (this.text.charAt(this.pos) === " ") this.pos += 1;
|
|
102
|
+
}
|
|
103
|
+
parseBackslash() {
|
|
104
|
+
this.pos += 1;
|
|
105
|
+
const next = this.text.charAt(this.pos);
|
|
106
|
+
if (next === "\n") {
|
|
107
|
+
this.pos += 1;
|
|
108
|
+
this.container.appendChild(new InlineNode("hardBreak"));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (isAsciiPunctuation(next)) {
|
|
112
|
+
this.appendText(next);
|
|
113
|
+
this.pos += 1;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
this.appendText("\\");
|
|
117
|
+
}
|
|
118
|
+
parseCodeSpan() {
|
|
119
|
+
const start = this.pos;
|
|
120
|
+
let openLength = 0;
|
|
121
|
+
while (this.text.charAt(start + openLength) === "`") openLength += 1;
|
|
122
|
+
const afterOpen = start + openLength;
|
|
123
|
+
let scan = afterOpen;
|
|
124
|
+
while (scan < this.text.length) {
|
|
125
|
+
if (this.text.charAt(scan) !== "`") {
|
|
126
|
+
scan += 1;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
let runLength = 0;
|
|
130
|
+
while (this.text.charAt(scan + runLength) === "`") runLength += 1;
|
|
131
|
+
if (runLength === openLength) {
|
|
132
|
+
const node = new InlineNode("codeSpan");
|
|
133
|
+
node.literal = stripOneSurroundingSpace(this.text.slice(afterOpen, scan).replace(/\n/g, " "));
|
|
134
|
+
this.container.appendChild(node);
|
|
135
|
+
this.pos = scan + runLength;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
scan += runLength;
|
|
139
|
+
}
|
|
140
|
+
this.appendText(this.text.slice(start, afterOpen));
|
|
141
|
+
this.pos = afterOpen;
|
|
142
|
+
}
|
|
143
|
+
parseAngleBracket() {
|
|
144
|
+
const uri = this.matchUriAutolink();
|
|
145
|
+
if (uri !== void 0) {
|
|
146
|
+
const node = new InlineNode("autolink");
|
|
147
|
+
node.destination = uri;
|
|
148
|
+
this.container.appendChild(node);
|
|
149
|
+
this.pos += uri.length + 2;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
EMAIL_AUTOLINK_PATTERN.lastIndex = this.pos;
|
|
153
|
+
const email = EMAIL_AUTOLINK_PATTERN.exec(this.text);
|
|
154
|
+
const address = email?.[1];
|
|
155
|
+
if (email !== null && address !== void 0) {
|
|
156
|
+
const node = new InlineNode("autolink");
|
|
157
|
+
node.destination = address;
|
|
158
|
+
node.email = true;
|
|
159
|
+
this.container.appendChild(node);
|
|
160
|
+
this.pos += email[0].length;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const tag = matchHtmlTag(this.text, this.pos);
|
|
164
|
+
if (tag !== void 0) {
|
|
165
|
+
const node = new InlineNode("rawHtml");
|
|
166
|
+
node.literal = tag;
|
|
167
|
+
this.container.appendChild(node);
|
|
168
|
+
this.pos += tag.length;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
this.appendText("<");
|
|
172
|
+
this.pos += 1;
|
|
173
|
+
}
|
|
174
|
+
matchUriAutolink() {
|
|
175
|
+
URI_AUTOLINK_PATTERN.lastIndex = this.pos;
|
|
176
|
+
const match = URI_AUTOLINK_PATTERN.exec(this.text);
|
|
177
|
+
if (match === null) return;
|
|
178
|
+
const uri = match[0].slice(1, match[0].length - 1);
|
|
179
|
+
return containsAsciiControlOrSpace(uri) ? void 0 : uri;
|
|
180
|
+
}
|
|
181
|
+
parseEntity() {
|
|
182
|
+
const entity = matchEntity(this.text, this.pos);
|
|
183
|
+
if (entity === void 0) {
|
|
184
|
+
this.appendText("&");
|
|
185
|
+
this.pos += 1;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const node = new InlineNode("entity");
|
|
189
|
+
node.raw = entity.raw;
|
|
190
|
+
node.literal = entity.value;
|
|
191
|
+
this.container.appendChild(node);
|
|
192
|
+
this.pos += entity.raw.length;
|
|
193
|
+
}
|
|
194
|
+
parseDelimiterRun(char) {
|
|
195
|
+
if (char === "~" && !this.gfmStrikethrough) {
|
|
196
|
+
this.parseLiteralRun(char);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const run = scanDelimiterRun(this.text, this.pos, char);
|
|
200
|
+
if (run === void 0) {
|
|
201
|
+
this.parseLiteralRun(char);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const node = this.appendText(this.text.slice(this.pos, this.pos + run.count));
|
|
205
|
+
this.pos += run.count;
|
|
206
|
+
if (run.canOpen || run.canClose) this.delimiters.push(char, run, node);
|
|
207
|
+
}
|
|
208
|
+
parseLiteralRun(char) {
|
|
209
|
+
let length = 0;
|
|
210
|
+
while (this.text.charAt(this.pos + length) === char) length += 1;
|
|
211
|
+
this.appendText(this.text.slice(this.pos, this.pos + length));
|
|
212
|
+
this.pos += length;
|
|
213
|
+
}
|
|
214
|
+
pushBracket(node, index, image) {
|
|
215
|
+
if (this.brackets !== void 0) this.brackets.bracketAfter = true;
|
|
216
|
+
this.brackets = {
|
|
217
|
+
node,
|
|
218
|
+
previous: this.brackets,
|
|
219
|
+
previousDelimiter: this.delimiters.top,
|
|
220
|
+
index,
|
|
221
|
+
image,
|
|
222
|
+
active: true,
|
|
223
|
+
bracketAfter: false
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
parseOpenBracket() {
|
|
227
|
+
const start = this.pos;
|
|
228
|
+
this.pos += 1;
|
|
229
|
+
this.pushBracket(this.appendText("["), start, false);
|
|
230
|
+
}
|
|
231
|
+
parseBang() {
|
|
232
|
+
const start = this.pos;
|
|
233
|
+
this.pos += 1;
|
|
234
|
+
if (this.text.charAt(this.pos) !== "[") {
|
|
235
|
+
this.appendText("!");
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.pos += 1;
|
|
239
|
+
this.pushBracket(this.appendText("!["), start + 1, true);
|
|
240
|
+
}
|
|
241
|
+
parseCloseBracket() {
|
|
242
|
+
this.pos += 1;
|
|
243
|
+
const afterCloseBracket = this.pos;
|
|
244
|
+
const opener = this.brackets;
|
|
245
|
+
if (opener === void 0) {
|
|
246
|
+
this.appendText("]");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (!opener.active) {
|
|
250
|
+
this.brackets = opener.previous;
|
|
251
|
+
this.appendText("]");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const target = this.resolveInlineLink() ?? this.resolveReferenceLink(opener, afterCloseBracket);
|
|
255
|
+
if (target === void 0) {
|
|
256
|
+
this.brackets = opener.previous;
|
|
257
|
+
this.pos = afterCloseBracket;
|
|
258
|
+
this.appendText("]");
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const node = new InlineNode(opener.image ? "image" : "link");
|
|
262
|
+
node.destination = target.destination;
|
|
263
|
+
node.title = target.title;
|
|
264
|
+
let moving = opener.node.next;
|
|
265
|
+
while (moving !== void 0) {
|
|
266
|
+
const following = moving.next;
|
|
267
|
+
node.appendChild(moving);
|
|
268
|
+
moving = following;
|
|
269
|
+
}
|
|
270
|
+
this.container.appendChild(node);
|
|
271
|
+
processEmphasis(this.delimiters, opener.previousDelimiter, createWrapper);
|
|
272
|
+
this.brackets = opener.previous;
|
|
273
|
+
opener.node.unlink();
|
|
274
|
+
if (!opener.image) {
|
|
275
|
+
let earlier = this.brackets;
|
|
276
|
+
while (earlier !== void 0) {
|
|
277
|
+
if (!earlier.image) earlier.active = false;
|
|
278
|
+
earlier = earlier.previous;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
resolveInlineLink() {
|
|
283
|
+
if (this.text.charAt(this.pos) !== "(") return;
|
|
284
|
+
const start = this.pos;
|
|
285
|
+
const destination = parseLinkDestination(this.text, skipInlineWhitespace(this.text, start + 1));
|
|
286
|
+
if (destination === void 0) {
|
|
287
|
+
this.pos = start;
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const afterDestination = destination.end;
|
|
291
|
+
let cursor = skipInlineWhitespace(this.text, afterDestination);
|
|
292
|
+
let title;
|
|
293
|
+
if (cursor > afterDestination) {
|
|
294
|
+
title = parseLinkTitle(this.text, cursor);
|
|
295
|
+
if (title !== void 0) cursor = skipInlineWhitespace(this.text, title.end);
|
|
296
|
+
}
|
|
297
|
+
if (this.text.charAt(cursor) !== ")") {
|
|
298
|
+
this.pos = start;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
this.pos = cursor + 1;
|
|
302
|
+
return {
|
|
303
|
+
destination: destination.value,
|
|
304
|
+
title: title?.value
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
resolveReferenceLink(opener, afterCloseBracket) {
|
|
308
|
+
const labelStart = this.pos;
|
|
309
|
+
const labelLength = matchLinkLabel(this.text, labelStart);
|
|
310
|
+
let label;
|
|
311
|
+
if (labelLength > EMPTY_LABEL_LENGTH) label = this.text.slice(labelStart, labelStart + labelLength);
|
|
312
|
+
else if (!opener.bracketAfter) label = this.text.slice(opener.index, afterCloseBracket);
|
|
313
|
+
if (labelLength > 0) this.pos = labelStart + labelLength;
|
|
314
|
+
if (label === void 0) return;
|
|
315
|
+
const definition = this.references.get(normalizeLinkLabel(label));
|
|
316
|
+
if (definition === void 0) {
|
|
317
|
+
this.pos = labelStart;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
destination: definition.destination,
|
|
322
|
+
title: definition.title
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
function mergeAdjacentText(node) {
|
|
327
|
+
let child = node.firstChild;
|
|
328
|
+
while (child !== void 0) {
|
|
329
|
+
if (child.kind === "text") {
|
|
330
|
+
let following = child.next;
|
|
331
|
+
while (following?.kind === "text") {
|
|
332
|
+
child.literal += following.literal;
|
|
333
|
+
const after = following.next;
|
|
334
|
+
following.unlink();
|
|
335
|
+
following = after;
|
|
336
|
+
}
|
|
337
|
+
child = child.next;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
mergeAdjacentText(child);
|
|
341
|
+
child = child.next;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function flattenToPlainText(node) {
|
|
345
|
+
switch (node.kind) {
|
|
346
|
+
case "text":
|
|
347
|
+
case "codeSpan":
|
|
348
|
+
case "rawHtml":
|
|
349
|
+
case "entity": return node.literal;
|
|
350
|
+
case "autolink": return node.destination;
|
|
351
|
+
case "softBreak":
|
|
352
|
+
case "hardBreak": return " ";
|
|
353
|
+
default: {
|
|
354
|
+
let result = "";
|
|
355
|
+
let child = node.firstChild;
|
|
356
|
+
while (child !== void 0) {
|
|
357
|
+
result += flattenToPlainText(child);
|
|
358
|
+
child = child.next;
|
|
359
|
+
}
|
|
360
|
+
return result;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function toChildAstNodes(node) {
|
|
365
|
+
const children = [];
|
|
366
|
+
let child = node.firstChild;
|
|
367
|
+
while (child !== void 0) {
|
|
368
|
+
const converted = toAstNode(child);
|
|
369
|
+
if (converted !== void 0) children.push(converted);
|
|
370
|
+
child = child.next;
|
|
371
|
+
}
|
|
372
|
+
return children;
|
|
373
|
+
}
|
|
374
|
+
function toLinkAstNode(node) {
|
|
375
|
+
const children = toChildAstNodes(node);
|
|
376
|
+
if (node.title === void 0) return {
|
|
377
|
+
type: "link",
|
|
378
|
+
destination: node.destination,
|
|
379
|
+
children
|
|
380
|
+
};
|
|
381
|
+
return {
|
|
382
|
+
type: "link",
|
|
383
|
+
destination: node.destination,
|
|
384
|
+
title: node.title,
|
|
385
|
+
children
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
function toImageAstNode(node) {
|
|
389
|
+
const alt = flattenToPlainText(node);
|
|
390
|
+
if (node.title === void 0) return {
|
|
391
|
+
type: "image",
|
|
392
|
+
destination: node.destination,
|
|
393
|
+
alt
|
|
394
|
+
};
|
|
395
|
+
return {
|
|
396
|
+
type: "image",
|
|
397
|
+
destination: node.destination,
|
|
398
|
+
title: node.title,
|
|
399
|
+
alt
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function toAstNode(node) {
|
|
403
|
+
switch (node.kind) {
|
|
404
|
+
case "text": return node.literal.length === 0 ? void 0 : {
|
|
405
|
+
type: "text",
|
|
406
|
+
value: node.literal
|
|
407
|
+
};
|
|
408
|
+
case "emphasis": return {
|
|
409
|
+
type: "emphasis",
|
|
410
|
+
marker: node.marker,
|
|
411
|
+
children: toChildAstNodes(node)
|
|
412
|
+
};
|
|
413
|
+
case "strong": return {
|
|
414
|
+
type: "strong",
|
|
415
|
+
marker: node.marker,
|
|
416
|
+
children: toChildAstNodes(node)
|
|
417
|
+
};
|
|
418
|
+
case "strikethrough": return {
|
|
419
|
+
type: "strikethrough",
|
|
420
|
+
children: toChildAstNodes(node)
|
|
421
|
+
};
|
|
422
|
+
case "codeSpan": return {
|
|
423
|
+
type: "codeSpan",
|
|
424
|
+
literal: node.literal
|
|
425
|
+
};
|
|
426
|
+
case "link": return toLinkAstNode(node);
|
|
427
|
+
case "image": return toImageAstNode(node);
|
|
428
|
+
case "autolink": return {
|
|
429
|
+
type: "autolink",
|
|
430
|
+
destination: node.destination,
|
|
431
|
+
email: node.email
|
|
432
|
+
};
|
|
433
|
+
case "hardBreak": return { type: "hardBreak" };
|
|
434
|
+
case "softBreak": return { type: "softBreak" };
|
|
435
|
+
case "rawHtml": return {
|
|
436
|
+
type: "rawHtml",
|
|
437
|
+
literal: node.literal
|
|
438
|
+
};
|
|
439
|
+
case "entity": return {
|
|
440
|
+
type: "entity",
|
|
441
|
+
raw: node.raw,
|
|
442
|
+
value: node.literal
|
|
443
|
+
};
|
|
444
|
+
case "container": return;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function parseInlines(content, references, options = {}) {
|
|
448
|
+
const root = new InlineParser(content, references, options).parse();
|
|
449
|
+
if (options.gfmAutolinks ?? true) {
|
|
450
|
+
applyGfmAutolinks(root);
|
|
451
|
+
mergeAdjacentText(root);
|
|
452
|
+
}
|
|
453
|
+
return toChildAstNodes(root);
|
|
454
|
+
}
|
|
455
|
+
//#endregion
|
|
456
|
+
export { parseInlines };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_inline_chars = require("./chars.cjs");
|
|
3
|
+
const require_inline_entity = require("./entity.cjs");
|
|
4
|
+
//#region src/inline/link.ts
|
|
5
|
+
const MAX_LINK_LABEL_LENGTH = 999;
|
|
6
|
+
function normalizeLinkLabel(labelWithBrackets) {
|
|
7
|
+
return labelWithBrackets.slice(1, labelWithBrackets.length - 1).replace(/^[ \t\r\n]+/, "").replace(/[ \t\r\n]+$/, "").replace(/[ \t\r\n]+/g, " ").toLowerCase().toUpperCase();
|
|
8
|
+
}
|
|
9
|
+
function matchLinkLabel(text, start) {
|
|
10
|
+
if (text.charAt(start) !== "[") return 0;
|
|
11
|
+
let index = start + 1;
|
|
12
|
+
while (index < text.length) {
|
|
13
|
+
const char = text.charAt(index);
|
|
14
|
+
if (char === "\\") {
|
|
15
|
+
index += 2;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (char === "[") return 0;
|
|
19
|
+
if (char === "]") {
|
|
20
|
+
const length = index + 1 - start;
|
|
21
|
+
return length - 2 > MAX_LINK_LABEL_LENGTH ? 0 : length;
|
|
22
|
+
}
|
|
23
|
+
index += 1;
|
|
24
|
+
}
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
function parseLinkDestination(text, start) {
|
|
28
|
+
if (text.charAt(start) === "<") {
|
|
29
|
+
let index = start + 1;
|
|
30
|
+
while (index < text.length) {
|
|
31
|
+
const char = text.charAt(index);
|
|
32
|
+
if (char === "\\") {
|
|
33
|
+
index += 2;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === "\n" || char === "<") return;
|
|
37
|
+
if (char === ">") return {
|
|
38
|
+
value: require_inline_entity.unescapeString(text.slice(start + 1, index)),
|
|
39
|
+
end: index + 1
|
|
40
|
+
};
|
|
41
|
+
index += 1;
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let index = start;
|
|
46
|
+
let openParens = 0;
|
|
47
|
+
while (index < text.length) {
|
|
48
|
+
const char = text.charAt(index);
|
|
49
|
+
if (char === "\\" && text.length > index + 1) {
|
|
50
|
+
index += 2;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (char === "(") {
|
|
54
|
+
openParens += 1;
|
|
55
|
+
index += 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (char === ")") {
|
|
59
|
+
if (openParens === 0) break;
|
|
60
|
+
openParens -= 1;
|
|
61
|
+
index += 1;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (char === " " || require_inline_chars.isAsciiControl(char)) break;
|
|
65
|
+
index += 1;
|
|
66
|
+
}
|
|
67
|
+
if (openParens !== 0) return;
|
|
68
|
+
if (index === start && text.charAt(index) !== ")") return;
|
|
69
|
+
return {
|
|
70
|
+
value: require_inline_entity.unescapeString(text.slice(start, index)),
|
|
71
|
+
end: index
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const TITLE_DELIMITERS = /* @__PURE__ */ new Map([
|
|
75
|
+
["\"", "\""],
|
|
76
|
+
["'", "'"],
|
|
77
|
+
["(", ")"]
|
|
78
|
+
]);
|
|
79
|
+
function parseLinkTitle(text, start) {
|
|
80
|
+
const opener = text.charAt(start);
|
|
81
|
+
const closer = TITLE_DELIMITERS.get(opener);
|
|
82
|
+
if (closer === void 0) return;
|
|
83
|
+
let index = start + 1;
|
|
84
|
+
while (index < text.length) {
|
|
85
|
+
const char = text.charAt(index);
|
|
86
|
+
if (char === "\\") {
|
|
87
|
+
index += 2;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (char === closer) return {
|
|
91
|
+
value: require_inline_entity.unescapeString(text.slice(start + 1, index)),
|
|
92
|
+
end: index + 1
|
|
93
|
+
};
|
|
94
|
+
if (opener === "(" && char === "(") return;
|
|
95
|
+
index += 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function skipInlineWhitespace(text, start) {
|
|
99
|
+
let index = start;
|
|
100
|
+
let seenLineEnding = false;
|
|
101
|
+
while (index < text.length) {
|
|
102
|
+
const char = text.charAt(index);
|
|
103
|
+
if (char === "\n") {
|
|
104
|
+
if (seenLineEnding) break;
|
|
105
|
+
seenLineEnding = true;
|
|
106
|
+
index += 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (char !== " " && char !== " ") break;
|
|
110
|
+
index += 1;
|
|
111
|
+
}
|
|
112
|
+
return index;
|
|
113
|
+
}
|
|
114
|
+
function isBlankRemainderOfLine(text, start) {
|
|
115
|
+
let index = start;
|
|
116
|
+
while (index < text.length) {
|
|
117
|
+
const char = text.charAt(index);
|
|
118
|
+
if (char === "\n") return true;
|
|
119
|
+
if (!require_inline_chars.isMarkdownSpace(char)) return false;
|
|
120
|
+
index += 1;
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
//#endregion
|
|
125
|
+
exports.isBlankRemainderOfLine = isBlankRemainderOfLine;
|
|
126
|
+
exports.matchLinkLabel = matchLinkLabel;
|
|
127
|
+
exports.normalizeLinkLabel = normalizeLinkLabel;
|
|
128
|
+
exports.parseLinkDestination = parseLinkDestination;
|
|
129
|
+
exports.parseLinkTitle = parseLinkTitle;
|
|
130
|
+
exports.skipInlineWhitespace = skipInlineWhitespace;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as matchLinkLabel, c as parseLinkTitle, i as isBlankRemainderOfLine, l as skipInlineWhitespace, n as LinkReferenceMap, o as normalizeLinkLabel, r as ParsedSpan, s as parseLinkDestination, t as LinkReferenceDefinition } from "../link-Dv4kxVjk.cjs";
|
|
2
|
+
export { LinkReferenceDefinition, LinkReferenceMap, ParsedSpan, isBlankRemainderOfLine, matchLinkLabel, normalizeLinkLabel, parseLinkDestination, parseLinkTitle, skipInlineWhitespace };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as matchLinkLabel, c as parseLinkTitle, i as isBlankRemainderOfLine, l as skipInlineWhitespace, n as LinkReferenceMap, o as normalizeLinkLabel, r as ParsedSpan, s as parseLinkDestination, t as LinkReferenceDefinition } from "../link-Dv4kxVjk.js";
|
|
2
|
+
export { LinkReferenceDefinition, LinkReferenceMap, ParsedSpan, isBlankRemainderOfLine, matchLinkLabel, normalizeLinkLabel, parseLinkDestination, parseLinkTitle, skipInlineWhitespace };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { isAsciiControl, isMarkdownSpace } from "./chars.js";
|
|
2
|
+
import { unescapeString } from "./entity.js";
|
|
3
|
+
//#region src/inline/link.ts
|
|
4
|
+
const MAX_LINK_LABEL_LENGTH = 999;
|
|
5
|
+
function normalizeLinkLabel(labelWithBrackets) {
|
|
6
|
+
return labelWithBrackets.slice(1, labelWithBrackets.length - 1).replace(/^[ \t\r\n]+/, "").replace(/[ \t\r\n]+$/, "").replace(/[ \t\r\n]+/g, " ").toLowerCase().toUpperCase();
|
|
7
|
+
}
|
|
8
|
+
function matchLinkLabel(text, start) {
|
|
9
|
+
if (text.charAt(start) !== "[") return 0;
|
|
10
|
+
let index = start + 1;
|
|
11
|
+
while (index < text.length) {
|
|
12
|
+
const char = text.charAt(index);
|
|
13
|
+
if (char === "\\") {
|
|
14
|
+
index += 2;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (char === "[") return 0;
|
|
18
|
+
if (char === "]") {
|
|
19
|
+
const length = index + 1 - start;
|
|
20
|
+
return length - 2 > MAX_LINK_LABEL_LENGTH ? 0 : length;
|
|
21
|
+
}
|
|
22
|
+
index += 1;
|
|
23
|
+
}
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
function parseLinkDestination(text, start) {
|
|
27
|
+
if (text.charAt(start) === "<") {
|
|
28
|
+
let index = start + 1;
|
|
29
|
+
while (index < text.length) {
|
|
30
|
+
const char = text.charAt(index);
|
|
31
|
+
if (char === "\\") {
|
|
32
|
+
index += 2;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (char === "\n" || char === "<") return;
|
|
36
|
+
if (char === ">") return {
|
|
37
|
+
value: unescapeString(text.slice(start + 1, index)),
|
|
38
|
+
end: index + 1
|
|
39
|
+
};
|
|
40
|
+
index += 1;
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let index = start;
|
|
45
|
+
let openParens = 0;
|
|
46
|
+
while (index < text.length) {
|
|
47
|
+
const char = text.charAt(index);
|
|
48
|
+
if (char === "\\" && text.length > index + 1) {
|
|
49
|
+
index += 2;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (char === "(") {
|
|
53
|
+
openParens += 1;
|
|
54
|
+
index += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (char === ")") {
|
|
58
|
+
if (openParens === 0) break;
|
|
59
|
+
openParens -= 1;
|
|
60
|
+
index += 1;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (char === " " || isAsciiControl(char)) break;
|
|
64
|
+
index += 1;
|
|
65
|
+
}
|
|
66
|
+
if (openParens !== 0) return;
|
|
67
|
+
if (index === start && text.charAt(index) !== ")") return;
|
|
68
|
+
return {
|
|
69
|
+
value: unescapeString(text.slice(start, index)),
|
|
70
|
+
end: index
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const TITLE_DELIMITERS = /* @__PURE__ */ new Map([
|
|
74
|
+
["\"", "\""],
|
|
75
|
+
["'", "'"],
|
|
76
|
+
["(", ")"]
|
|
77
|
+
]);
|
|
78
|
+
function parseLinkTitle(text, start) {
|
|
79
|
+
const opener = text.charAt(start);
|
|
80
|
+
const closer = TITLE_DELIMITERS.get(opener);
|
|
81
|
+
if (closer === void 0) return;
|
|
82
|
+
let index = start + 1;
|
|
83
|
+
while (index < text.length) {
|
|
84
|
+
const char = text.charAt(index);
|
|
85
|
+
if (char === "\\") {
|
|
86
|
+
index += 2;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (char === closer) return {
|
|
90
|
+
value: unescapeString(text.slice(start + 1, index)),
|
|
91
|
+
end: index + 1
|
|
92
|
+
};
|
|
93
|
+
if (opener === "(" && char === "(") return;
|
|
94
|
+
index += 1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function skipInlineWhitespace(text, start) {
|
|
98
|
+
let index = start;
|
|
99
|
+
let seenLineEnding = false;
|
|
100
|
+
while (index < text.length) {
|
|
101
|
+
const char = text.charAt(index);
|
|
102
|
+
if (char === "\n") {
|
|
103
|
+
if (seenLineEnding) break;
|
|
104
|
+
seenLineEnding = true;
|
|
105
|
+
index += 1;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (char !== " " && char !== " ") break;
|
|
109
|
+
index += 1;
|
|
110
|
+
}
|
|
111
|
+
return index;
|
|
112
|
+
}
|
|
113
|
+
function isBlankRemainderOfLine(text, start) {
|
|
114
|
+
let index = start;
|
|
115
|
+
while (index < text.length) {
|
|
116
|
+
const char = text.charAt(index);
|
|
117
|
+
if (char === "\n") return true;
|
|
118
|
+
if (!isMarkdownSpace(char)) return false;
|
|
119
|
+
index += 1;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { isBlankRemainderOfLine, matchLinkLabel, normalizeLinkLabel, parseLinkDestination, parseLinkTitle, skipInlineWhitespace };
|