-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(injector): add exec() method
- Remove injectorFilter from plugin dir - Move cache into Injector Class - Remove decache
- Loading branch information
Showing
7 changed files
with
202 additions
and
205 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 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,52 +1,6 @@ | ||
'use strict'; | ||
|
||
const { Cache } = require('hexo-util'); | ||
const cache = new Cache(); | ||
|
||
function injectFilter(data, locals = { page: {} }) { | ||
const Injector = this.extend.injector; | ||
|
||
const current = () => { | ||
const { page } = locals; | ||
|
||
if (page.__index) return 'home'; | ||
if (page.__post) return 'post'; | ||
if (page.__page) return 'page'; | ||
if (page.archive) return 'archive'; | ||
if (page.category) return 'category'; | ||
if (page.tag) return 'tag'; | ||
if (page.layout) return page.layout; | ||
return 'default'; | ||
}; | ||
|
||
function injector(data, pattern, flag, isBegin = true) { | ||
if (data.includes(`hexo injector ${flag}`)) return data; | ||
|
||
const currentType = current(); | ||
const code = cache.apply(`${flag}-${currentType}-code`, () => { | ||
const content = currentType === 'default' ? Injector.getText(flag, 'default') : Injector.getText(flag, currentType) + Injector.getText(flag, 'default'); | ||
|
||
if (!content.length) return ''; | ||
|
||
return '<!-- hexo injector ' + flag + ' start -->' + content + '<!-- hexo injector ' + flag + ' end -->'; | ||
}); | ||
|
||
// avoid unnesscary replace() for better performance | ||
if (!code.length) return data; | ||
|
||
return data.replace(pattern, str => { return isBegin ? str + code : code + str; }); | ||
} | ||
|
||
// Inject head_begin | ||
data = injector(data, /<head.*?>/, 'head_begin', true); | ||
// Inject head_end | ||
data = injector(data, '</head>', 'head_end', false); | ||
// Inject body_begin | ||
data = injector(data, /<body.*?>/, 'body_begin', true); | ||
// Inject body_end | ||
data = injector(data, '</body>', 'body_end', false); | ||
|
||
return data; | ||
} | ||
|
||
module.exports = injectFilter; | ||
module.exports = ctx => { | ||
// eslint-disable-next-line no-unused-vars | ||
const { injector } = ctx.extend; | ||
}; |
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
Oops, something went wrong.