markdown-it-deflist.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*! markdown-it-deflist 2.0.3 https://github.com//markdown-it/markdown-it-deflist @license MIT */(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.markdownitDeflist = 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 definition lists
  3. //
  4. 'use strict';
  5. module.exports = function deflist_plugin(md) {
  6. var isSpace = md.utils.isSpace;
  7. // Search `[:~][\n ]`, returns next pos after marker on success
  8. // or -1 on fail.
  9. function skipMarker(state, line) {
  10. var pos, marker,
  11. start = state.bMarks[line] + state.tShift[line],
  12. max = state.eMarks[line];
  13. if (start >= max) { return -1; }
  14. // Check bullet
  15. marker = state.src.charCodeAt(start++);
  16. if (marker !== 0x7E/* ~ */ && marker !== 0x3A/* : */) { return -1; }
  17. pos = state.skipSpaces(start);
  18. // require space after ":"
  19. if (start === pos) { return -1; }
  20. // no empty definitions, e.g. " : "
  21. if (pos >= max) { return -1; }
  22. return start;
  23. }
  24. function markTightParagraphs(state, idx) {
  25. var i, l,
  26. level = state.level + 2;
  27. for (i = idx + 2, l = state.tokens.length - 2; i < l; i++) {
  28. if (state.tokens[i].level === level && state.tokens[i].type === 'paragraph_open') {
  29. state.tokens[i + 2].hidden = true;
  30. state.tokens[i].hidden = true;
  31. i += 2;
  32. }
  33. }
  34. }
  35. function deflist(state, startLine, endLine, silent) {
  36. var ch,
  37. contentStart,
  38. ddLine,
  39. dtLine,
  40. itemLines,
  41. listLines,
  42. listTokIdx,
  43. max,
  44. nextLine,
  45. offset,
  46. oldDDIndent,
  47. oldIndent,
  48. oldParentType,
  49. oldSCount,
  50. oldTShift,
  51. oldTight,
  52. pos,
  53. prevEmptyEnd,
  54. tight,
  55. token;
  56. if (silent) {
  57. // quirk: validation mode validates a dd block only, not a whole deflist
  58. if (state.ddIndent < 0) { return false; }
  59. return skipMarker(state, startLine) >= 0;
  60. }
  61. nextLine = startLine + 1;
  62. if (nextLine >= endLine) { return false; }
  63. if (state.isEmpty(nextLine)) {
  64. nextLine++;
  65. if (nextLine >= endLine) { return false; }
  66. }
  67. if (state.sCount[nextLine] < state.blkIndent) { return false; }
  68. contentStart = skipMarker(state, nextLine);
  69. if (contentStart < 0) { return false; }
  70. // Start list
  71. listTokIdx = state.tokens.length;
  72. tight = true;
  73. token = state.push('dl_open', 'dl', 1);
  74. token.map = listLines = [ startLine, 0 ];
  75. //
  76. // Iterate list items
  77. //
  78. dtLine = startLine;
  79. ddLine = nextLine;
  80. // One definition list can contain multiple DTs,
  81. // and one DT can be followed by multiple DDs.
  82. //
  83. // Thus, there is two loops here, and label is
  84. // needed to break out of the second one
  85. //
  86. /*eslint no-labels:0,block-scoped-var:0*/
  87. OUTER:
  88. for (;;) {
  89. prevEmptyEnd = false;
  90. token = state.push('dt_open', 'dt', 1);
  91. token.map = [ dtLine, dtLine ];
  92. token = state.push('inline', '', 0);
  93. token.map = [ dtLine, dtLine ];
  94. token.content = state.getLines(dtLine, dtLine + 1, state.blkIndent, false).trim();
  95. token.children = [];
  96. token = state.push('dt_close', 'dt', -1);
  97. for (;;) {
  98. token = state.push('dd_open', 'dd', 1);
  99. token.map = itemLines = [ nextLine, 0 ];
  100. pos = contentStart;
  101. max = state.eMarks[ddLine];
  102. offset = state.sCount[ddLine] + contentStart - (state.bMarks[ddLine] + state.tShift[ddLine]);
  103. while (pos < max) {
  104. ch = state.src.charCodeAt(pos);
  105. if (isSpace(ch)) {
  106. if (ch === 0x09) {
  107. offset += 4 - offset % 4;
  108. } else {
  109. offset++;
  110. }
  111. } else {
  112. break;
  113. }
  114. pos++;
  115. }
  116. contentStart = pos;
  117. oldTight = state.tight;
  118. oldDDIndent = state.ddIndent;
  119. oldIndent = state.blkIndent;
  120. oldTShift = state.tShift[ddLine];
  121. oldSCount = state.sCount[ddLine];
  122. oldParentType = state.parentType;
  123. state.blkIndent = state.ddIndent = state.sCount[ddLine] + 2;
  124. state.tShift[ddLine] = contentStart - state.bMarks[ddLine];
  125. state.sCount[ddLine] = offset;
  126. state.tight = true;
  127. state.parentType = 'deflist';
  128. state.md.block.tokenize(state, ddLine, endLine, true);
  129. // If any of list item is tight, mark list as tight
  130. if (!state.tight || prevEmptyEnd) {
  131. tight = false;
  132. }
  133. // Item become loose if finish with empty line,
  134. // but we should filter last element, because it means list finish
  135. prevEmptyEnd = (state.line - ddLine) > 1 && state.isEmpty(state.line - 1);
  136. state.tShift[ddLine] = oldTShift;
  137. state.sCount[ddLine] = oldSCount;
  138. state.tight = oldTight;
  139. state.parentType = oldParentType;
  140. state.blkIndent = oldIndent;
  141. state.ddIndent = oldDDIndent;
  142. token = state.push('dd_close', 'dd', -1);
  143. itemLines[1] = nextLine = state.line;
  144. if (nextLine >= endLine) { break OUTER; }
  145. if (state.sCount[nextLine] < state.blkIndent) { break OUTER; }
  146. contentStart = skipMarker(state, nextLine);
  147. if (contentStart < 0) { break; }
  148. ddLine = nextLine;
  149. // go to the next loop iteration:
  150. // insert DD tag and repeat checking
  151. }
  152. if (nextLine >= endLine) { break; }
  153. dtLine = nextLine;
  154. if (state.isEmpty(dtLine)) { break; }
  155. if (state.sCount[dtLine] < state.blkIndent) { break; }
  156. ddLine = dtLine + 1;
  157. if (ddLine >= endLine) { break; }
  158. if (state.isEmpty(ddLine)) { ddLine++; }
  159. if (ddLine >= endLine) { break; }
  160. if (state.sCount[ddLine] < state.blkIndent) { break; }
  161. contentStart = skipMarker(state, ddLine);
  162. if (contentStart < 0) { break; }
  163. // go to the next loop iteration:
  164. // insert DT and DD tags and repeat checking
  165. }
  166. // Finilize list
  167. token = state.push('dl_close', 'dl', -1);
  168. listLines[1] = nextLine;
  169. state.line = nextLine;
  170. // mark paragraphs tight if needed
  171. if (tight) {
  172. markTightParagraphs(state, listTokIdx);
  173. }
  174. return true;
  175. }
  176. md.block.ruler.before('paragraph', 'deflist', deflist, { alt: [ 'paragraph', 'reference' ] });
  177. };
  178. },{}]},{},[1])(1)
  179. });