react-smart-copy 1.29.1 → 1.29.2
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/README.md +1 -1
- package/dist/index.cjs +22 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +22 -4
- package/dist/index.js.map +1 -1
- package/dist/markdown.cjs +109 -15
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.js +109 -15
- package/dist/markdown.js.map +1 -1
- package/package.json +6 -5
package/dist/markdown.cjs
CHANGED
|
@@ -9,7 +9,9 @@ const require_text_field = require("./chunks/text-field-BEzUVOag.cjs");
|
|
|
9
9
|
* CommonMark escaping. Pasted text must render as the same text, never as
|
|
10
10
|
* Markdown syntax and never as raw HTML, so every character that can start a
|
|
11
11
|
* construct is backslash-escaped. Backslash escapes are valid before any ASCII
|
|
12
|
-
* punctuation, so over-escaping is
|
|
12
|
+
* punctuation, so over-escaping is safe in CommonMark, with one exception GitHub
|
|
13
|
+
* adds: a bare URL is linked with its backslashes kept, so a URL is written as an
|
|
14
|
+
* autolink (`<https://…>`) instead, which every renderer shows exactly.
|
|
13
15
|
*/
|
|
14
16
|
/**
|
|
15
17
|
* Characters that are special anywhere in a line: emphasis, code spans,
|
|
@@ -18,16 +20,67 @@ const require_text_field = require("./chunks/text-field-BEzUVOag.cjs");
|
|
|
18
20
|
const INLINE = /[\\`*_[\]<~]/g;
|
|
19
21
|
/** `&` that would start an entity or numeric character reference. */
|
|
20
22
|
const ENTITY = /&(?=#?[A-Za-z0-9]+;)/g;
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
/** A letter or digit: an `_` between two of them never starts or ends emphasis. */
|
|
24
|
+
const WORD = /[\p{L}\p{N}]/u;
|
|
25
|
+
/**
|
|
26
|
+
* A bare `http(s)` URL where GitHub would link it: at the start or after anything but a
|
|
27
|
+
* letter. No lookbehind: older Safari cannot parse one.
|
|
28
|
+
*/
|
|
29
|
+
const URL_AT = /(^|[^A-Za-z])(https?:\/\/[^\s<>|`\\]+)/g;
|
|
30
|
+
/** What GitHub leaves out of a URL's end: trailing punctuation, and an unbalanced `)`. */
|
|
31
|
+
function trimUrl(url) {
|
|
32
|
+
let end = url.length;
|
|
33
|
+
for (;;) {
|
|
34
|
+
const last = url.charAt(end - 1);
|
|
35
|
+
const head = url.slice(0, end);
|
|
36
|
+
if (last !== "" && "?!.,:;*_~'\"".includes(last)) end -= 1;
|
|
37
|
+
else if (last === ")" && head.split(")").length > head.split("(").length) end -= 1;
|
|
38
|
+
else return head;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* GitHub links `http://` after any non-letter, and `www.` after a space or `*_~(`, keeping any
|
|
43
|
+
* backslash escapes inside the link. Where this module does not write an autolink, it breaks
|
|
44
|
+
* the pattern instead: `http\://` and `www\.` render as `http://` and `www.` everywhere, and
|
|
45
|
+
* GitHub leaves them as text.
|
|
46
|
+
*/
|
|
47
|
+
const BARE_PROTOCOL = /(^|[^A-Za-z])(https?)(:\/\/)/gi;
|
|
48
|
+
const BARE_WWW = /(^|[\s*_~(])(www)\.(?=\S*\\)/gi;
|
|
49
|
+
function isPlainUrl(url) {
|
|
50
|
+
if (!/^https?:\/\/[\x21-\x7e]+$/.test(url)) return false;
|
|
51
|
+
try {
|
|
52
|
+
return new URL(url).hostname !== "";
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function escapeText(text) {
|
|
58
|
+
return text.replace(INLINE, (char, offset) => char === "_" && WORD.test(text.charAt(offset - 1)) && WORD.test(text.charAt(offset + 1)) ? char : `\\${char}`).replace(ENTITY, "\\&").replace(BARE_PROTOCOL, "$1$2\\$3").replace(BARE_WWW, "$1$2\\.");
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Escapes text for use inside a line. With `pipes`, also `|`, for GFM table cells. With
|
|
62
|
+
* `autolink` (the default), bare URLs become autolinks; pass `false` inside a link's text,
|
|
63
|
+
* where a second link is not allowed, and inside image descriptions.
|
|
64
|
+
*/
|
|
65
|
+
function escapeInline(text, pipes = false, autolink = true) {
|
|
66
|
+
let escaped = "";
|
|
67
|
+
let from = 0;
|
|
68
|
+
for (const match of autolink ? text.matchAll(URL_AT) : []) {
|
|
69
|
+
const url = trimUrl(match[2]);
|
|
70
|
+
if (!isPlainUrl(url)) continue;
|
|
71
|
+
const start = match.index + match[1].length;
|
|
72
|
+
escaped += `${escapeText(text.slice(from, start))}<${url}>`;
|
|
73
|
+
from = start + url.length;
|
|
74
|
+
}
|
|
75
|
+
escaped += escapeText(text.slice(from));
|
|
24
76
|
return pipes ? escaped.replace(/\|/g, "\\|") : escaped;
|
|
25
77
|
}
|
|
26
|
-
const ORDERED_MARKER = /^(\d{1,9})([.)])/;
|
|
27
|
-
const BLOCK_START = /^[
|
|
78
|
+
const ORDERED_MARKER = /^(\d{1,9})([.)])(?=[ \t]|$)/;
|
|
79
|
+
const BLOCK_START = /^(?:#{1,6}(?:[ \t]|$)|[>|]|[-+](?:[ \t]|$)|=+[ \t]*$|-+[ \t]*$|(?=[-:| \t]*-)[-:| \t]+$)/;
|
|
28
80
|
/**
|
|
29
|
-
* Escapes the start of already inline-escaped text that begins a line, where
|
|
30
|
-
*
|
|
81
|
+
* Escapes the start of already inline-escaped text that begins a line, where it would start
|
|
82
|
+
* a block: a heading (`# `), a quote (`>`), a list (`- `, `+ `, `1. `, `1) `), a table (`|`,
|
|
83
|
+
* or a delimiter row such as `:-` that GitHub reads as one), or a heading underline (`===`, `---`).
|
|
31
84
|
*/
|
|
32
85
|
function escapeLineStart(text) {
|
|
33
86
|
const ordered = ORDERED_MARKER.exec(text);
|
|
@@ -203,15 +256,19 @@ function tokenize(runs, options) {
|
|
|
203
256
|
const closeLink = () => {
|
|
204
257
|
closeFrom(0);
|
|
205
258
|
if (href === null) return;
|
|
206
|
-
if (tokens.length - 1 === linkStart)
|
|
207
|
-
|
|
259
|
+
if (tokens.length - 1 === linkStart) {
|
|
260
|
+
tokens.pop();
|
|
261
|
+
href = null;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
tokens.push({
|
|
208
265
|
kind: "syntax",
|
|
209
266
|
s: `](${require_url.linkDestination(href)})`
|
|
210
267
|
});
|
|
211
268
|
href = null;
|
|
212
269
|
};
|
|
213
270
|
for (let index = 0; index < runs.length; index += 1) {
|
|
214
|
-
const run = runs
|
|
271
|
+
const run = selfLinkAsText(runs, index);
|
|
215
272
|
if (run.kind === "break") {
|
|
216
273
|
closeLink();
|
|
217
274
|
pending = "";
|
|
@@ -237,7 +294,7 @@ function tokenize(runs, options) {
|
|
|
237
294
|
setStyle(run.style & -9, "");
|
|
238
295
|
tokens.push({
|
|
239
296
|
kind: "syntax",
|
|
240
|
-
s: `})`
|
|
297
|
+
s: `})`
|
|
241
298
|
});
|
|
242
299
|
continue;
|
|
243
300
|
}
|
|
@@ -257,7 +314,7 @@ function tokenize(runs, options) {
|
|
|
257
314
|
s: codeSpan(core, options.pipes)
|
|
258
315
|
} : {
|
|
259
316
|
kind: "text",
|
|
260
|
-
s: escapeInline(core, options.pipes)
|
|
317
|
+
s: escapeInline(core, options.pipes, href === null)
|
|
261
318
|
});
|
|
262
319
|
pending = trail;
|
|
263
320
|
}
|
|
@@ -275,6 +332,29 @@ function neighbour(tokens, index, step) {
|
|
|
275
332
|
if (token === void 0 || token.kind === "break") return "\n";
|
|
276
333
|
return (step === 1 ? FIRST_CHAR : LAST_CHAR).exec(token.s)[0];
|
|
277
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* A link whose whole text is its own address (`<a href="https://x.y/">https://x.y</a>`, styled
|
|
337
|
+
* or not) is read as that text: the text then becomes the autolink `<https://x.y>`, the same
|
|
338
|
+
* Markdown its plain-text form gives, so converting the rendered result again is stable.
|
|
339
|
+
*/
|
|
340
|
+
function selfLinkAsText(runs, index) {
|
|
341
|
+
const run = runs[index];
|
|
342
|
+
if (run.kind !== "text" || run.href === null || (run.style & 8) !== 0 || hrefOf(runs[index - 1]) === run.href || hrefOf(runs[index + 1]) === run.href) return run;
|
|
343
|
+
const text = run.text.trim();
|
|
344
|
+
return sameAddress(text, run.href) && escapeInline(text) === `<${text}>` ? {
|
|
345
|
+
...run,
|
|
346
|
+
href: null
|
|
347
|
+
} : run;
|
|
348
|
+
}
|
|
349
|
+
const hrefOf = (run) => run === void 0 || run.kind === "break" ? void 0 : run.href;
|
|
350
|
+
/** Whether a browser reads two addresses as the same URL (`http://x.y` and `http://x.y/`). */
|
|
351
|
+
function sameAddress(text, href) {
|
|
352
|
+
try {
|
|
353
|
+
return decodeURI(new URL(text).href) === decodeURI(new URL(href).href);
|
|
354
|
+
} catch {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
278
358
|
const FIRST_CHAR = /^[\s\S]/u;
|
|
279
359
|
const LAST_CHAR = /[\s\S]$/u;
|
|
280
360
|
const leftFlanking = (before, after) => !WHITESPACE.test(after) && (!PUNCTUATION.test(after) || WHITESPACE.test(before) || PUNCTUATION.test(before));
|
|
@@ -461,12 +541,26 @@ function joinTouching(tokens) {
|
|
|
461
541
|
/** Escapes the first text of every line, once delimiters are final. */
|
|
462
542
|
function escapeLineStarts(tokens) {
|
|
463
543
|
let lineStart = true;
|
|
464
|
-
for (const token of tokens) if (token.kind === "break") lineStart = true;
|
|
544
|
+
for (const [index, token] of tokens.entries()) if (token.kind === "break") lineStart = true;
|
|
465
545
|
else {
|
|
466
|
-
if (lineStart && token.kind === "text")
|
|
546
|
+
if (lineStart && token.kind === "text") escapeStartOfLine(tokens, index);
|
|
467
547
|
lineStart = false;
|
|
468
548
|
}
|
|
469
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* Escapes the start of the line that begins at `tokens[from]`, judged on the whole line:
|
|
552
|
+
* whether `-` or `1.` starts a list depends on what follows, which may be in the next token
|
|
553
|
+
* (`-` then a link). The backslash always lands in this first token: a marker's digits cannot
|
|
554
|
+
* run on into the next token, since two text tokens only meet where the second starts with
|
|
555
|
+
* whitespace.
|
|
556
|
+
*/
|
|
557
|
+
function escapeStartOfLine(tokens, from) {
|
|
558
|
+
const first = tokens[from];
|
|
559
|
+
let rest = "";
|
|
560
|
+
for (let index = from + 1; index < tokens.length && tokens[index]?.kind !== "break"; index += 1) rest += tokens[index].s;
|
|
561
|
+
const escaped = escapeLineStart(first.s + rest);
|
|
562
|
+
first.s = escaped.slice(0, escaped.length - rest.length);
|
|
563
|
+
}
|
|
470
564
|
/** Inline Markdown for runs: one line, or several joined by hard breaks. Empty when there is no content. */
|
|
471
565
|
function renderInline(runs, options) {
|
|
472
566
|
const raw = tokenize(normalize(runs), options);
|