Skip to content
Snippets Groups Projects
Commit d8450990 authored by tatsy's avatar tatsy
Browse files

Support markdown-it 4.0.0

parent 27282834
No related branches found
No related tags found
No related merge requests found
...@@ -6,12 +6,11 @@ ...@@ -6,12 +6,11 @@
var sizeOf = require('./imsize'); var sizeOf = require('./imsize');
var parseImageSize = require('./helpers/parse_image_size'); var parseImageSize = require('./helpers/parse_image_size');
var normalizeReference = require('./helpers/normalize_reference.js');
function image_with_size(md, options) { function image_with_size(md, options) {
return function(state, silent) { return function(state, silent) {
var code, var attrs,
href, code,
label, label,
labelEnd, labelEnd,
labelStart, labelStart,
...@@ -21,8 +20,10 @@ function image_with_size(md, options) { ...@@ -21,8 +20,10 @@ function image_with_size(md, options) {
title, title,
width = '', width = '',
height = '', height = '',
token,
tokens, tokens,
start, start,
href = '',
oldPos = state.pos, oldPos = state.pos,
max = state.posMax; max = state.posMax;
...@@ -55,12 +56,14 @@ function image_with_size(md, options) { ...@@ -55,12 +56,14 @@ function image_with_size(md, options) {
// ^^^^^^ parsing link destination // ^^^^^^ parsing link destination
start = pos; start = pos;
res = md.helpers.parseLinkDestination(state.src, pos, state.posMax); res = md.helpers.parseLinkDestination(state.src, pos, state.posMax);
if (res.ok && state.md.inline.validateLink(res.str)) { if (res.ok) {
href = res.str; href = state.md.normalizeLink(res.str);
if (state.md.validateLink(href)) {
pos = res.pos; pos = res.pos;
} else { } else {
href = ''; href = '';
} }
}
// [link]( <href> "title" ) // [link]( <href> "title" )
// ^^ skipping these spaces // ^^ skipping these spaces
...@@ -146,7 +149,7 @@ function image_with_size(md, options) { ...@@ -146,7 +149,7 @@ function image_with_size(md, options) {
// (collapsed reference link and shortcut reference link respectively) // (collapsed reference link and shortcut reference link respectively)
if (!label) { label = state.src.slice(labelStart, labelEnd); } if (!label) { label = state.src.slice(labelStart, labelEnd); }
ref = state.env.references[normalizeReference(label)]; ref = state.env.references[md.utils.normalizeReference(label)];
if (!ref) { if (!ref) {
state.pos = oldPos; state.pos = oldPos;
return false; return false;
...@@ -184,40 +187,29 @@ function image_with_size(md, options) { ...@@ -184,40 +187,29 @@ function image_with_size(md, options) {
} }
} }
state.push({ token = state.push('image', 'img', 0);
type: 'image', token.attrs = attrs = [ [ 'src', href ],
src: href, [ 'alt', '' ] ];
title: title, token.children = tokens;
tokens: tokens, if (title) {
level: state.level, attrs.push([ 'title', title ]);
width: width,
height: height
});
} }
state.pos = pos; if (width !== '') {
state.posMax = max; attrs.push([ 'width', width ]);
return true;
};
} }
function tokenize_imsize(md) { if (height !== '') {
return function(tokens, idx, options, env, self) { attrs.push([ 'height', height ]);
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 + '"' : ''; state.pos = pos;
var size = width + height; state.posMax = max;
var suffix = options.xhtmlOut ? ' /' : ''; return true;
return '<img' + src + alt + title + size + suffix + '>';
}; };
} }
module.exports = function imsize_plugin(md, options) { 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)); md.inline.ruler.before('emphasis', 'image', image_with_size(md, options));
}; };
{ {
"name": "markdown-it-imsize", "name": "markdown-it-imsize",
"version": "1.1.6", "version": "2.0.0",
"description": "Markdown-it plugin to specify image size", "description": "Markdown-it plugin to specify image size",
"main": "lib/index.js", "main": "lib/index.js",
"scripts": { "scripts": {
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
], ],
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"chai": "^2.2.0",
"coveralls": "^2.11.2", "coveralls": "^2.11.2",
"eslint": "^0.18.0", "eslint": "^0.18.0",
"eslint-plugin-nodeca": "^1.0.3", "eslint-plugin-nodeca": "^1.0.3",
......
'use strict'; '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 () { describe('CommonMark', function () {
var md = require('markdown-it')('commonmark').use(require('../')); 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);
}); });
...@@ -94,7 +94,7 @@ Normalize link destination, but not text inside it: ...@@ -94,7 +94,7 @@ Normalize link destination, but not text inside it:
. .
<http://example.com/α%CE%B2γ%CE%B4> <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>
. .
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment