markdown-it-linkify-images
Advanced tools
Comparing version
@@ -0,7 +1,16 @@ | ||
# 3.0.0 | ||
- Fix bug where an markdown image with link would generate a double link #46 (thanks @tlylt) | ||
_Breaking Changes_ | ||
- Drop support for Node < v16 | ||
# 2.0.0 | ||
* Apply previously applied attributes to rendered image (thanks @harigopal) | ||
- Apply previously applied attributes to rendered image (thanks @harigopal) | ||
_Breaking Changes_ | ||
* Drop support for Node < v12 | ||
* Drop support for markdown-it < 9 | ||
- Drop support for Node < v12 | ||
- Drop support for markdown-it < 9 |
76
index.js
@@ -7,30 +7,48 @@ 'use strict' | ||
var token = tokens[idx] | ||
var srcIndex = token.attrIndex('src') | ||
var url = token.attrs[srcIndex][1] | ||
var caption = md.utils.escapeHtml(token.content) | ||
const token = tokens[idx] | ||
const srcIndex = token.attrIndex('src') | ||
const url = token.attrs[srcIndex][1] | ||
const caption = md.utils.escapeHtml(token.content) | ||
var target = generateTargetAttribute(config.target) | ||
var linkClass = generateClass(config.linkClass) | ||
var imgClass = generateClass(config.imgClass) | ||
var otherAttributes = generateAttributes(md, token) | ||
const target = generateTargetAttribute(config.target) | ||
const linkClass = generateClass(config.linkClass) | ||
const imgClass = generateClass(config.imgClass) | ||
const otherAttributes = generateAttributes(md, token) | ||
const imgAttributes = concatenateAttributes( | ||
`src="${url}"`, | ||
`alt="${caption}"`, | ||
imgClass, | ||
...otherAttributes | ||
) | ||
return '' + | ||
'<a href="' + url + '"' + linkClass + target + '>' + | ||
'<img src="' + url + '" alt="' + caption + '"' + imgClass + otherAttributes + '>' + | ||
'</a>' | ||
const imgElement = `<img ${imgAttributes}>` | ||
if (alreadyWrappedInLink(tokens, idx)) { | ||
return imgElement | ||
} | ||
const linkAttributes = concatenateAttributes( | ||
`href="${url}"`, | ||
linkClass, | ||
target | ||
) | ||
return `<a ${linkAttributes}>${imgElement}</a>` | ||
} | ||
} | ||
function concatenateAttributes (...attributes) { | ||
return attributes.filter((val) => val).join(' ') | ||
} | ||
function generateAttributes (md, token) { | ||
var ignore = ['src', 'alt'] | ||
var escape = ['title'] | ||
var attributes = '' | ||
const ignore = ['src', 'alt'] | ||
const escape = ['title'] | ||
token.attrs.forEach(function (entry) { | ||
var name = entry[0] | ||
return token.attrs.map((entry) => { | ||
const name = entry[0] | ||
if (ignore.includes(name)) return | ||
if (ignore.includes(name)) return '' | ||
var value = '' | ||
let value = '' | ||
@@ -43,6 +61,4 @@ if (escape.includes(name)) { | ||
attributes += ' ' + name + '="' + value + '"' | ||
return `${name}="${value}"` | ||
}) | ||
return attributes | ||
} | ||
@@ -53,3 +69,3 @@ | ||
return ' target="' + target + '"' | ||
return `target="${target}"` | ||
} | ||
@@ -60,5 +76,17 @@ | ||
return ' class="' + className + '"' | ||
return `class="${className}"` | ||
} | ||
function alreadyWrappedInLink (tokens, currentTokenIndex) { | ||
const previousToken = tokens[currentTokenIndex - 1] | ||
const nextToken = tokens[currentTokenIndex + 1] | ||
return ( | ||
previousToken && | ||
previousToken.type === 'link_open' && | ||
nextToken && | ||
nextToken.type === 'link_close' | ||
) | ||
} | ||
module.exports = markdownitLinkifyImages |
{ | ||
"name": "markdown-it-linkify-images", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A markdown-it plugin to add links to images", | ||
@@ -29,10 +29,10 @@ "main": "index.js", | ||
"devDependencies": { | ||
"browserify": "^16.5.1", | ||
"chai": "^4.1.1", | ||
"browserify": "^17.0.0", | ||
"chai": "^4.3.7", | ||
"chalk": "^4.1.0", | ||
"mocha": "^8.1.0", | ||
"mocha": "^10.1.0", | ||
"rimraf": "^3.0.2", | ||
"snazzy": "^8.0.0", | ||
"standard": "^14.3.4", | ||
"uglify-js": "^3.10.1" | ||
"snazzy": "^9.0.0", | ||
"standard": "^17.0.0", | ||
"uglify-js": "^3.17.4" | ||
}, | ||
@@ -57,3 +57,6 @@ "standard": { | ||
] | ||
}, | ||
"dependencies": { | ||
"markdown-it": "^13.0.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
6494
14.33%67
52.27%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added