You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
768 B
34 lines
768 B
1 month ago
|
// Merge adjacent text nodes into one, and re-calculate all token levels
|
||
|
//
|
||
|
'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
|
||
|
level += tokens[curr].nesting;
|
||
|
tokens[curr].level = level;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
};
|