markdown-it-variables
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

markdown-it-variables

When writing documentation or any markdown-based content, you might find yourself repeating a lot of text. Among these repetitive chunks of text, there might be some important units (e.g. product name, acronyms, etc.) that you would want to keep consistent and easy to write and replace. This markdown-it plugin is a straightforward way of achieving this.

  • ✅ Simple variables #{productName} -> My product
  • ✅ Acronyms: #{who|-} to <abbr title="World Health Organization">WHO</abbr>
  • ✅ Links: #{who|#} to <a href="https://www.who.int">World Health Organization</a>

Install

# Using npm
npm i markdown-it-variables
# Using Yarn
yarn add markdown-it-variables
# Using pnpm
pnpm add markdown-it-variables

Usage

import mardownIt from 'markdown-it';
import { variablesPlugin } from 'markdown-it-variables';

const md = mardownIt().use(variablesPlugin, {
  since: '1948',
  who: {
    url: 'https://www.who.int',
    abbr: 'WHO',
    label: 'World Health Organization',
  },
});

You can then reference your variables from a markdown file: #{since} and #{who}. You can also apply a list of pipe-separated modifiers: #{who|abbr|link}, and use modifier aliases to get even more concise: #{who|-|#}.

Modifiers

Name Alias Description
abbr - Displays the abbreviated variable and wraps it with a <abbr> tag, with the full label as a title.
link # Displays the final content of the variable as a link to provided url

Options

data: Record<string, SimpleVariable | ExpandedVariable>

List of variables you want to use.

There are two kinds of variables :

  1. Simple ones, whose values can be any string.
  2. Rich ones, which can define further piece of information that can later be used through modifiers.

The value of rich variables are objects with the following structure:

Property name Property type Description
label string Full label, mandatory and is used by default.
abbr string? Shorter version of the variable (e.g. abbreviation/acronym).
url string? URL to relevant page, used along with the link modifier.

severity?: 'error' | 'warn' | 'silent' (optional, defaults to 'warn')

Determines the severity of the plugin.

Here is an overview of how different severities change the behaviour of the plugin:

Throws │ ⏭️ Skips (does not transform) │ Transforms to fallback │ Prints a warning message

Scenario 'error' 'warn' 'silent'
No variables
Malformatted variable in options
Referencing an unknown variable ⏭️ ⏭️
Setting a modifier on a simple variable
Using link modifier on a rich variable without url
Using abbr modifier on a rich variable without abbr

ignoreMissingVariables?: boolean (optional, defaults to false)

When set to true, prevents the plugin from throwing when encountering references to missing variables.

This basically overwrites the above behaviours with the following:

Scenario 'error' 'warn' 'silent'
Referencing an unknown variable ⏭️ ⏭️ ⏭️
  • It does not throw even if severity is set to 'error'.
  • It does not print any warning even if severity is set to 'warn'.

className?: string (defaults to undefined)

When defined, the outter tags of any variable will have the specified class name. When specified and encountering variables without modifiers, the variable output will be wrapped in a span with the specified class name.


Example

Based on the options illustrated in the Usage section.

Input:

Founded in #{since}, #{who|#} is the United Nations agency that connects nations, partners and
people to promote health, keep the world safe and serve the vulnerable - so everyone, everywhere can
attain the highest level of health.

#{who|-} leads global efforts to expand universal health coverage. We direct and coordinate the
world's response to health emergencies.

Output:

Founded in 1948, <a href="https://www.who.int">World Health Organization</a> is the United Nations
agency that connects nations, partners and people to promote health, keep the world safe and serve
the vulnerable - so everyone, everywhere can attain the highest level of health.

<abbr title="World Health Organization">WHO</abbr> leads global efforts to expand universal health
coverage. We direct and coordinate the world's response to health emergencies.

Package Sidebar

Install

npm i markdown-it-variables

Weekly Downloads

7

Version

1.1.1

License

MIT

Unpacked Size

22.5 kB

Total Files

7

Last publish

Collaborators

  • charles.gruenais