Skip to content

Commit eb01dc5

Browse files
authored
Add order for theme_inject (#1136)
1 parent 2c630a1 commit eb01dc5

File tree

11 files changed

+49
-46
lines changed

11 files changed

+49
-46
lines changed

_config.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,18 +608,23 @@ comments:
608608
active:
609609
# Setting `true` means remembering the comment system selected by the visitor.
610610
storage: true
611-
# Modify icons and texts for any style, here are some examples.
611+
# Modify texts or order for any navs, here are some examples.
612612
nav:
613-
#disqus: disqus
614-
#facebook_comments_plugin: <i class="fa fa-facebook-official" aria-hidden="true"></i> facebook
615-
#gitalk: Load Gitalk
613+
#disqus:
614+
# text: Load Disqus
615+
# order: -1
616+
#facebook_comments_plugin:
617+
# text: <i class="fa fa-facebook-official" aria-hidden="true"></i> facebook
618+
#gitalk:
619+
# order: -2
616620

617621
# Disqus
618622
disqus:
619623
enable: false
620624
shortname:
621625
count: true
622626
lazyload: false
627+
#post_meta_order: 0
623628

624629
# DisqusJS
625630
# Alternative Disqus - Render comment component using Disqus API.
@@ -641,6 +646,7 @@ changyan:
641646
enable: false
642647
appid:
643648
appkey:
649+
#post_meta_order: 0
644650

645651
# Valine
646652
# You can get your appid and appkey from https://leancloud.cn
@@ -658,6 +664,7 @@ valine:
658664
language: # Language, available values: en, zh-cn
659665
visitor: false # leancloud-counter-security is not supported for now. When visitor is set to be true, appid and appkey are recommended to be the same as leancloud_visitors' for counter compatibility. Article reading statistic https://valine.js.org/visitor.html
660666
comment_count: true # If false, comment count will only be displayed in post page, not in home page
667+
#post_meta_order: 0
661668

662669
# LiveRe comments system
663670
# You can get your uid from https://livere.com/insight/myCode (General web site)
@@ -701,6 +708,7 @@ facebook_comments_plugin:
701708
num_of_posts: 10 # Minimum posts num is 1
702709
width: 100% # Default width is 550px
703710
scheme: light # Default scheme is light (light or dark)
711+
#post_meta_order: 0
704712

705713
# VKontakte API Support
706714
# To get your AppID visit https://vk.com/editapp?act=create

scripts/events/lib/injects.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ class ViewInject {
2020
this.raws = [];
2121
}
2222
raw(name, raw, ...args) {
23-
this.raws.push({
24-
name,
25-
raw,
26-
args
27-
});
23+
this.raws.push({name, raw, args});
2824
}
2925
file(name, file, ...args) {
30-
this.raw.apply(this, [name, fs.readFileSync(file).toString()].concat(args));
26+
this.raw(name, fs.readFileSync(file, 'utf8'), ...args);
3127
}
3228
}
3329

@@ -56,19 +52,24 @@ module.exports = hexo => {
5652

5753
// Inject views
5854
points.views.forEach(type => {
55+
let configs = Object.create(null);
5956
hexo.theme.config.injects[type] = [];
60-
injects[type].raws.forEach(injectObj => {
61-
// If there is no suffix, will add `.swig`
57+
injects[type].raws.forEach((injectObj, index) => {
58+
// If there is no suffix, will add `.swig`.
6259
if (injectObj.name.indexOf('.') < 0) {
6360
injectObj.name += '.swig';
6461
}
65-
let viewName = `inject/${type}/${injectObj.name}`;
66-
hexo.theme.setView(viewName, injectObj.raw);
67-
hexo.theme.config.injects[type].push({
68-
layout : viewName,
62+
let name = `inject/${type}/${injectObj.name}`;
63+
// Add or override view.
64+
hexo.theme.setView(name, injectObj.raw);
65+
configs[name] = {
66+
layout : name,
6967
locals : injectObj.args[0],
70-
options: injectObj.args[1]
71-
});
68+
options: injectObj.args[1],
69+
order : injectObj.args[2] || index
70+
};
7271
});
72+
hexo.theme.config.injects[type] = Object.values(configs)
73+
.sort((x, y) => x.order - y.order);
7374
});
7475
};

scripts/filters/comment/changyan.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const path = require('path');
66
const {iconText} = require('./common');
7-
const priority = hexo.config.inject_priority || {};
87

98
// Add comment
109
hexo.extend.filter.register('theme_inject', injects => {
@@ -19,7 +18,7 @@ hexo.extend.filter.register('theme_inject', injects => {
1918

2019
injects.bodyEnd.file('changyan', path.join(hexo.theme_dir, 'layout/_third-party/comments/changyan.swig'));
2120

22-
}, priority.changyan);
21+
});
2322

2423
// Add post_meta
2524
hexo.extend.filter.register('theme_inject', injects => {
@@ -41,6 +40,6 @@ hexo.extend.filter.register('theme_inject', injects => {
4140
{% endif %}
4241
</span>
4342
{% endif %}
44-
`);
43+
`, {}, {}, theme.changyan.post_meta_order);
4544

46-
}, priority.changyan_post_meta);
45+
});

scripts/filters/comment/default-config.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ hexo.extend.filter.register('theme_inject', injects => {
2222
}
2323
// Set custom button content
2424
if (config.nav) {
25-
let customButton = config.nav[locals.configKey];
26-
if (customButton) {
27-
locals.button = customButton;
25+
let nav = config.nav[locals.configKey] || {};
26+
if (nav.order) {
27+
element.args[2] = nav.order;
28+
}
29+
if (nav.text) {
30+
locals.button = nav.text;
2831
}
2932
}
3033
});

scripts/filters/comment/disqus.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const path = require('path');
66
const {iconText} = require('./common');
7-
const priority = hexo.config.inject_priority || {};
87

98
// Add comment
109
hexo.extend.filter.register('theme_inject', injects => {
@@ -23,7 +22,7 @@ hexo.extend.filter.register('theme_inject', injects => {
2322

2423
injects.bodyEnd.file('disqus', path.join(hexo.theme_dir, 'layout/_third-party/comments/disqus.swig'));
2524

26-
}, priority.disqus);
25+
});
2726

2827
// Add post_meta
2928
hexo.extend.filter.register('theme_inject', injects => {
@@ -39,6 +38,6 @@ hexo.extend.filter.register('theme_inject', injects => {
3938
#}</a>
4039
</span>
4140
{% endif %}
42-
`);
41+
`, {}, {}, theme.disqus.post_meta_order);
4342

44-
}, priority.disqus_post_meta);
43+
});

scripts/filters/comment/disqusjs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
'use strict';
44

55
const path = require('path');
6-
const priority = hexo.config.inject_priority || {};
76

87
// Add comment
98
hexo.extend.filter.register('theme_inject', injects => {
@@ -22,4 +21,4 @@ hexo.extend.filter.register('theme_inject', injects => {
2221

2322
injects.bodyEnd.file('disqusjs', path.join(hexo.theme_dir, 'layout/_third-party/comments/disqusjs.swig'));
2423

25-
}, priority.disqusjs);
24+
});

scripts/filters/comment/facebook-comments-plugin.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
'use strict';
44

55
const {iconText} = require('./common');
6-
const priority = hexo.config.inject_priority || {};
76

87
// Add comment
98
hexo.extend.filter.register('theme_inject', injects => {
@@ -24,7 +23,7 @@ hexo.extend.filter.register('theme_inject', injects => {
2423
button : '<i class="fa fa-facebook-official" aria-hidden="true"></i> facebook'
2524
});
2625

27-
}, priority.facebook_comments_plugin);
26+
});
2827

2928
// Add post_meta
3029
hexo.extend.filter.register('theme_inject', injects => {
@@ -40,6 +39,6 @@ hexo.extend.filter.register('theme_inject', injects => {
4039
#}</a>
4140
</span>
4241
{% endif %}
43-
`);
42+
`, {}, {}, theme.facebook_comments_plugin.post_meta_order);
4443

45-
}, priority.facebook_comments_plugin_post_meta);
44+
});

scripts/filters/comment/gitalk.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
'use strict';
44

55
const path = require('path');
6-
const priority = hexo.config.inject_priority || {};
76

87
// Add comment
98
hexo.extend.filter.register('theme_inject', injects => {
@@ -14,4 +13,4 @@ hexo.extend.filter.register('theme_inject', injects => {
1413

1514
injects.bodyEnd.file('gitalk', path.join(hexo.theme_dir, 'layout/_third-party/comments/gitalk.swig'));
1615

17-
}, priority.gitalk);
16+
});

scripts/filters/comment/livere.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
'use strict';
44

55
const path = require('path');
6-
const priority = hexo.config.inject_priority || {};
76

87
// Add comment
98
hexo.extend.filter.register('theme_inject', injects => {
@@ -18,4 +17,4 @@ hexo.extend.filter.register('theme_inject', injects => {
1817

1918
injects.bodyEnd.file('livere', path.join(hexo.theme_dir, 'layout/_third-party/comments/livere.swig'));
2019

21-
}, priority.livere);
20+
});

scripts/filters/comment/valine.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const path = require('path');
66
const {iconText} = require('./common');
7-
const priority = hexo.config.inject_priority || {};
87

98
// Add comment
109
hexo.extend.filter.register('theme_inject', injects => {
@@ -15,7 +14,7 @@ hexo.extend.filter.register('theme_inject', injects => {
1514

1615
injects.bodyEnd.file('valine', path.join(hexo.theme_dir, 'layout/_third-party/comments/valine.swig'));
1716

18-
}, priority.valine);
17+
});
1918

2019
// Add post_meta
2120
hexo.extend.filter.register('theme_inject', injects => {
@@ -31,6 +30,6 @@ hexo.extend.filter.register('theme_inject', injects => {
3130
#}</a>
3231
</span>
3332
{% endif %}
34-
`);
33+
`, {}, {}, theme.valine.post_meta_order);
3534

36-
}, priority.valine_post_meta);
35+
});

0 commit comments

Comments
 (0)