Skip to content

Commit

Permalink
list match: fix indentation on nested lists
Browse files Browse the repository at this point in the history
Nested lists use some special rules to allow for inserting another
list after a newline. Unfortunately, do to the current text rule,
the space(s) before the nested list can be added to the text rule
from the first line, rather than the start of the nested list.
This makes further nested items look double-nested, when they
are not.

For 1.0, we should consider changing the text rule to break on
newlines, or change how lookbehinds work (let them "move" text
from previous captures?).

Fixes #48
  • Loading branch information
ariabuckles committed Mar 11, 2018
1 parent cb2dca0 commit 3e926f5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
32 changes: 32 additions & 0 deletions __tests__/simple-markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,38 @@ describe("simple markdown", function() {
],
type: "list",
}]);

var parsed = blockParse(
" * hi\n" +
" * bye\n" +
" * there\n\n"
);
validateParse(parsed, [{
ordered: false,
start: undefined,
items: [
[{
content: 'hi\n ', // NOTE(aria): The extra space here is
type: 'text', // weird and we should consider fixing
},
{
ordered: false,
start: undefined,
items: [
[{
content: "bye",
type: "text",
}],
[{
content: "there",
type: "text",
}],
],
type: "list",
}]
],
type: "list",
}]);
});

it("should parse loose lists", function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-markdown",
"version": "0.3.2",
"version": "0.3.3",
"description": "Javascript markdown parsing, made simple",
"main": "simple-markdown.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions simple-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ var LIST_R = new RegExp(
// lists, where our content might end before we receive two `\n`s
"|\\s*\n*$)"
);
var LIST_LOOKBEHIND_R = /^$|\n *$/;
var LIST_LOOKBEHIND_R = /(?:^|\n)( *)$/;

var TABLES = (function() {
// predefine regexes so we don't have to create them inside functions
Expand Down Expand Up @@ -725,10 +725,12 @@ var defaultRules = {
// lists can be inline, because they might be inside another list,
// in which case we can parse with inline scope, but need to allow
// nested lists inside this inline scope.
var isStartOfLine = LIST_LOOKBEHIND_R.test(prevCapture);
var isStartOfLineCapture = LIST_LOOKBEHIND_R.exec(prevCapture);
var isListBlock = state._list || !state.inline;

if (isStartOfLine && isListBlock) {
if (isStartOfLineCapture && isListBlock) {
source = isStartOfLineCapture[1] + source;
var res = LIST_R.exec(source);
return LIST_R.exec(source);
} else {
return null;
Expand Down
Loading

0 comments on commit 3e926f5

Please sign in to comment.