Skip to content

Commit 6bc0526

Browse files
author
Xuanwo
committed
Merge pull request #1444 from liuhongjiang/prepend-root-to-img-tag
Support root path configure for img tag
2 parents 6b5fad0 + 657f9e7 commit 6bc0526

File tree

10 files changed

+129
-87
lines changed

10 files changed

+129
-87
lines changed

lib/models/post.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = function(ctx) {
4747
});
4848

4949
Post.virtual('permalink').get(function() {
50-
return ctx.config.url + '/' + this.path;
50+
var url_for = ctx.extend.helper.get('url_for');
51+
return ctx.config.url + url_for.call(ctx, this.path);
5152
});
5253

5354
Post.virtual('full_source').get(function() {

lib/plugins/tag/img.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,69 @@ 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 i = 0;
26+
var len = args.length;
27+
var url_for = ctx.extend.helper.get('url_for');
1528

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-
var i = 0;
26-
var len = args.length;
29+
// Find image URL and class name
30+
for (; i < len; i++) {
31+
item = args[i];
2732

28-
// Find image URL and class name
29-
for (; i < len; i++) {
30-
item = args[i];
31-
32-
if (rUrl.test(item)) {
33-
src = item;
34-
break;
35-
} else {
36-
if (item[0] === '/') {
37-
src = item;
33+
if (rUrl.test(item)) {
34+
src = url_for.call(ctx, item);
3835
break;
3936
} else {
40-
classes.push(item);
37+
if (item[0] === '/') {
38+
src = url_for.call(ctx, item);
39+
break;
40+
} else {
41+
classes.push(item);
42+
}
4143
}
4244
}
43-
}
4445

45-
// Delete image URL and class name from arguments
46-
args = args.slice(i + 1);
46+
// Delete image URL and class name from arguments
47+
args = args.slice(i + 1);
4748

48-
// Find image width and height
49-
if (args.length) {
50-
if (!/\D+/.test(args[0])) {
51-
width = args.shift();
49+
// Find image width and height
50+
if (args.length) {
51+
if (!/\D+/.test(args[0])) {
52+
width = args.shift();
5253

53-
if (args.length && !/\D+/.test(args[0])) {
54-
height = args.shift();
54+
if (args.length && !/\D+/.test(args[0])) {
55+
height = args.shift();
56+
}
5557
}
58+
59+
meta = args.join(' ');
5660
}
5761

58-
meta = args.join(' ');
59-
}
62+
// Find image title and alt
63+
if (meta && rMeta.test(meta)) {
64+
var match = meta.match(rMeta);
65+
title = match[1];
66+
alt = match[2];
67+
}
6068

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

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

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/helpers/list_archives.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ describe('list_archives', function() {
2222
}
2323

2424
before(function() {
25-
return Post.insert([
26-
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
27-
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
28-
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
29-
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
30-
]).then(function() {
25+
return hexo.init().then(function() {
26+
return Post.insert([
27+
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
28+
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
29+
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
30+
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
31+
]);
32+
}).then(function() {
3133
resetLocals();
3234
});
3335
});

test/scripts/helpers/list_categories.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('list_categories', function() {
1818
var listCategories = require('../../../lib/plugins/helper/list_categories').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
return Promise.each([
2830
['baz'],
2931
['baz', 'bar'],

test/scripts/helpers/list_posts.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ describe('list_posts', function() {
1818
hexo.config.permalink = ':title/';
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
23-
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
24-
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
25-
]).then(function() {
26-
return hexo.init();
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
24+
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
25+
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
26+
]);
2727
}).then(function() {
2828
hexo.locals.invalidate();
2929
ctx.site = hexo.locals.toObject();

test/scripts/helpers/list_tags.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('list_tags', function() {
1818
var listTags = require('../../../lib/plugins/helper/list_tags').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
// TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
2830
return Promise.each([
2931
['foo'],

test/scripts/helpers/tagcloud.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ describe('tagcloud', function() {
1818
var tagcloud = require('../../../lib/plugins/helper/tagcloud').bind(ctx);
1919

2020
before(function() {
21-
return Post.insert([
22-
{source: 'foo', slug: 'foo'},
23-
{source: 'bar', slug: 'bar'},
24-
{source: 'baz', slug: 'baz'},
25-
{source: 'boo', slug: 'boo'}
26-
]).then(function(posts) {
21+
return hexo.init().then(function() {
22+
return Post.insert([
23+
{source: 'foo', slug: 'foo'},
24+
{source: 'bar', slug: 'bar'},
25+
{source: 'baz', slug: 'baz'},
26+
{source: 'boo', slug: 'boo'}
27+
]);
28+
}).then(function(posts) {
2729
// TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
2830
return Promise.each([
2931
['bcd'],

test/scripts/models/post.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe('Post', function() {
7777
});
7878

7979
it('permalink - virtual', function() {
80+
hexo.config.root = '/';
8081
return Post.insert({
8182
source: 'foo.md',
8283
slug: 'bar'
@@ -86,6 +87,17 @@ describe('Post', function() {
8687
});
8788
});
8889

90+
it('permalink_root_prefix - virtual', function() {
91+
hexo.config.root = '/root/';
92+
return Post.insert({
93+
source: 'foo.md',
94+
slug: 'bar'
95+
}).then(function(data) {
96+
data.permalink.should.eql(hexo.config.url + '/root/' + data.path);
97+
return Post.removeById(data._id);
98+
});
99+
});
100+
89101
it('full_source - virtual', function() {
90102
return Post.insert({
91103
source: 'foo.md',

test/scripts/tags/img.js

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

3+
var pathFn = require('path');
34
var cheerio = require('cheerio');
45
var should = require('chai').should(); // eslint-disable-line
56

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

916
it('src', function() {
1017
var $ = cheerio.load(img(['http://placekitten.com/200/300']));
@@ -13,9 +20,13 @@ describe('img', function() {
1320
});
1421

1522
it('internal src', function() {
23+
hexo.config.root = '/';
1624
var $ = cheerio.load(img(['/images/test.jpg']));
17-
1825
$('img').attr('src').should.eql('/images/test.jpg');
26+
27+
hexo.config.root = '/root/';
28+
$ = cheerio.load(img(['/images/test.jpg']));
29+
$('img').attr('src').should.eql('/root/images/test.jpg');
1930
});
2031

2132
it('class + src', function() {
@@ -26,10 +37,15 @@ describe('img', function() {
2637
});
2738

2839
it('class + internal src', function() {
40+
hexo.config.root = '/';
2941
var $ = cheerio.load(img('left /images/test.jpg'.split(' ')));
30-
3142
$('img').attr('src').should.eql('/images/test.jpg');
3243
$('img').attr('class').should.eql('left');
44+
45+
hexo.config.root = '/root/';
46+
$ = cheerio.load(img('left /images/test.jpg'.split(' ')));
47+
$('img').attr('src').should.eql('/root/images/test.jpg');
48+
$('img').attr('class').should.eql('left');
3349
});
3450

3551
it('multiple classes + src', function() {
@@ -40,10 +56,15 @@ describe('img', function() {
4056
});
4157

4258
it('multiple classes + internal src', function() {
59+
hexo.config.root = '/';
4360
var $ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
44-
4561
$('img').attr('src').should.eql('/images/test.jpg');
4662
$('img').attr('class').should.eql('left top');
63+
64+
hexo.config.root = '/root/';
65+
$ = cheerio.load(img('left top /images/test.jpg'.split(' ')));
66+
$('img').attr('src').should.eql('/root/images/test.jpg');
67+
$('img').attr('class').should.eql('left top');
4768
});
4869

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

0 commit comments

Comments
 (0)