index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Process block-level custom containers
  2. //
  3. 'use strict';
  4. module.exports = function container_plugin(md, name, options) {
  5. function validateDefault(params) {
  6. return params.trim().split(' ', 2)[0] === name;
  7. }
  8. function renderDefault(tokens, idx, _options, env, self) {
  9. // add a class to the opening tag
  10. if (tokens[idx].nesting === 1) {
  11. tokens[idx].attrPush([ 'class', name ]);
  12. }
  13. return self.renderToken(tokens, idx, _options, env, self);
  14. }
  15. options = options || {};
  16. var min_markers = 3,
  17. marker_str = options.marker || ':',
  18. marker_char = marker_str.charCodeAt(0),
  19. marker_len = marker_str.length,
  20. validate = options.validate || validateDefault,
  21. render = options.render || renderDefault;
  22. function container(state, startLine, endLine, silent) {
  23. var pos, nextLine, marker_count, markup, params, token,
  24. old_parent, old_line_max,
  25. auto_closed = false,
  26. start = state.bMarks[startLine] + state.tShift[startLine],
  27. max = state.eMarks[startLine];
  28. // Check out the first character quickly,
  29. // this should filter out most of non-containers
  30. //
  31. if (marker_char !== state.src.charCodeAt(start)) { return false; }
  32. // Check out the rest of the marker string
  33. //
  34. for (pos = start + 1; pos <= max; pos++) {
  35. if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
  36. break;
  37. }
  38. }
  39. marker_count = Math.floor((pos - start) / marker_len);
  40. if (marker_count < min_markers) { return false; }
  41. pos -= (pos - start) % marker_len;
  42. markup = state.src.slice(start, pos);
  43. params = state.src.slice(pos, max);
  44. if (!validate(params)) { return false; }
  45. // Since start is found, we can report success here in validation mode
  46. //
  47. if (silent) { return true; }
  48. // Search for the end of the block
  49. //
  50. nextLine = startLine;
  51. for (;;) {
  52. nextLine++;
  53. if (nextLine >= endLine) {
  54. // unclosed block should be autoclosed by end of document.
  55. // also block seems to be autoclosed by end of parent
  56. break;
  57. }
  58. start = state.bMarks[nextLine] + state.tShift[nextLine];
  59. max = state.eMarks[nextLine];
  60. if (start < max && state.sCount[nextLine] < state.blkIndent) {
  61. // non-empty line with negative indent should stop the list:
  62. // - ```
  63. // test
  64. break;
  65. }
  66. if (marker_char !== state.src.charCodeAt(start)) { continue; }
  67. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  68. // closing fence should be indented less than 4 spaces
  69. continue;
  70. }
  71. for (pos = start + 1; pos <= max; pos++) {
  72. if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
  73. break;
  74. }
  75. }
  76. // closing code fence must be at least as long as the opening one
  77. if (Math.floor((pos - start) / marker_len) < marker_count) { continue; }
  78. // make sure tail has spaces only
  79. pos -= (pos - start) % marker_len;
  80. pos = state.skipSpaces(pos);
  81. if (pos < max) { continue; }
  82. // found!
  83. auto_closed = true;
  84. break;
  85. }
  86. old_parent = state.parentType;
  87. old_line_max = state.lineMax;
  88. state.parentType = 'container';
  89. // this will prevent lazy continuations from ever going past our end marker
  90. state.lineMax = nextLine;
  91. token = state.push('container_' + name + '_open', 'div', 1);
  92. token.markup = markup;
  93. token.block = true;
  94. token.info = params;
  95. token.map = [ startLine, nextLine ];
  96. state.md.block.tokenize(state, startLine + 1, nextLine);
  97. token = state.push('container_' + name + '_close', 'div', -1);
  98. token.markup = state.src.slice(start, pos);
  99. token.block = true;
  100. state.parentType = old_parent;
  101. state.lineMax = old_line_max;
  102. state.line = nextLine + (auto_closed ? 1 : 0);
  103. return true;
  104. }
  105. md.block.ruler.before('fence', 'container_' + name, container, {
  106. alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
  107. });
  108. md.renderer.rules['container_' + name + '_open'] = render;
  109. md.renderer.rules['container_' + name + '_close'] = render;
  110. };