/** * Form HTML ordered (numbered) and unordered (bulleted) lists. */ showdown.subParser('lists', function (text, options, globals) { 'use strict'; text = globals.converter._dispatch('lists.before', text, options, globals); /** * Process the contents of a single ordered or unordered list, splitting it * into individual list items. * @param {string} listStr * @param {boolean} trimTrailing * @returns {string} */ function processListItems (listStr, trimTrailing) { // The $g_list_level global keeps track of when we're inside a list. // Each time we enter a list, we increment it; when we leave a list, // we decrement. If it's zero, we're not in a list anymore. // // We do this because when we're not inside a list, we want to treat // something like this: // // I recommend upgrading to version // 8. Oops, now this line is treated // as a sub-list. // // As a single paragraph, despite the fact that the second line starts // with a digit-period-space sequence. // // Whereas when we're inside a list (or sub-list), that line will be // treated as the start of a sub-list. What a kludge, huh? This is // an aspect of Markdown's syntax that's hard to parse perfectly // without resorting to mind-reading. Perhaps the solution is to // change the syntax rules such that sub-lists must start with a // starting cardinal number; e.g. "1." or "a.". globals.gListLevel++; // trim trailing blank lines: listStr = listStr.replace(/\n{2,}$/, '\n'); // attacklab: add sentinel to emulate \z listStr += '~0'; var rgx = /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm, isParagraphed = (/\n[ \t]*\n(?!~0)/.test(listStr)); listStr = listStr.replace(rgx, function (wholeMatch, m1, m2, m3, m4, taskbtn, checked) { checked = (checked && checked.trim() !== ''); var item = showdown.subParser('outdent')(m4, options, globals), bulletStyle = ''; // Support for github tasklists if (taskbtn && options.tasklists) { bulletStyle = ' class="task-list-item" style="list-style-type: none;"'; item = item.replace(/^[ \t]*\[(x|X| )?]/m, function () { var otp = ' -1)) { item = showdown.subParser('githubCodeBlocks')(item, options, globals); item = showdown.subParser('blockGamut')(item, options, globals); } else { // Recursion for sub-lists: item = showdown.subParser('lists')(item, options, globals); item = item.replace(/\n$/, ''); // chomp(item) if (isParagraphed) { item = showdown.subParser('paragraphs')(item, options, globals); } else { item = showdown.subParser('spanGamut')(item, options, globals); } } item = '\n' + item + '\n'; return item; }); // attacklab: strip sentinel listStr = listStr.replace(/~0/g, ''); globals.gListLevel--; if (trimTrailing) { listStr = listStr.replace(/\s+$/, ''); } return listStr; } /** * Check and parse consecutive lists (better fix for issue #142) * @param {string} list * @param {string} listType * @param {boolean} trimTrailing * @returns {string} */ function parseConsecutiveLists(list, listType, trimTrailing) { // check if we caught 2 or more consecutive lists by mistake // we use the counterRgx, meaning if listType is UL we look for UL and vice versa var counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm, subLists = [], result = ''; if (list.search(counterRxg) !== -1) { (function parseCL(txt) { var pos = txt.search(counterRxg); if (pos !== -1) { // slice result += '\n<' + listType + '>' + processListItems(txt.slice(0, pos), !!trimTrailing) + '\n'; // invert counterType and listType listType = (listType === 'ul') ? 'ol' : 'ul'; counterRxg = (listType === 'ul') ? /^ {0,2}\d+\.[ \t]/gm : /^ {0,2}[*+-][ \t]/gm; //recurse parseCL(txt.slice(pos)); } else { result += '\n<' + listType + '>' + processListItems(txt, !!trimTrailing) + '\n'; } })(list); for (var i = 0; i < subLists.length; ++i) { } } else { result = '\n<' + listType + '>' + processListItems(list, !!trimTrailing) + '\n'; } return result; } // add sentinel to hack around khtml/safari bug: // http://bugs.webkit.org/show_bug.cgi?id=11231 text += '~0'; // Re-usable pattern to match any entire ul or ol list: var wholeList = /^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm; if (globals.gListLevel) { text = text.replace(wholeList, function (wholeMatch, list, m2) { var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol'; return parseConsecutiveLists(list, listType, true); }); } else { wholeList = /(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm; text = text.replace(wholeList, function (wholeMatch, m1, list, m3) { var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol'; return parseConsecutiveLists(list, listType); }); } // strip sentinel text = text.replace(/~0/, ''); text = globals.converter._dispatch('lists.after', text, options, globals); return text; });