https://github.com/markdown-it/markdown-it-for-inline
Inline tokens iterator for markdown-it markdown parser
https://github.com/markdown-it/markdown-it-for-inline
Last synced: 6 months ago
JSON representation
Inline tokens iterator for markdown-it markdown parser
- Host: GitHub
- URL: https://github.com/markdown-it/markdown-it-for-inline
- Owner: markdown-it
- License: mit
- Created: 2014-12-29T14:25:32.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-05T07:21:23.000Z (about 2 years ago)
- Last Synced: 2025-06-08T21:39:00.223Z (6 months ago)
- Language: JavaScript
- Size: 16.6 KB
- Stars: 28
- Watchers: 6
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# markdown-it-for-inline
[](https://github.com/markdown-it/markdown-it-for-inline/actions/workflows/ci.yml)
[](https://www.npmjs.org/package/markdown-it-for-inline)
[](https://coveralls.io/r/markdown-it/markdown-it-for-inline)
> Inline tokens iterator for [markdown-it](https://github.com/markdown-it/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:
```bash
npm install markdown-it-for-inline --save
bower install markdown-it-for-inline --save
```
## Use
```js
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
```js
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
```js
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](https://github.com/markdown-it/markdown-it-for-inline/blob/master/LICENSE)