lists.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Form HTML ordered (numbered) and unordered (bulleted) lists.
  3. */
  4. showdown.subParser('makehtml.lists', function (text, options, globals) {
  5. 'use strict';
  6. /**
  7. * Process the contents of a single ordered or unordered list, splitting it
  8. * into individual list items.
  9. * @param {string} listStr
  10. * @param {boolean} trimTrailing
  11. * @returns {string}
  12. */
  13. function processListItems (listStr, trimTrailing) {
  14. // The $g_list_level global keeps track of when we're inside a list.
  15. // Each time we enter a list, we increment it; when we leave a list,
  16. // we decrement. If it's zero, we're not in a list anymore.
  17. //
  18. // We do this because when we're not inside a list, we want to treat
  19. // something like this:
  20. //
  21. // I recommend upgrading to version
  22. // 8. Oops, now this line is treated
  23. // as a sub-list.
  24. //
  25. // As a single paragraph, despite the fact that the second line starts
  26. // with a digit-period-space sequence.
  27. //
  28. // Whereas when we're inside a list (or sub-list), that line will be
  29. // treated as the start of a sub-list. What a kludge, huh? This is
  30. // an aspect of Markdown's syntax that's hard to parse perfectly
  31. // without resorting to mind-reading. Perhaps the solution is to
  32. // change the syntax rules such that sub-lists must start with a
  33. // starting cardinal number; e.g. "1." or "a.".
  34. globals.gListLevel++;
  35. // trim trailing blank lines:
  36. listStr = listStr.replace(/\n{2,}$/, '\n');
  37. // attacklab: add sentinel to emulate \z
  38. listStr += '¨0';
  39. var rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0| {0,3}([*+-]|\d+[.])[ \t]+))/gm,
  40. isParagraphed = (/\n[ \t]*\n(?!¨0)/.test(listStr));
  41. // Since version 1.5, nesting sublists requires 4 spaces (or 1 tab) indentation,
  42. // which is a syntax breaking change
  43. // activating this option reverts to old behavior
  44. // This will be removed in version 2.0
  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('makehtml.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. // SPECIAL CASE: an heading followed by a paragraph of text that is not separated by a double newline
  76. // or/nor indented. ex:
  77. //
  78. // - # foo
  79. // bar is great
  80. //
  81. // While this does now follow the spec per se, not allowing for this might cause confusion since
  82. // header blocks don't need double newlines after
  83. if (/^#+.+\n.+/.test(item)) {
  84. item = item.replace(/^(#+.+)$/m, '$1\n');
  85. }
  86. // m1 - Leading line or
  87. // Has a double return (multi paragraph)
  88. if (m1 || (item.search(/\n{2,}/) > -1)) {
  89. item = showdown.subParser('makehtml.githubCodeBlocks')(item, options, globals);
  90. item = showdown.subParser('makehtml.blockGamut')(item, options, globals);
  91. } else {
  92. // Recursion for sub-lists:
  93. item = showdown.subParser('makehtml.lists')(item, options, globals);
  94. item = item.replace(/\n$/, ''); // chomp(item)
  95. item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);
  96. // Colapse double linebreaks
  97. item = item.replace(/\n\n+/g, '\n\n');
  98. if (isParagraphed) {
  99. item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
  100. } else {
  101. item = showdown.subParser('makehtml.spanGamut')(item, options, globals);
  102. }
  103. }
  104. // now we need to remove the marker (¨A)
  105. item = item.replace('¨A', '');
  106. // we can finally wrap the line in list item tags
  107. item = '<li' + bulletStyle + '>' + item + '</li>\n';
  108. return item;
  109. });
  110. // attacklab: strip sentinel
  111. listStr = listStr.replace(/¨0/g, '');
  112. globals.gListLevel--;
  113. if (trimTrailing) {
  114. listStr = listStr.replace(/\s+$/, '');
  115. }
  116. return listStr;
  117. }
  118. function styleStartNumber (list, listType) {
  119. // check if ol and starts by a number different than 1
  120. if (listType === 'ol') {
  121. var res = list.match(/^ *(\d+)\./);
  122. if (res && res[1] !== '1') {
  123. return ' start="' + res[1] + '"';
  124. }
  125. }
  126. return '';
  127. }
  128. /**
  129. * Check and parse consecutive lists (better fix for issue #142)
  130. * @param {string} list
  131. * @param {string} listType
  132. * @param {boolean} trimTrailing
  133. * @returns {string}
  134. */
  135. function parseConsecutiveLists (list, listType, trimTrailing) {
  136. // check if we caught 2 or more consecutive lists by mistake
  137. // we use the counterRgx, meaning if listType is UL we look for OL and vice versa
  138. var olRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?\d+\.[ \t]/gm : /^ {0,3}\d+\.[ \t]/gm,
  139. ulRgx = (options.disableForced4SpacesIndentedSublists) ? /^ ?[*+-][ \t]/gm : /^ {0,3}[*+-][ \t]/gm,
  140. counterRxg = (listType === 'ul') ? olRgx : ulRgx,
  141. result = '';
  142. if (list.search(counterRxg) !== -1) {
  143. (function parseCL (txt) {
  144. var pos = txt.search(counterRxg),
  145. style = styleStartNumber(list, listType);
  146. if (pos !== -1) {
  147. // slice
  148. result += '\n\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n';
  149. // invert counterType and listType
  150. listType = (listType === 'ul') ? 'ol' : 'ul';
  151. counterRxg = (listType === 'ul') ? olRgx : ulRgx;
  152. //recurse
  153. parseCL(txt.slice(pos));
  154. } else {
  155. result += '\n\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n';
  156. }
  157. })(list);
  158. } else {
  159. var style = styleStartNumber(list, listType);
  160. result = '\n\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n';
  161. }
  162. return result;
  163. }
  164. // Start of list parsing
  165. var subListRgx = /^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
  166. var mainListRgx = /(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
  167. text = globals.converter._dispatch('lists.before', text, options, globals).getText();
  168. // add sentinel to hack around khtml/safari bug:
  169. // http://bugs.webkit.org/show_bug.cgi?id=11231
  170. text += '¨0';
  171. if (globals.gListLevel) {
  172. text = text.replace(subListRgx, function (wholeMatch, list, m2) {
  173. var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  174. return parseConsecutiveLists(list, listType, true);
  175. });
  176. } else {
  177. text = text.replace(mainListRgx, function (wholeMatch, m1, list, m3) {
  178. var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  179. return parseConsecutiveLists(list, listType, false);
  180. });
  181. }
  182. // strip sentinel
  183. text = text.replace(/¨0/, '');
  184. text = globals.converter._dispatch('makehtml.lists.after', text, options, globals).getText();
  185. return text;
  186. });