fragments_join.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Clean up tokens after emphasis and strikethrough postprocessing:
  2. // merge adjacent text nodes into one and re-calculate all token levels
  3. //
  4. // This is necessary because initially emphasis delimiter markers (*, _, ~)
  5. // are treated as their own separate text tokens. Then emphasis rule either
  6. // leaves them as text (needed to merge with adjacent text) or turns them
  7. // into opening/closing tags (which messes up levels inside).
  8. //
  9. export default function fragments_join (state) {
  10. let curr, last
  11. let level = 0
  12. const tokens = state.tokens
  13. const max = state.tokens.length
  14. for (curr = last = 0; curr < max; curr++) {
  15. // re-calculate levels after emphasis/strikethrough turns some text nodes
  16. // into opening/closing tags
  17. if (tokens[curr].nesting < 0) level-- // closing tag
  18. tokens[curr].level = level
  19. if (tokens[curr].nesting > 0) level++ // opening tag
  20. if (tokens[curr].type === 'text' &&
  21. curr + 1 < max &&
  22. tokens[curr + 1].type === 'text') {
  23. // collapse two adjacent text nodes
  24. tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content
  25. } else {
  26. if (curr !== last) { tokens[last] = tokens[curr] }
  27. last++
  28. }
  29. }
  30. if (curr !== last) {
  31. tokens.length = last
  32. }
  33. }