par(md): fix parse many paragraphs

This commit is contained in:
Dmitriy Pleshevskiy 2022-06-13 22:54:39 +03:00
parent e8c6ce97c6
commit e909e21d7a
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
2 changed files with 64 additions and 8 deletions

View File

@ -106,11 +106,42 @@ Deno.test({
fn: () => {
const par = new MarkdownParser();
const input = `\
assertEquals(
ren.render(par.parse(`\
hello
world`;
world`)),
"<p>hello world</p>",
);
},
});
assertEquals(ren.render(par.parse(input)), "<p>hello world</p>");
Deno.test({
name: "should parse many big paragraphs",
fn: () => {
const par = new MarkdownParser();
assertEquals(
ren.render(par.parse(`\
hello
world`)),
"<p>hello world</p>",
);
assertEquals(
ren.render(par.parse(`\
Lorem Ipsum is simply dummy text of the printing
and typesetting industry.
Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s, when an unknown printer
took a galley of type and scrambled it to make a
type specimen book.
It has survived not only five centuries, but also
the leap into electronic typesetting, remaining
essentially unchanged.`)),
"<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>",
);
},
});
@ -258,3 +289,27 @@ Deno.test({
);
},
});
// Doc
Deno.test({
name: "should parse all document",
only: true,
fn: () => {
const par = new MarkdownParser();
const content = `\
# What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
- Sed in orci non lorem luctus dictum ac vel justo.
- Nunc non leo vel dolor fringilla imperdiet.
- Proin finibus ipsum quis molestie porta.`;
assertEquals(
ren.render(par.parse(content)),
"<h1>What is Lorem Ipsum?</h1><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p><ul><li>Sed in orci non lorem luctus dictum ac vel justo.</li><li>Nunc non leo vel dolor fringilla imperdiet.</li><li>Proin finibus ipsum quis molestie porta.</li></ul>",
);
},
});

View File

@ -2,7 +2,7 @@ import { AnyNode, Elem, Fragment, TextNode } from "../core/node.ts";
import { isNil, Nilable } from "../core/utils.ts";
import { Parser } from "./types.ts";
const RE_EMPTY_LINE = /^\s*$/;
const RE_EMPTY_LINE = /^[ \t]*\r?\n/;
const RE_OPEN_ATX_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
const RE_CLOSE_ATX_HEADING = /(^|\s+)#*\s*$/;
@ -166,19 +166,20 @@ function parseParagraph(
kind: AstKind.Paragraph,
content: [],
};
ast.content.push(paragraph);
let paragraphInlineContent = "";
while (!RE_EMPTY_LINE.test(readStr)) {
while (readStr && !RE_EMPTY_LINE.test(readStr)) {
const listMatch = RE_LIST_ITEM.exec(readStr);
if (!isNil(listMatch)) break;
paragraphInlineContent += readStr.includes("\n")
const newPart = readStr.includes("\n")
? readStr.slice(0, readStr.indexOf("\n") + 1)
: readStr;
readStr = readStr.slice(paragraphInlineContent.length);
paragraphInlineContent += newPart;
readStr = readStr.slice(newPart.length);
}
if (paragraphInlineContent.length) {
ast.content.push(paragraph);
parseInlineContent(paragraph, paragraphInlineContent);
}