Quick Start
This page gets you from installation to first useful output. If you haven’t installed yet, start with Installation.
Parse HTML
Section titled “Parse HTML”import { parseHtml } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();const tree = parseHtml(encoder.encode('<p>Hello <strong>world</strong></p>'));
console.log(tree.nodeCount); // total nodes in the flat ASTconsole.log(tree.root.kind); // numeric kind id for the Document rootparseHtml accepts a Uint8Array, not a string. Use TextEncoder to convert. It always returns a LexTree and never throws, even on malformed input.
Parse Markdown
Section titled “Parse Markdown”import { parseMarkdown } from '@lanexio/parser-grammar-markdown';
const encoder = new TextEncoder();const tree = parseMarkdown(encoder.encode('# Hello\n\nA **bold** paragraph.'));
console.log(tree.nodeCount);console.log(tree.root.kind);GFM (GitHub Flavored Markdown) extensions are enabled by default. Pass { gfm: false } to restrict to CommonMark only.
Parse JSON
Section titled “Parse JSON”import { parseJson } from '@lanexio/parser-grammar-json';
const encoder = new TextEncoder();const tree = parseJson(encoder.encode('{"key": [1, true, null]}'));
console.log(tree.nodeCount); // total nodes in the flat ASTconsole.log(tree.root.kind); // numeric kind id for the Document rootparseJson implements RFC 8259. It accepts a Uint8Array, never throws, and passes 375/375 JSONTestSuite cases (95 accept, 183 reject, 33 never-throw).
Parse YAML
Section titled “Parse YAML”import { parseYaml } from '@lanexio/parser-grammar-yaml';
const encoder = new TextEncoder();const tree = parseYaml(encoder.encode('key: value\nlist:\n - one\n - two'));
console.log(tree.nodeCount);console.log(tree.root.kind);parseYaml implements YAML 1.2.2 including flow collections ({...}, [...]), anchors, aliases, and tags. 267/268 yaml-test-suite valid cases pass.
Traverse the tree
Section titled “Traverse the tree”import { parseHtml } from '@lanexio/parser-grammar-html';import { HtmlKind } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();const tree = parseHtml(encoder.encode('<ul><li>one</li><li>two</li></ul>'));const cursor = tree.cursor();
do { const node = cursor.current; const src = tree.source; const text = new TextDecoder().decode(src.subarray(node.range.start, node.range.end)); console.log(node.kind, JSON.stringify(text.slice(0, 40)));} while (cursor.gotoFirstChild() || cursor.gotoNextSibling() || cursor.gotoParent());Check for errors
Section titled “Check for errors”import { parseHtml } from '@lanexio/parser-grammar-html';
const encoder = new TextEncoder();const tree = parseHtml(encoder.encode('<p><strong>unclosed'));
for (const node of tree.root.children()) { if (node.hasError) { console.log('LexError at', node.range); }}parseHtml never throws. Malformed input produces LexError nodes in the tree. You can detect and report them, or ignore them, depending on your use case.
Next steps
Section titled “Next steps”- Parsing HTML — options, serialization, kind constants
- Parsing Markdown — GFM options, kind constants
- LexQuery — pattern-based tree queries
- CLI — command-line usage