0.1.1 • Published 2 years ago

mdast-util-jaruby v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mdast-util-jaruby

mdast extension to support Japanese ruby.

Feature

  • compatible with latest mdast ecosystem
  • add custom mdast node type (see ruby.d.ts)
  • fully typed
  • ESM only

Install

npm install mdast-util-jaruby

Usage

Markdown => MDAST

import { fromMarkdown } from "mdast-util-from-markdown";
import { jarubyFromMarkdown } from "mdast-util-jaruby";
import { jaruby } from "micromark-extension-jaruby";

const markdown = `
{聖剣}^(エクスカリバー)
`;

console.dir(
  fromMarkdown(markdown, {
    extensions: [jaruby()],
    mdastExtensions: [jarubyFromMarkdown()],
  }),
  { depth: null }
);

generates...

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "ruby",
          "base": "聖剣",
          "text": "エクスカリバー",
          "children": [],
          "data": {
            "hName": "ruby",
            "hChildren": [
              { "type": "text", "value": "聖剣" },
              {
                "type": "element",
                "children": [{ "type": "text", "value": "(" }],
                "tagName": "rp"
              },
              {
                "type": "element",
                "children": [{ "type": "text", "value": "エクスカリバー" }],
                "tagName": "rt"
              },
              {
                "type": "element",
                "children": [{ "type": "text", "value": ")" }],
                "tagName": "rp"
              }
            ]
          },
          "position": {
            "start": { "line": 2, "column": 1, "offset": 1 },
            "end": { "line": 2, "column": 15, "offset": 15 }
          }
        }
      ],
      "position": {
        "start": { "line": 2, "column": 1, "offset": 1 },
        "end": { "line": 2, "column": 15, "offset": 15 }
      }
    }
  ],
  "position": {
    "start": { "line": 1, "column": 1, "offset": 0 },
    "end": { "line": 3, "column": 1, "offset": 16 }
  }
}

MDAST => Markdown

import { fromMarkdown } from "mdast-util-from-markdown";
import { toMarkdown } from "mdast-util-to-markdown";
import { jarubyFromMarkdown, jarubyToMarkdown } from "./index.js";
import { jaruby } from "../micromark-extension-jaruby/index.js";

const mdast = {
  type: "root",
  children: [
    {
      type: "paragraph",
      children: [
        {
          type: "ruby",
          base: "excalibur",
          text: "the holy sword",
          children: [],
          data: {
            hName: "ruby",
            hChildren: [
              { type: "text", value: "excalibur" },
              {
                type: "element",
                children: [{ type: "text", value: "(" }],
                tagName: "rp",
              },
              {
                type: "element",
                children: [{ type: "text", value: "the holy sword" }],
                tagName: "rt",
              },
              {
                type: "element",
                children: [{ type: "text", value: ")" }],
                tagName: "rp",
              },
            ],
          },
          position: {
            start: { line: 1, column: 1, offset: 0 },
            end: { line: 1, column: 29, offset: 28 },
          },
        },
      ],
      position: {
        start: { line: 1, column: 1, offset: 0 },
        end: { line: 1, column: 29, offset: 28 },
      },
    },
  ],
  position: {
    start: { line: 1, column: 1, offset: 0 },
    end: { line: 1, column: 29, offset: 28 },
  },
};

console.dir(
  toMarkdown(mdast, {
    extensions: [jarubyToMarkdown()],
  }),
  { depth: null }
);

generates...

'{excalibur}^(the holy sword)\n'

Note

  • This package is almost a refactoring of remark-ruby. Original package is licensed under MIT License.
  • Support for text delimitation & <rb> tag in original package was omitted, since only few browsers can display it.