Skip to content

Commit

Permalink
Set default extname from file's extname (theme-next#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtj authored Nov 22, 2019
1 parent e2ff43d commit a2abaaf
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions scripts/events/lib/injects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs');
const path = require('path');
const points = require('./injects-point');
const defaultExtname = '.swig';

// Defining stylus types
class StylusInject {
Expand All @@ -23,9 +24,17 @@ class ViewInject {
this.raws = [];
}
raw(name, raw, ...args) {
// Set default extname
if (path.extname(name) === '') {
name += defaultExtname;
}
this.raws.push({name, raw, args});
}
file(name, file, ...args) {
// Set default extname from file's extname
if (path.extname(name) === '') {
name += path.extname(file);
}
// Get absolute path base on hexo dir
this.raw(name, fs.readFileSync(path.resolve(this.base_dir, file), 'utf8'), ...args);
}
Expand Down Expand Up @@ -58,13 +67,9 @@ module.exports = hexo => {
points.views.forEach(type => {
let configs = Object.create(null);
hexo.theme.config.injects[type] = [];
// Add or override view.
injects[type].raws.forEach((injectObj, index) => {
// If there is no suffix, `.swig` will be added
if (injectObj.name.indexOf('.') < 0) {
injectObj.name += '.swig';
}
let name = `inject/${type}/${injectObj.name}`;
// Add or override view.
hexo.theme.setView(name, injectObj.raw);
configs[name] = {
layout : name,
Expand All @@ -73,6 +78,7 @@ module.exports = hexo => {
order : injectObj.args[2] || index
};
});
// Views sort.
hexo.theme.config.injects[type] = Object.values(configs)
.sort((x, y) => x.order - y.order);
});
Expand Down

0 comments on commit a2abaaf

Please sign in to comment.