34 lines
768 B
JavaScript
34 lines
768 B
JavaScript
// 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;
|
|
}
|
|
};
|