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