Skip to content

Commit 4ad77fe

Browse files
committed
Support root path configure for img tag
Current img tag doesn't prepend root path if it's not a slash. With this fix, the root path will be prepend to the img src if it's not a slash. This fix only works for the hexo tags, does not work for the markdown syntax of links and images. The issues of markdown links and images should be fixed in the hexo-renderer-marked module. Resolves: #1440
1 parent 14d2b9b commit 4ad77fe

File tree

3 files changed

+76
-55
lines changed

3 files changed

+76
-55
lines changed

lib/plugins/tag/img.js

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,66 @@ var rMeta = /["']?([^"']+)?["']?\s*["']?([^"']+)?["']?/;
1212
* Syntax:
1313
* {% img [class names] /path/to/image [width] [height] [title text [alt text]] %}
1414
*/
15+
module.exports = function(ctx) {
16+
return function imgTag(args, content){
17+
var classes = [];
18+
var meta = '';
19+
var width;
20+
var height;
21+
var title;
22+
var alt;
23+
var src;
24+
var item = '';
25+
var url_for = ctx.extend.helper.get('url_for');
1526

16-
function imgTag(args, content){
17-
var classes = [];
18-
var meta = '';
19-
var width;
20-
var height;
21-
var title;
22-
var alt;
23-
var src;
24-
var item = '';
25-
26-
// Find image URL and class name
27-
for (var i = 0, len = args.length; i < len; i++){
28-
item = args[i];
29-
30-
if (rUrl.test(item)){
31-
src = item;
32-
break;
33-
} else {
34-
if (item[0] === '/'){
35-
src = item;
27+
// Find image URL and class name
28+
for (var i = 0, len = args.length; i < len; i++){
29+
item = args[i];
30+
if (rUrl.test(item)){
31+
src = url_for.call(ctx, item);
3632
break;
3733
} else {
38-
classes.push(item);
34+
if (item[0] === '/'){
35+
src = url_for.call(ctx, item);
36+
break;
37+
} else {
38+
classes.push(item);
39+
}
3940
}
4041
}
41-
}
4242

43-
// Delete image URL and class name from arguments
44-
args = args.slice(i + 1);
43+
// Delete image URL and class name from arguments
44+
args = args.slice(i + 1);
4545

46-
// Find image width and height
47-
if (args.length){
48-
if (!/\D+/.test(args[0])){
49-
width = args.shift();
46+
// Find image width and height
47+
if (args.length){
48+
if (!/\D+/.test(args[0])){
49+
width = args.shift();
5050

51-
if (args.length && !/\D+/.test(args[0])){
52-
height = args.shift();
51+
if (args.length && !/\D+/.test(args[0])){
52+
height = args.shift();
53+
}
5354
}
55+
56+
meta = args.join(' ');
5457
}
5558

56-
meta = args.join(' ');
57-
}
59+
// Find image title and alt
60+
if (meta && rMeta.test(meta)){
61+
var match = meta.match(rMeta);
62+
title = match[1];
63+
alt = match[2];
64+
}
5865

59-
// Find image title and alt
60-
if (meta && rMeta.test(meta)){
61-
var match = meta.match(rMeta);
62-
title = match[1];
63-
alt = match[2];
64-
}
66+
var attrs = {
67+
src: src,
68+
class: classes.join(' '),
69+
width: width,
70+
height: height,
71+
title: title,
72+
alt: alt
73+
};
6574

66-
var attrs = {
67-
src: src,
68-
class: classes.join(' '),
69-
width: width,
70-
height: height,
71-
title: title,
72-
alt: alt
75+
return htmlTag('img', attrs);
7376
};
74-
75-
return htmlTag('img', attrs);
76-
}
77-
78-
module.exports = imgTag;
77+
};

lib/plugins/tag/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function(ctx){
1717

1818
tag.register('iframe', require('./iframe'));
1919

20-
var img = require('./img');
20+
var img = require('./img')(ctx);
2121

2222
tag.register('img', img);
2323
tag.register('image', img);

test/scripts/tags/img.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
'use strict';
22

3+
var pathFn = require('path');
34
var cheerio = require('cheerio');
45
var should = require('chai').should();
6+
var Promise = require('bluebird');
57

68
describe('img', function(){
7-
var img = require('../../../lib/plugins/tag/img');
9+
var Hexo = require('../../../lib/hexo');
10+
var hexo = new Hexo(pathFn.join(__dirname, 'img_test'));
11+
var img = require('../../../lib/plugins/tag/img')(hexo);
12+
13+
before(function(){
14+
return hexo.init();
15+
});
816

917
it('src', function(){
1018
var $ = cheerio.load(img(['http://placekitten.com/200/300']));
@@ -13,9 +21,13 @@ describe('img', function(){
1321
});
1422

1523
it('internal src', function(){
24+
hexo.config.root = '/';
1625
var $ = cheerio.load(img(['/images/test.jpg']));
17-
1826
$('img').attr('src').should.eql('/images/test.jpg');
27+
28+
hexo.config.root = '/root/';
29+
$ = cheerio.load(img(['/images/test.jpg']));
30+
$('img').attr('src').should.eql('/root/images/test.jpg');
1931
});
2032

2133
it('class + src', function(){
@@ -26,10 +38,15 @@ describe('img', function(){
2638
});
2739

2840
it('class + internal src', function(){
41+
hexo.config.root = '/';
2942
var $ = cheerio.load(img('left /images/test.jpg'.split(' ')));
30-
3143
$('img').attr('src').should.eql('/images/test.jpg');
3244
$('img').attr('class').should.eql('left');
45+
46+
hexo.config.root = '/root/';
47+
$ = cheerio.load(img('left /images/test.jpg'.split(' ')));
48+
$('img').attr('src').should.eql('/root/images/test.jpg');
49+
$('img').attr('class').should.eql('left');
3350
});
3451

3552
it('multiple classes + src', function(){
@@ -40,10 +57,15 @@ describe('img', function(){
4057
});
4158

4259
it('multiple classes + internal src', function(){
60+
hexo.config.root = '/';
4361
var $ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
44-
4562
$('img').attr('src').should.eql('/images/test.jpg');
4663
$('img').attr('class').should.eql('left top');
64+
65+
hexo.config.root = '/root/';
66+
$ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
67+
$('img').attr('src').should.eql('/root/images/test.jpg');
68+
$('img').attr('class').should.eql('left top');
4769
});
4870

4971
it('class + src + width', function(){

0 commit comments

Comments
 (0)