very-small-parser
Version:
A very small Markdown, HTML, and CSS parser.
43 lines (42 loc) • 1.18 kB
JavaScript
import { Parser } from '../Parser';
import { first, loop0 } from '../util';
import { el } from './parsers';
export class HtmlParser extends Parser {
first;
constructor(opts) {
super(opts);
this.first = first(this.parsers);
}
parse(src) {
const children = [];
const end = src.length;
let remaining = src;
let length = 0;
while (length < end) {
const tok = this.first(this, remaining);
if (!tok) {
const textToken = {
type: 'text',
value: remaining[0],
len: 1,
};
children.push(textToken);
length += 1;
remaining = remaining.slice(1);
continue;
}
children.push(tok);
length += tok.len || 0;
remaining = remaining.slice(tok.len);
}
return children;
}
parsef(src) {
const [children, len] = loop0(this, this.first, src);
const root = { type: 'root', children, len };
return root;
}
el(src) {
return el(this, src);
}
}