@tinta-lang/syntax 0.0.0-stage → 0.1.16

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/README.md +16 -2
  3. package/THIRD_PARTY_NOTICES.txt +13651 -0
  4. package/dist/directive.d.ts +40 -0
  5. package/dist/editing.d.ts +46 -0
  6. package/dist/enrichment.d.ts +5 -0
  7. package/dist/expression.d.ts +26 -0
  8. package/dist/image-destinations.d.ts +3 -0
  9. package/dist/index.d.ts +8 -0
  10. package/dist/index.js +10118 -0
  11. package/dist/parser.d.ts +40 -0
  12. package/dist/registry.d.ts +144 -0
  13. package/dist/source.d.ts +28 -0
  14. package/package.json +289 -3
  15. package/parser-provenance.json +1419 -0
  16. package/patches/README.md +5 -0
  17. package/patches/micromark.patch +84 -0
  18. package/vendor/@noble/hashes/LICENSE +21 -0
  19. package/vendor/@noble/hashes/_md.js +209 -0
  20. package/vendor/@noble/hashes/_u64.js +77 -0
  21. package/vendor/@noble/hashes/package.json +91 -0
  22. package/vendor/@noble/hashes/sha2.js +446 -0
  23. package/vendor/@noble/hashes/utils.js +687 -0
  24. package/vendor/character-entities/index.js +2132 -0
  25. package/vendor/character-entities/license +22 -0
  26. package/vendor/character-entities/package.json +78 -0
  27. package/vendor/decode-named-character-reference/index.js +19 -0
  28. package/vendor/decode-named-character-reference/license +22 -0
  29. package/vendor/decode-named-character-reference/package.json +90 -0
  30. package/vendor/devlop/lib/default.js +9 -0
  31. package/vendor/devlop/license +22 -0
  32. package/vendor/devlop/package.json +80 -0
  33. package/vendor/longest-streak/index.js +36 -0
  34. package/vendor/longest-streak/license +22 -0
  35. package/vendor/longest-streak/package.json +73 -0
  36. package/vendor/markdown-table/index.js +393 -0
  37. package/vendor/markdown-table/license +22 -0
  38. package/vendor/markdown-table/package.json +77 -0
  39. package/vendor/mdast-util-from-markdown/index.js +2 -0
  40. package/vendor/mdast-util-from-markdown/lib/index.js +1270 -0
  41. package/vendor/mdast-util-from-markdown/license +22 -0
  42. package/vendor/mdast-util-from-markdown/package.json +98 -0
  43. package/vendor/mdast-util-gfm-strikethrough/index.js +5 -0
  44. package/vendor/mdast-util-gfm-strikethrough/lib/index.js +133 -0
  45. package/vendor/mdast-util-gfm-strikethrough/license +22 -0
  46. package/vendor/mdast-util-gfm-strikethrough/package.json +84 -0
  47. package/vendor/mdast-util-gfm-table/index.js +2 -0
  48. package/vendor/mdast-util-gfm-table/lib/index.js +300 -0
  49. package/vendor/mdast-util-gfm-table/license +22 -0
  50. package/vendor/mdast-util-gfm-table/package.json +101 -0
  51. package/vendor/mdast-util-gfm-task-list-item/index.js +4 -0
  52. package/vendor/mdast-util-gfm-task-list-item/lib/index.js +141 -0
  53. package/vendor/mdast-util-gfm-task-list-item/license +22 -0
  54. package/vendor/mdast-util-gfm-task-list-item/package.json +90 -0
  55. package/vendor/mdast-util-phrasing/index.js +1 -0
  56. package/vendor/mdast-util-phrasing/lib/index.js +45 -0
  57. package/vendor/mdast-util-phrasing/license +23 -0
  58. package/vendor/mdast-util-phrasing/package.json +78 -0
  59. package/vendor/mdast-util-to-markdown/index.js +3 -0
  60. package/vendor/mdast-util-to-markdown/lib/configure.js +98 -0
  61. package/vendor/mdast-util-to-markdown/lib/handle/blockquote.js +40 -0
  62. package/vendor/mdast-util-to-markdown/lib/handle/break.js +41 -0
  63. package/vendor/mdast-util-to-markdown/lib/handle/code.js +86 -0
  64. package/vendor/mdast-util-to-markdown/lib/handle/definition.js +83 -0
  65. package/vendor/mdast-util-to-markdown/lib/handle/emphasis.js +67 -0
  66. package/vendor/mdast-util-to-markdown/lib/handle/heading.js +84 -0
  67. package/vendor/mdast-util-to-markdown/lib/handle/html.js +27 -0
  68. package/vendor/mdast-util-to-markdown/lib/handle/image-reference.js +78 -0
  69. package/vendor/mdast-util-to-markdown/lib/handle/image.js +92 -0
  70. package/vendor/mdast-util-to-markdown/lib/handle/index.js +45 -0
  71. package/vendor/mdast-util-to-markdown/lib/handle/inline-code.js +86 -0
  72. package/vendor/mdast-util-to-markdown/lib/handle/link-reference.js +78 -0
  73. package/vendor/mdast-util-to-markdown/lib/handle/link.js +129 -0
  74. package/vendor/mdast-util-to-markdown/lib/handle/list-item.js +73 -0
  75. package/vendor/mdast-util-to-markdown/lib/handle/list.js +107 -0
  76. package/vendor/mdast-util-to-markdown/lib/handle/paragraph.js +27 -0
  77. package/vendor/mdast-util-to-markdown/lib/handle/root.js +34 -0
  78. package/vendor/mdast-util-to-markdown/lib/handle/strong.js +67 -0
  79. package/vendor/mdast-util-to-markdown/lib/handle/text.js +30 -0
  80. package/vendor/mdast-util-to-markdown/lib/handle/thematic-break.js +27 -0
  81. package/vendor/mdast-util-to-markdown/lib/index.js +213 -0
  82. package/vendor/mdast-util-to-markdown/lib/join.js +56 -0
  83. package/vendor/mdast-util-to-markdown/lib/unsafe.js +160 -0
  84. package/vendor/mdast-util-to-markdown/lib/util/association.js +33 -0
  85. package/vendor/mdast-util-to-markdown/lib/util/character.js +32 -0
  86. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-ordered.js +25 -0
  87. package/vendor/mdast-util-to-markdown/lib/util/check-bullet-other.js +42 -0
  88. package/vendor/mdast-util-to-markdown/lib/util/check-bullet.js +25 -0
  89. package/vendor/mdast-util-to-markdown/lib/util/check-emphasis.js +25 -0
  90. package/vendor/mdast-util-to-markdown/lib/util/check-fence.js +25 -0
  91. package/vendor/mdast-util-to-markdown/lib/util/check-list-item-indent.js +25 -0
  92. package/vendor/mdast-util-to-markdown/lib/util/check-quote.js +25 -0
  93. package/vendor/mdast-util-to-markdown/lib/util/check-rule-repetition.js +25 -0
  94. package/vendor/mdast-util-to-markdown/lib/util/check-rule.js +25 -0
  95. package/vendor/mdast-util-to-markdown/lib/util/check-strong.js +25 -0
  96. package/vendor/mdast-util-to-markdown/lib/util/compile-pattern.js +30 -0
  97. package/vendor/mdast-util-to-markdown/lib/util/container-flow.js +93 -0
  98. package/vendor/mdast-util-to-markdown/lib/util/container-phrasing.js +704 -0
  99. package/vendor/mdast-util-to-markdown/lib/util/encode-character-reference.js +11 -0
  100. package/vendor/mdast-util-to-markdown/lib/util/encode-info.js +82 -0
  101. package/vendor/mdast-util-to-markdown/lib/util/format-code-as-indented.js +27 -0
  102. package/vendor/mdast-util-to-markdown/lib/util/format-heading-as-setext.js +39 -0
  103. package/vendor/mdast-util-to-markdown/lib/util/format-link-as-autolink.js +39 -0
  104. package/vendor/mdast-util-to-markdown/lib/util/html-kind.js +63 -0
  105. package/vendor/mdast-util-to-markdown/lib/util/indent-lines.js +40 -0
  106. package/vendor/mdast-util-to-markdown/lib/util/pattern-in-scope.js +52 -0
  107. package/vendor/mdast-util-to-markdown/lib/util/safe.js +263 -0
  108. package/vendor/mdast-util-to-markdown/lib/util/track.js +55 -0
  109. package/vendor/mdast-util-to-markdown/license +22 -0
  110. package/vendor/mdast-util-to-markdown/package.json +88 -0
  111. package/vendor/mdast-util-to-string/index.js +5 -0
  112. package/vendor/mdast-util-to-string/lib/index.js +108 -0
  113. package/vendor/mdast-util-to-string/license +22 -0
  114. package/vendor/mdast-util-to-string/package.json +78 -0
  115. package/vendor/micromark/index.js +60 -0
  116. package/vendor/micromark/lib/compile.js +1068 -0
  117. package/vendor/micromark/lib/constructs.js +85 -0
  118. package/vendor/micromark/lib/create-tokenizer.js +615 -0
  119. package/vendor/micromark/lib/initialize/content.js +79 -0
  120. package/vendor/micromark/lib/initialize/document.js +370 -0
  121. package/vendor/micromark/lib/initialize/flow.js +58 -0
  122. package/vendor/micromark/lib/initialize/text.js +216 -0
  123. package/vendor/micromark/lib/parse.js +56 -0
  124. package/vendor/micromark/lib/postprocess.js +18 -0
  125. package/vendor/micromark/lib/preprocess.js +112 -0
  126. package/vendor/micromark/license +22 -0
  127. package/vendor/micromark/package.json +87 -0
  128. package/vendor/micromark-core-commonmark/index.js +22 -0
  129. package/vendor/micromark-core-commonmark/lib/attention.js +231 -0
  130. package/vendor/micromark-core-commonmark/lib/autolink.js +233 -0
  131. package/vendor/micromark-core-commonmark/lib/blank-line.js +61 -0
  132. package/vendor/micromark-core-commonmark/lib/block-quote.js +143 -0
  133. package/vendor/micromark-core-commonmark/lib/character-escape.js +64 -0
  134. package/vendor/micromark-core-commonmark/lib/character-reference.js +149 -0
  135. package/vendor/micromark-core-commonmark/lib/code-fenced.js +422 -0
  136. package/vendor/micromark-core-commonmark/lib/code-indented.js +159 -0
  137. package/vendor/micromark-core-commonmark/lib/code-text.js +225 -0
  138. package/vendor/micromark-core-commonmark/lib/content.js +163 -0
  139. package/vendor/micromark-core-commonmark/lib/definition.js +254 -0
  140. package/vendor/micromark-core-commonmark/lib/hard-break-escape.js +60 -0
  141. package/vendor/micromark-core-commonmark/lib/heading-atx.js +191 -0
  142. package/vendor/micromark-core-commonmark/lib/html-flow.js +827 -0
  143. package/vendor/micromark-core-commonmark/lib/html-text.js +678 -0
  144. package/vendor/micromark-core-commonmark/lib/label-end.js +561 -0
  145. package/vendor/micromark-core-commonmark/lib/label-start-image.js +110 -0
  146. package/vendor/micromark-core-commonmark/lib/label-start-link.js +63 -0
  147. package/vendor/micromark-core-commonmark/lib/line-ending.js +33 -0
  148. package/vendor/micromark-core-commonmark/lib/list.js +213 -0
  149. package/vendor/micromark-core-commonmark/lib/partial-non-lazy-continuation.js +61 -0
  150. package/vendor/micromark-core-commonmark/lib/setext-underline.js +187 -0
  151. package/vendor/micromark-core-commonmark/lib/thematic-break.js +102 -0
  152. package/vendor/micromark-core-commonmark/license +22 -0
  153. package/vendor/micromark-core-commonmark/package.json +62 -0
  154. package/vendor/micromark-extension-gfm-strikethrough/index.js +3 -0
  155. package/vendor/micromark-extension-gfm-strikethrough/lib/html.js +26 -0
  156. package/vendor/micromark-extension-gfm-strikethrough/lib/syntax.js +142 -0
  157. package/vendor/micromark-extension-gfm-strikethrough/license +22 -0
  158. package/vendor/micromark-extension-gfm-strikethrough/package.json +129 -0
  159. package/vendor/micromark-extension-gfm-table/index.js +2 -0
  160. package/vendor/micromark-extension-gfm-table/lib/edit-map.js +222 -0
  161. package/vendor/micromark-extension-gfm-table/lib/html.js +135 -0
  162. package/vendor/micromark-extension-gfm-table/lib/infer.js +52 -0
  163. package/vendor/micromark-extension-gfm-table/lib/syntax.js +846 -0
  164. package/vendor/micromark-extension-gfm-table/license +22 -0
  165. package/vendor/micromark-extension-gfm-table/package.json +86 -0
  166. package/vendor/micromark-extension-gfm-task-list-item/index.js +2 -0
  167. package/vendor/micromark-extension-gfm-task-list-item/lib/html.js +29 -0
  168. package/vendor/micromark-extension-gfm-task-list-item/lib/syntax.js +158 -0
  169. package/vendor/micromark-extension-gfm-task-list-item/license +22 -0
  170. package/vendor/micromark-extension-gfm-task-list-item/package.json +131 -0
  171. package/vendor/micromark-factory-destination/index.js +206 -0
  172. package/vendor/micromark-factory-destination/license +22 -0
  173. package/vendor/micromark-factory-destination/package.json +57 -0
  174. package/vendor/micromark-factory-label/index.js +148 -0
  175. package/vendor/micromark-factory-label/license +22 -0
  176. package/vendor/micromark-factory-label/package.json +60 -0
  177. package/vendor/micromark-factory-space/index.js +116 -0
  178. package/vendor/micromark-factory-space/license +22 -0
  179. package/vendor/micromark-factory-space/package.json +46 -0
  180. package/vendor/micromark-factory-title/index.js +158 -0
  181. package/vendor/micromark-factory-title/license +22 -0
  182. package/vendor/micromark-factory-title/package.json +58 -0
  183. package/vendor/micromark-factory-whitespace/index.js +44 -0
  184. package/vendor/micromark-factory-whitespace/license +22 -0
  185. package/vendor/micromark-factory-whitespace/package.json +57 -0
  186. package/vendor/micromark-util-character/index.js +246 -0
  187. package/vendor/micromark-util-character/license +22 -0
  188. package/vendor/micromark-util-character/package.json +57 -0
  189. package/vendor/micromark-util-chunked/index.js +81 -0
  190. package/vendor/micromark-util-chunked/license +22 -0
  191. package/vendor/micromark-util-chunked/package.json +57 -0
  192. package/vendor/micromark-util-classify-character/index.js +27 -0
  193. package/vendor/micromark-util-classify-character/license +22 -0
  194. package/vendor/micromark-util-classify-character/package.json +59 -0
  195. package/vendor/micromark-util-combine-extensions/index.js +143 -0
  196. package/vendor/micromark-util-combine-extensions/license +22 -0
  197. package/vendor/micromark-util-combine-extensions/package.json +52 -0
  198. package/vendor/micromark-util-decode-numeric-character-reference/index.js +32 -0
  199. package/vendor/micromark-util-decode-numeric-character-reference/license +22 -0
  200. package/vendor/micromark-util-decode-numeric-character-reference/package.json +59 -0
  201. package/vendor/micromark-util-decode-string/index.js +45 -0
  202. package/vendor/micromark-util-decode-string/license +22 -0
  203. package/vendor/micromark-util-decode-string/package.json +63 -0
  204. package/vendor/micromark-util-edit-map/index.js +225 -0
  205. package/vendor/micromark-util-edit-map/license +22 -0
  206. package/vendor/micromark-util-edit-map/package.json +48 -0
  207. package/vendor/micromark-util-encode/index.js +33 -0
  208. package/vendor/micromark-util-encode/license +22 -0
  209. package/vendor/micromark-util-encode/package.json +47 -0
  210. package/vendor/micromark-util-html-tag-name/index.js +93 -0
  211. package/vendor/micromark-util-html-tag-name/license +22 -0
  212. package/vendor/micromark-util-html-tag-name/package.json +47 -0
  213. package/vendor/micromark-util-normalize-identifier/index.js +33 -0
  214. package/vendor/micromark-util-normalize-identifier/license +22 -0
  215. package/vendor/micromark-util-normalize-identifier/package.json +58 -0
  216. package/vendor/micromark-util-resolve-all/index.js +32 -0
  217. package/vendor/micromark-util-resolve-all/license +22 -0
  218. package/vendor/micromark-util-resolve-all/package.json +48 -0
  219. package/vendor/micromark-util-sanitize-uri/index.js +107 -0
  220. package/vendor/micromark-util-sanitize-uri/license +22 -0
  221. package/vendor/micromark-util-sanitize-uri/package.json +59 -0
  222. package/vendor/micromark-util-subtokenize/index.js +228 -0
  223. package/vendor/micromark-util-subtokenize/lib/splice-buffer.js +251 -0
  224. package/vendor/micromark-util-subtokenize/license +22 -0
  225. package/vendor/micromark-util-subtokenize/package.json +60 -0
  226. package/vendor/unist-util-is/index.js +7 -0
  227. package/vendor/unist-util-is/lib/index.js +296 -0
  228. package/vendor/unist-util-is/license +22 -0
  229. package/vendor/unist-util-is/package.json +103 -0
  230. package/vendor/unist-util-stringify-position/index.js +1 -0
  231. package/vendor/unist-util-stringify-position/lib/index.js +84 -0
  232. package/vendor/unist-util-stringify-position/license +22 -0
  233. package/vendor/unist-util-stringify-position/package.json +80 -0
  234. package/vendor/unist-util-visit/index.js +2 -0
  235. package/vendor/unist-util-visit/lib/index.js +312 -0
  236. package/vendor/unist-util-visit/license +22 -0
  237. package/vendor/unist-util-visit/package.json +108 -0
  238. package/vendor/unist-util-visit-parents/index.js +2 -0
  239. package/vendor/unist-util-visit-parents/lib/color.js +7 -0
  240. package/vendor/unist-util-visit-parents/lib/index.js +399 -0
  241. package/vendor/unist-util-visit-parents/license +22 -0
  242. package/vendor/unist-util-visit-parents/package.json +106 -0
  243. package/vendor/zwitch/index.js +118 -0
  244. package/vendor/zwitch/license +22 -0
  245. package/vendor/zwitch/package.json +72 -0
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @import {EncodeSides} from '../types.js'
3
+ */
4
+
5
+ import {classifyCharacter} from 'micromark-util-classify-character'
6
+
7
+ /**
8
+ * Check whether to encode (as a character reference) the characters
9
+ * surrounding an attention run.
10
+ *
11
+ * Which characters are around an attention run influence whether it works or
12
+ * not.
13
+ *
14
+ * See <https://github.com/orgs/syntax-tree/discussions/60> for more info.
15
+ * See this markdown in a particular renderer to see what works:
16
+ *
17
+ * ```markdown
18
+ * | | A (letter inside) | B (punctuation inside) | C (whitespace inside) | D (nothing inside) |
19
+ * | ----------------------- | ----------------- | ---------------------- | --------------------- | ------------------ |
20
+ * | 1 (letter outside) | x*y*z | x*.*z | x* *z | x**z |
21
+ * | 2 (punctuation outside) | .*y*. | .*.*. | .* *. | .**. |
22
+ * | 3 (whitespace outside) | x *y* z | x *.* z | x * * z | x ** z |
23
+ * | 4 (nothing outside) | *x* | *.* | * * | ** |
24
+ * ```
25
+ *
26
+ * @param {number} outside
27
+ * Code point on the outer side of the run.
28
+ * @param {number} inside
29
+ * Code point on the inner side of the run.
30
+ * @param {string} marker
31
+ * Marker of the run.
32
+ * Underscores are handled more strictly (they form less often) than
33
+ * asterisks.
34
+ * @returns {EncodeSides}
35
+ * Whether to encode characters.
36
+ */
37
+ // Important: punctuation must never be encoded.
38
+ // Punctuation is solely used by markdown constructs.
39
+ // And by encoding itself.
40
+ // Encoding them will break constructs or double encode things.
41
+ export function encodeInfo(outside, inside, marker) {
42
+ const outsideKind = classifyCharacter(outside)
43
+ const insideKind = classifyCharacter(inside)
44
+
45
+ // Letter outside:
46
+ if (outsideKind === undefined) {
47
+ return insideKind === undefined
48
+ ? // Letter inside:
49
+ // we have to encode *both* letters for `_` as it is looser.
50
+ // it already forms for `*` (and GFMs `~`).
51
+ marker === '_'
52
+ ? {inside: true, outside: true}
53
+ : {inside: false, outside: false}
54
+ : insideKind === 1
55
+ ? // Whitespace inside: encode both (letter, whitespace).
56
+ {inside: true, outside: true}
57
+ : // Punctuation inside: encode outer (letter)
58
+ {inside: false, outside: true}
59
+ }
60
+
61
+ // Whitespace outside:
62
+ if (outsideKind === 1) {
63
+ return insideKind === undefined
64
+ ? // Letter inside: already forms.
65
+ {inside: false, outside: false}
66
+ : insideKind === 1
67
+ ? // Whitespace inside: encode both (whitespace).
68
+ {inside: true, outside: true}
69
+ : // Punctuation inside: already forms.
70
+ {inside: false, outside: false}
71
+ }
72
+
73
+ // Punctuation outside:
74
+ return insideKind === undefined
75
+ ? // Letter inside: already forms.
76
+ {inside: false, outside: false}
77
+ : insideKind === 1
78
+ ? // Whitespace inside: encode inner (whitespace).
79
+ {inside: true, outside: false}
80
+ : // Punctuation inside: already forms.
81
+ {inside: false, outside: false}
82
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @import {State} from 'mdast-util-to-markdown'
3
+ * @import {Code} from 'mdast'
4
+ */
5
+
6
+ /**
7
+ * Checks if a code block can be formatted as an indented code block.
8
+ *
9
+ * @param {Code} node
10
+ * Node to check.
11
+ * @param {State} state
12
+ * Info passed around.
13
+ * @returns {boolean}
14
+ * Whether the code can be formatted as an indented code block.
15
+ */
16
+ export function formatCodeAsIndented(node, state) {
17
+ return Boolean(
18
+ state.options.fences === false &&
19
+ node.value &&
20
+ // If there’s no info…
21
+ !node.lang &&
22
+ // And there’s a non-whitespace character…
23
+ /[^\n\r ]/.test(node.value) &&
24
+ // And the value doesn’t start or end in a blank…
25
+ !/^[\t ]*(?:[\n\r]|$)|(?:^|[\n\r])[\t ]*$/.test(node.value)
26
+ )
27
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @import {State} from 'mdast-util-to-markdown'
3
+ * @import {Heading} from 'mdast'
4
+ */
5
+
6
+ import {EXIT, visit} from 'unist-util-visit'
7
+ import {toString} from 'mdast-util-to-string'
8
+
9
+ /**
10
+ * Checks if a heading can be formatted as a setext heading.
11
+ *
12
+ * @param {Heading} node
13
+ * Node to check.
14
+ * @param {State} state
15
+ * Info passed around.
16
+ * @returns {boolean}
17
+ * Whether the heading can be formatted as a setext heading.
18
+ */
19
+ export function formatHeadingAsSetext(node, state) {
20
+ let literalWithBreak = false
21
+
22
+ // Look for literals with a line break.
23
+ // Note that this also
24
+ visit(node, function (node) {
25
+ if (
26
+ ('value' in node && /\r?\n|\r/.test(node.value)) ||
27
+ node.type === 'break'
28
+ ) {
29
+ literalWithBreak = true
30
+ return EXIT
31
+ }
32
+ })
33
+
34
+ return Boolean(
35
+ (!node.depth || node.depth < 3) &&
36
+ toString(node) &&
37
+ (state.options.setext || literalWithBreak)
38
+ )
39
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @import {State} from 'mdast-util-to-markdown'
3
+ * @import {Link} from 'mdast'
4
+ */
5
+
6
+ import {toString} from 'mdast-util-to-string'
7
+
8
+ /**
9
+ * Checks if a link can be formatted as an autolink.
10
+ *
11
+ * @param {Link} node
12
+ * Node to check.
13
+ * @param {State} state
14
+ * Info passed around.
15
+ * @returns {boolean}
16
+ * Whether the link can be formatted as an autolink.
17
+ */
18
+ export function formatLinkAsAutolink(node, state) {
19
+ const raw = toString(node)
20
+
21
+ return Boolean(
22
+ !state.options.resourceLink &&
23
+ // If there’s a url…
24
+ node.url &&
25
+ // And there’s no title…
26
+ !node.title &&
27
+ // And the content of `node` is a single text node…
28
+ node.children &&
29
+ node.children.length === 1 &&
30
+ node.children[0].type === 'text' &&
31
+ // And if the url is the same as the content…
32
+ (raw === node.url || 'mailto:' + raw === node.url) &&
33
+ // And that starts w/ a protocol…
34
+ /^[a-z][+\-.a-z]+:/i.test(node.url) &&
35
+ // And that doesn’t contain ASCII control codes (character escapes and
36
+ // references don’t work), space, or angle brackets…
37
+ !/[\0- <>\u007F]/.test(node.url)
38
+ )
39
+ }
@@ -0,0 +1,63 @@
1
+ import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
2
+ import {htmlBlockNames, htmlRawNames} from 'micromark-util-html-tag-name'
3
+
4
+ /**
5
+ * @typedef {1 | 2 | 3 | 4 | 5 | 6 | 7} Kind
6
+ * Kind of HTML (flow) element.
7
+ */
8
+
9
+ /**
10
+ * Infer the kind of HTML that `value` is.
11
+ *
12
+ * This works on valid trees as produced by `micromark`.
13
+ * Kinds:
14
+ *
15
+ * 1. raw
16
+ * 2. comment
17
+ * 3. instruction
18
+ * 4. declaration
19
+ * 5. cdata
20
+ * 6. block
21
+ * 7. other
22
+ *
23
+ * See: <https://spec.commonmark.org/0.31.2/#html-blocks>.
24
+ *
25
+ * @param {string} value
26
+ * HTML.
27
+ * @returns {Kind | undefined}
28
+ * Kind; `undefined` if unknown.
29
+ */
30
+ export function htmlKind(value) {
31
+ if (value.charCodeAt(0) !== 60 /* `<` */) return
32
+
33
+ const next = value.charCodeAt(1)
34
+
35
+ if (next === 33 /* `!` */) {
36
+ const code = value.charCodeAt(2)
37
+ if (code === 45 /* `-` */) return 2
38
+ if (code === 91 /* `[` */) return 5
39
+ if (asciiAlpha(code)) return 4
40
+ return
41
+ }
42
+
43
+ if (next === 63 /* `?` */) return 3
44
+
45
+ const closing = next === 47 /* `/` */
46
+ const start = closing ? 2 : 1
47
+ let end = start
48
+ let code = value.charCodeAt(end)
49
+
50
+ if (!asciiAlpha(code)) return
51
+
52
+ while (code === 45 /* `-` */ || asciiAlphanumeric(code)) {
53
+ code = value.charCodeAt(++end)
54
+ }
55
+
56
+ const name = value.slice(start, end).toLowerCase()
57
+
58
+ if (!closing && code !== 47 /* `/` */ && htmlRawNames.includes(name)) {
59
+ return 1
60
+ }
61
+
62
+ return htmlBlockNames.includes(name) ? 6 : 7
63
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @import {IndentLines} from '../types.js'
3
+ */
4
+
5
+ const eol = /\r?\n|\r/g
6
+
7
+ /**
8
+ * Indents each line.
9
+ *
10
+ * @type {IndentLines}
11
+ */
12
+ export function indentLines(value, map) {
13
+ /** @type {Array<string>} */
14
+ const result = []
15
+ let start = 0
16
+ let line = 0
17
+ /** @type {RegExpExecArray | null} */
18
+ let match
19
+
20
+ while ((match = eol.exec(value))) {
21
+ one(value.slice(start, match.index))
22
+ result.push(match[0])
23
+ start = match.index + match[0].length
24
+ line++
25
+ }
26
+
27
+ one(value.slice(start))
28
+
29
+ return result.join('')
30
+
31
+ /**
32
+ * Indents a single line.
33
+ *
34
+ * @param {string} value
35
+ * Line.
36
+ */
37
+ function one(value) {
38
+ result.push(map(value, line, !value))
39
+ }
40
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @import {ConstructName, Unsafe} from 'mdast-util-to-markdown'
3
+ */
4
+
5
+ /**
6
+ * Check if a pattern is in scope based on the current stack of constructs.
7
+ *
8
+ * @param {Array<ConstructName>} stack
9
+ * Current stack of constructs.
10
+ * @param {Unsafe} pattern
11
+ * Pattern to check.
12
+ * @returns {boolean}
13
+ * Whether the pattern is in scope.
14
+ */
15
+ export function patternInScope(stack, pattern) {
16
+ return (
17
+ listInScope(stack, pattern.inConstruct, true) &&
18
+ !listInScope(stack, pattern.notInConstruct, false)
19
+ )
20
+ }
21
+
22
+ /**
23
+ * Check if any of the constructs in the list are present in the stack.
24
+ *
25
+ * @param {Array<ConstructName>} stack
26
+ * Current stack of constructs.
27
+ * @param {Unsafe['inConstruct']} list
28
+ * List of constructs to check for in the stack.
29
+ * @param {boolean} none
30
+ * Value to return if the list is empty or not provided.
31
+ * @returns {boolean}
32
+ * Whether any of the constructs in the list are present in the stack.
33
+ */
34
+ function listInScope(stack, list, none) {
35
+ if (typeof list === 'string') {
36
+ list = [list]
37
+ }
38
+
39
+ if (!list || list.length === 0) {
40
+ return none
41
+ }
42
+
43
+ let index = -1
44
+
45
+ while (++index < list.length) {
46
+ if (stack.includes(list[index])) {
47
+ return true
48
+ }
49
+ }
50
+
51
+ return false
52
+ }
@@ -0,0 +1,263 @@
1
+ /**
2
+ * @import {Nodes} from 'mdast'
3
+ * @import {SafeConfig, State, Unsafe} from 'mdast-util-to-markdown'
4
+ */
5
+
6
+ import {classifyCharacter} from 'micromark-util-classify-character'
7
+ import {encodeCharacterReference} from './encode-character-reference.js'
8
+ import {patternInScope} from './pattern-in-scope.js'
9
+
10
+ const own = {}.hasOwnProperty
11
+
12
+ /**
13
+ * Make a string safe for embedding in markdown constructs.
14
+ *
15
+ * In markdown, almost all punctuation characters can, in certain cases,
16
+ * result in something.
17
+ * Whether they do is highly subjective to where they happen and in what
18
+ * they happen.
19
+ *
20
+ * To solve this, `mdast-util-to-markdown` tracks:
21
+ *
22
+ * * characters before and after something;
23
+ * * what “constructs” we are in.
24
+ *
25
+ * This information is then used by this function to escape or encode
26
+ * special characters.
27
+ *
28
+ * @param {State} state
29
+ * Info passed around about the current state.
30
+ * @param {string | null | undefined} input
31
+ * Raw value to make safe.
32
+ * @param {SafeConfig} config
33
+ * Configuration.
34
+ * @returns {string}
35
+ * Serialized markdown safe for embedding.
36
+ */
37
+ export function safe(state, input, config) {
38
+ const value = (config.before || '') + (input || '') + (config.after || '')
39
+ /** @type {Array<number>} */
40
+ const positions = []
41
+ /** @type {Array<string>} */
42
+ const result = []
43
+ /** @type {Record<number, {before: boolean, after: boolean}>} */
44
+ const infos = {}
45
+ // Character escapes and references do not work in autolinks, so
46
+ // percent-encoding is used instead.
47
+ const percentEncode = state.stack.includes('autolink')
48
+ const offset = config.before ? config.before.length : 0
49
+ const end = value.length - (config.after ? config.after.length : 0)
50
+ let index = -1
51
+
52
+ while (++index < state.unsafe.length) {
53
+ const pattern = state.unsafe[index]
54
+
55
+ if (
56
+ !patternInScope(state.stack, pattern) ||
57
+ !nodeInScope(pattern.beforeNode, config.beforeNode) ||
58
+ !nodeInScope(pattern.afterNode, config.afterNode)
59
+ ) {
60
+ continue
61
+ }
62
+
63
+ const expression = state.compilePattern(pattern)
64
+ const from = pattern.beforeNode ? offset : 0
65
+ const subject = value.slice(from, pattern.afterNode ? end : undefined)
66
+ /** @type {RegExpExecArray | null} */
67
+ let match
68
+
69
+ while ((match = expression.exec(subject))) {
70
+ const before = 'before' in pattern || Boolean(pattern.atBreak)
71
+ const after = 'after' in pattern
72
+ const position = from + match.index + (before ? match[1].length : 0)
73
+
74
+ if (own.call(infos, position)) {
75
+ if (infos[position].before && !before) {
76
+ infos[position].before = false
77
+ }
78
+
79
+ if (infos[position].after && !after) {
80
+ infos[position].after = false
81
+ }
82
+ } else {
83
+ positions.push(position)
84
+ infos[position] = {before, after}
85
+ }
86
+ }
87
+ }
88
+
89
+ positions.sort(numerical)
90
+
91
+ let start = offset
92
+ index = -1
93
+
94
+ while (++index < positions.length) {
95
+ const position = positions[index]
96
+
97
+ // Character before or after matched:
98
+ if (position < start || position >= end) {
99
+ continue
100
+ }
101
+
102
+ // Special handling for “intraword” underscores (`a_b`, `a__b`).
103
+ // Those are different from asterisks in forming attention.
104
+ // Note that `a _ b` also cannot form but not escaping it is very complex,
105
+ // so that’s not handled.
106
+ if (
107
+ value.charAt(position) === '_' &&
108
+ // Preceded by an unescaped character that is not punctuation or whitespace:
109
+ position > 0 &&
110
+ classifyCharacter(value.charCodeAt(position - 1)) === undefined &&
111
+ !own.call(infos, position - 1) &&
112
+ // Characters next to attention markers can become character references
113
+ // later, turning `a` into `&#x61;`, so those do not count.
114
+ !(position - 1 === offset && /[*_]/.test(value.charAt(offset - 1)))
115
+ ) {
116
+ let sequenceEnd = position + 1
117
+
118
+ // Look for the whole run.
119
+ while (sequenceEnd < end && value.charAt(sequenceEnd) === '_') {
120
+ sequenceEnd++
121
+ }
122
+
123
+ const skip = sequenceEnd - position - 1
124
+
125
+ if (
126
+ // All underscores in the run are unsafe positions, so they can be
127
+ // skipped.
128
+ positions[index + skip] === sequenceEnd - 1 &&
129
+ // Rest as above.
130
+ sequenceEnd < value.length &&
131
+ classifyCharacter(value.charCodeAt(sequenceEnd)) === undefined &&
132
+ !own.call(infos, sequenceEnd) &&
133
+ !(sequenceEnd === end - 1 && /[*_]/.test(value.charAt(end)))
134
+ ) {
135
+ index += skip
136
+ continue
137
+ }
138
+ }
139
+
140
+ // If this character is supposed to be escaped because it has a condition on
141
+ // the next character, and the next character is definitly being escaped,
142
+ // then skip this escape.
143
+ if (
144
+ (position + 1 < end &&
145
+ positions[index + 1] === position + 1 &&
146
+ infos[position].after &&
147
+ !infos[position + 1].before &&
148
+ !infos[position + 1].after) ||
149
+ (positions[index - 1] === position - 1 &&
150
+ infos[position].before &&
151
+ !infos[position - 1].before &&
152
+ !infos[position - 1].after)
153
+ ) {
154
+ continue
155
+ }
156
+
157
+ if (start !== position) {
158
+ const slice = value.slice(start, position)
159
+ // If we have to use a character reference, an ampersand would be more
160
+ // correct, but as backslashes only care about punctuation, either will
161
+ // do the trick
162
+ result.push(percentEncode ? slice : escapeBackslashes(slice, '\\'))
163
+ }
164
+
165
+ start = position
166
+
167
+ // Percent-encoding such as `%20`.
168
+ if (percentEncode) {
169
+ result.push(
170
+ '%' +
171
+ value.charCodeAt(position).toString(16).toUpperCase().padStart(2, '0')
172
+ )
173
+ start++
174
+ } else if (
175
+ /[!-/:-@[-`{-~]/.test(value.charAt(position)) &&
176
+ (!config.encode || !config.encode.includes(value.charAt(position)))
177
+ ) {
178
+ // Character escape.
179
+ result.push('\\')
180
+ } else {
181
+ // Character reference.
182
+ result.push(encodeCharacterReference(value.charCodeAt(position)))
183
+ start++
184
+ }
185
+ }
186
+
187
+ const rest = value.slice(start, end)
188
+ result.push(percentEncode ? rest : escapeBackslashes(rest, config.after))
189
+
190
+ return result.join('')
191
+ }
192
+
193
+ /**
194
+ * Check if a node matches the node types of a pattern.
195
+ *
196
+ * @param {Array<Nodes['type']> | Nodes['type'] | null | undefined} types
197
+ * Node types.
198
+ * @param {Nodes | undefined} node
199
+ * Node.
200
+ * @returns {boolean}
201
+ * Whether `node` matches `types`.
202
+ */
203
+ function nodeInScope(types, node) {
204
+ if (!types) return true
205
+ if (!node) return false
206
+ return typeof types === 'string'
207
+ ? types === node.type
208
+ : types.includes(node.type)
209
+ }
210
+
211
+ /**
212
+ * Compare two numbers.
213
+ *
214
+ * @param {number} a
215
+ * Number.
216
+ * @param {number} b
217
+ * Other number.
218
+ * @returns {number}
219
+ * Result.
220
+ */
221
+ function numerical(a, b) {
222
+ return a - b
223
+ }
224
+
225
+ /**
226
+ * Escape backslashes in a string, considering characters that follow.
227
+ *
228
+ * @param {string} value
229
+ * Value.
230
+ * @param {string} after
231
+ * Characters that follow the value.
232
+ * @returns {string}
233
+ * Escaped string.
234
+ */
235
+ function escapeBackslashes(value, after) {
236
+ const expression = /\\(?=[!-/:-@[-`{-~])/g
237
+ /** @type {Array<number>} */
238
+ const positions = []
239
+ /** @type {Array<string>} */
240
+ const results = []
241
+ const whole = value + after
242
+ let index = -1
243
+ let start = 0
244
+ /** @type {RegExpExecArray | null} */
245
+ let match
246
+
247
+ while ((match = expression.exec(whole))) {
248
+ positions.push(match.index)
249
+ }
250
+
251
+ while (++index < positions.length) {
252
+ if (start !== positions[index]) {
253
+ results.push(value.slice(start, positions[index]))
254
+ }
255
+
256
+ results.push('\\')
257
+ start = positions[index]
258
+ }
259
+
260
+ results.push(value.slice(start))
261
+
262
+ return results.join('')
263
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @import {CreateTracker, TrackCurrent, TrackMove, TrackShift} from '../types.js'
3
+ */
4
+
5
+ /**
6
+ * Track positional info in the output.
7
+ *
8
+ * @type {CreateTracker}
9
+ */
10
+ export function track(config) {
11
+ // Defaults are used to prevent crashes when older utilities somehow activate
12
+ // this code.
13
+ /* c8 ignore next 5 */
14
+ const options = config || {}
15
+ const now = options.now || {}
16
+ let lineShift = options.lineShift || 0
17
+ let line = now.line || 1
18
+ let column = now.column || 1
19
+
20
+ return {move, current, shift}
21
+
22
+ /**
23
+ * Get the current tracked info.
24
+ *
25
+ * @type {TrackCurrent}
26
+ */
27
+ function current() {
28
+ return {now: {line, column}, lineShift}
29
+ }
30
+
31
+ /**
32
+ * Define an increased line shift (the typical indent for lines).
33
+ *
34
+ * @type {TrackShift}
35
+ */
36
+ function shift(value) {
37
+ lineShift += value
38
+ }
39
+
40
+ /**
41
+ * Move past some generated markdown.
42
+ *
43
+ * @type {TrackMove}
44
+ */
45
+ function move(input) {
46
+ // eslint-disable-next-line unicorn/prefer-default-parameters
47
+ const value = input || ''
48
+ const chunks = value.split(/\r?\n|\r/g)
49
+ const tail = chunks[chunks.length - 1]
50
+ line += chunks.length - 1
51
+ column =
52
+ chunks.length === 1 ? column + tail.length : 1 + tail.length + lineShift
53
+ return value
54
+ }
55
+ }
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) Titus Wormer <tituswormer@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.