1. markdown-it-for-inline
Inline tokens iterator for markdown-it markdown parser.
markdown-it-for-inline
Package: markdown-it-for-inline
Created by: markdown-it
Last modified: Tue, 05 Dec 2023 07:21:47 GMT
Version: 2.0.1
License: MIT
Downloads: 102,473
Repository: https://github.com/markdown-it/markdown-it-for-inline

Install

npm install markdown-it-for-inline
yarn add markdown-it-for-inline

markdown-it-for-inline

CI
NPM version
Coverage Status

Inline tokens iterator for markdown-it markdown parser.

This plugin allows to apply function to certain types of inline tokens. Speed
will be not fastest of possible, but you can do quick prototyping of certain
rule types.

Usage

Install

node.js, browser:

 npm install markdown-it-for-inline --save
bower install markdown-it-for-inline --save

Use

 var iterator = require('markdown-it-for-inline');

// plugin params are:
//
// - rule name (should be unique)
// - token type to apply
// - function
//
var md = require('markdown-it')()
            .use(iterator, 'foo_replace', 'text', function (tokens, idx) {
              tokens[idx].content = tokens[idx].content.replace(/foo/g, 'bar');
            });

Differences in browser. If you load script directly into the page, without
package system, module will add itself globally as window.markdownitForInline.

Example 2. Cut link prefixes

 var iterator = require('markdown-it-for-inline');

var md = require('markdown-it')({ linkify: true })
            .use(iterator, 'url_beautify', 'link_open', function (tokens, idx) {
              // Make sure link contains only text
              if ((tokens[idx + 2].type !== 'link_close') ||
                  (tokens[idx + 1].type !== 'text')) {
                return;
              }
              // Do replacement
              tokens[idx + 1].content = tokens[idx + 1].content
                                          .replace(/^https?:\/\//, '')
                                          .replace(/^www./, '');
            });

Example 3. Make links open in new window

 var iterator = require('markdown-it-for-inline');

var md = require('markdown-it')({ linkify: true })
            .use(iterator, 'url_new_win', 'link_open', function (tokens, idx) {
              tokens[idx].attrPush([ 'target', '_blank' ]);
            });

License

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code