Skip to content

Commit

Permalink
feat(inject): support inject dest
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 5, 2020
1 parent 9bb0f43 commit 044dad5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
42 changes: 39 additions & 3 deletions lib/plugins/filter/after_render/injector.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
'use strict';

function injectFilter(data) {
function injectFilter(data, locals = { page: {} }) {
const { injector: _Injector } = this.extend;

const is = input => {
let result = true;
const { page } = locals;

switch (input) {
case 'home':
result = Boolean(page.__index);
break;

case 'post':
result = Boolean(page.__post);
break;

case 'page':
result = Boolean(page.__page);
break;

case 'archive':
result = Boolean(page.archive);
break;

case 'category':
result = Boolean(page.category);
break;

case 'tag':
result = Boolean(page.tag);
break;

default:
result = true;
}

return result;
};

function injector(data, pattern, flag, isBegin = true) {
return data.replace(pattern, str => {
if (data.includes(`hexo injector ${flag}`)) return str;

const arr = _Injector.get(flag);
const arr = _Injector.get(flag).filter(i => is(i.to)).map(i => i.value);

if (arr.length) {
const code = arr.reduce((a, c) => a + c.value, '');
const code = arr.reduce((a, c) => a + c, '');

if (isBegin) {
return str
Expand Down
68 changes: 66 additions & 2 deletions test/scripts/filters/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

describe('Injector', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const injectorFilter = require('../../../lib/plugins/filter/after_render/injector').bind(hexo);
let hexo, injectorFilter;

beforeEach(() => {
hexo = new Hexo();
injectorFilter = require('../../../lib/plugins/filter/after_render/injector').bind(hexo);
});

const content = [
'<!DOCTYPE html>',
Expand Down Expand Up @@ -52,6 +56,12 @@ describe('Injector', () => {
'</html>'
].join('');

hexo.extend.injector.register('head_begin', '<!-- Powered by Hexo -->');
hexo.extend.injector.register('head_end', '<link href="prism.css" rel="stylesheet">');
hexo.extend.injector.register('head_end', '<link href="prism-linenumber.css" rel="stylesheet">');
hexo.extend.injector.register('body_begin', '<script>window.Prism = window.Prism || {}; window.Prism.manual = true;</script>');
hexo.extend.injector.register('body_end', '<script src="prism.js"></script>');

const result = injectorFilter(content);

result.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- hexo injector head_begin end -->');
Expand All @@ -73,11 +83,65 @@ describe('Injector', () => {
'</html>'
].join('\n');

hexo.extend.injector.register('head_begin', '<!-- Powered by Hexo -->');
hexo.extend.injector.register('head_end', '<link href="prism.css" rel="stylesheet">');
hexo.extend.injector.register('head_end', '<link href="prism-linenumber.css" rel="stylesheet">');
hexo.extend.injector.register('body_begin', '<script>window.Prism = window.Prism || {}; window.Prism.manual = true;</script>');
hexo.extend.injector.register('body_end', '<script src="prism.js"></script>');

const result = injectorFilter(content);

result.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- Powered by Hexo --><!-- hexo injector head_begin end -->');
result.should.contain('<!-- hexo injector head_end start --><link href="prism.css" rel="stylesheet"><link href="prism-linenumber.css" rel="stylesheet"><!-- hexo injector head_end end --></head>');
result.should.contain('<body id="body"><!-- hexo injector body_begin start --><script>window.Prism = window.Prism || {}; window.Prism.manual = true;</script><!-- hexo injector body_begin end -->');
result.should.contain('<!-- hexo injector body_end start --><script src="prism.js"></script><!-- hexo injector body_end end --></body>');
});

it('inject on specific page', () => {
const content = [
'<!DOCTYPE html>',
'<html lang="en">',
'<head id="head"><title>Test</title>',
'</head>',
'<body id="body">',
'<div></div>',
'<p></p>',
'</body>',
'</html>'
].join('\n');

hexo.extend.injector.register('head_begin', '<!-- head_begin_home -->', 'home');
hexo.extend.injector.register('head_begin', '<!-- head_begin_post -->', 'post');
hexo.extend.injector.register('head_begin', '<!-- head_begin_page -->', 'page');
hexo.extend.injector.register('head_begin', '<!-- head_begin_archive -->', 'archive');
hexo.extend.injector.register('head_begin', '<!-- head_begin_category -->', 'category');
hexo.extend.injector.register('head_begin', '<!-- head_begin_tag -->', 'tag');

const result1 = injectorFilter(content, { page: {} });
const result2 = injectorFilter(content, { page: { __index: true } });
const result3 = injectorFilter(content, { page: { __post: true } });
const result4 = injectorFilter(content, { page: { __page: true } });
const result5 = injectorFilter(content, { page: { archive: true } });
const result6 = injectorFilter(content, { page: { category: true } });
const result7 = injectorFilter(content, { page: { tag: true } });

// home
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_home --><!-- hexo injector head_begin end -->');
result2.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_home --><!-- hexo injector head_begin end -->');
// post
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_post --><!-- hexo injector head_begin end -->');
result3.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_post --><!-- hexo injector head_begin end -->');
// page
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_page --><!-- hexo injector head_begin end -->');
result4.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_page --><!-- hexo injector head_begin end -->');
// archive
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_archive --><!-- hexo injector head_begin end -->');
result5.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_archive --><!-- hexo injector head_begin end -->');
// category
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_category --><!-- hexo injector head_begin end -->');
result6.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_category --><!-- hexo injector head_begin end -->');
// tag
result1.should.not.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_tag --><!-- hexo injector head_begin end -->');
result7.should.contain('<head id="head"><!-- hexo injector head_begin start --><!-- head_begin_tag --><!-- hexo injector head_begin end -->');
});
});

0 comments on commit 044dad5

Please sign in to comment.