shepherdwind / match-pairs

A plugin that fixes the logic for bold text in markdown-it

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

markdown-match-pairs

CI NPM version Coverage Status

This is a plugin that fixes the logic for bold text in markdown-it.

This issue has been encountered by many people, and there have been many discussions about it, such as

Issue

By default, markdown-it identifies whether ** can be closed based on the following rules:

  • If there is a space on the left, then open = true
  • If there is a space on the right, then close = true
  • If neither side has a space, it checks for special characters such as +-= or other language separators for example Chinese semicolons . If these symbols are present, they are treated as spaces.

This leads to the following parsing results:

render('**==foo==**abc') === '<p>**<mark>foo</mark>**abc</p>';
render('**中文:**中文') === '<p>**中文:**中文</p>';

In reality, we expect both cases to output a strong tag.

Implementation Logic

The implementation logic is as follows: insert a function before balance_pairs. After completing inline rule parsing, adjust the delimiters. After adjustment, whether ** can be closed depends on whether there was an open ** before it.

After using this plugin, the above examples will output as follows:

render('**==foo==**abc') === '<p><strong><mark>foo</mark></strong>abc</p>';
render('**中文:**中文') === '<p><strong>中文:</strong>中文</p> ';

Install

npm install markdown-match-pairs --save

Usage

var md = require('markdown-it')()
         .use(require('markdown-match-pairs'));

Testing

This plugin has copied all existing test cases from the markdown-it repository to ensure that old test cases pass.

License

MIT

About

A plugin that fixes the logic for bold text in markdown-it

License:MIT License


Languages

Language:JavaScript 100.0%