Skip to content

Commit 570b46b

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 570b46b

File tree

3 files changed

+73
-55
lines changed

3 files changed

+73
-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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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+
hexo.init();
12+
var img = require('../../../lib/plugins/tag/img')(hexo);
813

914
it('src', function(){
1015
var $ = cheerio.load(img(['http://placekitten.com/200/300']));
@@ -13,9 +18,13 @@ describe('img', function(){
1318
});
1419

1520
it('internal src', function(){
21+
hexo.config.root = '/';
1622
var $ = cheerio.load(img(['/images/test.jpg']));
17-
1823
$('img').attr('src').should.eql('/images/test.jpg');
24+
25+
hexo.config.root = '/root/';
26+
$ = cheerio.load(img(['/images/test.jpg']));
27+
$('img').attr('src').should.eql('/root/images/test.jpg');
1928
});
2029

2130
it('class + src', function(){
@@ -26,10 +35,15 @@ describe('img', function(){
2635
});
2736

2837
it('class + internal src', function(){
38+
hexo.config.root = '/';
2939
var $ = cheerio.load(img('left /images/test.jpg'.split(' ')));
30-
3140
$('img').attr('src').should.eql('/images/test.jpg');
3241
$('img').attr('class').should.eql('left');
42+
43+
hexo.config.root = '/root/';
44+
$ = cheerio.load(img('left /images/test.jpg'.split(' ')));
45+
$('img').attr('src').should.eql('/root/images/test.jpg');
46+
$('img').attr('class').should.eql('left');
3347
});
3448

3549
it('multiple classes + src', function(){
@@ -40,10 +54,15 @@ describe('img', function(){
4054
});
4155

4256
it('multiple classes + internal src', function(){
57+
hexo.config.root = '/';
4358
var $ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
44-
4559
$('img').attr('src').should.eql('/images/test.jpg');
4660
$('img').attr('class').should.eql('left top');
61+
62+
hexo.config.root = '/root/';
63+
$ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
64+
$('img').attr('src').should.eql('/root/images/test.jpg');
65+
$('img').attr('class').should.eql('left top');
4766
});
4867

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

0 commit comments

Comments
 (0)