144 lines
3.5 KiB
TypeScript
144 lines
3.5 KiB
TypeScript
import { AnyNode, Elem, Fragment, TextNode } from "../core/node.ts";
|
|
import { isNil } from "../core/utils.ts";
|
|
import { Parser } from "./types.ts";
|
|
|
|
const RE_EMPTY_LINE = /^\s*$/;
|
|
|
|
const RE_OPEN_ATX_HEADING = /^\s{0,3}(#{1,6})(\s|$)/;
|
|
const RE_CLOSE_ATX_HEADING = /(^|\s+)#*\s*$/;
|
|
|
|
export class MarkdownParser implements Parser {
|
|
parse(input: string): AnyNode {
|
|
const astDoc: AstDocument = { kind: AstKind.Document, content: [] };
|
|
|
|
let readStr = input;
|
|
while (readStr.length) {
|
|
const newReadStr = skipEmptyLine(readStr) ??
|
|
parseAtxHeading(astDoc, readStr) ??
|
|
parseParagraph(astDoc, readStr);
|
|
if (isNil(newReadStr)) break;
|
|
readStr = newReadStr;
|
|
}
|
|
|
|
return new Fragment(astDoc.content.map(DocChild));
|
|
}
|
|
}
|
|
|
|
function DocChild(content: AstDocumentChild): Elem {
|
|
switch (content.kind) {
|
|
case AstKind.AtxHeading:
|
|
return Heading(content);
|
|
case AstKind.Paragraph:
|
|
return Paragraph(content);
|
|
}
|
|
}
|
|
|
|
function Heading(ast: AstAtxHeading): Elem {
|
|
return new Elem(`h${ast.level}`, {}, ast.content.map(Text));
|
|
}
|
|
|
|
function Paragraph(ast: AstParagraph): Elem {
|
|
return new Elem("p", {}, ast.content.map(Text));
|
|
}
|
|
|
|
function Text(ast: AstText): TextNode {
|
|
return new TextNode(ast.content);
|
|
}
|
|
|
|
// 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 {
|
|
const match = RE_OPEN_ATX_HEADING.exec(readStr);
|
|
if (isNil(match)) return null;
|
|
|
|
readStr = readStr.slice(match[0].length);
|
|
|
|
const atxHeading: AstAtxHeading = {
|
|
kind: AstKind.AtxHeading,
|
|
level: match[1].length as HeadingLevel,
|
|
content: [],
|
|
};
|
|
ast.content.push(atxHeading);
|
|
|
|
if (match[2].length === 0) return readStr;
|
|
|
|
const endMatch = RE_CLOSE_ATX_HEADING.exec(readStr);
|
|
|
|
const headingInlineContent = !isNil(endMatch)
|
|
? readStr.slice(0, endMatch.index)
|
|
: readStr.includes("\n")
|
|
? readStr.slice(0, readStr.indexOf("\n") + 1)
|
|
: readStr;
|
|
|
|
parseInlineContent(atxHeading, headingInlineContent);
|
|
|
|
return readStr.slice(
|
|
headingInlineContent.length + (endMatch?.[0].length ?? 0),
|
|
);
|
|
}
|
|
|
|
function parseParagraph(ast: AstDocument, readStr: string): string | null {
|
|
if (!readStr.length) return null;
|
|
|
|
const paragraph: AstParagraph = {
|
|
kind: AstKind.Paragraph,
|
|
content: [],
|
|
};
|
|
ast.content.push(paragraph);
|
|
|
|
const paragraphInlineContent = readStr.includes("\n")
|
|
? readStr.slice(0, readStr.indexOf("\n") + 1)
|
|
: readStr;
|
|
|
|
parseInlineContent(paragraph, paragraphInlineContent);
|
|
|
|
return readStr.slice(paragraphInlineContent.length);
|
|
}
|
|
|
|
function parseInlineContent(
|
|
ast: AstAtxHeading | AstParagraph,
|
|
readStr: string,
|
|
): string | null {
|
|
if (!readStr.length) return null;
|
|
|
|
const text: AstText = {
|
|
kind: AstKind.Text,
|
|
content: readStr.trim(),
|
|
};
|
|
ast.content.push(text);
|
|
|
|
return readStr;
|
|
}
|
|
|
|
// AST
|
|
|
|
type AstDocument = BaseAstItem<AstKind.Document, AstDocumentChild[]>;
|
|
type AstDocumentChild = AstAtxHeading | AstParagraph;
|
|
|
|
interface AstAtxHeading extends BaseAstItem<AstKind.AtxHeading, AstText[]> {
|
|
level: HeadingLevel;
|
|
}
|
|
|
|
type AstParagraph = BaseAstItem<AstKind.Paragraph, AstText[]>;
|
|
|
|
type AstText = BaseAstItem<AstKind.Text, string>;
|
|
|
|
type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
|
|
interface BaseAstItem<K extends AstKind, Cont> {
|
|
kind: K;
|
|
content: Cont;
|
|
}
|
|
|
|
enum AstKind {
|
|
Document,
|
|
AtxHeading,
|
|
Paragraph,
|
|
Text,
|
|
}
|