Closed ije closed 6 months ago
@pi0 please take a look, i did not add testings for all scenarios, but i think it should work now (may have bugs of course)
md4w also provides a mdToJSON
function to render the markdown to JSON.
const traverse = (node) => {
if (typeof node === "string") {
// text node
console.log(node);
return;
}
// element type
console.log(node.type);
// element attributes (may be undefined)
console.log(node.props);
// element children (may be undefined)
node.children?.forEach(traverse);
};
const tree = mdToJSON("Stay _foolish_, stay **hungry**!");
traverse(tree);
The node type is a number that represents the type of the node. You can import
the NodeType
enum to get the human-readable node type.
import { NodeType } from "md4w";
console.log(NodeType.P); // 9
console.log(NodeType.IMG); // 103
if (node.type === NodeType.IMG) {
console.log("This is an image node, `src` is", node.props.src);
}
All available node types are defined in the
NodeType
enum.
@pi0 ^ FYI, updated docs
Some interesting benchmark results (on small fixture): (would love to chat if you are in discord or x!)
closes #3
wasm change: 52kb -> 55kb