Skip to content

Commit

Permalink
fix(headings): inconsistent behavior in lists
Browse files Browse the repository at this point in the history
In text, headings only require a single linebreak to be treated as such.
However, in lists, they would require a double linebreak.
Now, the behavior in lists and text is consistent, requiring only a single
linebreak.

Closes #495
  • Loading branch information
tivie committed Sep 15, 2018
1 parent d9eea64 commit 26abc7a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 deletions.
50 changes: 29 additions & 21 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

43 changes: 28 additions & 15 deletions src/subParsers/makehtml/lists.js
Expand Up @@ -46,6 +46,7 @@ showdown.subParser('makehtml.lists', function (text, options, globals) {
// Since version 1.5, nesting sublists requires 4 spaces (or 1 tab) indentation,
// which is a syntax breaking change
// activating this option reverts to old behavior
// This will be removed in version 2.0
if (options.disableForced4SpacesIndentedSublists) {
rgx = /(\n)?(^ {0,3})([*+-]|\d+[.])[ \t]+((\[(x|X| )?])?[ \t]*[^\r]+?(\n{1,2}))(?=\n*(¨0|\2([*+-]|\d+[.])[ \t]+))/gm;
}
Expand Down Expand Up @@ -81,20 +82,33 @@ showdown.subParser('makehtml.lists', function (text, options, globals) {
return '¨A' + wm2;
});

// SPECIAL CASE: an heading followed by a paragraph of text that is not separated by a double newline
// or/nor indented. ex:
//
// - # foo
// bar is great
//
// While this does now follow the spec per se, not allowing for this might cause confusion since
// header blocks don't need double newlines after
if (/^#+.+\n.+/.test(item)) {
item = item.replace(/^(#+.+)$/m, '$1\n');
}

// m1 - Leading line or
// Has a double return (multi paragraph) or
// Has sublist
// Has a double return (multi paragraph)
if (m1 || (item.search(/\n{2,}/) > -1)) {
item = showdown.subParser('makehtml.githubCodeBlocks')(item, options, globals);
item = showdown.subParser('makehtml.blockGamut')(item, options, globals);
} else {

// Recursion for sub-lists:
item = showdown.subParser('makehtml.lists')(item, options, globals);
item = item.replace(/\n$/, ''); // chomp(item)
item = showdown.subParser('makehtml.hashHTMLBlocks')(item, options, globals);

// Colapse double linebreaks
item = item.replace(/\n\n+/g, '\n\n');

if (isParagraphed) {
item = showdown.subParser('makehtml.paragraphs')(item, options, globals);
} else {
Expand Down Expand Up @@ -174,26 +188,25 @@ showdown.subParser('makehtml.lists', function (text, options, globals) {
return result;
}

/** Start of list parsing **/
// Start of list parsing
var subListRgx = /^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
var mainListRgx = /(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;

text = globals.converter._dispatch('lists.before', text, options, globals).getText();
// add sentinel to hack around khtml/safari bug:
// http://bugs.webkit.org/show_bug.cgi?id=11231
text += '¨0';

if (globals.gListLevel) {
text = text.replace(/^(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
function (wholeMatch, list, m2) {
var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, true);
}
);
text = text.replace(subListRgx, function (wholeMatch, list, m2) {
var listType = (m2.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, true);
});
} else {
text = text.replace(/(\n\n|^\n?)(( {0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(¨0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm,
function (wholeMatch, m1, list, m3) {
var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, false);
}
);
text = text.replace(mainListRgx, function (wholeMatch, m1, list, m3) {
var listType = (m3.search(/[*+-]/g) > -1) ? 'ul' : 'ol';
return parseConsecutiveLists(list, listType, false);
});
}

// strip sentinel
Expand Down
@@ -0,0 +1,12 @@
<ul>
<li>Increase the number of water changes.</li>
<li><h1 id="proteinskimmers">Protein skimmers:</h1>
<p>This remove dissolved</p></li>
<li><h1 id="chemicalfiltermedia">Chemical filter media:</h1>
<p>When placed in your filter</p></li>
<li>#</li>
<li>something</li>
<li>#
something</li>
<li># something</li>
</ul>
@@ -0,0 +1,10 @@
- Increase the number of water changes.
- # Protein skimmers:
This remove dissolved
- # Chemical filter media:
When placed in your filter
- #
- something
- #
something
- \# something

0 comments on commit 26abc7a

Please sign in to comment.