lists.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Form HTML ordered (numbered) and unordered (bulleted) lists.
  3. */
  4. showdown.subParser('lists', function (text, options, globals) {
  5. 'use strict';
  6. text = globals.converter._dispatch('lists.before', text, options, globals);
  7. /**
  8. * Process the contents of a single ordered or unordered list, splitting it
  9. * into individual list items.
  10. * @param {string} listStr
  11. * @param {boolean} trimTrailing
  12. * @returns {string}
  13. */
  14. function processListItems (listStr, trimTrailing) {
  15. // The $g_list_level global keeps track of when we're inside a list.
  16. // Each time we enter a list, we increment it; when we leave a list,
  17. // we decrement. If it's zero, we're not in a list anymore.
  18. //
  19. // We do this because when we're not inside a list, we want to treat
  20. // something like this:
  21. //
  22. // I recommend upgrading to version
  23. // 8. Oops, now this line is treated
  24. // as a sub-list.
  25. //
  26. // As a single paragraph, despite the fact that the second line starts
  27. // with a digit-period-space sequence.
  28. //
  29. // Whereas when we're inside a list (or sub-list), that line will be
  30. // treated as the start of a sub-list. What a kludge, huh? This is
  31. // an aspect of Markdown's syntax that's hard to parse perfectly
  32. // without resorting to mind-reading. Perhaps the solution is to
  33. // change the syntax rules such that sub-lists must start with a
  34. // starting cardinal number; e.g. "1." or "a.".
  35. globals.gListLevel++;
  36. // trim trailing blank lines:
  37. listStr = listStr.replace(/\n{2,}$/, '\n');
  38. // attacklab: add sentinel to emulate \z
  39. listStr += '~0';
  40. var rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0| {0,3}([*+-]|\d+[.])[ \t]+))/gm,
  41. isParagraphed = (/\n[ \t]*\n(?!~0)/.test(listStr));
  42. // Since version 1.5, nesting sublists requires 4 spaces (or 1 tab) indentation,
  43. // which is a syntax breaking change
  44. // activating this option reverts to old behavior
  45. if (options.disableForced4SpacesIndentedSublists) {
  46. rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm;
  47. }
  48. listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) {
  49. checked = (checked && checked.trim() !== '');
  50. var item = showdown.subParser('outdent')(m4, options, globals),
  51. bulletStyle = '';
  52. // Support for github tasklists
  53. if (taskbtn && options.tasklists) {
  54. bulletStyle = ' class="task-list-item" style="list-style-type: none;"';
  55. item = item.replace(/^[ \t]*\[(x|X| )?]/m, function () {
  56. var otp = '<input type="checkbox" disabled style="margin: 0px 0.35em 0.25em -1.6em; vertical-align: middle;"';
  57. if (checked) {
  58. otp += ' checked';
  59. }
  60. otp += '>';
  61. return otp;
  62. });
  63. }
  64. // ISSUE #312
  65. // This input: - - - a
  66. // causes trouble to the parser, since it interprets it as:
  67. // <ul><li><li><li>a</li></li></li></ul>
  68. // instead of:
  69. // <ul><li>- - a</li></ul>
  70. // So, to prevent it, we will put a marker (~A)in the beginning of the line
  71. // Kind of hackish/monkey patching, but seems more effective than overcomplicating the list parser
  72. item = item.replace(/^([-*+]|\d\.)[ \t]+[\S\n ]*/g, function (wm2) {
  73. return '~A' + wm2;
  74. });
  75. // m1 - Leading line or
  76. // Has a double return (multi paragraph) or
  77. // Has sublist
  78. if (m1 || (item.search(/\n{2,}/) > -1)) {
  79. item = showdown.subParser('githubCodeBlocks')(item, options, globals);
  80. item = showdown.subParser('blockGamut')(item, options, globals);
  81. } else {
  82. // Recursion for sub-lists:
  83. item = showdown.subParser('lists')(item, options, globals);
  84. item = item.replace(/\n$/, ''); // chomp(item)
  85. if (isParagraphed) {
  86. item = showdown.subParser('paragraphs')(item, options, globals);
  87. } else {
  88. item = showdown.subParser('spanGamut')(item, options, globals);
  89. }
  90. }
  91. // now we need to remove the marker (~A)
  92. item = item.replace('~A', '');
  93. // we can finally wrap the line in list item tags
  94. item = '<li' + bulletStyle + '>' + item + '</li>\n';
  95. return item;
  96. });
  97. // attacklab: strip sentinel
  98. listStr = listStr.replace(/~0/g, '');
  99. globals.gListLevel--;
  100. if (trimTrailing) {
  101. listStr = listStr.replace(/\s+$/, '');
  102. }
  103. return listStr;
  104. }
  105. /**
  106. * Check and parse consecutive lists (better fix for issue #142)
  107. * @param {string} list
  108. * @param {string} listType
  109. * @param {boolean} trimTrailing
  110. * @returns {string}
  111. */
  112. function parseConsecutiveLists(list, listType, trimTrailing) {
  113. // check if we caught 2 or more consecutive lists by mistake
  114. // we use the counterRgx, meaning if listType is UL we look for OL and vice versa
  115. var olRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?\d+\.[ \t]/gm : /^ {0,3}\d+\.[ \t]/gm,
  116. ulRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?[*+-][ \t]/gm : /^ {0,3}[*+-][ \t]/gm,
  117. counterRxg = (listType === 'ul') ? olRgx : ulRgx,
  118. result = '';
  119. if (list.search(counterRxg) !== -1) {
  120. (function parseCL(txt) {
  121. var pos = txt.search(counterRxg);
  122. if (pos !== -1) {
  123. // slice
  124. result += '\n<' + listType + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
  125. // invert counterType and listType
  126. listType = (listType === 'ul') ? 'ol' : 'ul';
  127. counterRxg = (listType === 'ul') ? olRgx : ulRgx;
  128. //recurse
  129. parseCL(txt.slice(pos));
  130. } else {
  131. result += '\n<' + listType + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
  132. }
  133. })(list);
  134. } else {
  135. result = '\n<' + listType + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
  136. }
  137. return result;
  138. }
  139. // add sentinel to hack around khtml/safari bug:
  140. // http://bugs.webkit.org/show_bug.cgi?id=11231
  141. text += '~0';
  142. if (globals.gListLevel) {
  143. text = text.replace(/^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
  144. function (wholeMatch, list, m2) {
  145. var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  146. return parseConsecutiveLists(list, listType, true);
  147. }
  148. );
  149. } else {
  150. text = text.replace(/(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
  151. function (wholeMatch, m1, list, m3) {
  152. var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  153. return parseConsecutiveLists(list, listType, false);
  154. }
  155. );
  156. }
  157. // strip sentinel
  158. text = text.replace(/~0/, '');
  159. text = globals.converter._dispatch('lists.after', text, options, globals);
  160. return text;
  161. });