scholarly-parse 0.1.1 → 0.2.1

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.
Files changed (190) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +6 -6
  3. package/changelog/0.2.x/0.2.0.md +43 -0
  4. package/changelog/0.2.x/0.2.1.md +45 -0
  5. package/dist/detect.d.ts +3 -2
  6. package/dist/detect.d.ts.map +1 -1
  7. package/dist/detect.js +40 -18
  8. package/dist/detect.js.map +1 -1
  9. package/dist/formats/html/blocks.d.ts +3 -2
  10. package/dist/formats/html/blocks.d.ts.map +1 -1
  11. package/dist/formats/html/blocks.js +116 -114
  12. package/dist/formats/html/blocks.js.map +1 -1
  13. package/dist/formats/html/context.d.ts +17 -0
  14. package/dist/formats/html/context.d.ts.map +1 -1
  15. package/dist/formats/html/context.js +4 -0
  16. package/dist/formats/html/context.js.map +1 -1
  17. package/dist/formats/html/inline.d.ts +7 -6
  18. package/dist/formats/html/inline.d.ts.map +1 -1
  19. package/dist/formats/html/inline.js +30 -28
  20. package/dist/formats/html/inline.js.map +1 -1
  21. package/dist/formats/html/metadata.d.ts +7 -10
  22. package/dist/formats/html/metadata.d.ts.map +1 -1
  23. package/dist/formats/html/metadata.js +59 -23
  24. package/dist/formats/html/metadata.js.map +1 -1
  25. package/dist/formats/html/parse.d.ts.map +1 -1
  26. package/dist/formats/html/parse.js +207 -59
  27. package/dist/formats/html/parse.js.map +1 -1
  28. package/dist/formats/html/references.d.ts.map +1 -1
  29. package/dist/formats/html/references.js +15 -31
  30. package/dist/formats/html/references.js.map +1 -1
  31. package/dist/formats/html/subtree.d.ts +112 -0
  32. package/dist/formats/html/subtree.d.ts.map +1 -0
  33. package/dist/formats/html/subtree.js +316 -0
  34. package/dist/formats/html/subtree.js.map +1 -0
  35. package/dist/formats/jats/blocks.d.ts +4 -1
  36. package/dist/formats/jats/blocks.d.ts.map +1 -1
  37. package/dist/formats/jats/blocks.js +176 -59
  38. package/dist/formats/jats/blocks.js.map +1 -1
  39. package/dist/formats/jats/fragment.d.ts +8 -2
  40. package/dist/formats/jats/fragment.d.ts.map +1 -1
  41. package/dist/formats/jats/fragment.js +187 -7
  42. package/dist/formats/jats/fragment.js.map +1 -1
  43. package/dist/formats/jats/front.d.ts +0 -5
  44. package/dist/formats/jats/front.d.ts.map +1 -1
  45. package/dist/formats/jats/front.js +55 -33
  46. package/dist/formats/jats/front.js.map +1 -1
  47. package/dist/formats/jats/inline.d.ts +3 -3
  48. package/dist/formats/jats/inline.d.ts.map +1 -1
  49. package/dist/formats/jats/inline.js +118 -43
  50. package/dist/formats/jats/inline.js.map +1 -1
  51. package/dist/formats/jats/parse.js +43 -6
  52. package/dist/formats/jats/parse.js.map +1 -1
  53. package/dist/formats/jats/references.d.ts +0 -5
  54. package/dist/formats/jats/references.d.ts.map +1 -1
  55. package/dist/formats/jats/references.js +158 -50
  56. package/dist/formats/jats/references.js.map +1 -1
  57. package/dist/formats/jats/text.d.ts +16 -0
  58. package/dist/formats/jats/text.d.ts.map +1 -1
  59. package/dist/formats/jats/text.js +77 -1
  60. package/dist/formats/jats/text.js.map +1 -1
  61. package/dist/formats/latexml/blocks.d.ts.map +1 -1
  62. package/dist/formats/latexml/blocks.js +42 -31
  63. package/dist/formats/latexml/blocks.js.map +1 -1
  64. package/dist/formats/latexml/context.d.ts +4 -1
  65. package/dist/formats/latexml/context.d.ts.map +1 -1
  66. package/dist/formats/latexml/context.js.map +1 -1
  67. package/dist/formats/latexml/inline.d.ts +4 -3
  68. package/dist/formats/latexml/inline.d.ts.map +1 -1
  69. package/dist/formats/latexml/inline.js +10 -11
  70. package/dist/formats/latexml/inline.js.map +1 -1
  71. package/dist/formats/latexml/parse.d.ts +7 -1
  72. package/dist/formats/latexml/parse.d.ts.map +1 -1
  73. package/dist/formats/latexml/parse.js +61 -25
  74. package/dist/formats/latexml/parse.js.map +1 -1
  75. package/dist/formats/pdf/inline.d.ts +16 -0
  76. package/dist/formats/pdf/inline.d.ts.map +1 -1
  77. package/dist/formats/pdf/inline.js +45 -3
  78. package/dist/formats/pdf/inline.js.map +1 -1
  79. package/dist/formats/pdf/layout.d.ts.map +1 -1
  80. package/dist/formats/pdf/layout.js +32 -1
  81. package/dist/formats/pdf/layout.js.map +1 -1
  82. package/dist/formats/pdf/lines.d.ts +7 -1
  83. package/dist/formats/pdf/lines.d.ts.map +1 -1
  84. package/dist/formats/pdf/lines.js +17 -1
  85. package/dist/formats/pdf/lines.js.map +1 -1
  86. package/dist/formats/pdf/load.d.ts +15 -5
  87. package/dist/formats/pdf/load.d.ts.map +1 -1
  88. package/dist/formats/pdf/load.js +17 -5
  89. package/dist/formats/pdf/load.js.map +1 -1
  90. package/dist/formats/pdf/parse.d.ts +5 -0
  91. package/dist/formats/pdf/parse.d.ts.map +1 -1
  92. package/dist/formats/pdf/parse.js +9 -10
  93. package/dist/formats/pdf/parse.js.map +1 -1
  94. package/dist/formats/pdf/references.d.ts +0 -6
  95. package/dist/formats/pdf/references.d.ts.map +1 -1
  96. package/dist/formats/pdf/references.js +44 -8
  97. package/dist/formats/pdf/references.js.map +1 -1
  98. package/dist/formats/pdf/structure.d.ts +3 -1
  99. package/dist/formats/pdf/structure.d.ts.map +1 -1
  100. package/dist/formats/pdf/structure.js +89 -33
  101. package/dist/formats/pdf/structure.js.map +1 -1
  102. package/dist/formats/tei/inline.d.ts +4 -1
  103. package/dist/formats/tei/inline.d.ts.map +1 -1
  104. package/dist/formats/tei/inline.js +6 -3
  105. package/dist/formats/tei/inline.js.map +1 -1
  106. package/dist/formats/tei/parse.d.ts.map +1 -1
  107. package/dist/formats/tei/parse.js +50 -30
  108. package/dist/formats/tei/parse.js.map +1 -1
  109. package/dist/html/dom.d.ts +34 -7
  110. package/dist/html/dom.d.ts.map +1 -1
  111. package/dist/html/dom.js +459 -1
  112. package/dist/html/dom.js.map +1 -1
  113. package/dist/html/math.d.ts +7 -0
  114. package/dist/html/math.d.ts.map +1 -0
  115. package/dist/html/math.js +34 -0
  116. package/dist/html/math.js.map +1 -0
  117. package/dist/model/arxiv.d.ts +51 -0
  118. package/dist/model/arxiv.d.ts.map +1 -0
  119. package/dist/model/arxiv.js +167 -0
  120. package/dist/model/arxiv.js.map +1 -0
  121. package/dist/model/document.d.ts +22 -4
  122. package/dist/model/document.d.ts.map +1 -1
  123. package/dist/model/doi.d.ts +21 -3
  124. package/dist/model/doi.d.ts.map +1 -1
  125. package/dist/model/doi.js +45 -3
  126. package/dist/model/doi.js.map +1 -1
  127. package/dist/model/identifiers.d.ts +32 -0
  128. package/dist/model/identifiers.d.ts.map +1 -0
  129. package/dist/model/identifiers.js +61 -0
  130. package/dist/model/identifiers.js.map +1 -0
  131. package/dist/model/input.d.ts +2 -0
  132. package/dist/model/input.d.ts.map +1 -1
  133. package/dist/model/input.js +2 -1
  134. package/dist/model/input.js.map +1 -1
  135. package/dist/model/limits.d.ts +14 -0
  136. package/dist/model/limits.d.ts.map +1 -0
  137. package/dist/model/limits.js +14 -0
  138. package/dist/model/limits.js.map +1 -0
  139. package/dist/model/section-ids.d.ts +23 -2
  140. package/dist/model/section-ids.d.ts.map +1 -1
  141. package/dist/model/section-ids.js +38 -13
  142. package/dist/model/section-ids.js.map +1 -1
  143. package/dist/parse.d.ts +6 -1
  144. package/dist/parse.d.ts.map +1 -1
  145. package/dist/parse.js.map +1 -1
  146. package/dist/render/bare-url.d.ts +29 -0
  147. package/dist/render/bare-url.d.ts.map +1 -0
  148. package/dist/render/bare-url.js +207 -0
  149. package/dist/render/bare-url.js.map +1 -0
  150. package/dist/render/escape.d.ts +59 -8
  151. package/dist/render/escape.d.ts.map +1 -1
  152. package/dist/render/escape.js +225 -17
  153. package/dist/render/escape.js.map +1 -1
  154. package/dist/render/inline.d.ts +53 -9
  155. package/dist/render/inline.d.ts.map +1 -1
  156. package/dist/render/inline.js +789 -23
  157. package/dist/render/inline.js.map +1 -1
  158. package/dist/render/markdown.d.ts +14 -9
  159. package/dist/render/markdown.d.ts.map +1 -1
  160. package/dist/render/markdown.js +39 -18
  161. package/dist/render/markdown.js.map +1 -1
  162. package/dist/render/read-inline.d.ts +70 -0
  163. package/dist/render/read-inline.d.ts.map +1 -0
  164. package/dist/render/read-inline.js +376 -0
  165. package/dist/render/read-inline.js.map +1 -0
  166. package/dist/render/scan.d.ts +54 -0
  167. package/dist/render/scan.d.ts.map +1 -0
  168. package/dist/render/scan.js +145 -0
  169. package/dist/render/scan.js.map +1 -0
  170. package/dist/render/sections.d.ts +9 -5
  171. package/dist/render/sections.d.ts.map +1 -1
  172. package/dist/render/sections.js +20 -15
  173. package/dist/render/sections.js.map +1 -1
  174. package/dist/render/text.d.ts +3 -4
  175. package/dist/render/text.d.ts.map +1 -1
  176. package/dist/render/text.js +5 -182
  177. package/dist/render/text.js.map +1 -1
  178. package/dist/xml/mathml.d.ts +16 -12
  179. package/dist/xml/mathml.d.ts.map +1 -1
  180. package/dist/xml/mathml.js +110 -40
  181. package/dist/xml/mathml.js.map +1 -1
  182. package/dist/xml/ordered.d.ts +0 -6
  183. package/dist/xml/ordered.d.ts.map +1 -1
  184. package/dist/xml/ordered.js +111 -20
  185. package/dist/xml/ordered.js.map +1 -1
  186. package/dist/xml/well-formed.d.ts +11 -3
  187. package/dist/xml/well-formed.d.ts.map +1 -1
  188. package/dist/xml/well-formed.js +25 -7
  189. package/dist/xml/well-formed.js.map +1 -1
  190. package/package.json +12 -8
@@ -9,50 +9,241 @@
9
9
  * @module src/render/escape
10
10
  */
11
11
  import { largest } from '../model/extremes.js';
12
+ import { bareUrlEnd, bareUrlStarts } from './bare-url.js';
13
+ import { ESCAPABLE } from './scan.js';
12
14
  /** ASCII punctuation CommonMark allows a backslash to escape. */
13
15
  const ESCAPABLE_AFTER_BACKSLASH = /\\(?=[!-/:-@[-`{-~])/g;
16
+ /** A letter or digit ending the text before an underscore run. */
17
+ const ENDS_IN_LETTER_OR_DIGIT = /[\p{L}\p{N}]$/u;
18
+ /** A letter or digit starting the text after an underscore run. */
19
+ const STARTS_WITH_LETTER_OR_DIGIT = /^[\p{L}\p{N}]/u;
14
20
  /**
15
21
  * Escape source text for use inside inline Markdown.
16
22
  *
17
23
  * - `\` before punctuation, `*`, `` ` ``, `$` (math delimiter), `~` (GFM strikethrough) — always.
18
- * - `_` — only where it could delimit emphasis, so `snake_case` and `H_2` stay readable.
24
+ * - `_` — every one in a run that could delimit emphasis; a run with a letter or digit on
25
+ * both sides never can, so `snake_case`, `H_2`, and `foo__bar` stay readable.
19
26
  * - `](` — the sequence that would turn bracketed text into a link.
20
27
  * - `<` — only before a letter, `/`, `!`, or `?`, where it could open an HTML tag.
21
28
  * - `&` — only where it would read as an entity reference.
29
+ * - A bare URL GFM would link (`https://…`, `www.…`) that holds or meets one of those
30
+ * escapes becomes an explicit link to the URL as the source has it, and text that only
31
+ * the escapes would make a URL links nothing ({@link bareUrlReader}).
32
+ *
33
+ * Readers join the escaped text of adjacent elements, so the text's end is closed as if
34
+ * anything could follow it, as the `_` rule already does: a trailing `\` is doubled, and
35
+ * a trailing `<`, `!` (an image's opener), or `&` with a partial entity name is escaped.
36
+ * A `](` split across two pieces, and markup another piece writes after a bare URL, are
37
+ * left to `joinInlineSeams`.
22
38
  *
23
39
  * Soft hyphens are dropped: they only mark where a word may break.
24
40
  */
25
41
  export function escapeInline(text) {
42
+ return writeBareUrls(escapeText(text));
43
+ }
44
+ /**
45
+ * {@link escapeInline} without its bare-URL rule, for link text: GFM links no bare URL
46
+ * there, and an explicit link would nest inside the outer one.
47
+ */
48
+ export function escapeText(text) {
26
49
  return text
27
50
  .replace(/\u00AD/g, '')
28
51
  .replace(ESCAPABLE_AFTER_BACKSLASH, '\\\\')
52
+ .replace(/\\$/, '\\\\')
29
53
  .replace(/[*`$~]/g, '\\$&')
30
- .replace(/(^|[^\p{L}\p{N}])_|_(?=$|[^\p{L}\p{N}])/gu, (match) => match.replace('_', '\\_'))
54
+ .replace(/_+/g, (run, at, whole) => ENDS_IN_LETTER_OR_DIGIT.test(whole.slice(Math.max(0, at - 2), at)) &&
55
+ STARTS_WITH_LETTER_OR_DIGIT.test(whole.slice(at + run.length, at + run.length + 2))
56
+ ? run
57
+ : run.replace(/_/g, '\\_'))
31
58
  .replace(/\]\(/g, '\\](')
32
- .replace(/<(?=[A-Za-z/!?])/g, '\\<')
33
- .replace(/&(?=#?[A-Za-z0-9]+;)/g, '\\&');
59
+ .replace(/<(?=[A-Za-z/!?]|$)/g, '\\<')
60
+ .replace(/!$/, '\\!')
61
+ .replace(/&(?=#?[A-Za-z0-9]+;|#?[A-Za-z0-9]*$)/g, '\\&');
62
+ }
63
+ /** Where a bare URL can start. */
64
+ const BARE_URL_START = /https?:\/\/|www\./gi;
65
+ /** Escaped text with each bare URL in it written as {@link bareUrlReader} writes it, in one pass. */
66
+ function writeBareUrls(markdown) {
67
+ const bareUrlAt = bareUrlReader(markdown);
68
+ let out = '';
69
+ let at = 0;
70
+ const start = new RegExp(BARE_URL_START);
71
+ for (let found = start.exec(markdown); found; found = start.exec(markdown)) {
72
+ const url = bareUrlAt(found.index);
73
+ if (!url)
74
+ continue;
75
+ out += markdown.slice(at, found.index) + url.written;
76
+ at = start.lastIndex = url.end;
77
+ }
78
+ return out + markdown.slice(at);
79
+ }
80
+ /** Markup this package writes that GFM reads as part of a bare URL it touches. */
81
+ const MARKUP = new Set(['*', '~', '$', '`']);
82
+ const SPACE = /^\s$/;
83
+ /**
84
+ * A reader of the bare URLs GFM links in `markdown`: given where one may start, whether it
85
+ * does, where it ends, and how to write it. One that holds a backslash escape or markup
86
+ * this package writes (an emphasis marker, a math or code delimiter, or the `[` of a link,
87
+ * at one of `linkOpeners`) is written as an explicit link to the URL GFM would link in the
88
+ * text as the source has it — its escapes undone, up to the first marker — and ends there:
89
+ * an autolink, or for a `www.` URL a link that keeps its text. Any other bare URL is
90
+ * written as it is, to the end GFM gives it. Asked in ascending order, it reads each
91
+ * stretch of the text a bounded number of times.
92
+ */
93
+ export function bareUrlReader(markdown, linkOpeners = new Set()) {
94
+ /** The stretch last searched for markup: from where, and the markup, space, or `<` that ends it. */
95
+ let searchedFrom = -1;
96
+ let markupAt = -1;
97
+ const firstMarkup = (start) => {
98
+ if (start < searchedFrom || start > markupAt) {
99
+ searchedFrom = markupAt = start;
100
+ for (let c = markdown.charAt(markupAt); c && c !== '<' && !SPACE.test(c);) {
101
+ if (MARKUP.has(c) || linkOpeners.has(markupAt))
102
+ break;
103
+ if (c === '\\' && ESCAPABLE.test(markdown.charAt(markupAt + 1)))
104
+ break;
105
+ c = markdown.charAt(++markupAt);
106
+ }
107
+ }
108
+ return markupAt;
109
+ };
110
+ /**
111
+ * The stretch last read as the source has it, and where each of its characters starts in
112
+ * `markdown`. A URL starting later in the same stretch reads the rest of it, so the
113
+ * stretch is read once.
114
+ */
115
+ let stretch;
116
+ const sourceFrom = (start) => {
117
+ let offset = stretch?.at.get(start);
118
+ if (!stretch || offset === undefined) {
119
+ const { from, source } = sourceUrl(markdown, start, linkOpeners);
120
+ stretch = { at: new Map(from.map((position, index) => [position, index])), from, source };
121
+ offset = 0;
122
+ }
123
+ return { offset, ...stretch };
124
+ };
125
+ return (start) => {
126
+ if (!bareUrlStarts(markdown, start))
127
+ return;
128
+ const limit = firstMarkup(start);
129
+ const end = bareUrlEnd(markdown, start, limit);
130
+ if (end === -1)
131
+ return;
132
+ if (end <= limit)
133
+ return { end, written: markdown.slice(start, end) };
134
+ const { from, offset, source } = sourceFrom(start);
135
+ const urlEnd = bareUrlEnd(source, offset);
136
+ if (urlEnd === -1)
137
+ return unlinked(markdown, start);
138
+ const url = source.slice(offset, urlEnd);
139
+ // GFM links a `www.` URL whose dot it gives back as `www` alone.
140
+ const written = /^https?:/i.test(url)
141
+ ? `<${escapeAutolink(url)}>`
142
+ : `[${wwwLinkText(url)}](http://${escapeUrl(url)})`;
143
+ return {
144
+ end: from[urlEnd] ?? start,
145
+ // A backslash the text has before the link would escape its `<`.
146
+ written: oddBackslashesBefore(markdown, start) ? `\\${written}` : written,
147
+ };
148
+ };
149
+ }
150
+ /**
151
+ * A bare URL that only the escapes make one (a `_` escaped in the domain's last two
152
+ * segments, where GFM accepts none) written with its scheme's `:` or its `www.` dot
153
+ * escaped, so GFM links nothing there, and ending past that character.
154
+ */
155
+ function unlinked(markdown, start) {
156
+ const at = markdown.indexOf(/^www\./i.test(markdown.slice(start, start + 4)) ? '.' : ':', start);
157
+ return { end: at + 1, written: `${markdown.slice(start, at)}\\${markdown.charAt(at)}` };
158
+ }
159
+ /** True when an odd run of backslashes ends right before `at`: the last one escapes what follows. */
160
+ export function oddBackslashesBefore(markdown, at) {
161
+ return countBackslashesBefore(markdown, at) % 2 === 1;
162
+ }
163
+ /**
164
+ * The text of the bare URL at `start` as the source has it: escapes undone, up to a
165
+ * marker, a link's `[` (one of `linkOpeners`), a space, an angle bracket (which an
166
+ * autolink cannot hold as written), or a `](` or `][` (where the URL ends whatever
167
+ * follows), with where in `markdown` each character came from, and where the text stops
168
+ * last.
169
+ */
170
+ function sourceUrl(markdown, start, linkOpeners) {
171
+ let source = '';
172
+ const from = [];
173
+ let at = start;
174
+ for (;;) {
175
+ const escaped = markdown.charAt(at) === '\\' && ESCAPABLE.test(markdown.charAt(at + 1));
176
+ const next = at + (escaped ? 1 : 0);
177
+ const char = markdown.charAt(next);
178
+ const after = markdown.charAt(next + 1);
179
+ if (!char || char === '<' || char === '>' || SPACE.test(char))
180
+ break;
181
+ if (!escaped && (MARKUP.has(char) || linkOpeners.has(next)))
182
+ break;
183
+ if (char === ']' && (after === '(' || after === '['))
184
+ break;
185
+ from.push(at);
186
+ source += char;
187
+ at = next + 1;
188
+ }
189
+ from.push(at);
190
+ return { from, source };
191
+ }
192
+ /** A `www.` URL escaped as link text, every bracket escaped so the link's own enclose it. */
193
+ function wwwLinkText(url) {
194
+ return escapeText(url).replace(/\\[\s\S]|[[\]]/g, (m) => (m.length === 2 ? m : `\\${m}`));
34
195
  }
35
196
  /**
36
197
  * Escape a character sequence that would change meaning at the start of a block:
37
- * an ATX heading, a quote, a list marker, a thematic break, a table row, or a link
38
- * reference definition.
198
+ * an ATX heading, a quote, a list marker (alone on the line too, where it would open an
199
+ * empty item), a tilde code fence, a thematic break, a table row, or a link
200
+ * reference definition, whose label holds no unescaped bracket (a `[` before a link's
201
+ * text that holds `]:` opens no definition, and escaping it would break the link).
39
202
  */
40
203
  export function escapeBlockStart(text) {
41
204
  return text
42
205
  .replace(/^(#{1,6})(?=\s|$)/, '\\$1')
43
- .replace(/^([>|])/, '\\$1')
44
- .replace(/^([-+])(?=\s)/, '\\$1')
45
- .replace(/^(\d{1,9})([.)])(?=\s)/, '$1\\$2')
206
+ .replace(/^([>|]|~(?=~~))/, '\\$1')
207
+ .replace(/^([-+*])(?=\s|$)/, '\\$1')
208
+ .replace(/^(\d{1,9})([.)])(?=\s|$)/, '$1\\$2')
46
209
  .replace(/^(=+|-{3,})\s*$/, '\\$1')
47
- .replace(/^\[(?=[^\]]*\]:)/, '\\[');
210
+ .replace(/^\[(?=(?:\\[\s\S]|[^\\[\]])*\]:)/, '\\[');
48
211
  }
49
212
  /**
50
213
  * Keep TeX from reading as markup where a Markdown renderer without math support treats
51
- * `$…$` as ordinary text: a `<` that could open an HTML tag and a `](` that could open a
52
- * link get a space between them, which math mode ignores.
214
+ * it as ordinary text: a `<` that could open an HTML tag and a `](` that could open a link
215
+ * get a space between them, which math mode ignores, and a backtick, which could open a
216
+ * code span, is written `\grave{}` (the accent command `` \` `` as `\grave`), which both
217
+ * KaTeX and MathJax list. For TeX between `$…$` (`inline`), what would end the delimiters
218
+ * early is closed too: a `$` is escaped, and a trailing backslash, which would escape the
219
+ * closing `$`, becomes a control space. Every backslash run is read once.
53
220
  */
54
- export function escapeTex(tex) {
55
- return tex.replace(/<(?=[A-Za-z/!?])/g, '< ').replace(/\]\(/g, '] (');
221
+ export function escapeTex(tex, { inline = false } = {}) {
222
+ const escaped = tex
223
+ .replace(/\\+|[`$]/g, (match, at, whole) => {
224
+ if (match.startsWith('\\')) {
225
+ // An odd run escapes the character after it: `` \` `` is the grave accent.
226
+ return match.length % 2 === 1 && whole.charAt(at + match.length) === '`'
227
+ ? `${match.slice(0, -1)}\\grave `
228
+ : match;
229
+ }
230
+ const escapedBefore = countBackslashesBefore(whole, at) % 2 === 1;
231
+ if (match === '`')
232
+ return escapedBefore ? '' : '\\grave{}';
233
+ return inline && !escapedBefore ? `\\${match}` : match;
234
+ })
235
+ .replace(/<(?=[A-Za-z/!?])/g, '< ')
236
+ .replace(/\]\(/g, '] (');
237
+ return inline && countBackslashesBefore(escaped, escaped.length) % 2 === 1
238
+ ? `${escaped} `
239
+ : escaped;
240
+ }
241
+ /** How many backslashes come right before `at`. */
242
+ function countBackslashesBefore(text, at) {
243
+ let backslashes = 0;
244
+ while (text.charAt(at - 1 - backslashes) === '\\')
245
+ backslashes++;
246
+ return backslashes;
56
247
  }
57
248
  /** Escape a value for a GFM table cell: pipes and line breaks would break the row. */
58
249
  export function escapeTableCell(text) {
@@ -82,13 +273,30 @@ const SAFE_URL = /^(?:https?|ftp|mailto):/i;
82
273
  export function isSafeUrl(url) {
83
274
  return SAFE_URL.test(url.trim());
84
275
  }
276
+ /**
277
+ * Escape a URL for use as an autolink's target, `<…>`: whitespace, control characters,
278
+ * and angle brackets, which an autolink cannot hold, are percent-encoded. Parentheses are
279
+ * left as written.
280
+ */
281
+ export function escapeAutolink(url) {
282
+ return url.trim().replace(/[\p{Cc}\s<>]/gu, encodeURIComponent);
283
+ }
85
284
  /** Parentheses, which `encodeURIComponent` leaves as they are. */
86
285
  const PARENTHESES = { '(': '%28', ')': '%29' };
87
286
  /**
88
- * Escape a URL for use as a Markdown link destination: whitespace, angle brackets, and
89
- * parentheses (an unbalanced one ends the destination) are percent-encoded.
287
+ * Escape a URL for use as a Markdown link destination, so the destination holds the URL as
288
+ * written: whitespace, angle brackets, parentheses (an unbalanced one ends the destination),
289
+ * and backticks (one could open a code span across the link) are percent-encoded; a
290
+ * backslash before punctuation or at the end, an `&` that would read as a character
291
+ * reference, and a `$` (which the readers of inline math would pair with another) are
292
+ * escaped.
90
293
  */
91
294
  export function escapeUrl(url) {
92
- return url.trim().replace(/[\s()<>]/g, (c) => PARENTHESES[c] ?? encodeURIComponent(c));
295
+ return url
296
+ .trim()
297
+ .replace(/[\s()<>`]/g, (c) => PARENTHESES[c] ?? encodeURIComponent(c))
298
+ .replace(/\\(?=[!-/:-@[-`{-~]|$)/g, '\\\\')
299
+ .replace(/&(?=#?[A-Za-z0-9]+;)/g, '\\&')
300
+ .replace(/\$/g, '\\$');
93
301
  }
94
302
  //# sourceMappingURL=escape.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"escape.js","sourceRoot":"","sources":["../../src/render/escape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,iEAAiE;AACjE,MAAM,yBAAyB,GAAG,uBAAuB,CAAC;AAE1D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,OAAO,IAAI;SACR,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;SACtB,OAAO,CAAC,yBAAyB,EAAE,MAAM,CAAC;SAC1C,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC;SAC1B,OAAO,CAAC,2CAA2C,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;SAC1F,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC;SACxB,OAAO,CAAC,mBAAmB,EAAE,KAAK,CAAC;SACnC,OAAO,CAAC,uBAAuB,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,OAAO,IAAI;SACR,OAAO,CAAC,mBAAmB,EAAE,MAAM,CAAC;SACpC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC;SAC1B,OAAO,CAAC,eAAe,EAA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5C,iEAAiE;AACjE,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,gBAAgB,EAAE,kBAAkB,CAAC,CAAC;AAClE,CAAC;AAED,kEAAkE;AAClE,MAAM,WAAW,GAAqC,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;AAEjF;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,OAAO,GAAG;SACP,IAAI,EAAE;SACN,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,kBAAkB,CAAC,CAAC,CAAC,CAAC;SACrE,OAAO,CAAC,yBAAyB,EAAE,MAAM,CAAC;SAC1C,OAAO,CAAC,uBAAuB,EAAE,KAAK,CAAC;SACvC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAC3B,CAAC"}
@@ -1,24 +1,32 @@
1
1
  /**
2
2
  * Wrap already-rendered Markdown in an emphasis marker (`*`, `**`, `~~`). Whitespace at
3
3
  * the edges moves outside the markers, where CommonMark requires it, and empty content
4
- * renders as itself. The edges are found by trimming, which reads each end once.
4
+ * renders as itself. The edges are found by trimming, which reads each end once; a
5
+ * backslash the trim leaves at the end is escaped, so it does not escape the closer. A
6
+ * marker between punctuation inside and a word outside cannot open or close;
7
+ * `joinInlineSeams` repairs that once the text beside it is known.
5
8
  */
6
9
  export declare function emphasis(markdown: string, marker: string): string;
7
10
  /**
8
11
  * A superscript. Citation numbers become a bracketed marker (`[12,13]`), so they do not
9
12
  * fuse with the word before them; anything else uses TeX-style notation (`10^{-5}`,
10
- * `x^2`) that stays readable as plain text.
13
+ * `x^2`) that stays readable as plain text. A marker's text is escaped like any source
14
+ * text, a backslash the trim leaves at the end is escaped, so it escapes nothing after the
15
+ * script, and a bare URL that ends a group is written as a link ({@link scriptGroup}).
11
16
  *
12
17
  * `isCitation` is the parser's judgment that the superscript holds only citation links;
13
18
  * an unlinked run of several citation numbers is recognized from its text.
14
19
  */
15
20
  export declare function superscript(markdown: string, plain: string, isCitation: boolean): string;
16
- /** A subscript in TeX-style notation: `CO_2`, `IC_{50}`. */
21
+ /** A subscript in TeX-style notation: `CO_2`, `IC_{50}`, a group as {@link superscript} writes one. */
17
22
  export declare function subscript(markdown: string, plain: string): string;
18
23
  /**
19
24
  * A link. An unsafe scheme (`javascript:`, `data:`) renders as its text only, or as the
20
- * target as escaped text when there is none; a link whose text is its own URL renders as
21
- * an autolink.
25
+ * target as escaped text when there is none; a link whose text is its own URL, as written
26
+ * or as `escapeInline` writes it, renders as an autolink. A bracket in the text with no
27
+ * partner, or in a pair a `(` follows, is escaped, and so is a backslash the trim leaves at
28
+ * its end, so the link's own brackets enclose the whole text; a link the text holds is
29
+ * written as its text: links do not nest.
22
30
  */
23
31
  export declare function link(markdown: string, url: string | undefined): string;
24
32
  /**
@@ -29,11 +37,47 @@ export declare const FORMULA_IMAGE = "[formula]";
29
37
  /** Inline math: `$tex$`. Empty TeX renders nothing. */
30
38
  export declare function inlineMath(tex: string): string;
31
39
  /**
32
- * Inline Markdown with each pair of touching inline formulas joined into one: `$a$$b$`
33
- * would read as a display-math delimiter. Source text never yields `$$` (its dollar
34
- * signs are escaped), so every `$$` in inline Markdown is such a seam.
40
+ * Inline Markdown assembled from separately escaped pieces, with the seams between them
41
+ * repaired: the escaping of each piece cannot see what the next one adds.
42
+ *
43
+ * - Touching inline formulas join into one (`$a$$b$` would read as a display-math
44
+ * delimiter), with a space where the TeX at the joint would open an HTML tag or a link,
45
+ * as `escapeTex` writes it, or where a control word (`\alpha`) would take in the letters
46
+ * after it. Source text never yields `$$` (its dollar signs are escaped), so every `$$`
47
+ * is such a seam.
48
+ * - Touching code spans join into one (`touchingCodeReader`): their fences would meet as
49
+ * one run that closes neither.
50
+ * - A `](` that `link` did not write, one not followed by a web or mail destination as
51
+ * `escapeUrl` writes one, is escaped, so no link forms with any other destination, nor
52
+ * one that runs into a code span, autolink, or formula written after it.
53
+ * - A bare URL that an escape, a marker, or a link after it would join is written as an
54
+ * explicit link to the URL as the source has it (`bareUrlReader`), unless it stands inside
55
+ * link text, where GFM links no bare URL.
56
+ * - A `!` before a link is escaped, so the link does not read as an image.
57
+ * - Two emphasis, strong, or strikethrough spans of one kind that touch, whose markers GFM
58
+ * reads as one run, join into one span ({@link joinTouchingSpans}): `**K****-step**`
59
+ * becomes `**K-step**`.
60
+ * - An emphasis, strong, or strikethrough pair that cannot form because punctuation at
61
+ * the emphasized text's edge meets a word outside the marker is repaired
62
+ * ({@link repairStuckMarkers}): the punctuation moves outside the marker
63
+ * (`*daf-16-*dependent` becomes `*daf-16*-dependent`), or, where that cannot work, the
64
+ * markers are dropped and the text kept.
65
+ *
66
+ * Code spans, autolinks, and link destinations are left as written, and a backslash
67
+ * escapes only the one character after it, so an escaped backslash escapes nothing. A
68
+ * joint or a repair can put a character GFM starts a bare URL after before a `www.`
69
+ * (`x*-www.` becomes `x-*www.`), and the backslash before a `](` can meet the bare URL
70
+ * that ends there (`https://a.co/x\](`), so the seams are read again wherever the text
71
+ * changed. The text is read once, plus once more when it holds a `![`, up to four times
72
+ * more when spans touch, up to four times more when a marker stands between punctuation
73
+ * and a word, and once more after any change.
74
+ */
75
+ export declare function joinInlineSeams(markdown: string): string;
76
+ /**
77
+ * Inline Markdown as one finished run: whitespace collapsed and trimmed, then the seams
78
+ * between its separately escaped pieces repaired ({@link joinInlineSeams}).
35
79
  */
36
- export declare function joinAdjacentMath(markdown: string): string;
80
+ export declare function collapseInline(markdown: string): string;
37
81
  /** Inline code. */
38
82
  export declare function inlineCode(text: string): string;
39
83
  //# sourceMappingURL=inline.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../src/render/inline.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAKjE;AAQD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,GAAG,MAAM,CAOxF;AAED,4DAA4D;AAC5D,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAIjE;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAMtE;AAOD;;;GAGG;AACH,eAAO,MAAM,aAAa,cAAc,CAAC;AAEzC,uDAAuD;AACvD,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG9C;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,mBAAmB;AACnB,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/C"}
1
+ {"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../src/render/inline.ts"],"names":[],"mappings":"AA4BA;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMjE;AAeD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,GAAG,MAAM,CASxF;AAED,uGAAuG;AACvG,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAMjE;AAYD;;;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