summaryrefslogtreecommitdiff
path: root/node_modules/markdown-it/lib/rules_inline/text_collapse.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-01-20 13:43:34 +0100
committerMinteck <contact@minteck.org>2022-01-20 13:43:34 +0100
commitc2aa7bf38fb30de2d04f87f8e7780e4c768ae6b1 (patch)
tree226598e8d17d20e3721358f7c60b1cc6b851163a /node_modules/markdown-it/lib/rules_inline/text_collapse.js
downloadcobalt-c2aa7bf38fb30de2d04f87f8e7780e4c768ae6b1.tar.gz
cobalt-c2aa7bf38fb30de2d04f87f8e7780e4c768ae6b1.tar.bz2
cobalt-c2aa7bf38fb30de2d04f87f8e7780e4c768ae6b1.zip
Initial commit
Diffstat (limited to 'node_modules/markdown-it/lib/rules_inline/text_collapse.js')
-rw-r--r--node_modules/markdown-it/lib/rules_inline/text_collapse.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/node_modules/markdown-it/lib/rules_inline/text_collapse.js b/node_modules/markdown-it/lib/rules_inline/text_collapse.js
new file mode 100644
index 0000000..390b0fe
--- /dev/null
+++ b/node_modules/markdown-it/lib/rules_inline/text_collapse.js
@@ -0,0 +1,41 @@
+// 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).
+//
+'use strict';
+
+
+module.exports = function text_collapse(state) {
+ var curr, last,
+ level = 0,
+ tokens = state.tokens,
+ 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;
+ }
+};