From d84509903630e221aecde51a9191957dd1b9a6b2 Mon Sep 17 00:00:00 2001
From: tatsy <tatsy.mail@gmail.com>
Date: Thu, 2 Apr 2015 19:13:49 +0900
Subject: [PATCH] Support markdown-it 4.0.0

---
 lib/index.js                                | 62 +++++++++------------
 package.json                                |  3 +-
 test/commonmark.js                          | 26 ++++++++-
 test/fixtures/markdown-it-imsize/imsize.txt |  2 +-
 4 files changed, 53 insertions(+), 40 deletions(-)

diff --git a/lib/index.js b/lib/index.js
index 3b74210..ac39034 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 3e33b85..8b68adb 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 3b76b10..ff4a0aa 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 95c429b..6b23e5a 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>
 .
 
 
-- 
GitLab