par(md): skip empty line...
... instead of clearing new line character
This commit is contained in:
parent
5f1b6f2b1c
commit
9551f3ae1e
2 changed files with 12 additions and 14 deletions
|
@ -7,12 +7,13 @@ const ren = new HtmlStrRenderer();
|
||||||
// Misc
|
// Misc
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "should skip new line character",
|
name: "should skip empty line",
|
||||||
fn: () => {
|
fn: () => {
|
||||||
const par = new MarkdownParser();
|
const par = new MarkdownParser();
|
||||||
assertEquals(ren.render(par.parse("\n")), "");
|
assertEquals(ren.render(par.parse("\n")), "");
|
||||||
assertEquals(ren.render(par.parse("\r\n")), "");
|
assertEquals(ren.render(par.parse("\r\n")), "");
|
||||||
assertEquals(ren.render(par.parse("\n\r\n")), "");
|
assertEquals(ren.render(par.parse("\n\r\n")), "");
|
||||||
|
assertEquals(ren.render(par.parse("\n \n")), "");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -51,13 +52,12 @@ Deno.test({
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "should parse ATX header if line contains additional spaces",
|
name: "should parse ATX header if line contains additional spaces",
|
||||||
|
only: true,
|
||||||
fn: () => {
|
fn: () => {
|
||||||
const par = new MarkdownParser();
|
const par = new MarkdownParser();
|
||||||
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
||||||
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
||||||
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
assertEquals(ren.render(par.parse(" # hello")), "<h1>hello</h1>");
|
||||||
assertEquals(ren.render(par.parse("\n # hello")), "<h1>hello</h1>");
|
|
||||||
assertEquals(ren.render(par.parse("\r\n # hello")), "<h1>hello</h1>");
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
20
par/md.ts
20
par/md.ts
|
@ -2,7 +2,7 @@ import { AnyNode, Elem, Fragment, TextNode } from "../core/node.ts";
|
||||||
import { isNil } from "../core/utils.ts";
|
import { isNil } from "../core/utils.ts";
|
||||||
import { Parser } from "./types.ts";
|
import { Parser } from "./types.ts";
|
||||||
|
|
||||||
const RE_NEW_LINE = /^\r?\n/;
|
const RE_EMPTY_LINE = /^\s*$/;
|
||||||
|
|
||||||
const RE_OPEN_ATX_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
|
const RE_OPEN_ATX_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
|
||||||
const RE_CLOSE_ATX_HEADING = /(^|\s+)#*\s*$/;
|
const RE_CLOSE_ATX_HEADING = /(^|\s+)#*\s*$/;
|
||||||
|
@ -12,17 +12,9 @@ export class MarkdownParser implements Parser {
|
||||||
const astDoc: AstDocument = { kind: AstKind.Document, content: [] };
|
const astDoc: AstDocument = { kind: AstKind.Document, content: [] };
|
||||||
|
|
||||||
let readStr = input;
|
let readStr = input;
|
||||||
|
|
||||||
while (readStr.length) {
|
while (readStr.length) {
|
||||||
// 1. clear new line character
|
const newReadStr = skipEmptyLine(readStr) ??
|
||||||
const match = RE_NEW_LINE.exec(readStr);
|
parseAtxHeading(astDoc, readStr) ??
|
||||||
if (!isNil(match)) {
|
|
||||||
readStr = readStr.slice(match[0].length);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. try to find atx heading sequence
|
|
||||||
const newReadStr = parseAtxHeading(astDoc, readStr) ??
|
|
||||||
parseParagraph(astDoc, readStr);
|
parseParagraph(astDoc, readStr);
|
||||||
if (isNil(newReadStr)) break;
|
if (isNil(newReadStr)) break;
|
||||||
readStr = newReadStr;
|
readStr = newReadStr;
|
||||||
|
@ -55,6 +47,12 @@ function Text(ast: AstText): TextNode {
|
||||||
|
|
||||||
// parse utils
|
// parse utils
|
||||||
|
|
||||||
|
function skipEmptyLine(readStr: string): string | null {
|
||||||
|
const match = RE_EMPTY_LINE.exec(readStr);
|
||||||
|
if (isNil(match)) return null;
|
||||||
|
return readStr.slice(match[0].length);
|
||||||
|
}
|
||||||
|
|
||||||
function parseAtxHeading(ast: AstDocument, readStr: string): string | null {
|
function parseAtxHeading(ast: AstDocument, readStr: string): string | null {
|
||||||
const match = RE_OPEN_ATX_HEADING.exec(readStr);
|
const match = RE_OPEN_ATX_HEADING.exec(readStr);
|
||||||
if (isNil(match)) return null;
|
if (isNil(match)) return null;
|
||||||
|
|
Reference in a new issue