1.0.1 • Published 7 months ago

remark-taggable v1.0.1

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

remark-taggable

npm.io license

Plugin for remark to support generalized taggables like #tag and @user. Relies on micromark-extension-taggable for tokenization and mdast-util-taggable for converting markdown to/from abstract syntax trees.

Install

Install remark-taggable on npm.

npm install remark-taggable

Usage

const unified = require("unified");
const markdown = require("remark-parse");
const taggablePlugin = require("remark-taggable");

let processor = unified().use(markdown).use(taggablePlugin);

// Or use with options

let processor = unified()
  .use(markdown)
  .use(citePlugin, {
    classes: ["micromark-taggable"],
    rules: [
      {
        marker: "!",
        type: "warn",
        toUrl: (val) => `/warnings/${val}`,
        classes: ["warning"],
      },
      {
        marker: "$",
        type: "variable",
        toUrl: (val) => `/variables/${val}`,
        classes: ["variable"],
      },
    ],
    allowEmail: false,
  });

Running the processor on the following markdown (without options being defined):

This is a #hashtag and this is a @mentionedUser.

Will produce the following taggable node:

{
  "type": "paragraph",
  "children": [
    {
      "type": "text",
      "value": "This is a "
    },
    {
      "type": "taggable",
      "value": "hashtag",
      "data": {
        "type": "tag",
        "marker": "#",
        "url": "/tags/hashtag"
      }
    },
    {
      "type": "text",
      "value": "This is a "
    },
    {
      "type": "taggable",
      "value": "mentionedUser",
      "data": {
        "type": "mention",
        "marker": "@",
        "url": "/users/mentionedUser"
      }
    },
    {
      "type": "text",
      "value": "."
    }
  ]
}

Configuration

For details about the syntax tree structure, see mdast-util-taggable. For details about the configuration and supported syntax, see micromark-extension-cite.

Credits

Big shoutout to @benrbray's repositry @benrbray/remark-cite for inspiration and for help as a template for this plugin. I would highly recommend it in case you need citation support for remark.