@tinta-lang/syntax 0.0.0-stage → 0.1.0-dev.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.
Files changed (245) hide show
  1. package/LICENSE +21 -0
  2. package/THIRD_PARTY_NOTICES.txt +13651 -0
  3. package/dist/directive.d.ts +40 -0
  4. package/dist/editing.d.ts +46 -0
  5. package/dist/enrichment.d.ts +5 -0
  6. package/dist/expression.d.ts +26 -0
  7. package/dist/image-destinations.d.ts +3 -0
  8. package/dist/index.d.ts +8 -0
  9. package/dist/index.js +10118 -0
  10. package/dist/parser.d.ts +40 -0
  11. package/dist/registry.d.ts +144 -0
  12. package/dist/source.d.ts +28 -0
  13. package/package.json +280 -3
  14. package/parser-provenance.json +1419 -0
  15. package/patches/README.md +5 -0
  16. package/patches/micromark.patch +84 -0
  17. package/vendor/@noble/hashes/LICENSE +21 -0
  18. package/vendor/@noble/hashes/_md.js +209 -0
  19. package/vendor/@noble/hashes/_u64.js +77 -0
  20. package/vendor/@noble/hashes/package.json +91 -0
  21. package/vendor/@noble/hashes/sha2.js +446 -0
  22. package/vendor/@noble/hashes/utils.js +687 -0
  23. package/vendor/character-entities/index.js +2132 -0
  24. package/vendor/character-entities/license +22 -0
  25. package/vendor/character-entities/package.json +78 -0
  26. package/vendor/decode-named-character-reference/index.js +19 -0
  27. package/vendor/decode-named-character-reference/license +22 -0
  28. package/vendor/decode-named-character-reference/package.json +90 -0
  29. package/vendor/devlop/lib/default.js +9 -0
  30. package/vendor/devlop/license +22 -0
  31. package/vendor/devlop/package.json +80 -0
  32. package/vendor/longest-streak/index.js +36 -0
  33. package/vendor/longest-streak/license +22 -0
  34. package/vendor/longest-streak/package.json +73 -0
  35. package/vendor/markdown-table/index.js +393 -0
  36. package/vendor/markdown-table/license +22 -0
  37. package/vendor/markdown-table/package.json +77 -0
  38. package/vendor/mdast-util-from-markdown/index.js +2 -0
  39. package/vendor/mdast-util-from-markdown/lib/index.js +1270 -0
  40. package/vendor/mdast-util-from-markdown/license +22 -0
  41. package/vendor/mdast-util-from-markdown/package.json +98 -0
  42. package/vendor/mdast-util-gfm-strikethrough/index.js +5 -0
  43. package/vendor/mdast-util-gfm-strikethrough/lib/index.js +133 -0
  44. package/vendor/mdast-util-gfm-strikethrough/license +22 -0
  45. package/vendor/mdast-util-gfm-strikethrough/package.json +84 -0
  46. package/vendor/mdast-util-gfm-table/index.js +2 -0
  47. package/vendor/mdast-util-gfm-table/lib/index.js +300 -0
  48. package/vendor/mdast-util-gfm-table/license +22 -0
  49. package/vendor/mdast-util-gfm-table/package.json +101 -0
  50. package/vendor/mdast-util-gfm-task-list-item/index.js +4 -0
  51. package/vendor/mdast-util-gfm-task-list-item/lib/index.js +141 -0
  52. package/vendor/mdast-util-gfm-task-list-item/license +22 -0
  53. package/vendor/mdast-util-gfm-task-list-item/package.json +90 -0
  54. package/vendor/mdast-util-phrasing/index.js +1 -0
  55. package/vendor/mdast-util-phrasing/lib/index.js +45 -0
  56. package/vendor/mdast-util-phrasing/license +23 -0
  57. package/vendor/mdast-util-phrasing/package.json +78 -0
  58. package/vendor/mdast-util-to-markdown/index.js +3 -0
  59. package/vendor/mdast-util-to-markdown/lib/configure.js +98 -0
  60. package/vendor/mdast-util-to-markdown/lib/handle/blockquote.js +40 -0
  61. package/vendor/mdast-util-to-markdown/lib/handle/break.js +41 -0
  62. package/vendor/mdast-util-to-markdown/lib/handle/code.js +86 -0
  63. package/vendor/mdast-util-to-markdown/lib/handle/definition.js +83 -0
  64. package/vendor/mdast-util-to-markdown/lib/handle/emphasis.js +67 -0
  65. package/vendor/mdast-util-to-markdown/lib/handle/heading.js +84 -0
  66. package/vendor/mdast-util-to-markdown/lib/handle/html.js +27 -0
  67. package/vendor/mdast-util-to-markdown/lib/handle/image-reference.js +78 -0
  68. package/vendor/mdast-util-to-markdown/lib/handle/image.js +92 -0
  69. package/vendor/mdast-util-to-markdown/lib/handle/index.js +45 -0
  70. package/vendor/mdast-util-to-markdown/lib/handle/inline-code.js +86 -0
  71. package/vendor/mdast-util-to-markdown/lib/handle/link-reference.js +78 -0
  72. package/vendor/mdast-util-to-markdown/lib/handle/link.js +129 -0
  73. package/vendor/mdast-util-to-markdown/lib/handle/list-item.js +73 -0
  74. package/vendor/mdast-util-to-markdown/lib/handle/list.js +107 -0
  75. package/vendor/mdast-util-to-markdown/lib/handle/paragraph.js +27 -0
  76. package/vendor/mdast-util-to-markdown/lib/handle/root.js +34 -0
  77. package/vendor/mdast-util-to-markdown/lib/handle/strong.js +67 -0
  78. package/vendor/mdast-util-to-markdown/lib/handle/text.js +30 -0
  79. package/vendor/mdast-util-to-markdown/lib/handle/thematic-break.js +27 -0
  80. package/vendor/mdast-util-to-markdown/lib/index.js +213 -0
  81. package/vendor/mdast-util-to-markdown/lib/join.js +56 -0
  82. package/vendor/mdast-util-to-markdown/lib/unsafe.js +160 -0
  83. package/vendor/mdast-util-to-markdown/lib/util/association.js +33 -0
  84. package/vendor/mdast-util-to-markdown/lib/util/character.js +32 -0
  85. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-ordered.js +25 -0
  86. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-other.js +42 -0
  87. package/vendor/mdast-util-to-markdown/lib/util/check-bullet.js +25 -0
  88. package/vendor/mdast-util-to-markdown/lib/util/check-emphasis.js +25 -0
  89. package/vendor/mdast-util-to-markdown/lib/util/check-fence.js +25 -0
  90. package/vendor/mdast-util-to-markdown/lib/util/check-list-item-indent.js +25 -0
  91. package/vendor/mdast-util-to-markdown/lib/util/check-quote.js +25 -0
  92. package/vendor/mdast-util-to-markdown/lib/util/check-rule-repetition.js +25 -0
  93. package/vendor/mdast-util-to-markdown/lib/util/check-rule.js +25 -0
  94. package/vendor/mdast-util-to-markdown/lib/util/check-strong.js +25 -0
  95. package/vendor/mdast-util-to-markdown/lib/util/compile-pattern.js +30 -0
  96. package/vendor/mdast-util-to-markdown/lib/util/container-flow.js +93 -0
  97. package/vendor/mdast-util-to-markdown/lib/util/container-phrasing.js +704 -0
  98. package/vendor/mdast-util-to-markdown/lib/util/encode-character-reference.js +11 -0
  99. package/vendor/mdast-util-to-markdown/lib/util/encode-info.js +82 -0
  100. package/vendor/mdast-util-to-markdown/lib/util/format-code-as-indented.js +27 -0
  101. package/vendor/mdast-util-to-markdown/lib/util/format-heading-as-setext.js +39 -0
  102. package/vendor/mdast-util-to-markdown/lib/util/format-link-as-autolink.js +39 -0
  103. package/vendor/mdast-util-to-markdown/lib/util/html-kind.js +63 -0
  104. package/vendor/mdast-util-to-markdown/lib/util/indent-lines.js +40 -0
  105. package/vendor/mdast-util-to-markdown/lib/util/pattern-in-scope.js +52 -0
  106. package/vendor/mdast-util-to-markdown/lib/util/safe.js +263 -0
  107. package/vendor/mdast-util-to-markdown/lib/util/track.js +55 -0
  108. package/vendor/mdast-util-to-markdown/license +22 -0
  109. package/vendor/mdast-util-to-markdown/package.json +88 -0
  110. package/vendor/mdast-util-to-string/index.js +5 -0
  111. package/vendor/mdast-util-to-string/lib/index.js +108 -0
  112. package/vendor/mdast-util-to-string/license +22 -0
  113. package/vendor/mdast-util-to-string/package.json +78 -0
  114. package/vendor/micromark/index.js +60 -0
  115. package/vendor/micromark/lib/compile.js +1068 -0
  116. package/vendor/micromark/lib/constructs.js +85 -0
  117. package/vendor/micromark/lib/create-tokenizer.js +615 -0
  118. package/vendor/micromark/lib/initialize/content.js +79 -0
  119. package/vendor/micromark/lib/initialize/document.js +370 -0
  120. package/vendor/micromark/lib/initialize/flow.js +58 -0
  121. package/vendor/micromark/lib/initialize/text.js +216 -0
  122. package/vendor/micromark/lib/parse.js +56 -0
  123. package/vendor/micromark/lib/postprocess.js +18 -0
  124. package/vendor/micromark/lib/preprocess.js +112 -0
  125. package/vendor/micromark/license +22 -0
  126. package/vendor/micromark/package.json +87 -0
  127. package/vendor/micromark-core-commonmark/index.js +22 -0
  128. package/vendor/micromark-core-commonmark/lib/attention.js +231 -0
  129. package/vendor/micromark-core-commonmark/lib/autolink.js +233 -0
  130. package/vendor/micromark-core-commonmark/lib/blank-line.js +61 -0
  131. package/vendor/micromark-core-commonmark/lib/block-quote.js +143 -0
  132. package/vendor/micromark-core-commonmark/lib/character-escape.js +64 -0
  133. package/vendor/micromark-core-commonmark/lib/character-reference.js +149 -0
  134. package/vendor/micromark-core-commonmark/lib/code-fenced.js +422 -0
  135. package/vendor/micromark-core-commonmark/lib/code-indented.js +159 -0
  136. package/vendor/micromark-core-commonmark/lib/code-text.js +225 -0
  137. package/vendor/micromark-core-commonmark/lib/content.js +163 -0
  138. package/vendor/micromark-core-commonmark/lib/definition.js +254 -0
  139. package/vendor/micromark-core-commonmark/lib/hard-break-escape.js +60 -0
  140. package/vendor/micromark-core-commonmark/lib/heading-atx.js +191 -0
  141. package/vendor/micromark-core-commonmark/lib/html-flow.js +827 -0
  142. package/vendor/micromark-core-commonmark/lib/html-text.js +678 -0
  143. package/vendor/micromark-core-commonmark/lib/label-end.js +561 -0
  144. package/vendor/micromark-core-commonmark/lib/label-start-image.js +110 -0
  145. package/vendor/micromark-core-commonmark/lib/label-start-link.js +63 -0
  146. package/vendor/micromark-core-commonmark/lib/line-ending.js +33 -0
  147. package/vendor/micromark-core-commonmark/lib/list.js +213 -0
  148. package/vendor/micromark-core-commonmark/lib/partial-non-lazy-continuation.js +61 -0
  149. package/vendor/micromark-core-commonmark/lib/setext-underline.js +187 -0
  150. package/vendor/micromark-core-commonmark/lib/thematic-break.js +102 -0
  151. package/vendor/micromark-core-commonmark/license +22 -0
  152. package/vendor/micromark-core-commonmark/package.json +62 -0
  153. package/vendor/micromark-extension-gfm-strikethrough/index.js +3 -0
  154. package/vendor/micromark-extension-gfm-strikethrough/lib/html.js +26 -0
  155. package/vendor/micromark-extension-gfm-strikethrough/lib/syntax.js +142 -0
  156. package/vendor/micromark-extension-gfm-strikethrough/license +22 -0
  157. package/vendor/micromark-extension-gfm-strikethrough/package.json +129 -0
  158. package/vendor/micromark-extension-gfm-table/index.js +2 -0
  159. package/vendor/micromark-extension-gfm-table/lib/edit-map.js +222 -0
  160. package/vendor/micromark-extension-gfm-table/lib/html.js +135 -0
  161. package/vendor/micromark-extension-gfm-table/lib/infer.js +52 -0
  162. package/vendor/micromark-extension-gfm-table/lib/syntax.js +846 -0
  163. package/vendor/micromark-extension-gfm-table/license +22 -0
  164. package/vendor/micromark-extension-gfm-table/package.json +86 -0
  165. package/vendor/micromark-extension-gfm-task-list-item/index.js +2 -0
  166. package/vendor/micromark-extension-gfm-task-list-item/lib/html.js +29 -0
  167. package/vendor/micromark-extension-gfm-task-list-item/lib/syntax.js +158 -0
  168. package/vendor/micromark-extension-gfm-task-list-item/license +22 -0
  169. package/vendor/micromark-extension-gfm-task-list-item/package.json +131 -0
  170. package/vendor/micromark-factory-destination/index.js +206 -0
  171. package/vendor/micromark-factory-destination/license +22 -0
  172. package/vendor/micromark-factory-destination/package.json +57 -0
  173. package/vendor/micromark-factory-label/index.js +148 -0
  174. package/vendor/micromark-factory-label/license +22 -0
  175. package/vendor/micromark-factory-label/package.json +60 -0
  176. package/vendor/micromark-factory-space/index.js +116 -0
  177. package/vendor/micromark-factory-space/license +22 -0
  178. package/vendor/micromark-factory-space/package.json +46 -0
  179. package/vendor/micromark-factory-title/index.js +158 -0
  180. package/vendor/micromark-factory-title/license +22 -0
  181. package/vendor/micromark-factory-title/package.json +58 -0
  182. package/vendor/micromark-factory-whitespace/index.js +44 -0
  183. package/vendor/micromark-factory-whitespace/license +22 -0
  184. package/vendor/micromark-factory-whitespace/package.json +57 -0
  185. package/vendor/micromark-util-character/index.js +246 -0
  186. package/vendor/micromark-util-character/license +22 -0
  187. package/vendor/micromark-util-character/package.json +57 -0
  188. package/vendor/micromark-util-chunked/index.js +81 -0
  189. package/vendor/micromark-util-chunked/license +22 -0
  190. package/vendor/micromark-util-chunked/package.json +57 -0
  191. package/vendor/micromark-util-classify-character/index.js +27 -0
  192. package/vendor/micromark-util-classify-character/license +22 -0
  193. package/vendor/micromark-util-classify-character/package.json +59 -0
  194. package/vendor/micromark-util-combine-extensions/index.js +143 -0
  195. package/vendor/micromark-util-combine-extensions/license +22 -0
  196. package/vendor/micromark-util-combine-extensions/package.json +52 -0
  197. package/vendor/micromark-util-decode-numeric-character-reference/index.js +32 -0
  198. package/vendor/micromark-util-decode-numeric-character-reference/license +22 -0
  199. package/vendor/micromark-util-decode-numeric-character-reference/package.json +59 -0
  200. package/vendor/micromark-util-decode-string/index.js +45 -0
  201. package/vendor/micromark-util-decode-string/license +22 -0
  202. package/vendor/micromark-util-decode-string/package.json +63 -0
  203. package/vendor/micromark-util-edit-map/index.js +225 -0
  204. package/vendor/micromark-util-edit-map/license +22 -0
  205. package/vendor/micromark-util-edit-map/package.json +48 -0
  206. package/vendor/micromark-util-encode/index.js +33 -0
  207. package/vendor/micromark-util-encode/license +22 -0
  208. package/vendor/micromark-util-encode/package.json +47 -0
  209. package/vendor/micromark-util-html-tag-name/index.js +93 -0
  210. package/vendor/micromark-util-html-tag-name/license +22 -0
  211. package/vendor/micromark-util-html-tag-name/package.json +47 -0
  212. package/vendor/micromark-util-normalize-identifier/index.js +33 -0
  213. package/vendor/micromark-util-normalize-identifier/license +22 -0
  214. package/vendor/micromark-util-normalize-identifier/package.json +58 -0
  215. package/vendor/micromark-util-resolve-all/index.js +32 -0
  216. package/vendor/micromark-util-resolve-all/license +22 -0
  217. package/vendor/micromark-util-resolve-all/package.json +48 -0
  218. package/vendor/micromark-util-sanitize-uri/index.js +107 -0
  219. package/vendor/micromark-util-sanitize-uri/license +22 -0
  220. package/vendor/micromark-util-sanitize-uri/package.json +59 -0
  221. package/vendor/micromark-util-subtokenize/index.js +228 -0
  222. package/vendor/micromark-util-subtokenize/lib/splice-buffer.js +251 -0
  223. package/vendor/micromark-util-subtokenize/license +22 -0
  224. package/vendor/micromark-util-subtokenize/package.json +60 -0
  225. package/vendor/unist-util-is/index.js +7 -0
  226. package/vendor/unist-util-is/lib/index.js +296 -0
  227. package/vendor/unist-util-is/license +22 -0
  228. package/vendor/unist-util-is/package.json +103 -0
  229. package/vendor/unist-util-stringify-position/index.js +1 -0
  230. package/vendor/unist-util-stringify-position/lib/index.js +84 -0
  231. package/vendor/unist-util-stringify-position/license +22 -0
  232. package/vendor/unist-util-stringify-position/package.json +80 -0
  233. package/vendor/unist-util-visit/index.js +2 -0
  234. package/vendor/unist-util-visit/lib/index.js +312 -0
  235. package/vendor/unist-util-visit/license +22 -0
  236. package/vendor/unist-util-visit/package.json +108 -0
  237. package/vendor/unist-util-visit-parents/index.js +2 -0
  238. package/vendor/unist-util-visit-parents/lib/color.js +7 -0
  239. package/vendor/unist-util-visit-parents/lib/index.js +399 -0
  240. package/vendor/unist-util-visit-parents/license +22 -0
  241. package/vendor/unist-util-visit-parents/package.json +106 -0
  242. package/vendor/zwitch/index.js +118 -0
  243. package/vendor/zwitch/license +22 -0
  244. package/vendor/zwitch/package.json +72 -0
  245. package/README.md +0 -3
@@ -0,0 +1,1270 @@
1
+ /**
2
+ * @import {
3
+ * Break,
4
+ * Blockquote,
5
+ * Code,
6
+ * Definition,
7
+ * Emphasis,
8
+ * Heading,
9
+ * Html,
10
+ * Image,
11
+ * InlineCode,
12
+ * Link,
13
+ * ListItem,
14
+ * List,
15
+ * Nodes,
16
+ * Paragraph,
17
+ * PhrasingContent,
18
+ * ReferenceType,
19
+ * Root,
20
+ * Strong,
21
+ * Text,
22
+ * ThematicBreak
23
+ * } from 'mdast'
24
+ * @import {
25
+ * Encoding,
26
+ * Event,
27
+ * Token,
28
+ * Value
29
+ * } from 'micromark-util-types'
30
+ * @import {Point} from 'unist'
31
+ * @import {
32
+ * CompileContext,
33
+ * CompileData,
34
+ * Config,
35
+ * Extension,
36
+ * Handle,
37
+ * OnEnterError,
38
+ * Options
39
+ * } from './types.js'
40
+ */
41
+
42
+ import { toString } from 'mdast-util-to-string';
43
+ import { parse, postprocess, preprocess } from 'micromark';
44
+ import { decodeNumericCharacterReference } from 'micromark-util-decode-numeric-character-reference';
45
+ import { decodeString } from 'micromark-util-decode-string';
46
+ import { normalizeIdentifier } from 'micromark-util-normalize-identifier';
47
+ import { decodeNamedCharacterReference } from 'decode-named-character-reference';
48
+ import { stringifyPosition } from 'unist-util-stringify-position';
49
+ const own = {}.hasOwnProperty;
50
+
51
+ /**
52
+ * Turn markdown into a syntax tree.
53
+ *
54
+ * @overload
55
+ * @param {Value} value
56
+ * @param {Encoding | null | undefined} [encoding]
57
+ * @param {Options | null | undefined} [options]
58
+ * @returns {Root}
59
+ *
60
+ * @overload
61
+ * @param {Value} value
62
+ * @param {Options | null | undefined} [options]
63
+ * @returns {Root}
64
+ *
65
+ * @param {Value} value
66
+ * Markdown to parse.
67
+ * @param {Encoding | Options | null | undefined} [encoding]
68
+ * Character encoding for when `value` is `Buffer`.
69
+ * @param {Options | null | undefined} [options]
70
+ * Configuration.
71
+ * @returns {Root}
72
+ * mdast tree.
73
+ */
74
+ export function fromMarkdown(value, encoding, options) {
75
+ if (encoding && typeof encoding === 'object') {
76
+ options = encoding;
77
+ encoding = undefined;
78
+ }
79
+ return compiler(options)(postprocess(parse(options).document().write(preprocess()(value, encoding, true))));
80
+ }
81
+
82
+ /**
83
+ * Note this compiler only understand complete buffering, not streaming.
84
+ *
85
+ * @param {Options | null | undefined} [options]
86
+ */
87
+ function compiler(options) {
88
+ /** @type {Config} */
89
+ const config = {
90
+ afterExit: [],
91
+ beforeEnter: [],
92
+ transforms: [],
93
+ canContainEols: ['emphasis', 'fragment', 'heading', 'paragraph', 'strong'],
94
+ enter: {
95
+ autolink: opener(link),
96
+ autolinkProtocol: onenterdata,
97
+ autolinkEmail: onenterdata,
98
+ atxHeading: opener(heading),
99
+ blockQuote: opener(blockQuote),
100
+ characterEscape: onenterdata,
101
+ characterReference: onenterdata,
102
+ codeFenced: opener(codeFlow),
103
+ codeFencedFenceInfo: buffer,
104
+ codeFencedFenceMeta: buffer,
105
+ codeIndented: opener(codeFlow, buffer),
106
+ codeText: opener(codeText, buffer),
107
+ codeTextData: onenterdata,
108
+ data: onenterdata,
109
+ codeFlowValue: onenterdata,
110
+ definition: opener(definition),
111
+ definitionDestinationString: buffer,
112
+ definitionLabelString: buffer,
113
+ definitionTitleString: buffer,
114
+ emphasis: opener(emphasis),
115
+ hardBreakEscape: opener(hardBreak),
116
+ hardBreakTrailing: opener(hardBreak),
117
+ htmlFlow: opener(html, buffer),
118
+ htmlFlowData: onenterdata,
119
+ htmlText: opener(html, buffer),
120
+ htmlTextData: onenterdata,
121
+ image: opener(image),
122
+ label: buffer,
123
+ link: opener(link),
124
+ listItem: opener(listItem),
125
+ listItemValue: onenterlistitemvalue,
126
+ listOrdered: opener(list, onenterlistordered),
127
+ listUnordered: opener(list),
128
+ paragraph: opener(paragraph),
129
+ reference: onenterreference,
130
+ referenceString: buffer,
131
+ resourceDestinationString: buffer,
132
+ resourceTitleString: buffer,
133
+ setextHeading: opener(heading),
134
+ strong: opener(strong),
135
+ thematicBreak: opener(thematicBreak)
136
+ },
137
+ exit: {
138
+ atxHeading: closer(),
139
+ atxHeadingSequence: onexitatxheadingsequence,
140
+ autolink: closer(),
141
+ autolinkEmail: onexitautolinkemail,
142
+ autolinkProtocol: onexitautolinkprotocol,
143
+ blockQuote: closer(),
144
+ characterEscapeValue: onexitdata,
145
+ characterReferenceMarkerHexadecimal: onexitcharacterreferencemarker,
146
+ characterReferenceMarkerNumeric: onexitcharacterreferencemarker,
147
+ characterReferenceValue: onexitcharacterreferencevalue,
148
+ characterReference: onexitcharacterreference,
149
+ codeFenced: closer(onexitcodefenced),
150
+ codeFencedFence: onexitcodefencedfence,
151
+ codeFencedFenceInfo: onexitcodefencedfenceinfo,
152
+ codeFencedFenceMeta: onexitcodefencedfencemeta,
153
+ codeFlowValue: onexitdata,
154
+ codeIndented: closer(onexitcodeindented),
155
+ codeText: closer(onexitcodetext),
156
+ codeTextData: onexitdata,
157
+ data: onexitdata,
158
+ definition: closer(),
159
+ definitionDestinationString: onexitdefinitiondestinationstring,
160
+ definitionLabelString: onexitdefinitionlabelstring,
161
+ definitionTitleString: onexitdefinitiontitlestring,
162
+ emphasis: closer(),
163
+ hardBreakEscape: closer(onexithardbreak),
164
+ hardBreakTrailing: closer(onexithardbreak),
165
+ htmlFlow: closer(onexithtmlflow),
166
+ htmlFlowData: onexitdata,
167
+ htmlText: closer(onexithtmltext),
168
+ htmlTextData: onexitdata,
169
+ image: closer(onexitimage),
170
+ label: onexitlabel,
171
+ labelText: onexitlabeltext,
172
+ lineEnding: onexitlineending,
173
+ link: closer(onexitlink),
174
+ listItem: closer(),
175
+ listOrdered: closer(),
176
+ listUnordered: closer(),
177
+ paragraph: closer(),
178
+ referenceString: onexitreferencestring,
179
+ resourceDestinationString: onexitresourcedestinationstring,
180
+ resourceTitleString: onexitresourcetitlestring,
181
+ resource: onexitresource,
182
+ setextHeading: closer(onexitsetextheading),
183
+ setextHeadingLineSequence: onexitsetextheadinglinesequence,
184
+ setextHeadingText: onexitsetextheadingtext,
185
+ strong: closer(),
186
+ thematicBreak: closer()
187
+ }
188
+ };
189
+ configure(config, (options || {}).mdastExtensions || []);
190
+
191
+ /** @type {CompileData} */
192
+ const data = {};
193
+ return compile;
194
+
195
+ /**
196
+ * Turn micromark events into an mdast tree.
197
+ *
198
+ * @param {Array<Event>} events
199
+ * Events.
200
+ * @returns {Root}
201
+ * mdast tree.
202
+ */
203
+ function compile(events) {
204
+ /** @type {Root} */
205
+ let tree = {
206
+ type: 'root',
207
+ children: [],
208
+ position: undefined
209
+ };
210
+ /** @type {Omit<CompileContext, 'sliceSerialize'>} */
211
+ const context = {
212
+ stack: [tree],
213
+ tokenStack: [],
214
+ config,
215
+ enter,
216
+ exit,
217
+ buffer,
218
+ resume,
219
+ data
220
+ };
221
+ /** @type {Array<number>} */
222
+ const listStack = [];
223
+ /** @type {Array<Event>} */
224
+ const prepared = [];
225
+ let index = -1;
226
+
227
+ // Add `listItem` tokens and infer spread.
228
+ // Each list is prepared while it is last in `prepared`, so nothing shifts.
229
+ while (++index < events.length) {
230
+ prepared.push(events[index]);
231
+ if (events[index][1].type === "listOrdered" || events[index][1].type === "listUnordered") {
232
+ if (events[index][0] === 'enter') {
233
+ listStack.push(prepared.length - 1);
234
+ } else {
235
+ const tail = listStack.pop();
236
+ prepareList(prepared, tail);
237
+ }
238
+ }
239
+ }
240
+ events = prepared;
241
+ index = -1;
242
+ while (++index < events.length) {
243
+ const handler = config[events[index][0]];
244
+ if (events[index][0] === 'enter' && config.beforeEnter.length > 0) {
245
+ callListeners(config.beforeEnter, {
246
+ ...context,
247
+ sliceSerialize: events[index][2].sliceSerialize
248
+ }, events[index][1]);
249
+ }
250
+ if (own.call(handler, events[index][1].type)) {
251
+ // Note: spread first, V8 has optimizations for object shape cloning.
252
+ handler[events[index][1].type].call({
253
+ ...context,
254
+ sliceSerialize: events[index][2].sliceSerialize
255
+ }, events[index][1]);
256
+ }
257
+ if (events[index][0] === 'exit' && config.afterExit.length > 0) {
258
+ callListeners(config.afterExit, {
259
+ ...context,
260
+ sliceSerialize: events[index][2].sliceSerialize
261
+ }, events[index][1]);
262
+ }
263
+ }
264
+
265
+ // Handle tokens still being open.
266
+ if (context.tokenStack.length > 0) {
267
+ const tail = context.tokenStack[context.tokenStack.length - 1];
268
+ const handler = tail[1] || defaultOnError;
269
+ handler.call(context, undefined, tail[0]);
270
+ }
271
+
272
+ // Figure out `root` position.
273
+ tree.position = {
274
+ start: point(events.length > 0 ? events[0][1].start : {
275
+ line: 1,
276
+ column: 1,
277
+ offset: 0
278
+ }),
279
+ end: point(events.length > 0 ? events[events.length - 2][1].end : {
280
+ line: 1,
281
+ column: 1,
282
+ offset: 0
283
+ })
284
+ };
285
+
286
+ // Call transforms.
287
+ index = -1;
288
+ while (++index < config.transforms.length) {
289
+ tree = config.transforms[index](tree) || tree;
290
+ }
291
+ return tree;
292
+ }
293
+
294
+ /**
295
+ * @param {Array<Handle>} listeners
296
+ * Listeners.
297
+ * @param {CompileContext} context
298
+ * Context.
299
+ * @param {Token} token
300
+ * Token.
301
+ * @returns {undefined}
302
+ * Nothing.
303
+ */
304
+ function callListeners(listeners, context, token) {
305
+ let index = -1;
306
+ while (++index < listeners.length) {
307
+ listeners[index].call(context, token);
308
+ }
309
+ }
310
+
311
+ /**
312
+ * @param {Array<Event>} events
313
+ * Events, ending with the list to prepare.
314
+ * @param {number} start
315
+ * Index of the list enter.
316
+ * @returns {undefined}
317
+ */
318
+ function prepareList(events, start) {
319
+ const end = events.length - 1;
320
+ let index = start - 1;
321
+ let containerBalance = -1;
322
+ let listSpread = false;
323
+ /** @type {Token | undefined} */
324
+ let listItem;
325
+ /** @type {number | undefined} */
326
+ let lineIndex;
327
+ /** @type {number | undefined} */
328
+ let firstBlankLineIndex;
329
+ /** @type {boolean | undefined} */
330
+ let atMarker;
331
+ /**
332
+ * `listItem` events to insert before the event at `at`, in walk order.
333
+ * Merged in after the walk, as inserting while walking would shift the
334
+ * rest of the list once per item.
335
+ * @type {Array<{at: number, event: Event}>}
336
+ */
337
+ const insertions = [];
338
+ while (++index <= end) {
339
+ const event = events[index];
340
+ switch (event[1].type) {
341
+ case "listUnordered":
342
+ case "listOrdered":
343
+ case "blockQuote":
344
+ {
345
+ if (event[0] === 'enter') {
346
+ containerBalance++;
347
+ } else {
348
+ containerBalance--;
349
+ }
350
+ atMarker = undefined;
351
+ break;
352
+ }
353
+ case "lineEndingBlank":
354
+ {
355
+ if (event[0] === 'enter') {
356
+ if (listItem && !atMarker && !containerBalance && !firstBlankLineIndex) {
357
+ firstBlankLineIndex = index;
358
+ }
359
+ atMarker = undefined;
360
+ }
361
+ break;
362
+ }
363
+ case "linePrefix":
364
+ case "listItemValue":
365
+ case "listItemMarker":
366
+ case "listItemPrefix":
367
+ case "listItemPrefixWhitespace":
368
+ {
369
+ // Empty.
370
+
371
+ break;
372
+ }
373
+ default:
374
+ {
375
+ atMarker = undefined;
376
+ }
377
+ }
378
+ if (!containerBalance && event[0] === 'enter' && event[1].type === "listItemPrefix" || containerBalance === -1 && event[0] === 'exit' && (event[1].type === "listUnordered" || event[1].type === "listOrdered")) {
379
+ if (listItem) {
380
+ let tailIndex = index;
381
+ lineIndex = undefined;
382
+ while (tailIndex--) {
383
+ const tailEvent = events[tailIndex];
384
+ if (tailEvent[1].type === "lineEnding" || tailEvent[1].type === "lineEndingBlank") {
385
+ if (tailEvent[0] === 'exit') continue;
386
+ if (lineIndex) {
387
+ events[lineIndex][1].type = "lineEndingBlank";
388
+ listSpread = true;
389
+ }
390
+ tailEvent[1].type = "lineEnding";
391
+ lineIndex = tailIndex;
392
+ } else if (tailEvent[1].type === "linePrefix" || tailEvent[1].type === "blockQuotePrefix" || tailEvent[1].type === "blockQuotePrefixWhitespace" || tailEvent[1].type === "blockQuoteMarker" || tailEvent[1].type === "listItemIndent") {
393
+ // Empty
394
+ } else {
395
+ break;
396
+ }
397
+ }
398
+ if (firstBlankLineIndex && (!lineIndex || firstBlankLineIndex < lineIndex)) {
399
+ listItem._spread = true;
400
+ }
401
+
402
+ // Fix position.
403
+ listItem.end = Object.assign({}, lineIndex ? events[lineIndex][1].start : event[1].end);
404
+ insertions.push({
405
+ at: lineIndex || index,
406
+ event: ['exit', listItem, event[2]]
407
+ });
408
+ }
409
+
410
+ // Create a new list item.
411
+ if (event[1].type === "listItemPrefix") {
412
+ /** @type {Token} */
413
+ const item = {
414
+ type: 'listItem',
415
+ _spread: false,
416
+ start: Object.assign({}, event[1].start),
417
+ // @ts-expect-error: we’ll add `end` in a second.
418
+ end: undefined
419
+ };
420
+ listItem = item;
421
+ insertions.push({
422
+ at: index,
423
+ event: ['enter', item, event[2]]
424
+ });
425
+ firstBlankLineIndex = undefined;
426
+ atMarker = true;
427
+ }
428
+ }
429
+ }
430
+
431
+ // The list is at the end of `events`: take it off, then push it back with
432
+ // the insertions merged in.
433
+ // Nothing follows the list, so nothing shifts.
434
+ const listEvents = events.splice(start);
435
+ let insertion = 0;
436
+ index = -1;
437
+ while (++index < listEvents.length) {
438
+ while (insertion < insertions.length && insertions[insertion].at === start + index) {
439
+ events.push(insertions[insertion++].event);
440
+ }
441
+ events.push(listEvents[index]);
442
+ }
443
+ events[start][1]._spread = listSpread;
444
+ }
445
+
446
+ /**
447
+ * Create an opener handle.
448
+ *
449
+ * @param {(token: Token) => Nodes} create
450
+ * Create a node.
451
+ * @param {Handle | undefined} [and]
452
+ * Optional function to also run.
453
+ * @returns {Handle}
454
+ * Handle.
455
+ */
456
+ function opener(create, and) {
457
+ return open;
458
+
459
+ /**
460
+ * @this {CompileContext}
461
+ * @param {Token} token
462
+ * @returns {undefined}
463
+ */
464
+ function open(token) {
465
+ enter.call(this, create(token), token);
466
+ if (and) and.call(this, token);
467
+ }
468
+ }
469
+
470
+ /**
471
+ * @type {CompileContext['buffer']}
472
+ */
473
+ function buffer() {
474
+ this.stack.push({
475
+ type: 'fragment',
476
+ children: []
477
+ });
478
+ }
479
+
480
+ /**
481
+ * @type {CompileContext['enter']}
482
+ */
483
+ function enter(node, token, errorHandler) {
484
+ const parent = this.stack[this.stack.length - 1];
485
+ /** @type {Array<Nodes>} */
486
+ const siblings = parent.children;
487
+ siblings.push(node);
488
+ this.stack.push(node);
489
+ this.tokenStack.push([token, errorHandler || undefined]);
490
+ node.position = {
491
+ start: point(token.start),
492
+ // @ts-expect-error: `end` will be patched later.
493
+ end: undefined
494
+ };
495
+ }
496
+
497
+ /**
498
+ * Create a closer handle.
499
+ *
500
+ * @param {Handle | undefined} [and]
501
+ * Optional function to also run.
502
+ * @returns {Handle}
503
+ * Handle.
504
+ */
505
+ function closer(and) {
506
+ return close;
507
+
508
+ /**
509
+ * @this {CompileContext}
510
+ * @param {Token} token
511
+ * @returns {undefined}
512
+ */
513
+ function close(token) {
514
+ if (and) and.call(this, token);
515
+ exit.call(this, token);
516
+ }
517
+ }
518
+
519
+ /**
520
+ * @type {CompileContext['exit']}
521
+ */
522
+ function exit(token, onExitError) {
523
+ const node = this.stack.pop();
524
+ const open = this.tokenStack.pop();
525
+ if (!open) {
526
+ throw new Error('Cannot close `' + token.type + '` (' + stringifyPosition({
527
+ start: token.start,
528
+ end: token.end
529
+ }) + '): it’s not open');
530
+ }
531
+ if (open[0].type !== token.type) {
532
+ if (onExitError) {
533
+ onExitError.call(this, token, open[0]);
534
+ } else {
535
+ const handler = open[1] || defaultOnError;
536
+ handler.call(this, token, open[0]);
537
+ }
538
+ }
539
+ node.position.end = point(token.end);
540
+ }
541
+
542
+ /**
543
+ * @type {CompileContext['resume']}
544
+ */
545
+ function resume() {
546
+ return toString(this.stack.pop());
547
+ }
548
+
549
+ //
550
+ // Handlers.
551
+ //
552
+
553
+ /**
554
+ * @this {CompileContext}
555
+ * @type {Handle}
556
+ */
557
+ function onenterlistordered() {
558
+ this.data.expectingFirstListItemValue = true;
559
+ }
560
+
561
+ /**
562
+ * @this {CompileContext}
563
+ * @type {Handle}
564
+ */
565
+ function onenterlistitemvalue(token) {
566
+ if (this.data.expectingFirstListItemValue) {
567
+ const ancestor = this.stack[this.stack.length - 2];
568
+ ancestor.start = Number.parseInt(this.sliceSerialize(token), 10);
569
+ this.data.expectingFirstListItemValue = undefined;
570
+ }
571
+ }
572
+
573
+ /**
574
+ * @this {CompileContext}
575
+ * @type {Handle}
576
+ */
577
+ function onexitcodefencedfenceinfo() {
578
+ const data = this.resume();
579
+ const node = this.stack[this.stack.length - 1];
580
+ node.lang = data;
581
+ }
582
+
583
+ /**
584
+ * @this {CompileContext}
585
+ * @type {Handle}
586
+ */
587
+ function onexitcodefencedfencemeta() {
588
+ const data = this.resume();
589
+ const node = this.stack[this.stack.length - 1];
590
+ node.meta = data;
591
+ }
592
+
593
+ /**
594
+ * @this {CompileContext}
595
+ * @type {Handle}
596
+ */
597
+ function onexitcodefencedfence() {
598
+ // Exit if this is the closing fence.
599
+ if (this.data.flowCodeInside) return;
600
+ this.buffer();
601
+ this.data.flowCodeInside = true;
602
+ }
603
+
604
+ /**
605
+ * @this {CompileContext}
606
+ * @type {Handle}
607
+ */
608
+ function onexitcodefenced() {
609
+ const data = this.resume();
610
+ const node = this.stack[this.stack.length - 1];
611
+ node.value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '');
612
+ this.data.flowCodeInside = undefined;
613
+ }
614
+
615
+ /**
616
+ * @this {CompileContext}
617
+ * @type {Handle}
618
+ */
619
+ function onexitcodeindented() {
620
+ const data = this.resume();
621
+ const node = this.stack[this.stack.length - 1];
622
+ node.value = data.replace(/(\r?\n|\r)$/g, '');
623
+ }
624
+
625
+ /**
626
+ * @this {CompileContext}
627
+ * @type {Handle}
628
+ */
629
+ function onexitdefinitionlabelstring(token) {
630
+ const label = this.resume();
631
+ const node = this.stack[this.stack.length - 1];
632
+ node.label = label;
633
+ node.identifier = normalizeIdentifier(this.sliceSerialize(token)).toLowerCase();
634
+ }
635
+
636
+ /**
637
+ * @this {CompileContext}
638
+ * @type {Handle}
639
+ */
640
+ function onexitdefinitiontitlestring() {
641
+ const data = this.resume();
642
+ const node = this.stack[this.stack.length - 1];
643
+ node.title = data;
644
+ }
645
+
646
+ /**
647
+ * @this {CompileContext}
648
+ * @type {Handle}
649
+ */
650
+ function onexitdefinitiondestinationstring() {
651
+ const data = this.resume();
652
+ const node = this.stack[this.stack.length - 1];
653
+ node.url = data;
654
+ }
655
+
656
+ /**
657
+ * @this {CompileContext}
658
+ * @type {Handle}
659
+ */
660
+ function onexitatxheadingsequence(token) {
661
+ const node = this.stack[this.stack.length - 1];
662
+ if (!node.depth) {
663
+ const depth = this.sliceSerialize(token).length;
664
+ node.depth = depth;
665
+ }
666
+ }
667
+
668
+ /**
669
+ * @this {CompileContext}
670
+ * @type {Handle}
671
+ */
672
+ function onexitsetextheadingtext() {
673
+ this.data.setextHeadingSlurpLineEnding = true;
674
+ }
675
+
676
+ /**
677
+ * @this {CompileContext}
678
+ * @type {Handle}
679
+ */
680
+ function onexitsetextheadinglinesequence(token) {
681
+ const node = this.stack[this.stack.length - 1];
682
+ node.depth = this.sliceSerialize(token).codePointAt(0) === 61 ? 1 : 2;
683
+ }
684
+
685
+ /**
686
+ * @this {CompileContext}
687
+ * @type {Handle}
688
+ */
689
+ function onexitsetextheading() {
690
+ this.data.setextHeadingSlurpLineEnding = undefined;
691
+ }
692
+
693
+ /**
694
+ * @this {CompileContext}
695
+ * @type {Handle}
696
+ */
697
+
698
+ function onenterdata(token) {
699
+ const node = this.stack[this.stack.length - 1];
700
+ /** @type {Array<Nodes>} */
701
+ const siblings = node.children;
702
+ let tail = siblings[siblings.length - 1];
703
+ if (!tail || tail.type !== 'text') {
704
+ // Add a new text node.
705
+ tail = text();
706
+ tail.position = {
707
+ start: point(token.start),
708
+ // @ts-expect-error: we’ll add `end` later.
709
+ end: undefined
710
+ };
711
+ siblings.push(tail);
712
+ }
713
+ this.stack.push(tail);
714
+ }
715
+
716
+ /**
717
+ * @this {CompileContext}
718
+ * @type {Handle}
719
+ */
720
+
721
+ function onexitdata(token) {
722
+ const tail = this.stack.pop();
723
+ tail.value += this.sliceSerialize(token);
724
+ tail.position.end = point(token.end);
725
+ }
726
+
727
+ /**
728
+ * @this {CompileContext}
729
+ * @type {Handle}
730
+ */
731
+
732
+ function onexitlineending(token) {
733
+ const context = this.stack[this.stack.length - 1];
734
+ // If we’re at a hard break, include the line ending in there.
735
+ if (this.data.atHardBreak) {
736
+ const tail = context.children[context.children.length - 1];
737
+ tail.position.end = point(token.end);
738
+ this.data.atHardBreak = undefined;
739
+ return;
740
+ }
741
+ if (!this.data.setextHeadingSlurpLineEnding && config.canContainEols.includes(context.type)) {
742
+ onenterdata.call(this, token);
743
+ onexitdata.call(this, token);
744
+ }
745
+ }
746
+
747
+ /**
748
+ * @this {CompileContext}
749
+ * @type {Handle}
750
+ */
751
+
752
+ function onexithardbreak() {
753
+ this.data.atHardBreak = true;
754
+ }
755
+
756
+ /**
757
+ * @this {CompileContext}
758
+ * @type {Handle}
759
+ */
760
+
761
+ function onexithtmlflow() {
762
+ const data = this.resume();
763
+ const node = this.stack[this.stack.length - 1];
764
+ node.value = data;
765
+ }
766
+
767
+ /**
768
+ * @this {CompileContext}
769
+ * @type {Handle}
770
+ */
771
+
772
+ function onexithtmltext() {
773
+ const data = this.resume();
774
+ const node = this.stack[this.stack.length - 1];
775
+ node.value = data;
776
+ }
777
+
778
+ /**
779
+ * @this {CompileContext}
780
+ * @type {Handle}
781
+ */
782
+
783
+ function onexitcodetext() {
784
+ const data = this.resume();
785
+ const node = this.stack[this.stack.length - 1];
786
+ node.value = data;
787
+ }
788
+
789
+ /**
790
+ * @this {CompileContext}
791
+ * @type {Handle}
792
+ */
793
+
794
+ function onexitlink() {
795
+ const node = this.stack[this.stack.length - 1];
796
+ // Note: there are also `identifier` and `label` fields on this link node!
797
+ // These are used / cleaned here.
798
+
799
+ // To do: clean.
800
+ if (this.data.inReference) {
801
+ /** @type {ReferenceType} */
802
+ const referenceType = this.data.referenceType || 'shortcut';
803
+ node.type += 'Reference';
804
+ // @ts-expect-error: mutate.
805
+ node.referenceType = referenceType;
806
+ // @ts-expect-error: mutate.
807
+ delete node.url;
808
+ delete node.title;
809
+ } else {
810
+ // @ts-expect-error: mutate.
811
+ delete node.identifier;
812
+ // @ts-expect-error: mutate.
813
+ delete node.label;
814
+ }
815
+ this.data.referenceType = undefined;
816
+ }
817
+
818
+ /**
819
+ * @this {CompileContext}
820
+ * @type {Handle}
821
+ */
822
+
823
+ function onexitimage() {
824
+ const node = this.stack[this.stack.length - 1];
825
+ // Note: there are also `identifier` and `label` fields on this link node!
826
+ // These are used / cleaned here.
827
+
828
+ // To do: clean.
829
+ if (this.data.inReference) {
830
+ /** @type {ReferenceType} */
831
+ const referenceType = this.data.referenceType || 'shortcut';
832
+ node.type += 'Reference';
833
+ // @ts-expect-error: mutate.
834
+ node.referenceType = referenceType;
835
+ // @ts-expect-error: mutate.
836
+ delete node.url;
837
+ delete node.title;
838
+ } else {
839
+ // @ts-expect-error: mutate.
840
+ delete node.identifier;
841
+ // @ts-expect-error: mutate.
842
+ delete node.label;
843
+ }
844
+ this.data.referenceType = undefined;
845
+ }
846
+
847
+ /**
848
+ * @this {CompileContext}
849
+ * @type {Handle}
850
+ */
851
+
852
+ function onexitlabeltext(token) {
853
+ const string = this.sliceSerialize(token);
854
+ const ancestor = this.stack[this.stack.length - 2];
855
+ // @ts-expect-error: stash this on the node, as it might become a reference
856
+ // later.
857
+ ancestor.label = decodeString(string);
858
+ // @ts-expect-error: same as above.
859
+ ancestor.identifier = normalizeIdentifier(string).toLowerCase();
860
+ }
861
+
862
+ /**
863
+ * @this {CompileContext}
864
+ * @type {Handle}
865
+ */
866
+
867
+ function onexitlabel() {
868
+ const fragment = this.stack[this.stack.length - 1];
869
+ const value = this.resume();
870
+ const node = this.stack[this.stack.length - 1];
871
+ // Assume a reference.
872
+ this.data.inReference = true;
873
+ if (node.type === 'link') {
874
+ /** @type {Array<PhrasingContent>} */
875
+ const children = fragment.children;
876
+ node.children = children;
877
+ } else {
878
+ node.alt = value;
879
+ }
880
+ }
881
+
882
+ /**
883
+ * @this {CompileContext}
884
+ * @type {Handle}
885
+ */
886
+
887
+ function onexitresourcedestinationstring() {
888
+ const data = this.resume();
889
+ const node = this.stack[this.stack.length - 1];
890
+ node.url = data;
891
+ }
892
+
893
+ /**
894
+ * @this {CompileContext}
895
+ * @type {Handle}
896
+ */
897
+
898
+ function onexitresourcetitlestring() {
899
+ const data = this.resume();
900
+ const node = this.stack[this.stack.length - 1];
901
+ node.title = data;
902
+ }
903
+
904
+ /**
905
+ * @this {CompileContext}
906
+ * @type {Handle}
907
+ */
908
+
909
+ function onexitresource() {
910
+ this.data.inReference = undefined;
911
+ }
912
+
913
+ /**
914
+ * @this {CompileContext}
915
+ * @type {Handle}
916
+ */
917
+
918
+ function onenterreference() {
919
+ this.data.referenceType = 'collapsed';
920
+ }
921
+
922
+ /**
923
+ * @this {CompileContext}
924
+ * @type {Handle}
925
+ */
926
+
927
+ function onexitreferencestring(token) {
928
+ const label = this.resume();
929
+ const node = this.stack[this.stack.length - 1];
930
+ // @ts-expect-error: stash this on the node, as it might become a reference
931
+ // later.
932
+ node.label = label;
933
+ // @ts-expect-error: same as above.
934
+ node.identifier = normalizeIdentifier(this.sliceSerialize(token)).toLowerCase();
935
+ this.data.referenceType = 'full';
936
+ }
937
+
938
+ /**
939
+ * @this {CompileContext}
940
+ * @type {Handle}
941
+ */
942
+
943
+ function onexitcharacterreferencemarker(token) {
944
+ this.data.characterReferenceType = token.type;
945
+ }
946
+
947
+ /**
948
+ * @this {CompileContext}
949
+ * @type {Handle}
950
+ */
951
+ function onexitcharacterreferencevalue(token) {
952
+ const data = this.sliceSerialize(token);
953
+ const type = this.data.characterReferenceType;
954
+ /** @type {string} */
955
+ let value;
956
+ if (type) {
957
+ value = decodeNumericCharacterReference(data, type === "characterReferenceMarkerNumeric" ? 10 : 16);
958
+ this.data.characterReferenceType = undefined;
959
+ } else {
960
+ const result = decodeNamedCharacterReference(data);
961
+ value = result;
962
+ }
963
+ const tail = this.stack[this.stack.length - 1];
964
+ tail.value += value;
965
+ }
966
+
967
+ /**
968
+ * @this {CompileContext}
969
+ * @type {Handle}
970
+ */
971
+ function onexitcharacterreference(token) {
972
+ const tail = this.stack.pop();
973
+ tail.position.end = point(token.end);
974
+ }
975
+
976
+ /**
977
+ * @this {CompileContext}
978
+ * @type {Handle}
979
+ */
980
+ function onexitautolinkprotocol(token) {
981
+ onexitdata.call(this, token);
982
+ const node = this.stack[this.stack.length - 1];
983
+ node.url = this.sliceSerialize(token);
984
+ }
985
+
986
+ /**
987
+ * @this {CompileContext}
988
+ * @type {Handle}
989
+ */
990
+ function onexitautolinkemail(token) {
991
+ onexitdata.call(this, token);
992
+ const node = this.stack[this.stack.length - 1];
993
+ node.url = 'mailto:' + this.sliceSerialize(token);
994
+ }
995
+
996
+ //
997
+ // Creaters.
998
+ //
999
+
1000
+ // Each node factory pre-declares `position` as undefined as the trailing
1001
+ // property. enter() patches it to a real value, but the property already
1002
+ // exists, so V8 sees a single hidden class for each node type from
1003
+ // construction through the rest of the pipeline. Without the pre-declare,
1004
+ // each node took a hidden-class transition when enter() added position.
1005
+
1006
+ /** @returns {Blockquote} */
1007
+ function blockQuote() {
1008
+ return {
1009
+ type: 'blockquote',
1010
+ children: [],
1011
+ position: undefined
1012
+ };
1013
+ }
1014
+
1015
+ /** @returns {Code} */
1016
+ function codeFlow() {
1017
+ return {
1018
+ type: 'code',
1019
+ lang: null,
1020
+ meta: null,
1021
+ value: '',
1022
+ position: undefined
1023
+ };
1024
+ }
1025
+
1026
+ /** @returns {InlineCode} */
1027
+ function codeText() {
1028
+ return {
1029
+ type: 'inlineCode',
1030
+ value: '',
1031
+ position: undefined
1032
+ };
1033
+ }
1034
+
1035
+ /** @returns {Definition} */
1036
+ function definition() {
1037
+ return {
1038
+ type: 'definition',
1039
+ identifier: '',
1040
+ label: null,
1041
+ title: null,
1042
+ url: '',
1043
+ position: undefined
1044
+ };
1045
+ }
1046
+
1047
+ /** @returns {Emphasis} */
1048
+ function emphasis() {
1049
+ return {
1050
+ type: 'emphasis',
1051
+ children: [],
1052
+ position: undefined
1053
+ };
1054
+ }
1055
+
1056
+ /** @returns {Heading} */
1057
+ function heading() {
1058
+ return {
1059
+ type: 'heading',
1060
+ // @ts-expect-error `depth` will be set later.
1061
+ depth: 0,
1062
+ children: [],
1063
+ position: undefined
1064
+ };
1065
+ }
1066
+
1067
+ /** @returns {Break} */
1068
+ function hardBreak() {
1069
+ return {
1070
+ type: 'break',
1071
+ position: undefined
1072
+ };
1073
+ }
1074
+
1075
+ /** @returns {Html} */
1076
+ function html() {
1077
+ return {
1078
+ type: 'html',
1079
+ value: '',
1080
+ position: undefined
1081
+ };
1082
+ }
1083
+
1084
+ /** @returns {Image} */
1085
+ function image() {
1086
+ return {
1087
+ type: 'image',
1088
+ title: null,
1089
+ url: '',
1090
+ alt: null,
1091
+ position: undefined
1092
+ };
1093
+ }
1094
+
1095
+ /** @returns {Link} */
1096
+ function link() {
1097
+ return {
1098
+ type: 'link',
1099
+ title: null,
1100
+ url: '',
1101
+ children: [],
1102
+ position: undefined
1103
+ };
1104
+ }
1105
+
1106
+ /**
1107
+ * @param {Token} token
1108
+ * @returns {List}
1109
+ */
1110
+ function list(token) {
1111
+ return {
1112
+ type: 'list',
1113
+ ordered: token.type === 'listOrdered',
1114
+ start: null,
1115
+ spread: token._spread,
1116
+ children: [],
1117
+ position: undefined
1118
+ };
1119
+ }
1120
+
1121
+ /**
1122
+ * @param {Token} token
1123
+ * @returns {ListItem}
1124
+ */
1125
+ function listItem(token) {
1126
+ return {
1127
+ type: 'listItem',
1128
+ spread: token._spread,
1129
+ checked: null,
1130
+ children: [],
1131
+ position: undefined
1132
+ };
1133
+ }
1134
+
1135
+ /** @returns {Paragraph} */
1136
+ function paragraph() {
1137
+ return {
1138
+ type: 'paragraph',
1139
+ children: [],
1140
+ position: undefined
1141
+ };
1142
+ }
1143
+
1144
+ /** @returns {Strong} */
1145
+ function strong() {
1146
+ return {
1147
+ type: 'strong',
1148
+ children: [],
1149
+ position: undefined
1150
+ };
1151
+ }
1152
+
1153
+ /** @returns {Text} */
1154
+ function text() {
1155
+ return {
1156
+ type: 'text',
1157
+ value: '',
1158
+ position: undefined
1159
+ };
1160
+ }
1161
+
1162
+ /** @returns {ThematicBreak} */
1163
+ function thematicBreak() {
1164
+ return {
1165
+ type: 'thematicBreak',
1166
+ position: undefined
1167
+ };
1168
+ }
1169
+ }
1170
+
1171
+ /**
1172
+ * Copy a point-like value.
1173
+ *
1174
+ * @param {Point} d
1175
+ * Point-like value.
1176
+ * @returns {Point}
1177
+ * unist point.
1178
+ */
1179
+ function point(d) {
1180
+ return {
1181
+ line: d.line,
1182
+ column: d.column,
1183
+ offset: d.offset
1184
+ };
1185
+ }
1186
+
1187
+ /**
1188
+ * @param {Config} combined
1189
+ * @param {Array<Array<Extension> | Extension>} extensions
1190
+ * @returns {undefined}
1191
+ */
1192
+ function configure(combined, extensions) {
1193
+ let index = -1;
1194
+ while (++index < extensions.length) {
1195
+ const value = extensions[index];
1196
+ if (Array.isArray(value)) {
1197
+ configure(combined, value);
1198
+ } else {
1199
+ extension(combined, value);
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ /**
1205
+ * @param {Config} combined
1206
+ * @param {Extension} extension
1207
+ * @returns {undefined}
1208
+ */
1209
+ function extension(combined, extension) {
1210
+ /** @type {keyof Extension} */
1211
+ let key;
1212
+ for (key in extension) {
1213
+ if (own.call(extension, key)) {
1214
+ switch (key) {
1215
+ case 'canContainEols':
1216
+ {
1217
+ const right = extension[key];
1218
+ if (right) {
1219
+ combined[key].push(...right);
1220
+ }
1221
+ break;
1222
+ }
1223
+ case 'transforms':
1224
+ {
1225
+ const right = extension[key];
1226
+ if (right) {
1227
+ combined[key].push(...right);
1228
+ }
1229
+ break;
1230
+ }
1231
+ case 'afterExit':
1232
+ case 'beforeEnter':
1233
+ {
1234
+ const right = extension[key];
1235
+ if (right) {
1236
+ combined[key].push(right);
1237
+ }
1238
+ break;
1239
+ }
1240
+ case 'enter':
1241
+ case 'exit':
1242
+ {
1243
+ const right = extension[key];
1244
+ if (right) {
1245
+ Object.assign(combined[key], right);
1246
+ }
1247
+ break;
1248
+ }
1249
+ // No default
1250
+ }
1251
+ }
1252
+ }
1253
+ }
1254
+
1255
+ /** @type {OnEnterError} */
1256
+ function defaultOnError(left, right) {
1257
+ if (left) {
1258
+ throw new Error('Cannot close `' + left.type + '` (' + stringifyPosition({
1259
+ start: left.start,
1260
+ end: left.end
1261
+ }) + '): a different token (`' + right.type + '`, ' + stringifyPosition({
1262
+ start: right.start,
1263
+ end: right.end
1264
+ }) + ') is open');
1265
+ }
1266
+ throw new Error('Cannot close document, a token (`' + right.type + '`, ' + stringifyPosition({
1267
+ start: right.start,
1268
+ end: right.end
1269
+ }) + ') is still open');
1270
+ }