包详细信息

remark-github-admonitions-to-directives

incentro-dc30.9kMIT2.1.0

A Remark plugin to convert Github style alerts to admonitions directives.

remark, remark-plugin, markdown, github

自述文件

Remark Github Alerts to Directives

NPM version Build status Test status code style: prettier

💫 Introduction

Github introduced alerts in markdown files with their own proprietary syntax instead of using Remark directives. This plugin converts Github's blockquote alert style to Remark admonitions syntax.

It will transform this:

> [!NOTE]
> Content

Into this:

:::note
Content
:::

💾 Installation

You can install this plugin with:

pnpm add -D remark-github-admonitions-to-directives

🪛 Usage

This plugin is just a generic unified (remark) plugin to transform one syntax into another. Below are some examples of how to use it with various plugins / systems:

📃 With Remark (and Directives)

import { remark } from "remark";
import remarkDirective from "remark-directive";
import remarkGithubAdmonitionsToDirectives from "remark-github-admonitions-to-directives";

const processor = remark()
  .use(remarkGithubAdmonitionsToDirectives)
  .use(remarkDirective);

const result = processor.processSync(`
> [!NOTE]
> content
`);

console.log(result.toString());

// should output:
// :::note
// content
// :::

🦖 With Docusaurus

Admonitions are a core feature of Docusaurus and this plugin was actually built with the use case of reusing markdown files, written with Github's syntax, in Docusaurus.

To use this plugin, just use the instructions for adding MDX plugins and add this plugin to the beforeDefaultRemarkPlugins section of your docusaurus.config.js file:

import remarkGithubAdmonitionsToDirectives from "remark-github-admonitions-to-directives";

export default {
  presets: [
    [
      "@docusaurus/preset-classic",
      {
        docs: {
          path: "docs",
          beforeDefaultRemarkPlugins: [remarkGithubAdmonitionsToDirectives],
        },
      },
    ],
  ],
};

[!IMPORTANT] Because this plugin converts Github's syntax to the directives syntax, and Docusaurus then uses the directives syntax to create the adminitions, this plugin has to be processed before any of the Docusaurus plugins. This is why it's added to the beforeDefaultRemarkPlugins array and not the remarkPlugins array.

⚙️ Customizing the mapping

By default, this plugin will map the Github alerts to the Remark admonitions as follows:

  • NOTE -> note
  • TIP -> tip
  • WARNING -> warning
  • IMPORTANT -> info
  • CAUTION -> danger

If you want to customize this mapping, you can pass an object with the mapping to the plugin:

import { remark } from "remark";
import remarkDirective from "remark-directive";
import remarkGithubAdmonitionsToDirectives, {
  DEFAULT_MAPPING,
  DirectiveName,
  GithubAlertType,
  type AlertTypeMapping,
} from "remark-github-admonitions-to-directives";

const mapping: AlertTypeMapping = {
  ...DEFAULT_MAPPING,
  [GithubAlertType.IMPORTANT]: DirectiveName.WARNING,
};

const processor = remark()
  .use(remarkGithubAdmonitionsToDirectives, { mapping })
  .use(remarkDirective);

const result = processor.processSync(`
> [!IMPORTANT]
> content
`);

console.log(result.toString());

// should output:
// :::info
// content
// :::

🙌 Contributing

This plugin was created and is maintained by Incentro. If you're running into issues, please open an issue. If you want to contribute, please read our contributing guidelines.

更新日志

Changelog

2.0.0 (2024-09-05)

Bug Fixes

  • map GitHub "IMPORTANT" admonition to Docusaurus "info" admonition - fixes #6 (4126c1d)

1.0.5 (2024-04-18)

Features

  • add util to check whether type is valid GitHub alert type (de20d72)
  • simplify code and rename file to function name (2ba5d63)
  • simplify code and return semantically correct type (c66d99c)
  • use new return value in plugin (ddb6eb9)

1.0.4 (2024-03-05)

Bug Fixes

  • loosen dependency ranges (62d46c0)

1.0.3 (2024-03-04)

Bug Fixes

  • fix release process to prevent empty builds (4f9ad07)

1.0.2 (2024-03-04)

Bug Fixes

  • add build output to NPM export (45198f5)

1.0.1 (2024-03-04)

Bug Fixes

  • add dependencies only used for types to dependencies (fb3f53d)
  • change exports order and put types first (4147e4d)
  • remove typo from main export (c7bfe75)
  • type plugin to specify the node type it accepts and returns (8de9dfc)

1.0.0 (2024-03-03)

Features