Markdown-It Mark Plugin
The `markdown-it-mark` package is a plugin for the `markdown-it` markdown parser, designed to extend its functionality by adding support for the HTML `<mark>` tag. It enables users to highlight text within their Markdown documents using the `==text==` syntax, which is then rendered as `<mark>text</mark>`. The current stable version is 4.0.0, last published approximately two years ago, indicating a mature and stable project likely in maintenance mode rather than rapid feature development. This plugin is highly focused, providing a straightforward implementation for text highlighting that adheres to similar rules as CommonMark emphasis. Its key differentiator is its seamless integration with the `markdown-it` ecosystem, offering a lightweight solution for a common formatting need without introducing complex parsing behaviors.
Common errors
-
TypeError: md.use is not a function
cause This error typically occurs if `markdown-it` has not been correctly initialized or imported, or if `md` is not an instance of `MarkdownIt` before calling `.use()`.fixEnsure `markdownit` is imported and instantiated correctly (e.g., `const markdownit = require('markdown-it'); const md = markdownit();` or `import markdownit from 'markdown-it'; const md = markdownit();`). -
Error: Cannot find module 'markdown-it-mark'
cause The `markdown-it-mark` package is not installed or the `require`/`import` path is incorrect.fixRun `npm install markdown-it-mark` to install the package. Verify the import/require path matches your module system configuration. -
Text enclosed in '==' is not being rendered as <mark> tags.
cause The plugin might not be registered with `markdown-it`, or the input Markdown syntax for marking is incorrect (e.g., contains spaces inside `==` delimiters or has unbalanced delimiters).fixEnsure `md.use(markdownitMark)` is called after `markdown-it` initialization. Check that the markdown syntax is `==text==` with no internal spaces, similar to CommonMark emphasis rules.
Warnings
- breaking Version 3.0.0 and subsequent major versions of `markdown-it-mark` (including 4.0.0) introduced a breaking change that requires `markdown-it` version 10.0.0 or higher. Older versions of `markdown-it-mark` may work with older `markdown-it` versions, but for v3.x and v4.x, ensure `markdown-it` is updated.
- gotcha The `==text==` syntax for marking text strictly follows the same conditions as CommonMark emphasis rules. This means that the delimiters `==` must be balanced and not contain whitespace immediately inside them to correctly parse. For instance, `== mark ==` will not render as `<mark> mark </mark>`.
Install
-
npm install markdown-it-mark -
yarn add markdown-it-mark -
pnpm add markdown-it-mark
Imports
- markdownitMark
import { markdownitMark } from 'markdown-it-mark';import markdownit from 'markdown-it'; import markdownitMark from 'markdown-it-mark'; const md = markdownit().use(markdownitMark);
- markdownitMark (CommonJS)
const { markdownitMark } = require('markdown-it-mark');const markdownit = require('markdown-it'); const markdownitMark = require('markdown-it-mark'); const md = markdownit().use(markdownitMark); - window.markdownitMark
// When loaded directly in a browser via <script> tag: const md = window.markdownit().use(window.markdownitMark);
Quickstart
import markdownit from 'markdown-it'; import markdownitMark from 'markdown-it-mark'; // Initialize markdown-it with the mark plugin const md = markdownit(); md.use(markdownitMark); // Markdown input with mark syntax const markdownInput = 'This is ==marked== text and also ==important== content.'; // Render to HTML const htmlOutput = md.render(markdownInput); console.log(htmlOutput); // Expected output: <p>This is <mark>marked</mark> text and also <mark>important</mark> content.</p>