Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. var MarkdownIt = require("markdown-it");
  2. var md = new MarkdownIt('commonmark');
  3.  
  4.  
  5.  
  6.  
  7. // Add custom INLINE rule that pulls in anything wrapped in a ::: <TEXT> ::: and wrap in a span tag
  8. function custom_inline(state, silent) {
  9. let pos = state.pos;
  10.  
  11. let matchStart = pos;
  12.  
  13. let marker = '';
  14. while (true) {
  15. let ch = state.src.charCodeAt(pos);
  16. if (ch !== 0x3A/* : */) {
  17. break;
  18. }
  19. marker += String.fromCharCode(ch);
  20. pos++;
  21. }
  22.  
  23. if (marker.length < 3) {
  24. return false;
  25. }
  26.  
  27. let contentStart = pos;
  28. let contentEnd = state.src.indexOf(marker, pos + marker.length);
  29.  
  30. if (contentEnd === -1) {
  31. return false;
  32. }
  33.  
  34. let matchEnd = contentEnd + marker.length;
  35.  
  36. if (!silent) {
  37. let token;
  38. token = state.push('custom_inline', '', 0);
  39. // token.attrs = [ [ 'key1', val1 ], [ 'key2', val2] ];
  40. token.markup = marker;
  41. token.content = state.src.slice(contentStart, contentEnd).trim();
  42. }
  43. state.pos = matchEnd;
  44. return true;
  45. }
  46.  
  47. md.inline.ruler.before('backticks', 'custom_inline', custom_inline);
  48. md.renderer.rules.custom_inline = function (tokens, idx, options, env, self) {
  49. return '<span>' + md.utils.escapeHtml(tokens[idx].content) + '</span>';
  50. };
  51.  
  52.  
  53.  
  54.  
  55.  
  56. // Add custom BLOCK rule that pulls in anything sandwiched between a startings and ending ::: lines and wraps in a div tag
  57. function getLine(state, lineNum) {
  58. return state.src.slice(state.bMarks[lineNum], state.eMarks[lineNum]);
  59. }
  60.  
  61. function custom(state, startLine, endLine, silent) {
  62. let line;
  63. let content = [];
  64.  
  65. // no whitespace allowed before beginning
  66. if (state.sCount[startLine] - state.blkIndent > 0) {
  67. return false;
  68. }
  69.  
  70. // read start marker
  71. line = getLine(state, startLine);
  72. let marker = '';
  73. for (let i = 0; i < line.length; i++) {
  74. const ch = line.charCodeAt(i);
  75. if (ch !== 0x3A/* : */) {
  76. break;
  77. }
  78. marker += String.fromCharCode(ch);
  79. }
  80.  
  81. // if no marker found, skip
  82. if (marker.length < 3) {
  83. return false;
  84. }
  85.  
  86. // first line must be just the marker
  87. if (line.indexOf(marker) !== 0 || line.trim() !== marker) {
  88. return false;
  89. }
  90.  
  91. // keep reading until end marker
  92. let properlyEnded = false;
  93. let nextLine = startLine;
  94. for (nextLine = startLine + 1; nextLine < endLine; nextLine++) {
  95. line = getLine(state, nextLine);
  96. if (line.indexOf(marker) === 0 && line.trim() === marker) {
  97. properlyEnded = true;
  98. break;
  99. }
  100.  
  101. content.push(line);
  102. }
  103.  
  104. if (!properlyEnded) {
  105. return false;
  106. }
  107.  
  108. state.line = nextLine + 1;
  109.  
  110. if (silent) {
  111. return true;
  112. }
  113.  
  114. let token;
  115. token = state.push('custom', '', 0);
  116. token.content = content.join('\n');
  117. token.markup = marker;
  118. token.map = [ startLine, state.line ];
  119.  
  120. return true;
  121. }
  122.  
  123. md.block.ruler.before('fence', 'custom', custom);
  124. md.renderer.rules.custom = function (tokens, idx, options, env, self) {
  125. return '<div>' + md.utils.escapeHtml(tokens[idx].content) + '</div>';
  126. };
  127.  
  128.  
  129.  
  130.  
  131.  
  132. var result = md.render('# hello!\npooper :::hello1::: ```end```\n\n:::\nhello2\ntest\n\n\ntesttest\n:::\n\n');
  133. console.log(result);
  134.  
  135. // OUTPUT
  136. // ------
  137. // <h1>hello!</h1>
  138. // <p>pooper <span>hello1</span> <code>end</code></p>
  139. // <div>hello2
  140. // test
  141. //
  142. //
  143. // testtest</div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement