Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render to json #4

Merged
merged 23 commits into from
Feb 19, 2024
Merged

render to json #4

merged 23 commits into from
Feb 19, 2024

Conversation

ije
Copy link
Owner

@ije ije commented Feb 16, 2024

closes #3

wasm change: 52kb -> 55kb

@ije ije changed the title wip render to json Feb 16, 2024
@ije ije mentioned this pull request Feb 16, 2024
@pi0 pi0 mentioned this pull request Feb 16, 2024
14 tasks
@ije ije marked this pull request as ready for review February 18, 2024 01:16
@ije
Copy link
Owner Author

ije commented Feb 18, 2024

@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
Copy link
Owner Author

ije commented Feb 18, 2024

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
Copy link
Owner Author

ije commented Feb 18, 2024

@pi0 ^ FYI, updated docs

@ije ije merged commit 2303989 into main Feb 19, 2024
1 check passed
@ije ije deleted the md-to-json branch February 19, 2024 13:19
@pi0
Copy link
Collaborator

pi0 commented Feb 19, 2024

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exposing parse utils
2 participants