diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index 43c05343a9..cfd5d0c53e 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -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*["']?([^"']+)?["']?/; @@ -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 = []; @@ -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);