markdown-it-attr-link-replacer

markdown-it plugin for replacing links (image & text) in the markdown document.

Usage no npm install needed!

<script type="module">
  import markdownItAttrLinkReplacer from 'https://cdn.skypack.dev/markdown-it-attr-link-replacer';
</script>

README

markdown-it-attr-link-replacer

TypeScript

This is a fork of Martin Heidegger's markdown-it-replace-link plugin.

However, I needed it for Ditto website with some more customizations that are probably too heavy for a lot of users of the original library.

What are the differences?

  • Original library only handled links for a and img tags
  • Original will ignore other raw html attributes that may be links.
  • This plugin however allows additional specifications

markdown-it plugin for replacing links (image & text) in the markdown document.

Usage

Installation

npm install markdown-it-attr-link-replacer -S

Enable plugin

var md = require('markdown-it')({
    html: true    
}).use(require('markdown-it-attr-link-replacer'), {
    attributes: ['src', 'href']
    replaceLink: function (link, env) {
        return link + "?c=" + Date.now();
    }
}); // <-- this use(package_name) is required

Example

[Hello](test)

and use this

var md = require('markdown-it')({
    html: true
}).use(require('markdown-it-attr-link-replacer'), {
    replaceLink: function (link, env) {
        return "http://me.com/" + link;
    }
});

This will result in the link prefixed with the http://me.com/ like:

<p><a href="http://me.com/test">Hello</a></p>

Both images and html links will be processed by default

Testing

To run the tests use:

npm run test

License

MIT