markdown-it-shortcode-tag
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:
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 = shortcodescustom shortcodescustom
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: {...} inline: true ... var options = { ... } var md = html: true ; md;
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)
.
attrs
object properties
Translation from tag attributes to 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: { if supported < 0 throw 'unsupported medium'; var out = '<' + attrsmethod; out += ' src="' + attrssrc || '' + '"'; out += ' alt="' + attrsalt || '' + '"'; if attrswidth out += ' width="' + attrswidth + '"'; if attrsside out += ' style="float:' + attrsside + '"'; return out + '>'; } var md = html: true ; console;
Output:
Render an enviroment variable using shortcodes
var shortcodes = variable: { var name = Object0; return envname; } var md = html: true ; console;
Output:
Hello World
Render an environment variable using interpolation
var shortcodes = footer: { return '<footer><p>' + attrsmeta + '</p></footer>'; } var options = { return 'Authored by ' + envexprauthor + ' on ' + Dateenvexprdate + '.'; } var md = html: true ; console;
Output:
Authored by Janet on 2018/3/1
License
see LICENSE