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