Skip to content

Commit

Permalink
refactor(imgTag): utilize url_for (#3973)
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh authored and SukkaW committed Dec 16, 2019
1 parent f37557a commit c82082c
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions lib/plugins/tag/img.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const url = require('url');
const { htmlTag } = require('hexo-util');
const { htmlTag, url_for } = require('hexo-util');

const rUrl = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\w]*))?)/;
const rMeta = /["']?([^"']+)?["']?\s*["']?([^"']+)?["']?/;
Expand All @@ -13,23 +12,6 @@ const rMeta = /["']?([^"']+)?["']?\s*["']?([^"']+)?["']?/;
* {% img [class names] /path/to/image [width] [height] [title text [alt text]] %}
*/
module.exports = ctx => {
const { config } = ctx;

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

const data = url.parse(path);

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

path = config.root + path;

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

return function imgTag(args, content) {
const classes = [];
Expand All @@ -39,7 +21,7 @@ module.exports = ctx => {
while (args.length > 0) {
const item = args.shift();
if (rUrl.test(item) || item[0] === '/') {
src = makeUrl(item);
src = url_for.call(ctx, item);
break;
} else {
classes.push(item);
Expand Down

0 comments on commit c82082c

Please sign in to comment.