0.0.1 • Published 9 months ago

@moritzrs/jcast-util-to-json v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

jcast-util-to-json

Build NPM Version NPM Bundle Size NPM Downloads GitHub License

jcast utility to parse jcast syntax tree back into JSON Canvas.

What is this?

This package contains a utility to parse a jcast syntax tree back into a JSON Canvas string. The resulting string can be written to a file and used directly in Obsidian.md.

When to use this

You can use this utility when you want to serialize a modified jcast syntax tree back to jsoncanvas.

If you just want to parse JSON Canvas into a type-safe object, use jsoncanvas instead.

Install

This package is ESM only. In Node.js (version 18+), install with npm:

npm install @moritzrs/jcast-util-to-json

Use

import { readFileSync, writeFileSync } from "fs";
import { visit } from "unist-util-visit";
import { fromJson } from "@moritzrs/jcast-util-from-json";
import { toJson } from "@moritzrs/jcast-util-to-json";

const json = readFileSync("Untitled.canvas", "utf-8");
const tree = fromJson(json);

// Rename all groups numerically
let counter = 0;
visit(tree, "group", (group) => {
	counter += 1;
	group.value = `Group ${counter}`;
});

const modified = toJson(tree);
writeFileSync("Untitled.canvas", modified);

API

This package exports only the identifier toJson. There is no default export.

toJson(tree: Root): string

Parse a jcast syntax tree back into a JSON Canvas string. Due to jcast having a different understanding of groups, it recalculates the geometry of group nodes.

Parameters
  • tree: Root - JCAST syntax tree representing the JSON Canvas.
Returns

Serialized JSON Canvas string.

Types

This package utilized the types from the jcast package.

Related

Contribute

I don't know if i can check PRs in a timely manner, but feel free to open PRs or issues. If participation is high I will need to add a code of conduct and contribution guidelines.

Disclaimer

This package is not affiliated with Obsidian.md or the developers of Obsidian.md.

License

MIT © Moritz R. Schulz