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.

140 lines
3.9 KiB

  1. // Test if potential opening or closing delimieter
  2. // Assumes that there is a "$" at state.src[pos]
  3. function isValidDelim (state, pos) {
  4. let prevChar
  5. let nextChar
  6. let max = state.posMax
  7. let canOpen = true
  8. let canClose = true
  9. prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1
  10. nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1
  11. // Check non-whitespace conditions for opening and closing, and
  12. // check that closing delimeter isn't followed by a number
  13. if (prevChar === 0x20/* " " */ || prevChar === 0x09/* \t */ ||
  14. (nextChar >= 0x30/* "0" */ && nextChar <= 0x39/* "9" */)) {
  15. canClose = false
  16. }
  17. if (nextChar === 0x20/* " " */ || nextChar === 0x09/* \t */) {
  18. canOpen = false
  19. }
  20. return {
  21. canOpen: canOpen,
  22. canClose: canClose
  23. }
  24. }
  25. export default {
  26. katexInline (state, silent) {
  27. let start, match, token, res, pos
  28. if (state.src[state.pos] !== '$') { return false }
  29. res = isValidDelim(state, state.pos)
  30. if (!res.canOpen) {
  31. if (!silent) { state.pending += '$' }
  32. state.pos += 1
  33. return true
  34. }
  35. // First check for and bypass all properly escaped delimieters
  36. // This loop will assume that the first leading backtick can not
  37. // be the first character in state.src, which is known since
  38. // we have found an opening delimieter already.
  39. start = state.pos + 1
  40. match = start
  41. while ((match = state.src.indexOf('$', match)) !== -1) {
  42. // Found potential $, look for escapes, pos will point to
  43. // first non escape when complete
  44. pos = match - 1
  45. while (state.src[pos] === '\\') { pos -= 1 }
  46. // Even number of escapes, potential closing delimiter found
  47. if (((match - pos) % 2) === 1) { break }
  48. match += 1
  49. }
  50. // No closing delimter found. Consume $ and continue.
  51. if (match === -1) {
  52. if (!silent) { state.pending += '$' }
  53. state.pos = start
  54. return true
  55. }
  56. // Check if we have empty content, ie: $$. Do not parse.
  57. if (match - start === 0) {
  58. if (!silent) { state.pending += '$$' }
  59. state.pos = start + 1
  60. return true
  61. }
  62. // Check for valid closing delimiter
  63. res = isValidDelim(state, match)
  64. if (!res.canClose) {
  65. if (!silent) { state.pending += '$' }
  66. state.pos = start
  67. return true
  68. }
  69. if (!silent) {
  70. token = state.push('katex_inline', 'math', 0)
  71. token.markup = '$'
  72. token.content = state.src.slice(start, match)
  73. }
  74. state.pos = match + 1
  75. return true
  76. },
  77. katexBlock (state, start, end, silent) {
  78. let firstLine; let lastLine; let next; let lastPos; let found = false; let token
  79. let pos = state.bMarks[start] + state.tShift[start]
  80. let max = state.eMarks[start]
  81. if (pos + 2 > max) { return false }
  82. if (state.src.slice(pos, pos + 2) !== '$$') { return false }
  83. pos += 2
  84. firstLine = state.src.slice(pos, max)
  85. if (silent) { return true }
  86. if (firstLine.trim().slice(-2) === '$$') {
  87. // Single line expression
  88. firstLine = firstLine.trim().slice(0, -2)
  89. found = true
  90. }
  91. for (next = start; !found;) {
  92. next++
  93. if (next >= end) { break }
  94. pos = state.bMarks[next] + state.tShift[next]
  95. max = state.eMarks[next]
  96. if (pos < max && state.tShift[next] < state.blkIndent) {
  97. // non-empty line with negative indent should stop the list:
  98. break
  99. }
  100. if (state.src.slice(pos, max).trim().slice(-2) === '$$') {
  101. lastPos = state.src.slice(0, max).lastIndexOf('$$')
  102. lastLine = state.src.slice(pos, lastPos)
  103. found = true
  104. }
  105. }
  106. state.line = next + 1
  107. token = state.push('katex_block', 'math', 0)
  108. token.block = true
  109. token.content = (firstLine && firstLine.trim() ? firstLine + '\n' : '') +
  110. state.getLines(start + 1, next, state.tShift[start], true) +
  111. (lastLine && lastLine.trim() ? lastLine : '')
  112. token.map = [ start, state.line ]
  113. token.markup = '$$'
  114. return true
  115. }
  116. }