strikethrough.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // ~~strike through~~
  2. //
  3. // Insert each marker as a separate text token, and add it to delimiter list
  4. //
  5. function strikethrough_tokenize (state, silent) {
  6. const start = state.pos
  7. const marker = state.src.charCodeAt(start)
  8. if (silent) { return false }
  9. if (marker !== 0x7E/* ~ */) { return false }
  10. const scanned = state.scanDelims(state.pos, true)
  11. let len = scanned.length
  12. const ch = String.fromCharCode(marker)
  13. if (len < 2) { return false }
  14. let token
  15. if (len % 2) {
  16. token = state.push('text', '', 0)
  17. token.content = ch
  18. len--
  19. }
  20. for (let i = 0; i < len; i += 2) {
  21. token = state.push('text', '', 0)
  22. token.content = ch + ch
  23. state.delimiters.push({
  24. marker,
  25. length: 0, // disable "rule of 3" length checks meant for emphasis
  26. token: state.tokens.length - 1,
  27. end: -1,
  28. open: scanned.can_open,
  29. close: scanned.can_close
  30. })
  31. }
  32. state.pos += scanned.length
  33. return true
  34. }
  35. function postProcess (state, delimiters) {
  36. let token
  37. const loneMarkers = []
  38. const max = delimiters.length
  39. for (let i = 0; i < max; i++) {
  40. const startDelim = delimiters[i]
  41. if (startDelim.marker !== 0x7E/* ~ */) {
  42. continue
  43. }
  44. if (startDelim.end === -1) {
  45. continue
  46. }
  47. const endDelim = delimiters[startDelim.end]
  48. token = state.tokens[startDelim.token]
  49. token.type = 's_open'
  50. token.tag = 's'
  51. token.nesting = 1
  52. token.markup = '~~'
  53. token.content = ''
  54. token = state.tokens[endDelim.token]
  55. token.type = 's_close'
  56. token.tag = 's'
  57. token.nesting = -1
  58. token.markup = '~~'
  59. token.content = ''
  60. if (state.tokens[endDelim.token - 1].type === 'text' &&
  61. state.tokens[endDelim.token - 1].content === '~') {
  62. loneMarkers.push(endDelim.token - 1)
  63. }
  64. }
  65. // If a marker sequence has an odd number of characters, it's splitted
  66. // like this: `~~~~~` -> `~` + `~~` + `~~`, leaving one marker at the
  67. // start of the sequence.
  68. //
  69. // So, we have to move all those markers after subsequent s_close tags.
  70. //
  71. while (loneMarkers.length) {
  72. const i = loneMarkers.pop()
  73. let j = i + 1
  74. while (j < state.tokens.length && state.tokens[j].type === 's_close') {
  75. j++
  76. }
  77. j--
  78. if (i !== j) {
  79. token = state.tokens[j]
  80. state.tokens[j] = state.tokens[i]
  81. state.tokens[i] = token
  82. }
  83. }
  84. }
  85. // Walk through delimiter list and replace text tokens with tags
  86. //
  87. function strikethrough_postProcess (state) {
  88. const tokens_meta = state.tokens_meta
  89. const max = state.tokens_meta.length
  90. postProcess(state, state.delimiters)
  91. for (let curr = 0; curr < max; curr++) {
  92. if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
  93. postProcess(state, tokens_meta[curr].delimiters)
  94. }
  95. }
  96. }
  97. export default {
  98. tokenize: strikethrough_tokenize,
  99. postProcess: strikethrough_postProcess
  100. }