markdown-it-replace-link
Advanced tools
Comparing version
73
index.js
@@ -1,34 +0,43 @@ | ||
'use strict'; | ||
function replaceAttr (token, attrName, replace, env) { | ||
token.attrs.forEach(function (attr) { | ||
if (attr[0] === attrName) { | ||
attr[1] = replace(attr[1], env, token) | ||
} | ||
}) | ||
} | ||
function replaceAttr(token, attrName, replace, env) { | ||
token.attrs.forEach(function (attr) { | ||
if (attr[0] === attrName) { | ||
attr[1] = replace(attr[1], env, token); | ||
} | ||
}); | ||
module.exports = function (md, opts) { | ||
md.core.ruler.after( | ||
'inline', | ||
'replace-link', | ||
function (state) { | ||
var replace | ||
if (md.options.replaceLink && typeof md.options.replaceLink === 'function') { | ||
// Use markdown options (default so far) | ||
replace = md.options.replaceLink | ||
} else if (opts && opts.replaceLink && typeof opts.replaceLink === 'function') { | ||
// Alternatively use plugin options provided upon .use(..) | ||
replace = opts.replaceLink | ||
} else { | ||
return false | ||
} | ||
if (typeof replace === 'function') { | ||
state.tokens.forEach(function (blockToken) { | ||
if (blockToken.type === 'inline' && blockToken.children) { | ||
blockToken.children.forEach(function (token) { | ||
var type = token.type | ||
if (type === 'link_open') { | ||
replaceAttr(token, 'href', replace, state.env) | ||
} else if (type === 'image') { | ||
replaceAttr(token, 'src', replace, state.env) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
return false | ||
} | ||
) | ||
} | ||
module.exports = function (md) { | ||
md.core.ruler.after( | ||
'inline', | ||
'replace-link', | ||
function (state) { | ||
var replace = md.options.replaceLink; | ||
if (typeof replace === 'function') { | ||
state.tokens.forEach(function (blockToken) { | ||
if (blockToken.type === 'inline' && blockToken.children) { | ||
blockToken.children.forEach(function (token) { | ||
var type = token.type; | ||
if (type === 'link_open') { | ||
replaceAttr(token, 'href', replace, state.env); | ||
} else if (type === 'image') { | ||
replaceAttr(token, 'src', replace, state.env); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
return false; | ||
} | ||
); | ||
}; |
{ | ||
"name": "markdown-it-replace-link", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "markdown-it plugin for replacing links (image & text) in the markdown document.", | ||
@@ -9,8 +9,3 @@ "main": "index.js", | ||
"unit": "mocha -R spec", | ||
"lint": "eslint --reset .", | ||
"browserify": "(echo \"$HEADER\"; browserify ./ -s markdownitReplaceLink) > dist/markdown-it-replace-link.js", | ||
"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", | ||
"uglify": "uglifyjs dist/markdown-it-replace-link.js -b beautify=false,ascii-only=true -c -m --preamble \"$HEADER\" > dist/markdown-it-replace-link.min.js", | ||
"coverage": "rm -rf coverage;istanbul cover node_modules/.bin/_mocha", | ||
"test-ci": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" | ||
"lint": "standard" | ||
}, | ||
@@ -33,12 +28,8 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "^2.3.0", | ||
"coveralls": "^2.11.2", | ||
"eslint": "^0.21.0", | ||
"eslint-plugin-nodeca": "^1.0.3", | ||
"istanbul": "^0.3.14", | ||
"markdown-it": "^4.1.0", | ||
"markdown-it-testgen": "^0.1.4", | ||
"mocha": "^2.2.4" | ||
"markdown-it": "*", | ||
"markdown-it-testgen": "*", | ||
"mocha": "^7.2.0", | ||
"standard": "*" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -51,1 +51,5 @@ # markdown-it-replace-link | ||
``` | ||
### License | ||
[MIT](./LICENSE) |
@@ -1,9 +0,23 @@ | ||
'use strict'; | ||
/* eslint-env mocha */ | ||
var path = require('path') | ||
var generate = require('markdown-it-testgen') | ||
var expect = require('chai').expect | ||
var fs = require('fs') | ||
var fixtures = { | ||
env: fs.readFileSync(path.join(__dirname, 'fixtures/env.txt'), 'utf-8'), | ||
tocPath: path.join(__dirname, 'fixtures/toc.txt') | ||
} | ||
var mdReplaceLink = require('../') | ||
var path = require('path'); | ||
var generate = require('markdown-it-testgen'); | ||
var expect = require('chai').expect; | ||
var fs = require('fs'); | ||
function replaceLink (link, env, token) { | ||
if (token.type === 'image') { | ||
return 'image/' + link | ||
} | ||
if (link === 'a') { | ||
return env.x + link | ||
} | ||
return 'http://me.com/' + link | ||
} | ||
describe('markdown-it-replace-link', function() { | ||
describe('markdown-it-replace-link', function () { | ||
var md = require('markdown-it')({ | ||
@@ -13,21 +27,32 @@ html: true, | ||
typography: true, | ||
replaceLink: function (link, env, token) { | ||
if (token.type === 'image') { | ||
return 'image/' + link | ||
} | ||
if (link === 'a') { | ||
return env.x + link; | ||
} | ||
return "http://me.com/" + link; | ||
} | ||
}).use(require('../')); | ||
generate(path.join(__dirname, 'fixtures/toc.txt'), md); | ||
replaceLink: replaceLink | ||
}).use(mdReplaceLink) | ||
generate(fixtures.tocPath, md) | ||
it("Passes on env", function (done) { | ||
var html = md.render(fs.readFileSync(path.join(__dirname, 'fixtures/env.txt'), 'utf-8'), { | ||
it('Passes on env', function (done) { | ||
var html = md.render(fixtures.env, { | ||
x: 'test/' | ||
}) | ||
expect(html).to.equal("<p><a href=\"test/a\">Hello</a></p>\n"); | ||
done(); | ||
}); | ||
}); | ||
expect(html).to.equal('<p><a href="test/a">Hello</a></p>\n') | ||
done() | ||
}) | ||
}) | ||
describe('markdown-it-replace-link w. plugin options', function () { | ||
var md = require('markdown-it')({ | ||
html: true, | ||
linkify: true, | ||
typography: true | ||
}).use(mdReplaceLink, { | ||
replaceLink: replaceLink | ||
}) | ||
generate(fixtures.tocPath, md) | ||
it('Passes on env', function (done) { | ||
var html = md.render(fixtures.env, { | ||
x: 'test/' | ||
}) | ||
expect(html).to.equal('<p><a href="test/a">Hello</a></p>\n') | ||
done() | ||
}) | ||
}) |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
4
-50%0
-100%55
7.84%2
-66.67%5777
-43.7%7
-41.67%92
-25.81%