Git Product home page Git Product logo

markdown-to-djot-ast's Introduction

markdown-to-djot-ast

markdown-to-djot-ast is a lightweight Javascript/Typescript library to convert Markdown content into a djot.js AST.

This is useful for supporting Djot and Markdown files, offering consistent filtering and rendering processes across formats.

The library leverages the famous markdown-it package to parse Markdown content to tokens. The tokens are subsequently transformed into a Djot.js AST.

Install

npm install markdown-to-djot-ast

Use

import { readFileSync } from "fs";
import * as path from "path";
import markdownit from "markdown-it";
import { parseMarkdown } from "markdown-to-djot-ast";
import { parse as parseDjot, renderAST } from "@djot/djot";

const md = markdownit();
const input = readFileSync(filename, "utf8");

// Parse `*.md` files with markdown-it and `*.dj` files with djot.js:
const doc =
  path.extname(filename) === ".md"
    ? parseMarkdown(md, input, { sourcePositions: true })
    : parseDjot(input, { sourcePositions: true });

// Process Markdown and Djot in the same way, f.ex:
console.log(renderAST(doc));

Supported markdown-it plugins

Custom token handler

You can provide a token handler to parseMarkdown to support custom markdown-it plugins.

parseMarkdown(md, input, {
  tokenHandlers: { mytoken_open: (token) => ({ tag: "div", children: [] }) },
});

Handling of unsupported tokens

Unknown tokens will be converted into a Div Djot node with a warning:

// Print all warnings to the console
parseMarkdown(md, input, {
  warn: console.warn,
});

markdown-to-djot-ast's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.