ije / md4w

A Markdown renderer written in Zig & C, compiled to WebAssymbly.
MIT License
58 stars 0 forks source link

render to json #4

Closed ije closed 6 months ago

ije commented 6 months ago

closes #3

wasm change: 52kb -> 55kb

ije commented 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)

ije commented 6 months ago

Rendering to JSON

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);

Node Type

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.

ije commented 6 months ago

@pi0 ^ FYI, updated docs

pi0 commented 6 months ago

Some interesting benchmark results (on small fixture): (would love to chat if you are in discord or x!)

image