Skip to content

Commit

Permalink
Remove url_for helper reference in img tag
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jun 28, 2016
1 parent fc4ca63 commit b530591
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/plugins/tag/img.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var url = require('url');
var util = require('hexo-util');
var htmlTag = util.htmlTag;

Expand All @@ -13,7 +14,26 @@ var rMeta = /["']?([^"']+)?["']?\s*["']?([^"']+)?["']?/;
* {% img [class names] /path/to/image [width] [height] [title text [alt text]] %}
*/
module.exports = function(ctx) {
var config = ctx.config;

function makeUrl(path) {
if (path[0] === '#' || path.substring(0, 2) === '//') {
return path;
}

var data = url.parse(path);

if (data && data.protocol) {
return path;
}

path = config.root + path;

return path.replace(/\/{2,}/g, '/');
}

return function imgTag(args, content) {

var classes = [];
var meta = '';
var width;
Expand All @@ -24,18 +44,17 @@ module.exports = function(ctx) {
var item = '';
var i = 0;
var len = args.length;
var url_for = ctx.extend.helper.get('url_for');

// Find image URL and class name
for (; i < len; i++) {
item = args[i];

if (rUrl.test(item)) {
src = url_for.call(ctx, item);
src = makeUrl(item);
break;
} else {
if (item[0] === '/') {
src = url_for.call(ctx, item);
src = makeUrl(item);
break;
} else {
classes.push(item);
Expand Down

0 comments on commit b530591

Please sign in to comment.