diff --git a/lib/index.js b/lib/index.js index 3b74210d144a7f1cfe6deea9a9a2e349fc9d421f..ac390349d9dcc0bf635be39e9279c307b5dbf09f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,12 +6,11 @@ var sizeOf = require('./imsize'); var parseImageSize = require('./helpers/parse_image_size'); -var normalizeReference = require('./helpers/normalize_reference.js'); function image_with_size(md, options) { return function(state, silent) { - var code, - href, + var attrs, + code, label, labelEnd, labelStart, @@ -21,8 +20,10 @@ function image_with_size(md, options) { title, width = '', height = '', + token, tokens, start, + href = '', oldPos = state.pos, max = state.posMax; @@ -55,11 +56,13 @@ function image_with_size(md, options) { // ^^^^^^ parsing link destination start = pos; res = md.helpers.parseLinkDestination(state.src, pos, state.posMax); - if (res.ok && state.md.inline.validateLink(res.str)) { - href = res.str; - pos = res.pos; - } else { - href = ''; + if (res.ok) { + href = state.md.normalizeLink(res.str); + if (state.md.validateLink(href)) { + pos = res.pos; + } else { + href = ''; + } } // [link]( <href> "title" ) @@ -146,7 +149,7 @@ function image_with_size(md, options) { // (collapsed reference link and shortcut reference link respectively) if (!label) { label = state.src.slice(labelStart, labelEnd); } - ref = state.env.references[normalizeReference(label)]; + ref = state.env.references[md.utils.normalizeReference(label)]; if (!ref) { state.pos = oldPos; return false; @@ -184,15 +187,21 @@ function image_with_size(md, options) { } } - state.push({ - type: 'image', - src: href, - title: title, - tokens: tokens, - level: state.level, - width: width, - height: height - }); + token = state.push('image', 'img', 0); + token.attrs = attrs = [ [ 'src', href ], + [ 'alt', '' ] ]; + token.children = tokens; + if (title) { + attrs.push([ 'title', title ]); + } + + if (width !== '') { + attrs.push([ 'width', width ]); + } + + if (height !== '') { + attrs.push([ 'height', height ]); + } } state.pos = pos; @@ -201,23 +210,6 @@ function image_with_size(md, options) { }; } -function tokenize_imsize(md) { - return function(tokens, idx, options, env, self) { - var src = ' src="' + md.utils.escapeHtml(tokens[idx].src) + '"'; - var title = ''; - if (tokens[idx].title) { - title = ' title="' + md.utils.escapeHtml(md.utils.replaceEntities(tokens[idx].title)) + '"'; - } - var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"'; - var width = tokens[idx].width !== '' ? ' width="' + tokens[idx].width + '"' : ''; - var height = tokens[idx].height !== '' ? ' height="' + tokens[idx].height + '"' : ''; - var size = width + height; - var suffix = options.xhtmlOut ? ' /' : ''; - return '<img' + src + alt + title + size + suffix + '>'; - }; -} - module.exports = function imsize_plugin(md, options) { - md.renderer.rules.image = tokenize_imsize(md); md.inline.ruler.before('emphasis', 'image', image_with_size(md, options)); }; diff --git a/package.json b/package.json index 3e33b85cd674e37c256491ce4915a38dd6bc0ba5..8b68adbb76c7bdfa348153f7bf9d301be25fe0e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "markdown-it-imsize", - "version": "1.1.6", + "version": "2.0.0", "description": "Markdown-it plugin to specify image size", "main": "lib/index.js", "scripts": { @@ -22,6 +22,7 @@ ], "dependencies": {}, "devDependencies": { + "chai": "^2.2.0", "coveralls": "^2.11.2", "eslint": "^0.18.0", "eslint-plugin-nodeca": "^1.0.3", diff --git a/test/commonmark.js b/test/commonmark.js index 3b76b10b8ec7fdf66888a49d3758b5ce5862a803..ff4a0aa3d251dc0b0848cc2fbc5e3a5c242bf46d 100644 --- a/test/commonmark.js +++ b/test/commonmark.js @@ -1,13 +1,33 @@ 'use strict'; -var path = require('path'); +var p = require('path'); +var load = require('markdown-it-testgen').load; +var assert = require('chai').assert; +function normalize(text) { + return text.replace(/<blockquote>\n<\/blockquote>/g, '<blockquote></blockquote>'); +} -var generate = require('markdown-it-testgen'); + +function generate(path, md) { + load(path, function (data) { + data.meta = data.meta || {}; + + var desc = data.meta.desc || p.relative(path, data.file); + + (data.meta.skip ? describe.skip : describe)(desc, function () { + data.fixtures.forEach(function (fixture) { + it(fixture.header ? fixture.header : 'line ' + (fixture.first.range[0] - 1), function () { + assert.strictEqual(md.render(fixture.first.text), normalize(fixture.second.text)); + }); + }); + }); + }); +} describe('CommonMark', function () { var md = require('markdown-it')('commonmark').use(require('../')); - generate(path.join(__dirname, 'fixtures/commonmark/good.txt'), md); + generate(p.join(__dirname, 'fixtures/commonmark/good.txt'), md); }); diff --git a/test/fixtures/markdown-it-imsize/imsize.txt b/test/fixtures/markdown-it-imsize/imsize.txt index 95c429b8c41101d4847b9d5bb933ed1cf4af7ffa..6b23e5a2af8a3512fa4ad63cc36bd2c1bce0b0a3 100644 --- a/test/fixtures/markdown-it-imsize/imsize.txt +++ b/test/fixtures/markdown-it-imsize/imsize.txt @@ -94,7 +94,7 @@ Normalize link destination, but not text inside it: . <http://example.com/α%CE%B2γ%CE%B4> . -<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/α%CE%B2γ%CE%B4</a></p> +<p><a href="http://example.com/%CE%B1%CE%B2%CE%B3%CE%B4">http://example.com/αβγδ</a></p> .