-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new filter type
theme_inject
(#868)
* Add theme_inject filter * Fix QA * Move to merge-config.js and add default injects * fix config * Fix QA * Fix file function args * Add new injectType * Fix QA * Update config file * Format. remove inject_name * Format * Fix QA * Add a new inject point `footer` * Add warn log * Update doc * Add `postMeta` and `postBodyEnd`
- Loading branch information
Showing
19 changed files
with
167 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
{{ partial('_partials/header/brand.swig', {}, {cache: theme.cache.enable}) }} | ||
{% include 'menu.swig' %} | ||
|
||
{% if theme.custom_file_path.header %} | ||
{% set custom_header = '../../../../../' + theme.custom_file_path.header %} | ||
{% else %} | ||
{% set custom_header = '../../_custom/header.swig' %} | ||
{% endif %} | ||
{% include custom_header %} | ||
{% for inject_item in theme.injects.header %} | ||
{{ partial(inject_item.layout, inject_item.locals, inject_item.options) }} | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* global hexo */ | ||
|
||
'use strict'; | ||
|
||
const points = require('./../injects-point'); | ||
|
||
hexo.extend.filter.register('theme_inject', (injects) => { | ||
let filePath = hexo.theme.config.custom_file_path; | ||
|
||
points.views.forEach((key) => { | ||
if (filePath[key]) { | ||
injects[key].file('custom', filePath[key]); | ||
} | ||
}); | ||
|
||
points.styles.forEach((key) => { | ||
if (filePath[key]) { | ||
injects[key].push(filePath[key]); | ||
} | ||
// Compatible, but like head header etc, it shouldn't add 's' suffix. | ||
let oldKey = key + 's'; | ||
if (filePath[oldKey]) { | ||
injects[key].push(filePath[oldKey]); | ||
hexo.log.warn(`WARNING: Format. Please use 'custom_file_path.${key}' instead 'custom_file_path.${oldKey}'.`); | ||
} | ||
}); | ||
|
||
// Compatible, but please use custom_file_path. | ||
if (hexo.theme.config.footer.custom_text) { | ||
injects.footer.raw('custom-text', ` | ||
<div class="footer-custom">{{ theme.footer.custom_text }}</div> | ||
`, {}, {cache: true}); | ||
hexo.log.warn(`WARNING: 'footer.custom_text' will not longer be supported. Please use 'custom_file_path.footer' instead`); | ||
} | ||
|
||
}, 99); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
views: ['head', 'header', 'sidebar', 'postMeta', 'postBodyEnd', 'footer', 'bodyEnd'], | ||
styles: ['variable', 'mixin', 'style'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const points = require('./injects-point'); | ||
|
||
// Defining stylus types | ||
class StylusInject { | ||
constructor() { | ||
this.files = []; | ||
} | ||
push(file) { | ||
this.files.push(file); | ||
} | ||
} | ||
|
||
// Defining view types | ||
class ViewInject { | ||
constructor() { | ||
this.raws = []; | ||
} | ||
raw(name, raw, ...args) { | ||
this.raws.push({ | ||
name, | ||
raw, | ||
args | ||
}); | ||
} | ||
file(name, file, ...args) { | ||
this.raw.apply(this, [name, fs.readFileSync(file).toString()].concat(args)); | ||
} | ||
} | ||
|
||
// Init injects | ||
function initInject() { | ||
let injects = {}; | ||
points.styles.forEach((item) => { | ||
injects[item] = new StylusInject(); | ||
}); | ||
points.views.forEach((item) => { | ||
injects[item] = new ViewInject(); | ||
}); | ||
return injects; | ||
} | ||
|
||
module.exports = function(hexo) { | ||
// Exec theme_inject filter | ||
let injects = initInject(); | ||
hexo.execFilterSync('theme_inject', injects); | ||
hexo.theme.config.injects = {}; | ||
|
||
// Inject stylus, and get relative path base on hexo dir. | ||
points.styles.forEach((type) => { | ||
hexo.theme.config.injects[type] = injects[type].files.map((item) => path.relative(hexo.base_dir, item)); | ||
}); | ||
|
||
// Inject views | ||
points.views.forEach((type) => { | ||
hexo.theme.config.injects[type] = []; | ||
injects[type].raws.forEach((injectObj) => { | ||
// If there is no suffix, will add `.swig` | ||
if (injectObj.name.indexOf('.') < 0) { | ||
injectObj.name += '.swig'; | ||
} | ||
let viewName = `inject/${type}/${injectObj.name}`; | ||
hexo.theme.setView(viewName, injectObj.raw); | ||
hexo.theme.config.injects[type].push({ | ||
layout: viewName, | ||
locals: injectObj.args[0], | ||
options: injectObj.args[1] | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,8 @@ hexo.on('generateBefore', function() { | |
} | ||
} | ||
} | ||
|
||
// Add filter type `theme_inject` | ||
require('./injects')(hexo); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters