turnish 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 +22 -0
- package/README.md +270 -0
- package/dist/index.cjs +68 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.iife.js +68 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.mjs +818 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +68 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/types/collapse-whitespace.d.ts +39 -0
- package/dist/types/default-rules.d.ts +4 -0
- package/dist/types/html-parser.d.ts +4 -0
- package/dist/types/index.d.ts +113 -0
- package/dist/types/node.d.ts +22 -0
- package/dist/types/root-node.d.ts +5 -0
- package/dist/types/rules.d.ts +30 -0
- package/dist/types/utilities.d.ts +16 -0
- package/package.json +56 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
function B(t, e) {
|
|
2
|
+
const n = t;
|
|
3
|
+
return n.isBlock = E(n), n.isCode = n.nodeName === "CODE" || n.parentNode?.isCode, n.isBlank = P(n), n.flankingWhitespace = H(n, e), n;
|
|
4
|
+
}
|
|
5
|
+
function P(t) {
|
|
6
|
+
return !S(t) && !U(t) && /^\s*$/i.test(t.textContent || "") && !$(t) && !z(t);
|
|
7
|
+
}
|
|
8
|
+
function H(t, e) {
|
|
9
|
+
const n = t;
|
|
10
|
+
if (n.isBlock || e.preformattedCode && n.isCode)
|
|
11
|
+
return { leading: "", trailing: "" };
|
|
12
|
+
const r = _(t.textContent || "");
|
|
13
|
+
return r.leadingAscii && A("left", t, e) && (r.leading = r.leadingNonAscii), r.trailingAscii && A("right", t, e) && (r.trailing = r.trailingNonAscii), { leading: r.leading, trailing: r.trailing };
|
|
14
|
+
}
|
|
15
|
+
function _(t) {
|
|
16
|
+
const e = t.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);
|
|
17
|
+
return e ? {
|
|
18
|
+
leading: e[1],
|
|
19
|
+
// whole string for whitespace-only strings
|
|
20
|
+
leadingAscii: e[2],
|
|
21
|
+
leadingNonAscii: e[3],
|
|
22
|
+
trailing: e[4],
|
|
23
|
+
// empty for whitespace-only strings
|
|
24
|
+
trailingNonAscii: e[5],
|
|
25
|
+
trailingAscii: e[6]
|
|
26
|
+
} : {
|
|
27
|
+
leading: "",
|
|
28
|
+
leadingAscii: "",
|
|
29
|
+
leadingNonAscii: "",
|
|
30
|
+
trailing: "",
|
|
31
|
+
trailingNonAscii: "",
|
|
32
|
+
trailingAscii: ""
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function A(t, e, n) {
|
|
36
|
+
let r, i, l;
|
|
37
|
+
return t === "left" ? (r = e.previousElementSibling, i = / $/) : (r = e.nextElementSibling, i = /^ /), r && (r.nodeType === 3 ? l = i.test(r.nodeValue || "") : n.preformattedCode && r.nodeName === "CODE" ? l = !1 : r.nodeType === 1 && !E(r) && (l = i.test(r.textContent || ""))), l || !1;
|
|
38
|
+
}
|
|
39
|
+
const m = {
|
|
40
|
+
Element: 1,
|
|
41
|
+
Text: 3,
|
|
42
|
+
CDATASection: 4
|
|
43
|
+
};
|
|
44
|
+
function N(t, e) {
|
|
45
|
+
return Array(e + 1).join(t);
|
|
46
|
+
}
|
|
47
|
+
function D(t) {
|
|
48
|
+
return t.replace(/^\n*/, "");
|
|
49
|
+
}
|
|
50
|
+
function w(t) {
|
|
51
|
+
let e = t.length;
|
|
52
|
+
for (; e > 0 && t[e - 1] === `
|
|
53
|
+
`; ) e--;
|
|
54
|
+
return t.substring(0, e);
|
|
55
|
+
}
|
|
56
|
+
function x(t) {
|
|
57
|
+
return w(D(t));
|
|
58
|
+
}
|
|
59
|
+
const v = [
|
|
60
|
+
"ADDRESS",
|
|
61
|
+
"ARTICLE",
|
|
62
|
+
"ASIDE",
|
|
63
|
+
"AUDIO",
|
|
64
|
+
"BLOCKQUOTE",
|
|
65
|
+
"BODY",
|
|
66
|
+
"CANVAS",
|
|
67
|
+
"CENTER",
|
|
68
|
+
"DD",
|
|
69
|
+
"DIR",
|
|
70
|
+
"DIV",
|
|
71
|
+
"DL",
|
|
72
|
+
"DT",
|
|
73
|
+
"FIELDSET",
|
|
74
|
+
"FIGCAPTION",
|
|
75
|
+
"FIGURE",
|
|
76
|
+
"FOOTER",
|
|
77
|
+
"FORM",
|
|
78
|
+
"FRAMESET",
|
|
79
|
+
"H1",
|
|
80
|
+
"H2",
|
|
81
|
+
"H3",
|
|
82
|
+
"H4",
|
|
83
|
+
"H5",
|
|
84
|
+
"H6",
|
|
85
|
+
"HEADER",
|
|
86
|
+
"HGROUP",
|
|
87
|
+
"HR",
|
|
88
|
+
"HTML",
|
|
89
|
+
"ISINDEX",
|
|
90
|
+
"LI",
|
|
91
|
+
"MAIN",
|
|
92
|
+
"MENU",
|
|
93
|
+
"NAV",
|
|
94
|
+
"NOFRAMES",
|
|
95
|
+
"NOSCRIPT",
|
|
96
|
+
"OL",
|
|
97
|
+
"OUTPUT",
|
|
98
|
+
"P",
|
|
99
|
+
"PRE",
|
|
100
|
+
"SECTION",
|
|
101
|
+
"TABLE",
|
|
102
|
+
"TBODY",
|
|
103
|
+
"TD",
|
|
104
|
+
"TFOOT",
|
|
105
|
+
"TH",
|
|
106
|
+
"THEAD",
|
|
107
|
+
"TR",
|
|
108
|
+
"UL"
|
|
109
|
+
];
|
|
110
|
+
function E(t) {
|
|
111
|
+
return k(t, v);
|
|
112
|
+
}
|
|
113
|
+
const O = [
|
|
114
|
+
"AREA",
|
|
115
|
+
"BASE",
|
|
116
|
+
"BR",
|
|
117
|
+
"COL",
|
|
118
|
+
"COMMAND",
|
|
119
|
+
"EMBED",
|
|
120
|
+
"HR",
|
|
121
|
+
"IMG",
|
|
122
|
+
"INPUT",
|
|
123
|
+
"KEYGEN",
|
|
124
|
+
"LINK",
|
|
125
|
+
"META",
|
|
126
|
+
"PARAM",
|
|
127
|
+
"SOURCE",
|
|
128
|
+
"TRACK",
|
|
129
|
+
"WBR"
|
|
130
|
+
];
|
|
131
|
+
function S(t) {
|
|
132
|
+
return k(t, O);
|
|
133
|
+
}
|
|
134
|
+
function $(t) {
|
|
135
|
+
return M(t, O);
|
|
136
|
+
}
|
|
137
|
+
const L = [
|
|
138
|
+
"A",
|
|
139
|
+
"TABLE",
|
|
140
|
+
"THEAD",
|
|
141
|
+
"TBODY",
|
|
142
|
+
"TFOOT",
|
|
143
|
+
"TH",
|
|
144
|
+
"TD",
|
|
145
|
+
"IFRAME",
|
|
146
|
+
"SCRIPT",
|
|
147
|
+
"AUDIO",
|
|
148
|
+
"VIDEO"
|
|
149
|
+
], F = [
|
|
150
|
+
"P",
|
|
151
|
+
"BR",
|
|
152
|
+
"H1",
|
|
153
|
+
"H2",
|
|
154
|
+
"H3",
|
|
155
|
+
"H4",
|
|
156
|
+
"H5",
|
|
157
|
+
"H6",
|
|
158
|
+
"BLOCKQUOTE",
|
|
159
|
+
"UL",
|
|
160
|
+
"OL",
|
|
161
|
+
"LI",
|
|
162
|
+
"PRE",
|
|
163
|
+
"CODE",
|
|
164
|
+
"HR",
|
|
165
|
+
"A",
|
|
166
|
+
"EM",
|
|
167
|
+
"I",
|
|
168
|
+
"STRONG",
|
|
169
|
+
"B",
|
|
170
|
+
"IMG",
|
|
171
|
+
"DIV",
|
|
172
|
+
"SPAN",
|
|
173
|
+
"TABLE",
|
|
174
|
+
"THEAD",
|
|
175
|
+
"TBODY",
|
|
176
|
+
"TR",
|
|
177
|
+
"TH",
|
|
178
|
+
"TD"
|
|
179
|
+
];
|
|
180
|
+
function U(t) {
|
|
181
|
+
return k(t, L);
|
|
182
|
+
}
|
|
183
|
+
function z(t) {
|
|
184
|
+
return M(t, L);
|
|
185
|
+
}
|
|
186
|
+
function k(t, e) {
|
|
187
|
+
return e.indexOf(t.nodeName) >= 0;
|
|
188
|
+
}
|
|
189
|
+
function M(t, e) {
|
|
190
|
+
return e.some(function(n) {
|
|
191
|
+
return t.nodeType !== m.Element ? !1 : t.getElementsByTagName(n).length;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
function I(t) {
|
|
195
|
+
return t ? t.replace(/(\n+\s*)+/g, `
|
|
196
|
+
`) : "";
|
|
197
|
+
}
|
|
198
|
+
function V(t) {
|
|
199
|
+
return I(t).replace(/[\t\r\n]+/g, " ").replace(/ {2,}/g, " ").replace(/([()])/g, "\\$1").trim();
|
|
200
|
+
}
|
|
201
|
+
function p(t) {
|
|
202
|
+
return I(t).replace(/[\t\r\n]+/g, " ");
|
|
203
|
+
}
|
|
204
|
+
const f = {};
|
|
205
|
+
f.paragraph = {
|
|
206
|
+
filter: "p",
|
|
207
|
+
replacement: function(t) {
|
|
208
|
+
return `
|
|
209
|
+
|
|
210
|
+
` + t + `
|
|
211
|
+
|
|
212
|
+
`;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
f.lineBreak = {
|
|
216
|
+
filter: "br",
|
|
217
|
+
replacement: function(t, e, n) {
|
|
218
|
+
return n.br + `
|
|
219
|
+
`;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
f.heading = {
|
|
223
|
+
filter: ["h1", "h2", "h3", "h4", "h5", "h6"],
|
|
224
|
+
replacement: function(t, e, n) {
|
|
225
|
+
const r = Number(e.nodeName.charAt(1));
|
|
226
|
+
if (n.headingStyle === "setext" && r < 3) {
|
|
227
|
+
const i = N(r === 1 ? "=" : "-", t.length);
|
|
228
|
+
return `
|
|
229
|
+
|
|
230
|
+
` + t + `
|
|
231
|
+
` + i + `
|
|
232
|
+
|
|
233
|
+
`;
|
|
234
|
+
} else
|
|
235
|
+
return `
|
|
236
|
+
|
|
237
|
+
` + N("#", r) + " " + t + `
|
|
238
|
+
|
|
239
|
+
`;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
f.blockquote = {
|
|
243
|
+
filter: "blockquote",
|
|
244
|
+
replacement: function(t) {
|
|
245
|
+
return t = x(t).replace(/^/gm, "> "), `
|
|
246
|
+
|
|
247
|
+
` + t + `
|
|
248
|
+
|
|
249
|
+
`;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
f.list = {
|
|
253
|
+
filter: ["ul", "ol"],
|
|
254
|
+
replacement: function(t, e) {
|
|
255
|
+
const n = e.parentNode;
|
|
256
|
+
return n.nodeName === "LI" && n.lastElementChild === e ? `
|
|
257
|
+
` + t : `
|
|
258
|
+
|
|
259
|
+
` + t + `
|
|
260
|
+
|
|
261
|
+
`;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
f.listItem = {
|
|
265
|
+
filter: "li",
|
|
266
|
+
replacement: function(t, e, n) {
|
|
267
|
+
let r = n.bulletListMarker + " ";
|
|
268
|
+
const i = e.parentNode;
|
|
269
|
+
if (i.nodeName === "OL") {
|
|
270
|
+
const a = i.getAttribute("start"), o = Array.prototype.indexOf.call(i.children, e);
|
|
271
|
+
r = (a ? Number(a) + o : o + 1) + ". ";
|
|
272
|
+
}
|
|
273
|
+
const l = /\n$/.test(t);
|
|
274
|
+
return t = x(t) + (l ? `
|
|
275
|
+
` : ""), t = t.replace(/\n/gm, `
|
|
276
|
+
` + " ".repeat(r.length)), r + t + (e.nextSibling ? `
|
|
277
|
+
` : "");
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
f.indentedCodeBlock = {
|
|
281
|
+
filter: function(t, e) {
|
|
282
|
+
return !!(e && e.codeBlockStyle === "indented" && t.nodeName === "PRE" && t.firstChild && t.firstChild.nodeName === "CODE");
|
|
283
|
+
},
|
|
284
|
+
replacement: function(t, e) {
|
|
285
|
+
return !e || !e.firstChild ? "" : `
|
|
286
|
+
|
|
287
|
+
` + e.firstChild.textContent.replace(/\n/g, `
|
|
288
|
+
`) + `
|
|
289
|
+
|
|
290
|
+
`;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
f.fencedCodeBlock = {
|
|
294
|
+
filter: function(t, e) {
|
|
295
|
+
return !!(e && e.codeBlockStyle === "fenced" && t.nodeName === "PRE" && t.firstChild && t.firstChild.nodeName === "CODE");
|
|
296
|
+
},
|
|
297
|
+
replacement: function(t, e, n) {
|
|
298
|
+
if (!e.firstChild)
|
|
299
|
+
return "";
|
|
300
|
+
const r = e.firstChild, l = ((r.getAttribute("class") || "").match(/language-(\S+)/) || [null, ""])[1], a = r.textContent || "", o = n.fence?.charAt(0) || "`";
|
|
301
|
+
let s = 3;
|
|
302
|
+
const u = new RegExp("^" + o + "{3,}", "gm");
|
|
303
|
+
let c;
|
|
304
|
+
for (; c = u.exec(a); )
|
|
305
|
+
c[0].length >= s && (s = c[0].length + 1);
|
|
306
|
+
const d = N(o, s);
|
|
307
|
+
return `
|
|
308
|
+
|
|
309
|
+
` + d + l + `
|
|
310
|
+
` + a.replace(/\n$/, "") + `
|
|
311
|
+
` + d + `
|
|
312
|
+
|
|
313
|
+
`;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
f.horizontalRule = {
|
|
317
|
+
filter: "hr",
|
|
318
|
+
replacement: function(t, e, n) {
|
|
319
|
+
return `
|
|
320
|
+
|
|
321
|
+
` + n.hr + `
|
|
322
|
+
|
|
323
|
+
`;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
f.inlineLink = {
|
|
327
|
+
filter: function(t, e) {
|
|
328
|
+
return !!(e?.linkStyle === "inlined" && t.nodeName === "A" && t.getAttribute("href"));
|
|
329
|
+
},
|
|
330
|
+
replacement: function(t, e) {
|
|
331
|
+
const n = V(t);
|
|
332
|
+
let r = e.getAttribute("href")?.replace(/([()])/g, "\\$1"), i;
|
|
333
|
+
const l = e.getAttribute("title");
|
|
334
|
+
return l ? i = ' "' + p(l).replace(/"/g, '\\"') + '"' : i = "", "[" + n + "](" + r + i + ")";
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const R = {
|
|
338
|
+
filter: function(t, e) {
|
|
339
|
+
return !!(e && e.linkStyle === "referenced" && t.nodeName === "A" && t.getAttribute("href"));
|
|
340
|
+
},
|
|
341
|
+
replacement: function(t, e, n) {
|
|
342
|
+
const r = R, i = e.getAttribute("href");
|
|
343
|
+
let l;
|
|
344
|
+
const a = e.getAttribute("title");
|
|
345
|
+
a ? l = ' "' + p(a) + '"' : l = "";
|
|
346
|
+
const o = i + l;
|
|
347
|
+
let s, u;
|
|
348
|
+
switch (n.linkReferenceStyle) {
|
|
349
|
+
case "collapsed":
|
|
350
|
+
s = "[" + t + "][]", u = "[" + t + "]: " + o;
|
|
351
|
+
break;
|
|
352
|
+
case "shortcut":
|
|
353
|
+
s = "[" + t + "]", u = "[" + t + "]: " + o;
|
|
354
|
+
break;
|
|
355
|
+
default: {
|
|
356
|
+
let c;
|
|
357
|
+
const d = r.urlReferenceIdMap.get(o);
|
|
358
|
+
n.linkReferenceDeduplication === "full" && d ? (c = d, u = "[" + c + "]: " + i + l) : (c = r.references.length + 1, r.urlReferenceIdMap.set(o, c), u = "[" + c + "]: " + i + l, r.references.push(u)), s = "[" + t + "][" + c + "]";
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return n.linkReferenceStyle !== "full" && (n.linkReferenceDeduplication === "full" ? r.urlReferenceIdMap.has(o) || (r.urlReferenceIdMap.set(o, 1), r.references.push(u)) : r.references.push(u)), s;
|
|
363
|
+
},
|
|
364
|
+
references: [],
|
|
365
|
+
urlReferenceIdMap: /* @__PURE__ */ new Map(),
|
|
366
|
+
append: () => {
|
|
367
|
+
const t = R;
|
|
368
|
+
let e = "";
|
|
369
|
+
return t.references && t.references.length && (e = `
|
|
370
|
+
|
|
371
|
+
` + t.references.join(`
|
|
372
|
+
`) + `
|
|
373
|
+
|
|
374
|
+
`, t.references = [], t.urlReferenceIdMap = /* @__PURE__ */ new Map()), e;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
f.referenceLink = R;
|
|
378
|
+
f.emphasis = {
|
|
379
|
+
filter: ["em", "i"],
|
|
380
|
+
replacement: (t, e, n) => (t = t.trim(), t ? n.emDelimiter + t + n.emDelimiter : "")
|
|
381
|
+
};
|
|
382
|
+
f.strong = {
|
|
383
|
+
filter: ["strong", "b"],
|
|
384
|
+
replacement: (t, e, n) => (t = t.trim(), t ? n.strongDelimiter + t + n.strongDelimiter : "")
|
|
385
|
+
};
|
|
386
|
+
f.code = {
|
|
387
|
+
filter: (t) => {
|
|
388
|
+
const e = t.previousSibling || t.nextSibling, r = t.parentNode.nodeName === "PRE" && !e;
|
|
389
|
+
return t.nodeName === "CODE" && !r;
|
|
390
|
+
},
|
|
391
|
+
replacement: (t) => {
|
|
392
|
+
const e = t.replace(/\r?\n|\r/g, " "), n = /^`|^ .*?[^ ].* $|`$/.test(e) ? " " : "";
|
|
393
|
+
let r = "`";
|
|
394
|
+
const i = e.match(/`+/gm) || [];
|
|
395
|
+
for (; i.includes(r); ) r = r + "`";
|
|
396
|
+
return r + n + e + n + r;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
f.image = {
|
|
400
|
+
filter: "img",
|
|
401
|
+
replacement: function(t, e) {
|
|
402
|
+
const n = e.getAttribute("alt"), r = n ? p(n) : "", i = e.getAttribute("src") || "", l = e.getAttribute("title"), a = l ? p(l) : "", o = a ? ' "' + a + '"' : "";
|
|
403
|
+
return i ? "" : "";
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
class W {
|
|
407
|
+
options;
|
|
408
|
+
_keep;
|
|
409
|
+
_remove;
|
|
410
|
+
blankRule;
|
|
411
|
+
keepReplacement;
|
|
412
|
+
markdownIncludingHtmlReplacement;
|
|
413
|
+
defaultRule;
|
|
414
|
+
array;
|
|
415
|
+
constructor(e) {
|
|
416
|
+
this.options = e, this._keep = [], this._remove = [], this.blankRule = {
|
|
417
|
+
replacement: e.blankReplacement
|
|
418
|
+
}, this.keepReplacement = e.keepReplacement, this.markdownIncludingHtmlReplacement = e.markdownIncludingHtmlReplacement, this.defaultRule = {
|
|
419
|
+
replacement: e.defaultReplacement
|
|
420
|
+
}, this.array = [];
|
|
421
|
+
for (const n in e.rules)
|
|
422
|
+
this.array.push(e.rules[n]);
|
|
423
|
+
}
|
|
424
|
+
add(e, n) {
|
|
425
|
+
this.array.unshift(n);
|
|
426
|
+
}
|
|
427
|
+
keep(e) {
|
|
428
|
+
this._keep.unshift({
|
|
429
|
+
filter: e,
|
|
430
|
+
replacement: this.keepReplacement
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
remove(e) {
|
|
434
|
+
this._remove.unshift({
|
|
435
|
+
filter: e,
|
|
436
|
+
replacement: function() {
|
|
437
|
+
return "";
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
forNode(e) {
|
|
442
|
+
if (e.isBlank)
|
|
443
|
+
return this.blankRule;
|
|
444
|
+
if (this.options.htmlRetentionMode === "preserveAll" && this.isUnsupportedElement(e))
|
|
445
|
+
return {
|
|
446
|
+
replacement: this.keepReplacement
|
|
447
|
+
};
|
|
448
|
+
if (this.options.htmlRetentionMode === "markdownIncludingHtml" && this.isUnsupportedElement(e))
|
|
449
|
+
return {
|
|
450
|
+
replacement: this.markdownIncludingHtmlReplacement
|
|
451
|
+
};
|
|
452
|
+
let n;
|
|
453
|
+
return (n = h(this.array, e, this.options)) || (n = h(this._keep, e, this.options)) || (n = h(this._remove, e, this.options)) ? n : this.defaultRule;
|
|
454
|
+
}
|
|
455
|
+
/// Check if an element is unsupported for Markdown conversion.
|
|
456
|
+
isUnsupportedElement(e) {
|
|
457
|
+
const n = e.nodeName;
|
|
458
|
+
if (n === "PRE" && e.firstChild && e.firstChild.nodeName === "CODE") {
|
|
459
|
+
const r = e.firstChild;
|
|
460
|
+
if (r.attributes && r.attributes.length > 0) {
|
|
461
|
+
for (let i = 0; i < r.attributes.length; i++)
|
|
462
|
+
if (r.attributes[i].name.toLowerCase() !== "class")
|
|
463
|
+
return !0;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (e.attributes && e.attributes.length > 0)
|
|
467
|
+
switch (n) {
|
|
468
|
+
case "IMG":
|
|
469
|
+
for (let i = 0; i < e.attributes.length; i++) {
|
|
470
|
+
const l = e.attributes[i].name.toLowerCase();
|
|
471
|
+
if (l !== "src" && l !== "alt" && l !== "title")
|
|
472
|
+
return !0;
|
|
473
|
+
}
|
|
474
|
+
return !1;
|
|
475
|
+
case "A":
|
|
476
|
+
for (let i = 0; i < e.attributes.length; i++) {
|
|
477
|
+
const l = e.attributes[i].name.toLowerCase();
|
|
478
|
+
if (l !== "href" && l !== "title")
|
|
479
|
+
return !0;
|
|
480
|
+
}
|
|
481
|
+
return !1;
|
|
482
|
+
case "CODE":
|
|
483
|
+
const r = e.parentNode;
|
|
484
|
+
if (r && r.nodeName === "PRE") {
|
|
485
|
+
for (let i = 0; i < e.attributes.length; i++)
|
|
486
|
+
if (e.attributes[i].name.toLowerCase() !== "class")
|
|
487
|
+
return !0;
|
|
488
|
+
return !1;
|
|
489
|
+
}
|
|
490
|
+
default:
|
|
491
|
+
return !0;
|
|
492
|
+
}
|
|
493
|
+
return F.indexOf(n) === -1;
|
|
494
|
+
}
|
|
495
|
+
forEach(e) {
|
|
496
|
+
for (let n = 0; n < this.array.length; n++)
|
|
497
|
+
e(this.array[n], n);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function h(t, e, n) {
|
|
501
|
+
for (let r = 0; r < t.length; r++) {
|
|
502
|
+
const i = t[r];
|
|
503
|
+
if (G(i, e, n)) return i;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
function G(t, e, n) {
|
|
507
|
+
const r = t.filter;
|
|
508
|
+
if (typeof r == "string") {
|
|
509
|
+
if (r === e.nodeName.toLowerCase())
|
|
510
|
+
return !0;
|
|
511
|
+
} else if (Array.isArray(r)) {
|
|
512
|
+
if (r.indexOf(e.nodeName.toLowerCase()) > -1)
|
|
513
|
+
return !0;
|
|
514
|
+
} else if (typeof r == "function") {
|
|
515
|
+
if (r(e, n))
|
|
516
|
+
return !0;
|
|
517
|
+
} else
|
|
518
|
+
throw new TypeError("`filter` needs to be a string, array, or function");
|
|
519
|
+
return !1;
|
|
520
|
+
}
|
|
521
|
+
function K(t) {
|
|
522
|
+
const e = t.element, n = t.isBlock, r = t.isVoid, i = t.isPre || function(u) {
|
|
523
|
+
return u.nodeName === "PRE";
|
|
524
|
+
};
|
|
525
|
+
if (!e.firstChild || i(e)) return;
|
|
526
|
+
let l = null, a = !1, o = null, s = y(o, e, i);
|
|
527
|
+
for (; s !== e; ) {
|
|
528
|
+
if (s.nodeType === m.Text || s.nodeType === m.CDATASection) {
|
|
529
|
+
const c = s;
|
|
530
|
+
let d = c.data.replace(/[ \r\n\t]+/g, " ");
|
|
531
|
+
if ((!l || / $/.test(l.data)) && !a && d[0] === " " && (d = d.substr(1)), !d) {
|
|
532
|
+
s = g(s);
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
c.data = d, l = c;
|
|
536
|
+
} else if (s.nodeType === m.Element)
|
|
537
|
+
n(s) || s.nodeName === "BR" ? (l && (l.data = l.data.replace(/ $/, "")), l = null, a = !1) : r(s) || i(s) ? (l = null, a = !0) : l && (a = !1);
|
|
538
|
+
else {
|
|
539
|
+
s = g(s);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
const u = y(o, s, i);
|
|
543
|
+
o = s, s = u;
|
|
544
|
+
}
|
|
545
|
+
l && (l.data = l.data.replace(/ $/, ""), l.data || g(l));
|
|
546
|
+
}
|
|
547
|
+
function g(t) {
|
|
548
|
+
const e = t.nextSibling ?? t.parentNode;
|
|
549
|
+
return t.parentNode && t.parentNode.removeChild(t), e;
|
|
550
|
+
}
|
|
551
|
+
function y(t, e, n) {
|
|
552
|
+
return t && t.parentNode === e || n(e) ? e.nextSibling ?? e.parentNode : e.firstChild ?? e.nextSibling ?? e.parentNode;
|
|
553
|
+
}
|
|
554
|
+
const T = typeof window < "u" ? window : typeof globalThis < "u" ? globalThis : {};
|
|
555
|
+
function j() {
|
|
556
|
+
const t = typeof T.DOMParser < "u" ? T.DOMParser : void 0;
|
|
557
|
+
let e = !1;
|
|
558
|
+
if (!t) return !1;
|
|
559
|
+
try {
|
|
560
|
+
new t().parseFromString("", "text/html") && (e = !0);
|
|
561
|
+
} catch {
|
|
562
|
+
}
|
|
563
|
+
return e;
|
|
564
|
+
}
|
|
565
|
+
class C {
|
|
566
|
+
// This will be assigned per environment below
|
|
567
|
+
parseFromString(e, n) {
|
|
568
|
+
throw new Error("Not implemented");
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function Y() {
|
|
572
|
+
if (typeof window < "u" && typeof document < "u" && (typeof process > "u" || process.browser), typeof window < "u") {
|
|
573
|
+
class t extends C {
|
|
574
|
+
parseFromString(n, r) {
|
|
575
|
+
const i = document.implementation.createHTMLDocument("");
|
|
576
|
+
return i.open(), i.write(n), i.close(), i;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return new t();
|
|
580
|
+
} else {
|
|
581
|
+
const t = require("@mixmark-io/domino");
|
|
582
|
+
class e extends C {
|
|
583
|
+
parseFromString(r, i) {
|
|
584
|
+
return t.createDocument(r);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return new e();
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const q = () => j() ? new T.DOMParser() : Y();
|
|
591
|
+
function Q(t, { preformattedCode: e }) {
|
|
592
|
+
let n;
|
|
593
|
+
return typeof t == "string" ? n = J().parseFromString(
|
|
594
|
+
// DOM parsers arrange elements in the <head> and <body>.
|
|
595
|
+
// Wrapping in a custom element ensures elements are reliably arranged in
|
|
596
|
+
// a single element.
|
|
597
|
+
'<x-turnish id="turnish-root">' + t + "</x-turnish>",
|
|
598
|
+
"text/html"
|
|
599
|
+
).getElementById("turnish-root") : n = t.cloneNode(!0), K({
|
|
600
|
+
element: n,
|
|
601
|
+
isBlock: E,
|
|
602
|
+
isVoid: S,
|
|
603
|
+
isPre: e ? Z : void 0
|
|
604
|
+
}), n;
|
|
605
|
+
}
|
|
606
|
+
let X;
|
|
607
|
+
function J() {
|
|
608
|
+
return X ??= q();
|
|
609
|
+
}
|
|
610
|
+
function Z(t) {
|
|
611
|
+
return t.nodeName === "PRE" || t.nodeName === "CODE";
|
|
612
|
+
}
|
|
613
|
+
const ee = [
|
|
614
|
+
[/\\/g, "\\\\"],
|
|
615
|
+
[/\*/g, "\\*"],
|
|
616
|
+
[/_/g, "\\_"],
|
|
617
|
+
[/^-/g, "\\-"],
|
|
618
|
+
[/^\+ /g, "\\+ "],
|
|
619
|
+
[/^(=+)/g, "\\$1"],
|
|
620
|
+
[/^(#{1,6}) /g, "\\$1 "],
|
|
621
|
+
[/`/g, "\\`"],
|
|
622
|
+
[/^~~~/g, "\\~~~"],
|
|
623
|
+
[/\[/g, "\\["],
|
|
624
|
+
[/\]/g, "\\]"],
|
|
625
|
+
[/<([^>]*)>/g, "\\<$1\\>"],
|
|
626
|
+
[/^>/g, "\\>"],
|
|
627
|
+
[/^(\d+)\. /g, "$1\\. "]
|
|
628
|
+
], te = {
|
|
629
|
+
rules: f,
|
|
630
|
+
headingStyle: "atx",
|
|
631
|
+
hr: "---",
|
|
632
|
+
bulletListMarker: "-",
|
|
633
|
+
codeBlockStyle: "fenced",
|
|
634
|
+
fence: "```",
|
|
635
|
+
emDelimiter: "*",
|
|
636
|
+
strongDelimiter: "**",
|
|
637
|
+
linkStyle: "inlined",
|
|
638
|
+
linkReferenceStyle: "full",
|
|
639
|
+
linkReferenceDeduplication: "full",
|
|
640
|
+
br: " ",
|
|
641
|
+
preformattedCode: !1,
|
|
642
|
+
htmlRetentionMode: "standard",
|
|
643
|
+
blankReplacement: (t, e) => e.isBlock ? `
|
|
644
|
+
|
|
645
|
+
` : "",
|
|
646
|
+
keepReplacement: (t, e) => e.isBlock ? `
|
|
647
|
+
|
|
648
|
+
` + e.outerHTML + `
|
|
649
|
+
|
|
650
|
+
` : e.outerHTML,
|
|
651
|
+
markdownIncludingHtmlReplacement: (t, e) => {
|
|
652
|
+
const n = e.nodeName.toLowerCase();
|
|
653
|
+
let r = "";
|
|
654
|
+
if (e.attributes && e.attributes.length > 0) {
|
|
655
|
+
const s = [];
|
|
656
|
+
for (let u = 0; u < e.attributes.length; u++) {
|
|
657
|
+
const c = e.attributes[u];
|
|
658
|
+
s.push(`${c.name}="${c.value}"`);
|
|
659
|
+
}
|
|
660
|
+
r = " " + s.join(" ");
|
|
661
|
+
}
|
|
662
|
+
r += ' markdown="1"';
|
|
663
|
+
const i = `<${n}${r}>`, l = `</${n}>`, a = t.trim(), o = i + `
|
|
664
|
+
` + a + `
|
|
665
|
+
` + l;
|
|
666
|
+
return e.isBlock ? `
|
|
667
|
+
|
|
668
|
+
` + o + `
|
|
669
|
+
|
|
670
|
+
` : o;
|
|
671
|
+
},
|
|
672
|
+
defaultReplacement: function(t, e) {
|
|
673
|
+
return e.isBlock ? `
|
|
674
|
+
|
|
675
|
+
` + t + `
|
|
676
|
+
|
|
677
|
+
` : t;
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
class re {
|
|
681
|
+
options;
|
|
682
|
+
rules;
|
|
683
|
+
constructor(e) {
|
|
684
|
+
this.options = Object.assign({}, te, e), this.rules = new W(this.options);
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* The entry point for converting a string or DOM node to Markdown
|
|
688
|
+
* @public
|
|
689
|
+
* @param {InputType} input The string or DOM node to convert
|
|
690
|
+
* @returns A Markdown representation of the input
|
|
691
|
+
* @type string
|
|
692
|
+
*/
|
|
693
|
+
render(e) {
|
|
694
|
+
if (!ne(e))
|
|
695
|
+
throw new TypeError(
|
|
696
|
+
e + " is not a string, or an element/document/fragment node."
|
|
697
|
+
);
|
|
698
|
+
if (e === "")
|
|
699
|
+
return "";
|
|
700
|
+
const n = this.process(Q(e, this.options));
|
|
701
|
+
return this.postProcess(n);
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Add one or more plugins
|
|
705
|
+
* @public
|
|
706
|
+
* @param {Plugin|Plugin[]} plugin The plugin or array of plugins to add
|
|
707
|
+
* @returns The Turnish instance for chaining
|
|
708
|
+
* @type Object
|
|
709
|
+
*/
|
|
710
|
+
use(e) {
|
|
711
|
+
if (Array.isArray(e))
|
|
712
|
+
for (let n = 0; n < e.length; n++) this.use(e[n]);
|
|
713
|
+
else if (typeof e == "function")
|
|
714
|
+
e(this);
|
|
715
|
+
else
|
|
716
|
+
throw new TypeError("plugin must be a Function or an Array of Functions");
|
|
717
|
+
return this;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Adds a rule
|
|
721
|
+
* @public
|
|
722
|
+
* @param {string} key The unique key of the rule
|
|
723
|
+
* @param {Object} rule The rule
|
|
724
|
+
* @returns The Turnish instance for chaining
|
|
725
|
+
* @type Object
|
|
726
|
+
*/
|
|
727
|
+
addRule(e, n) {
|
|
728
|
+
return this.rules.add(e, n), this;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Keep a node (as HTML) that matches the filter
|
|
732
|
+
* @public
|
|
733
|
+
* @param {RuleFilter} filter The unique key of the rule
|
|
734
|
+
* @returns The Turnish instance for chaining
|
|
735
|
+
* @type Object
|
|
736
|
+
*/
|
|
737
|
+
keep(e) {
|
|
738
|
+
return this.rules.keep(e), this;
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Remove a node that matches the filter
|
|
742
|
+
* @public
|
|
743
|
+
* @param {string|Array|Function} filter The unique key of the rule
|
|
744
|
+
* @returns The Turnish instance for chaining
|
|
745
|
+
* @type Object
|
|
746
|
+
*/
|
|
747
|
+
remove(e) {
|
|
748
|
+
return this.rules.remove(e), this;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Escapes Markdown syntax
|
|
752
|
+
* @public
|
|
753
|
+
* @param {string} string The string to escape
|
|
754
|
+
* @returns A string with Markdown syntax escaped
|
|
755
|
+
* @type string
|
|
756
|
+
*/
|
|
757
|
+
escape(e) {
|
|
758
|
+
return ee.reduce(function(n, r) {
|
|
759
|
+
return n.replace(r[0], r[1]);
|
|
760
|
+
}, e);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Reduces a DOM node down to its Markdown string equivalent
|
|
764
|
+
* @private
|
|
765
|
+
* @param {HTMLElement} parentNode The node to convert
|
|
766
|
+
* @returns A Markdown representation of the node
|
|
767
|
+
* @type string
|
|
768
|
+
*/
|
|
769
|
+
process(e) {
|
|
770
|
+
return Array.from(e.childNodes).reduce((n, r) => {
|
|
771
|
+
const i = B(r, this.options);
|
|
772
|
+
let l = "";
|
|
773
|
+
if (i.nodeType === m.Text) {
|
|
774
|
+
const a = i.nodeValue ?? "";
|
|
775
|
+
l = i.isCode ? a : this.escape(a);
|
|
776
|
+
} else i.nodeType === m.Element && (l = this.replacementForNode(i));
|
|
777
|
+
return b(n, l);
|
|
778
|
+
}, "");
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Appends strings as each rule requires and trims the output
|
|
782
|
+
* @private
|
|
783
|
+
* @param {string} output The conversion output
|
|
784
|
+
* @returns A trimmed version of the ouput
|
|
785
|
+
* @type string
|
|
786
|
+
*/
|
|
787
|
+
postProcess(e) {
|
|
788
|
+
for (const n of this.rules.array)
|
|
789
|
+
n.append && (e = b(e, n.append(this.options)));
|
|
790
|
+
return e.replace(/^[\t\r\n]+/, "").replace(/[\t\r\n\s]+$/, "");
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Converts an element node to its Markdown equivalent
|
|
794
|
+
* @private
|
|
795
|
+
* @param {ExtendedNode} node The node to convert
|
|
796
|
+
* @returns A Markdown representation of the node
|
|
797
|
+
* @type string
|
|
798
|
+
*/
|
|
799
|
+
replacementForNode(e) {
|
|
800
|
+
const n = this.rules.forNode(e);
|
|
801
|
+
let r = this.process(e);
|
|
802
|
+
const i = e.flankingWhitespace;
|
|
803
|
+
return (i.leading || i.trailing) && (r = r.trim()), i.leading + n.replacement(r, e, this.options) + i.trailing;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
function b(t, e) {
|
|
807
|
+
const n = w(t), r = D(e), i = Math.max(t.length - n.length, e.length - r.length), l = `
|
|
808
|
+
|
|
809
|
+
`.substring(0, i);
|
|
810
|
+
return n + l + r;
|
|
811
|
+
}
|
|
812
|
+
function ne(t) {
|
|
813
|
+
return t != null && (typeof t == "string" || t.nodeType && (t.nodeType === 1 || t.nodeType === 9 || t.nodeType === 11));
|
|
814
|
+
}
|
|
815
|
+
export {
|
|
816
|
+
re as default
|
|
817
|
+
};
|
|
818
|
+
//# sourceMappingURL=index.mjs.map
|