markdown-it-front-matter.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitFrontMatter = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. // Process front matter and pass to cb
  3. //
  4. 'use strict';
  5. module.exports = function front_matter_plugin(md, cb) {
  6. var min_markers = 3,
  7. marker_str = '-',
  8. marker_char = marker_str.charCodeAt(0),
  9. marker_len = marker_str.length
  10. function frontMatter(state, startLine, endLine, silent) {
  11. var pos, nextLine, marker_count, markup, token,
  12. old_parent, old_line_max, start_content,
  13. auto_closed = false,
  14. start = state.bMarks[startLine] + state.tShift[startLine],
  15. max = state.eMarks[startLine];
  16. // Check out the first character of the first line quickly,
  17. // this should filter out non-front matter
  18. //
  19. if (startLine !== 0 || marker_char !== state.src.charCodeAt(0)) { return false; }
  20. // Check out the rest of the marker string
  21. //
  22. for (pos = start + 1; pos <= max; pos++) { // while pos <= 3
  23. if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
  24. start_content = pos + 1
  25. break;
  26. }
  27. }
  28. marker_count = Math.floor((pos - start) / marker_len);
  29. if (marker_count < min_markers) { return false; }
  30. pos -= (pos - start) % marker_len;
  31. // Since start is found, we can report success here in validation mode
  32. //
  33. if (silent) { return true; }
  34. // Search for the end of the block
  35. //
  36. nextLine = startLine;
  37. for (;;) {
  38. nextLine++;
  39. if (nextLine >= endLine) {
  40. // unclosed block should be autoclosed by end of document.
  41. // also block seems to be autoclosed by end of parent
  42. break;
  43. }
  44. start = state.bMarks[nextLine] + state.tShift[nextLine];
  45. max = state.eMarks[nextLine];
  46. if (start < max && state.sCount[nextLine] < state.blkIndent) {
  47. // non-empty line with negative indent should stop the list:
  48. // - ```
  49. // test
  50. break;
  51. }
  52. if (marker_char !== state.src.charCodeAt(start)) { continue; }
  53. if (state.sCount[nextLine] - state.blkIndent >= 4) {
  54. // closing fence should be indented less than 4 spaces
  55. continue;
  56. }
  57. for (pos = start + 1; pos <= max; pos++) {
  58. if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
  59. break;
  60. }
  61. }
  62. // closing code fence must be at least as long as the opening one
  63. if (Math.floor((pos - start) / marker_len) < marker_count) { continue; }
  64. // make sure tail has spaces only
  65. pos -= (pos - start) % marker_len;
  66. pos = state.skipSpaces(pos);
  67. if (pos < max) { continue; }
  68. // found!
  69. auto_closed = true;
  70. break;
  71. }
  72. old_parent = state.parentType;
  73. old_line_max = state.lineMax;
  74. state.parentType = 'container';
  75. // this will prevent lazy continuations from ever going past our end marker
  76. state.lineMax = nextLine;
  77. token = state.push('front_matter', null, 0);
  78. token.hidden = true;
  79. token.markup = state.src.slice(startLine, pos)
  80. token.block = true;
  81. token.map = [ startLine, pos ];
  82. state.parentType = old_parent;
  83. state.lineMax = old_line_max;
  84. state.line = nextLine + (auto_closed ? 1 : 0);
  85. cb(state.src.slice(start_content, start - 1))
  86. return true;
  87. }
  88. md.block.ruler.before('table', 'front_matter', frontMatter, {
  89. alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
  90. });
  91. };
  92. },{}]},{},[1])(1)
  93. });