remarkable-katex.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* MIT License
  2. Copyright (c) 2017 Brad Howes
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. /**
  20. * Plugin for Remarkable Markdown processor which transforms $..$ and $$..$$
  21. * sequences into math HTML using the KaTeX package.
  22. */
  23. module.exports = function(md, options) {
  24. const katex = require("../../");
  25. function renderKatex(source, displayMode) {
  26. return katex.renderToString(source, {displayMode, throwOnError: false});
  27. }
  28. /**
  29. * Parse '$$' as a block. Based off of similar method in remarkable.
  30. */
  31. function parseBlockKatex(state, startLine, endLine) {
  32. let len;
  33. let params;
  34. let nextLine;
  35. let mem;
  36. let haveEndMarker = false;
  37. let pos = state.bMarks[startLine] + state.tShift[startLine];
  38. let max = state.eMarks[startLine];
  39. const dollar = 0x24;
  40. if (pos + 1 > max) { return false; }
  41. const marker = state.src.charCodeAt(pos);
  42. if (marker !== dollar) { return false; }
  43. // scan marker length
  44. mem = pos;
  45. pos = state.skipChars(pos, marker);
  46. len = pos - mem;
  47. if (len !== 2) { return false; }
  48. // search end of block
  49. nextLine = startLine;
  50. for (;;) {
  51. ++nextLine;
  52. if (nextLine >= endLine) {
  53. // unclosed block should be autoclosed by end of document.
  54. // also block seems to be autoclosed by end of parent
  55. break;
  56. }
  57. pos = mem = state.bMarks[nextLine] + state.tShift[nextLine];
  58. max = state.eMarks[nextLine];
  59. if (pos < max && state.tShift[nextLine] < state.blkIndent) {
  60. // non-empty line with negative indent should stop the list:
  61. // - ```
  62. // test
  63. break;
  64. }
  65. if (state.src.charCodeAt(pos) !== dollar) { continue; }
  66. if (state.tShift[nextLine] - state.blkIndent >= 4) {
  67. // closing fence should be indented less than 4 spaces
  68. continue;
  69. }
  70. pos = state.skipChars(pos, marker);
  71. // closing code fence must be at least as long as the opening one
  72. if (pos - mem < len) { continue; }
  73. // make sure tail has spaces only
  74. pos = state.skipSpaces(pos);
  75. if (pos < max) { continue; }
  76. haveEndMarker = true;
  77. // found!
  78. break;
  79. }
  80. // If a fence has heading spaces, they should be removed from
  81. // its inner block
  82. len = state.tShift[startLine];
  83. state.line = nextLine + (haveEndMarker ? 1 : 0);
  84. const content = state.getLines(startLine + 1, nextLine, len, true)
  85. .replace(/[ \n]+/g, ' ')
  86. .trim();
  87. state.tokens.push({
  88. type: 'katex',
  89. params,
  90. content,
  91. lines: [startLine, state.line],
  92. level: state.level,
  93. block: true,
  94. });
  95. return true;
  96. }
  97. /**
  98. * Look for '$' or '$$' spans in Markdown text.
  99. * Based off of the 'fenced' parser in remarkable.
  100. */
  101. function parseInlineKatex(state, silent) {
  102. const dollar = 0x24;
  103. const backslash = 0x5c;
  104. let pos = state.pos;
  105. const start = pos;
  106. const max = state.posMax;
  107. let matchStart;
  108. let matchEnd;
  109. let esc;
  110. if (state.src.charCodeAt(pos) !== dollar) { return false; }
  111. ++pos;
  112. while (pos < max && state.src.charCodeAt(pos) === dollar) {
  113. ++pos;
  114. }
  115. const marker = state.src.slice(start, pos);
  116. if (marker.length > 2) { return false; }
  117. matchStart = matchEnd = pos;
  118. while ((matchStart = state.src.indexOf('$', matchEnd)) !== -1) {
  119. matchEnd = matchStart + 1;
  120. // bypass escaped delimiters
  121. esc = matchStart - 1;
  122. while (state.src.charCodeAt(esc) === backslash) {
  123. --esc;
  124. }
  125. if ((matchStart - esc) % 2 === 0) { continue; }
  126. while (matchEnd < max && state.src.charCodeAt(matchEnd) === dollar) {
  127. ++matchEnd;
  128. }
  129. if (matchEnd - matchStart === marker.length) {
  130. if (!silent) {
  131. const content = state.src.slice(pos, matchStart)
  132. .replace(/[ \n]+/g, ' ')
  133. .trim();
  134. state.push({
  135. type: 'katex',
  136. content,
  137. block: marker.length > 1,
  138. level: state.level,
  139. });
  140. }
  141. state.pos = matchEnd;
  142. return true;
  143. }
  144. }
  145. if (!silent) {
  146. state.pending += marker;
  147. }
  148. state.pos += marker.length;
  149. return true;
  150. }
  151. md.inline.ruler.push('katex', parseInlineKatex, options);
  152. md.block.ruler.push('katex', parseBlockKatex, options);
  153. md.renderer.rules.katex = function(tokens, idx) {
  154. return renderKatex(tokens[idx].content, tokens[idx].block);
  155. };
  156. };