lists.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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);
  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)?(^[ \t]*)([*+-]|\d+[.])[ \t]+((\[(x| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
  41. isParagraphed = (/\n[ \t]*\n(?!~0)/.test(listStr));
  42. listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) {
  43. checked = (checked && checked.trim() !== '');
  44. var item = showdown.subParser('outdent')(m4, options, globals),
  45. bulletStyle = '';
  46. // Support for github tasklists
  47. if (taskbtn && options.tasklists) {
  48. bulletStyle = ' class="task-list-item" style="list-style-type: none;"';
  49. item = item.replace(/^[ \t]*\[(x| )?]/m, function () {
  50. var otp = '<input type="checkbox" disabled style="margin: 0px 0.35em 0.25em -1.6em; vertical-align: middle;"';
  51. if (checked) {
  52. otp += ' checked';
  53. }
  54. otp += '>';
  55. return otp;
  56. });
  57. }
  58. // m1 - Leading line or
  59. // Has a double return (multi paragraph) or
  60. // Has sublist
  61. if (m1 || (item.search(/\n{2,}/) > -1)) {
  62. item = showdown.subParser('githubCodeBlocks')(item, options, globals);
  63. item = showdown.subParser('blockGamut')(item, options, globals);
  64. } else {
  65. // Recursion for sub-lists:
  66. item = showdown.subParser('lists')(item, options, globals);
  67. item = item.replace(/\n$/, ''); // chomp(item)
  68. if (isParagraphed) {
  69. item = showdown.subParser('paragraphs')(item, options, globals);
  70. } else {
  71. item = showdown.subParser('spanGamut')(item, options, globals);
  72. }
  73. }
  74. item = '\n<li' + bulletStyle + '>' + item + '</li>\n';
  75. return item;
  76. });
  77. // attacklab: strip sentinel
  78. listStr = listStr.replace(/~0/g, '');
  79. globals.gListLevel--;
  80. if (trimTrailing) {
  81. listStr = listStr.replace(/\s+$/, '');
  82. }
  83. return listStr;
  84. }
  85. /**
  86. * Check and parse consecutive lists (better fix for issue #142)
  87. * @param {string} list
  88. * @param {string} listType
  89. * @param {boolean} trimTrailing
  90. * @returns {string}
  91. */
  92. function parseConsecutiveLists(list, listType, trimTrailing) {
  93. // check if we caught 2 or more consecutive lists by mistake
  94. // we use the counterRgx, meaning if listType is UL we look for UL and vice versa
  95. var counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm,
  96. subLists = [],
  97. result = '';
  98. if (list.search(counterRxg) !== -1) {
  99. (function parseCL(txt) {
  100. var pos = txt.search(counterRxg);
  101. if (pos !== -1) {
  102. // slice
  103. result += '\n\n<' + listType + '>' + processListItems(txt.slice(0, pos), !!trimTrailing) + '</' + listType + '>\n\n';
  104. // invert counterType and listType
  105. listType = (listType === 'ul') ? 'ol' : 'ul';
  106. counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm;
  107. //recurse
  108. parseCL(txt.slice(pos));
  109. } else {
  110. result += '\n\n<' + listType + '>' + processListItems(txt, !!trimTrailing) + '</' + listType + '>\n\n';
  111. }
  112. })(list);
  113. for (var i = 0; i < subLists.length; ++i) {
  114. }
  115. } else {
  116. result = '\n\n<' + listType + '>' + processListItems(list, !!trimTrailing) + '</' + listType + '>\n\n';
  117. }
  118. return result;
  119. }
  120. // attacklab: add sentinel to hack around khtml/safari bug:
  121. // http://bugs.webkit.org/show_bug.cgi?id=11231
  122. text += '~0';
  123. // Re-usable pattern to match any entire ul or ol list:
  124. var wholeList = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
  125. if (globals.gListLevel) {
  126. text = text.replace(wholeList, function (wholeMatch, list, m2) {
  127. var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  128. return parseConsecutiveLists(list, listType, true);
  129. });
  130. } else {
  131. wholeList = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
  132. //wholeList = /(\n\n|^\n?)( {0,3}([*+-]|\d+\.)[ \t]+[\s\S]+?)(?=(~0)|(\n\n(?!\t| {2,}| {0,3}([*+-]|\d+\.)[ \t])))/g;
  133. text = text.replace(wholeList, function (wholeMatch, m1, list, m3) {
  134. var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
  135. return parseConsecutiveLists(list, listType);
  136. });
  137. }
  138. // attacklab: strip sentinel
  139. text = text.replace(/~0/, '');
  140. text = globals.converter._dispatch('lists.after', text, options);
  141. return text;
  142. });