Compare commits
No commits in common. "328343ca76d892955bd4732c32aff9e72c319a64" and "ef1baea81bb18dba5de6b55d59c0bd0b233c4481" have entirely different histories.
328343ca76
...
ef1baea81b
2 changed files with 30 additions and 95 deletions
|
@ -5,17 +5,7 @@ import { MarkdownParser } from "./md.ts";
|
|||
const ren = new HtmlStrRenderer();
|
||||
|
||||
Deno.test({
|
||||
name: "should skip new line character",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
assertEquals(ren.render(par.parse("\n")), "");
|
||||
assertEquals(ren.render(par.parse("\r\n")), "");
|
||||
assertEquals(ren.render(par.parse("\n\r\n")), "");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse empty ATX header",
|
||||
name: "should parse header",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
const res = par.parse("#");
|
||||
|
@ -24,16 +14,16 @@ Deno.test({
|
|||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse ATX header with text",
|
||||
name: "should parse header with text",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
assertEquals(ren.render(par.parse("# hello")), "<h1>hello</h1>");
|
||||
assertEquals(ren.render(par.parse("# hello#")), "<h1>hello#</h1>");
|
||||
const res = par.parse("# hello");
|
||||
assertEquals(ren.render(res), "<h1>hello</h1>");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse ATX header with specific level",
|
||||
name: "should parse header with specific level",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
assertEquals(ren.render(par.parse("# hello")), "<h1>hello</h1>");
|
||||
|
@ -46,43 +36,11 @@ Deno.test({
|
|||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse ATX header if line contains additional spaces",
|
||||
name: "should parse header if line contains additional spaces",
|
||||
fn: () => {
|
||||
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("\n # hello")), "<h1>hello</h1>");
|
||||
assertEquals(ren.render(par.parse("\r\n # hello")), "<h1>hello</h1>");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse ATX header with closing sequence",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
assertEquals(ren.render(par.parse("# #")), "<h1></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 #")), "<h6>hello</h6>");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "should parse many headers with text",
|
||||
fn: () => {
|
||||
const par = new MarkdownParser();
|
||||
|
||||
const input = `\
|
||||
# hello
|
||||
## world
|
||||
### this is
|
||||
#### my world!`;
|
||||
|
||||
assertEquals(
|
||||
ren.render(par.parse(input)),
|
||||
"<h1>hello</h1><h2>world</h2><h3>this is</h3><h4>my world!</h4>",
|
||||
);
|
||||
},
|
||||
});
|
||||
|
|
55
par/md.ts
55
par/md.ts
|
@ -2,10 +2,7 @@ import { AnyNode, Elem, Fragment, TextNode } from "../core/node.ts";
|
|||
import { isNil } from "../core/utils.ts";
|
||||
import { Parser } from "./types.ts";
|
||||
|
||||
const RE_NEW_LINE = /^\r?\n/;
|
||||
|
||||
const RE_OPEN_ATX_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
|
||||
const RE_CLOSE_ATX_HEADING = /(^|\s+)#*\s*$/;
|
||||
const RE_OPEN_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
|
||||
|
||||
export class MarkdownParser implements Parser {
|
||||
parse(input: string): AnyNode {
|
||||
|
@ -13,49 +10,29 @@ export class MarkdownParser implements Parser {
|
|||
|
||||
let readStr = input;
|
||||
|
||||
while (readStr.trim().length) {
|
||||
{
|
||||
// 1. clear new line character
|
||||
const match = RE_NEW_LINE.exec(readStr);
|
||||
if (!isNil(match)) {
|
||||
readStr = readStr.slice(match[0].length);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. try to find atx heading sequence
|
||||
const match = RE_OPEN_ATX_HEADING.exec(readStr);
|
||||
const match = RE_OPEN_HEADING.exec(readStr);
|
||||
if (!isNil(match)) {
|
||||
readStr = readStr.slice(match[0].length);
|
||||
|
||||
const atxHeading: AstAtxHeading = {
|
||||
kind: AstKind.AtxHeading,
|
||||
console.log({ match });
|
||||
|
||||
const heading: AstHeading = {
|
||||
kind: AstKind.Heading,
|
||||
level: match[1].length as HeadingLevel,
|
||||
content: [],
|
||||
};
|
||||
ast.content.push(atxHeading);
|
||||
ast.content.push(heading);
|
||||
|
||||
if (match[2].length > 0) {
|
||||
const endMatch = RE_CLOSE_ATX_HEADING.exec(readStr);
|
||||
const textContent = readStr.split("\n", 1)[0];
|
||||
readStr = readStr.slice(textContent.length);
|
||||
|
||||
const headingContent = !isNil(endMatch)
|
||||
? readStr.slice(0, endMatch.index)
|
||||
: readStr.includes("\n")
|
||||
? readStr.slice(0, readStr.indexOf("\n") + 1)
|
||||
: readStr;
|
||||
readStr = readStr.slice(
|
||||
headingContent.length + (endMatch?.[0].length ?? 0),
|
||||
);
|
||||
|
||||
if (headingContent.length) {
|
||||
const text: AstText = {
|
||||
kind: AstKind.Text,
|
||||
content: headingContent.trim(),
|
||||
content: textContent,
|
||||
};
|
||||
atxHeading.content.push(text);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
|
||||
heading.content.push(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +40,7 @@ export class MarkdownParser implements Parser {
|
|||
}
|
||||
}
|
||||
|
||||
function Heading(ast: AstAtxHeading): Elem {
|
||||
function Heading(ast: AstHeading): Elem {
|
||||
return new Elem(`h${ast.level}`, {}, ast.content.map(Text));
|
||||
}
|
||||
|
||||
|
@ -74,9 +51,9 @@ function Text(ast: AstText): TextNode {
|
|||
// AST
|
||||
|
||||
type AstDocument = BaseAstItem<AstKind.Document, AstDocumentChild[]>;
|
||||
type AstDocumentChild = AstAtxHeading;
|
||||
type AstDocumentChild = AstHeading;
|
||||
|
||||
interface AstAtxHeading extends BaseAstItem<AstKind.AtxHeading, AstText[]> {
|
||||
interface AstHeading extends BaseAstItem<AstKind.Heading, AstText[]> {
|
||||
level: HeadingLevel;
|
||||
}
|
||||
|
||||
|
@ -91,6 +68,6 @@ interface BaseAstItem<K extends AstKind, Cont> {
|
|||
|
||||
enum AstKind {
|
||||
Document,
|
||||
AtxHeading,
|
||||
Heading,
|
||||
Text,
|
||||
}
|
||||
|
|
Reference in a new issue