Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set inject default extname from file's extname #1281

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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