summaryrefslogtreecommitdiff
path: root/node_modules/markdown-it/lib/rules_inline/fragments_join.mjs
diff options
context:
space:
mode:
authorShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
committerShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
commit7a52ddeba2a68388b544f529d2d92104420f77b0 (patch)
tree15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/markdown-it/lib/rules_inline/fragments_join.mjs
parent53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff)
Changed from static to 11ty!
Diffstat (limited to 'node_modules/markdown-it/lib/rules_inline/fragments_join.mjs')
-rw-r--r--node_modules/markdown-it/lib/rules_inline/fragments_join.mjs38
1 files changed, 38 insertions, 0 deletions
diff --git a/node_modules/markdown-it/lib/rules_inline/fragments_join.mjs b/node_modules/markdown-it/lib/rules_inline/fragments_join.mjs
new file mode 100644
index 0000000..e89ef9e
--- /dev/null
+++ b/node_modules/markdown-it/lib/rules_inline/fragments_join.mjs
@@ -0,0 +1,38 @@
+// Clean up tokens after emphasis and strikethrough postprocessing:
+// merge adjacent text nodes into one and re-calculate all token levels
+//
+// This is necessary because initially emphasis delimiter markers (*, _, ~)
+// are treated as their own separate text tokens. Then emphasis rule either
+// leaves them as text (needed to merge with adjacent text) or turns them
+// into opening/closing tags (which messes up levels inside).
+//
+
+export default function fragments_join (state) {
+ let curr, last
+ let level = 0
+ const tokens = state.tokens
+ const max = state.tokens.length
+
+ for (curr = last = 0; curr < max; curr++) {
+ // re-calculate levels after emphasis/strikethrough turns some text nodes
+ // into opening/closing tags
+ if (tokens[curr].nesting < 0) level-- // closing tag
+ tokens[curr].level = level
+ if (tokens[curr].nesting > 0) level++ // opening tag
+
+ if (tokens[curr].type === 'text' &&
+ curr + 1 < max &&
+ tokens[curr + 1].type === 'text') {
+ // collapse two adjacent text nodes
+ tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content
+ } else {
+ if (curr !== last) { tokens[last] = tokens[curr] }
+
+ last++
+ }
+ }
+
+ if (curr !== last) {
+ tokens.length = last
+ }
+}