├── test ├── fixtures │ ├── env.txt │ └── attr-modification.txt ├── .eslintrc └── test.js ├── .gitignore ├── bower.json ├── index.js ├── package.json └── README.md /test/fixtures/env.txt: -------------------------------------------------------------------------------- 1 | [Hello](a) -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | env: 2 | node: true 3 | mocha: true 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | npm-debug.log 3 | bower_components/* 4 | *~ 5 | -------------------------------------------------------------------------------- /test/fixtures/attr-modification.txt: -------------------------------------------------------------------------------- 1 | Test1 2 | . 3 | [Hello](test) 4 | . 5 |

Hello

6 | . 7 | 8 | Test2 9 | . 10 | ![Hello](b) 11 | . 12 |

Hello

13 | . 14 | 15 | 16 | Test3 17 | . 18 | ![Hello](foo.gif) 19 | . 20 |

Hello

21 | . 22 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markdown-it-modify-token", 3 | "main": "index.js", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/jeffbski/markdown-it-modify-token", 6 | "authors": [ 7 | "Jeff Barczewski " 8 | ], 9 | "description": "markdown-it plugin for modifying tokens in a markdown document. It can for example modify content or attributes for certain type of elements like links or images.", 10 | "moduleType": [ 11 | "amd" 12 | ], 13 | "keywords": [ 14 | "markdown", 15 | "markdown-it" 16 | ], 17 | "license": "MIT", 18 | "ignore": [ 19 | "**/.*", 20 | "node_modules", 21 | "bower_components", 22 | "test", 23 | "tests" 24 | ], 25 | "dependencies": {} 26 | } 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function modifyToken(token, modifyFn, env) { 4 | // create attrObj for convenient get/set of attributes 5 | var attrObj = (token.attrs) ? token.attrs.reduce(function (acc, pair) { 6 | acc[pair[0]] = pair[1]; 7 | return acc; 8 | }, {}) : {}; 9 | token.attrObj = attrObj; 10 | modifyFn(token, env); 11 | // apply any overrides or new attributes from attrObj 12 | Object.keys(token.attrObj).forEach(function (k) { 13 | token.attrSet(k, token.attrObj[k]); 14 | }); 15 | } 16 | 17 | function noop() { } 18 | 19 | module.exports = function (md) { 20 | md.core.ruler.push( 21 | 'modify-token', 22 | function (state) { 23 | var modifyFn = md.options.modifyToken || noop; 24 | state.tokens.forEach(function (token) { 25 | if (token.children && token.children.length) { 26 | token.children.forEach(function (token) { 27 | modifyToken(token, modifyFn, state.env); 28 | }); 29 | } 30 | modifyToken(token, modifyFn, state.env); 31 | }); 32 | return false; 33 | } 34 | ); 35 | }; 36 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var generate = require('markdown-it-testgen'); 5 | var expect = require('chai').expect; 6 | var fs = require('fs'); 7 | 8 | describe('markdown-it-modify-token', function() { 9 | var md = require('markdown-it')({ 10 | html: true, 11 | linkify: true, 12 | typography: true, 13 | modifyToken: function (token, env) { 14 | switch (token.type) { 15 | case 'image': // set all images to 200px width except for foo.gif 16 | if (token.attrObj.src !== 'foo.gif') { 17 | token.attrObj.width = '200px'; 18 | } 19 | break; 20 | case 'link_open': 21 | token.attrObj.target = '_blank'; // set all links to open in new window 22 | if (env.linkPrefix && token.attrObj.href) { 23 | token.attrObj.href = env.linkPrefix + token.attrObj.href; 24 | } 25 | break; 26 | } 27 | // return a new or modified token otherwise it will use previous token 28 | return token; 29 | } 30 | }).use(require('../')); 31 | generate(path.join(__dirname, 'fixtures/attr-modification.txt'), md); 32 | 33 | it("Passes on env", function (done) { 34 | var html = md.render(fs.readFileSync(path.join(__dirname, 'fixtures/env.txt'), 'utf-8'), { 35 | linkPrefix: 'test/' 36 | }); 37 | expect(html).to.equal("

Hello

\n"); 38 | done(); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markdown-it-modify-token", 3 | "version": "1.0.2", 4 | "description": "markdown-it plugin for modifying tokens in a markdown document. It can for example modify content or attributes for certain type of elements like links or images.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm run unit", 8 | "unit": "mocha -R spec", 9 | "browserify": "mkdir -p dist && (echo \"$HEADER\"; browserify ./ -s markdownitModifyToken) > dist/markdown-it-modify-token.js", 10 | "dist": "npm run test-ci; mkdir -p dist; export HEADER=\"/*! $(npm v . name) $(npm v . version) $(npm v . repository.url) @license $(npm v . license) */\"; npm run browserify; npm run uglify", 11 | "uglify": "uglifyjs dist/markdown-it-modify-token.js -b beautify=false,ascii-only=true -c -m --preamble \"$HEADER\" > dist/markdown-it-modify-token.min.js", 12 | "coverage": "rm -rf coverage;istanbul cover node_modules/.bin/_mocha", 13 | "test-ci": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && rm -rf ./coverage" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/jeffbski/markdown-it-modify-token" 18 | }, 19 | "keywords": [ 20 | "markdown", 21 | "markdown-it", 22 | "markdown-it-plugin", 23 | "modify", 24 | "token", 25 | "link", 26 | "image", 27 | "element", 28 | "attributes", 29 | "attr" 30 | ], 31 | "author": "Jeff Barczewski", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/jeffbski/markdown-it-modify-token/issues" 35 | }, 36 | "homepage": "https://github.com/jeffbski/markdown-it-modify-token", 37 | "devDependencies": { 38 | "browserify": "^12.0.2", 39 | "chai": "^3.5.0", 40 | "istanbul": "^0.4.2", 41 | "markdown-it": "^5.1.0", 42 | "markdown-it-testgen": "^0.1.4", 43 | "mocha": "^2.2.4", 44 | "uglify-js": "^2.6.1" 45 | }, 46 | "dependencies": {} 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # markdown-it-modify-token 2 | 3 | > markdown-it plugin for modifying tokens including content or element attributes in the markdown document. For example it can modify image or link attributes. 4 | 5 | ## Usage 6 | 7 | #### Enable plugin 8 | 9 | ```js 10 | var md = require('markdown-it')({ 11 | modifyToken: function (token, env) { 12 | // see API https://markdown-it.github.io/markdown-it/#Token 13 | // token will also have an attrObj property added for convenience 14 | // which allows easy get and set of attribute values. 15 | // It is prepopulated with the current attr values. 16 | // Values returned in token.attrObj will override existing attr values. 17 | // env will contain any properties passed to markdown-it's render 18 | // Token can be modified in place, no return is necessary 19 | switch (token.type) { 20 | case 'image': // set all images to 200px width except for foo.gif 21 | if (token.attrObj.src !== 'foo.gif') { 22 | token.attrObj.width = '200px'; 23 | } 24 | break; 25 | case 'link_open': 26 | token.attrObj.target = '_blank'; // set all links to open in new window 27 | break; 28 | } 29 | } 30 | }).use(require('markdown-it-modify-token')); // <-- this use(package_name) is required 31 | ``` 32 | 33 | #### Example 34 | 35 | ```md 36 | [Hello](test) 37 | ![Image](foo.gif) 38 | ``` 39 | 40 | with this config 41 | 42 | ```js 43 | var md = require('markdown-it')({ 44 | modifyToken: function (token, env) { 45 | switch (token.type) { 46 | case 'image': // set all images to 200px width 47 | token.attrObj.width = '200px'; 48 | break; 49 | case 'link_open': 50 | token.attrObj.target = '_blank'; // set all links to open in new window 51 | break; 52 | } 53 | } 54 | }).use(require('markdown-it-modify-token')); // <-- this use(package_name) is required 55 | ``` 56 | 57 | Will result in roughly 58 | 59 | ```html 60 | Hello 61 | 62 | ``` 63 | 64 | Due to the token types we are handling in our switch statement, we can affect both image and link attributes. 65 | 66 | ### Testing 67 | 68 | ```bash 69 | npm test 70 | ``` 71 | 72 | ### Inspiration 73 | 74 | Thanks to Martin Heidegger for https://github.com/martinheidegger/markdown-it-replace-link which I used as a starting point for building this plugin. 75 | --------------------------------------------------------------------------------