Skip to content

Commit

Permalink
Move to merge-config.js and add default injects
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtj committed May 7, 2019
1 parent 1840a3a commit e781a25
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ cache:
# Redefine custom file paths. Introduced in NexT v6.0.2. If commented, will be used default custom file paths.
# For example, you want to put your custom styles file outside theme directory in root `source/_data`, set `styles: source/_data/styles.styl`
#custom_file_path:
#head: source/_data/head.swig
#header: source/_data/header.swig
#sidebar: source/_data/sidebar.swig

# Default path: source/css/_variables/custom.styl
#variables: source/_data/variables.styl
# Default path: source/css/_mixins/custom.styl
Expand Down
18 changes: 18 additions & 0 deletions scripts/filters/default-injects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* global hexo */

'use strict';

hexo.extend.filter.register('theme_inject', function (injects) {
let custom_file_path = hexo.theme.config.custom_file_path;
if (custom_file_path) {
if (custom_file_path.head) {
injects.head.file('custom', custom_file_path.head);
}
if (custom_file_path.header) {
injects.header.file('custom', custom_file_path.header);
}
if (custom_file_path.sidebar) {
injects.sidebar.file('custom', custom_file_path.sidebar);
}
}
});
14 changes: 5 additions & 9 deletions scripts/filters/injects.js → scripts/injects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

const fs = require('fs');

const injectType = ['head', 'header', 'sidebar'];

class Inject {
constructor() {
this.raws = [];
Expand All @@ -16,22 +18,16 @@ class Inject {
});
}
file(name, file, ...args) {
this.raws.push({
name,
raw: fs.readFileSync(file).toString(),
args
});
this.raw(name, fs.readFileSync(file).toString(), args);
}
}

const injectType = ['head', 'header', 'sidebar'];

const injects = {};
injectType.forEach((item) => {
injects[item] = new Inject();
});

hexo.on('generateBefore', function() {
module.exports = function(hexo) {

hexo.execFilterSync('theme_inject', injects);
hexo.theme.config.injects = {};
Expand All @@ -48,4 +44,4 @@ hexo.on('generateBefore', function() {
});
});

});
}
4 changes: 4 additions & 0 deletions scripts/merge-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ hexo.on('generateBefore', function() {
}
}
}

// Add filter type `theme_inject`
require('./injects')(hexo);

});

0 comments on commit e781a25

Please sign in to comment.