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

markdown-it-shortcode-tag

v1.1.0

Published

shortcodes for markdown-it in HTML5 tag format

Downloads

390

Readme

markdown-it-shortcode-tag

Build Status

With this plugin you can define shortcodes for arbitrary content to be rendered when parsing markdown. The shortcodes take the form of self-closing HTML5 tags:

<custom first second="string" third=3.7 fourth=#{myvar} >

You cannot write closing tags and consequently no inner content. Attributes are either interpolated using a marker #{ } or passed directly to a rendering function you define for every tag to be interpreted:

const indirect = shortcodes.custom.interpolator("myvar", env)

shortcodes.custom.render({
    first: true,
    second: "string",
    third: 3.7
    fourth: indirect
}, env)

html must be enabled in markdown-it options. Otherwise, the tag will be rendered in escaped form. With this option, unknown tags (or all, if the plugin is not active) are passed through. This means that, as long as the tag is not defined in HTML5, it is ignored by browsers.

Installation

$ npm install markdown-it-shortcode-tag --save

API

var shortcodes = {
    tag: {
        render: function (attrs, env) {...} [,
        inline: true ]
    } [,
    ...]
}

var options = {
    interpolator: function(expr, env) { ... }
}

var md = require('markdown-it')({html: true})
        .use(require('markdown-it-shortcode-tag'), shortcodes, options);


md.render(content, env);

shortcodes

  • tag - set a property name to identify shortcodes of the form <tag ...>. HTML5 rules for tag names apply.
  • render - function that returns the rendered shortcode
    • attrs - Object with name-value pairs for all attributes listed in the tag. HTML5 rules for attribute syntax apply. See below for the relation between tag attributes and object property values.
    • env - the enviroment variable passed to markdown-it in a md.render(content, env).
  • inline - optional, if true the shortcode is always rendered inline (surrounded by <p></p> tags), even if stands on its own separated line

options

  • interpolator - optional, function that interpolates an attribute value given unquoted and surrounded by an interpolation marker #{ }.
    Defaults to looking up the enviroment value: (expr, env) => env[expr].
    • expr - string content of the curly braces.
    • env - the enviroment variable passed to markdown-it in a md.render(content, env).

Translation from tag attributes to attrs object properties

name
Attributes without values are represented as boolean true:

attrs.name = true

name=3.7
Unquoted values are converted to numbers using parseFloat():

attrs.name = 3.7

name=#{expr}
Values surrounded by the interpolation marker are passed to the options.interpolator function:

attrs.name = options.interpolator("expr", env)

or, if missing, to its default:

attrs.name = env["expr"]

Note that for this syntax whitespace inside the curly braces is not allowed, as per HTML attribute syntax rules.

name="value"
strings are copied to the property:

attrs.name = "value"

name="val1#{expr}val2"
In strings that contain an interpolation marker #{ }, the marker is exchanged for the string value of the contained enviroment variable:

attrs.name = "val1" + env["expr"] + "val2"

Examples

Style subcontent layout

var supported = ['img', 'video', 'iframe'];

var shortcodes = {
    media: {
        render: function (attrs, env) {
            if (supported.indexOf(attrs.method) < 0) {
                throw new Error('unsupported medium');
            }
            var out = '<' + attrs.method;
            out += ' src="' + (attrs.src || '') + '"';
            out += ' alt="' + (attrs.alt || '') + '"';
            if (attrs.width) out += ' width="' + attrs.width + '"';
            if (attrs.side) out += ' style="float:' + attrs.side + '"';
            return out + '>';
        }
    }
}

var md = require('markdown-it')({html: true})
        .use(require('markdown-it-shortcode-tag'), shortcodes);

console.log(md.render('<media method="img" src={picture} width=400 side="left">', {
    picture: "assets/picture.jpg"
}));

Output:

<img src="assets/picture.jpg" alt="" width="400" style="float:left">

Render an enviroment variable using shortcodes

var shortcodes = {
    variable: {
        render: function (attrs, env) {
            var name = Object.getOwnPropertyNames(attrs)[0];
            return env[name];
        }
    }
}

var md = require('markdown-it')({html: true})
        .use(require('markdown-it-shortcode-tag'), shortcodes);

console.log(md.render('# <variable title>', { title: "Hello World" }));

Output:

<h1>Hello World</h1>

Render an environment variable using interpolation

var shortcodes = {
    footer: {
        render: function (attrs) {
            return '<footer><p>' + attrs[meta] + '</p></footer>';
        }
    }
}

var options = {
    interpolator: function (expr, env) {
        return 'Authored by ' + env[expr].author + ' on ' +
               Date(env[expr].date).toLocaleDateString(env.locale) + '.';
    }
}

var md = require('markdown-it')({html: true})
        .use(require('markdown-it-shortcode-tag'), shortcodes, options);

console.log(md.render('<footer meta=#{lastpost} >', {
    lastpost: {
        author: "Janet",
        date: "2018-01-03"
    },
    locale: "en_US"
}));

Output:

<footer><p>Authored by Janet on 2018/3/1</p></footer>

License

see LICENSE