npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ineentho/remark-shortcodes

v0.3.0

Published

Shortcode parser plugin for Remark

Downloads

74

Readme

remark-shortcodes NPM Package Build Status Coverage Status

A custom Markdown syntax parser for remark that adds support for shortcodes.

What are shortcodes? They are a way to provide hooks for macros and/or template partials inside a markdown file. They consist of start & end blocks, in between which the shortcode has a name defined and an optional set of key:value attributes in HTML style. They can look like this:

The example below uses the default start & end blocks that
this parser ships with:

[[ MailchimpForm id="chfk2" ]]

But they are configurable, so if you're coming from Hugo say,
you can set them as such:

{{< MailchimpForm id="chfk2" >}}

Why use them? Because sometimes you'd like to insert content inline without copy pasting raw HTML in to your Markdown file. The copy paste approach means that hard-to-modify code is littered throughout your content and is therefore very hard to maintain; whereas the shortcode approach can result in a partial being updated in only one place.

Both Wordpress & Hugo have support for shortcodes; this parser's implementation is much closer to Wordpress's, as it does not support inner content or nested shortcodes like Hugo does - but can actually be used for the simpler partials. It was made for my use, but if you'd like to extend it to support more cases, please feel free! Everyone is welcome.

AST Block: Shortcode

Shortcode (Node) is a simple node that has an identifier and an optional object with string values, respresenting the attributes parsed.

interface Shortcode <: Node {
  type: "shortcode";
  identifier: string;
  attributes: { key: string, ...}
}

For example, the following markdown:

[[ MailchimpForm id="chfk2" ]]

Yields:

{
  "type": "shortcode",
  "identifier": "MailchimpForm",
  "attributes": { "id": "chfk2" }
}

Installation

npm:

npm install --save remark-shortcodes

Usage

Say example.js looks as follows:

var unified = require('unified');
var parse = require('remark-parse');
var shortcodes = require('remark-shortcodes');

var markdown = 'Example paragraph\n\n{{> MailchimpForm id="chfk2" <}}'

var tree = unified()
  .use(parse)
  // Plugin inserted below, with custom options for start/end blocks.
  .use(shortcodes, {startBlock: "{{>", endBlock: "<}}"})
  // Turn off position output for legibility below.
  .data('settings', {position: false})
  .parse(markdown);

console.dir(tree, {depth: null});

Running node example yields:

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [{ "type": "text", "value": "Example paragraph" }]
    },
    {
      "type": "shortcode",
      "identifier": "MailchimpForm",
      "attributes": { "id": "chfk2" }
    }
  ]
}

Say example2.js looks as follows:

var unified = require('unified');
var parse = require('remark-parse');
var shortcodes = require('remark-shortcodes');

var ast = {
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [{ "type": "text", "value": "Example paragraph" }]
    },
    {
      "type": "shortcode",
      "identifier": "MailchimpForm",
      "attributes": { "id": "chfk2" }
    }
  ]
};

var tree = unified()
  .use(parse)
  // Plugin inserted below, with custom options for start/end blocks.
  .use(shortcodes, {startBlock: "{{>", endBlock: "<}}"})
  .stringify(ast);

console.log(tree);

Running node example2 yields:

Example paragraph\n\n{{> MailchimpForm id="chfk2" <}}

Say example3.js looks as follows:

var unified = require('unified');
var parse = require('remark-parse');
var shortcodes = require('remark-shortcodes');

var markdown = 'Example paragraph {{> MailchimpForm id="chfk2" <}}'

var tree = unified()
  .use(parse)
  // Plugin inserted below, with custom options for start/end blocks.
  .use(shortcodes, {startBlock: "{{>", endBlock: "<}}", inlineMode: true})
  // Turn off position output for legibility below.
  .data('settings', {position: false})
  .parse(markdown);

console.dir(tree, {depth: null});

Running node example3 yields:

{
  "type": "root",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "Example paragraph "
        },
        {
          "type": "shortcode",
          "identifier": "MailchimpForm",
          "attributes": { "id": "chfk2" }
        }
      ]
    }
  ]
}

API

remark.use(shortcodes, {options})

Where options support the keys:

  • startBlock: the start block to look for. Default: [[.
  • endBlock: the end block to look for. Default: ]].
  • inlineMode: shortcodes will be parsed inline rather than in block mode. Default: false.

NB: Be careful when using custom start/end blocks, your choices may clash with other markdown syntax and/or other remark plugins.

Testing

To run the tests, run:

npm run test-code

To run build, tests & coverage, run:

npm run test

Releasing

TravisCI releases on tag to npm & github releases.

  • Bump version in package.json
  • Commit and tag with version number (git tag -a v0.1.x -m v0.1.x)
  • git push --tags
  • Travis setup handles the rest

License

MIT © Darian Moody

With thanks to woorm et. al for remark.