diff --git a/par/md.test.ts b/par/md.test.ts index 46dbf84..e451967 100644 --- a/par/md.test.ts +++ b/par/md.test.ts @@ -106,11 +106,42 @@ Deno.test({ fn: () => { const par = new MarkdownParser(); - const input = `\ + assertEquals( + ren.render(par.parse(`\ hello -world`; +world`)), + "

hello world

", + ); + }, +}); - assertEquals(ren.render(par.parse(input)), "

hello world

"); +Deno.test({ + name: "should parse many big paragraphs", + fn: () => { + const par = new MarkdownParser(); + + assertEquals( + ren.render(par.parse(`\ +hello +world`)), + "

hello world

", + ); + + 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.`)), + "

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.

", + ); }, }); @@ -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)), + "

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

", + ); + }, +}); diff --git a/par/md.ts b/par/md.ts index 78987fc..276bd8f 100644 --- a/par/md.ts +++ b/par/md.ts @@ -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); }