emphasis.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Process *this* and _that_
  2. //
  3. // Insert each marker as a separate text token, and add it to delimiter list
  4. //
  5. function emphasis_tokenize (state, silent) {
  6. const start = state.pos
  7. const marker = state.src.charCodeAt(start)
  8. if (silent) { return false }
  9. if (marker !== 0x5F /* _ */ && marker !== 0x2A /* * */) { return false }
  10. const scanned = state.scanDelims(state.pos, marker === 0x2A)
  11. for (let i = 0; i < scanned.length; i++) {
  12. const token = state.push('text', '', 0)
  13. token.content = String.fromCharCode(marker)
  14. state.delimiters.push({
  15. // Char code of the starting marker (number).
  16. //
  17. marker,
  18. // Total length of these series of delimiters.
  19. //
  20. length: scanned.length,
  21. // A position of the token this delimiter corresponds to.
  22. //
  23. token: state.tokens.length - 1,
  24. // If this delimiter is matched as a valid opener, `end` will be
  25. // equal to its position, otherwise it's `-1`.
  26. //
  27. end: -1,
  28. // Boolean flags that determine if this delimiter could open or close
  29. // an emphasis.
  30. //
  31. open: scanned.can_open,
  32. close: scanned.can_close
  33. })
  34. }
  35. state.pos += scanned.length
  36. return true
  37. }
  38. function postProcess (state, delimiters) {
  39. const max = delimiters.length
  40. for (let i = max - 1; i >= 0; i--) {
  41. const startDelim = delimiters[i]
  42. if (startDelim.marker !== 0x5F/* _ */ && startDelim.marker !== 0x2A/* * */) {
  43. continue
  44. }
  45. // Process only opening markers
  46. if (startDelim.end === -1) {
  47. continue
  48. }
  49. const endDelim = delimiters[startDelim.end]
  50. // If the previous delimiter has the same marker and is adjacent to this one,
  51. // merge those into one strong delimiter.
  52. //
  53. // `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
  54. //
  55. const isStrong = i > 0 &&
  56. delimiters[i - 1].end === startDelim.end + 1 &&
  57. // check that first two markers match and adjacent
  58. delimiters[i - 1].marker === startDelim.marker &&
  59. delimiters[i - 1].token === startDelim.token - 1 &&
  60. // check that last two markers are adjacent (we can safely assume they match)
  61. delimiters[startDelim.end + 1].token === endDelim.token + 1
  62. const ch = String.fromCharCode(startDelim.marker)
  63. const token_o = state.tokens[startDelim.token]
  64. token_o.type = isStrong ? 'strong_open' : 'em_open'
  65. token_o.tag = isStrong ? 'strong' : 'em'
  66. token_o.nesting = 1
  67. token_o.markup = isStrong ? ch + ch : ch
  68. token_o.content = ''
  69. const token_c = state.tokens[endDelim.token]
  70. token_c.type = isStrong ? 'strong_close' : 'em_close'
  71. token_c.tag = isStrong ? 'strong' : 'em'
  72. token_c.nesting = -1
  73. token_c.markup = isStrong ? ch + ch : ch
  74. token_c.content = ''
  75. if (isStrong) {
  76. state.tokens[delimiters[i - 1].token].content = ''
  77. state.tokens[delimiters[startDelim.end + 1].token].content = ''
  78. i--
  79. }
  80. }
  81. }
  82. // Walk through delimiter list and replace text tokens with tags
  83. //
  84. function emphasis_post_process (state) {
  85. const tokens_meta = state.tokens_meta
  86. const max = state.tokens_meta.length
  87. postProcess(state, state.delimiters)
  88. for (let curr = 0; curr < max; curr++) {
  89. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  90. postProcess(state, tokens_meta[curr].delimiters)
  91. }
  92. }
  93. }
  94. export default {
  95. tokenize: emphasis_tokenize,
  96. postProcess: emphasis_post_process
  97. }