Skip to content

Commit

Permalink
Merge branch 'master' into post-render-backtick-code-escape
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Apr 27, 2020
2 parents 28f25b4 + c51b87e commit e08cc70
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 213 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tester

on: [push, pull_request]

jobs:
tester:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: ['10.x', '12.x', '13.x']
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache NPM dependencies
uses: actions/cache@v1
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: ${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Test
run: npm test
env:
CI: true
4 changes: 2 additions & 2 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { stripIndent } = require('hexo-util');
const { cyan } = require('chalk');
const nunjucks = require('nunjucks');
const { Environment } = require('hexo-renderer-nunjucks');
const Promise = require('bluebird');
const placeholder = '\uFFFC';
const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
Expand Down Expand Up @@ -178,7 +178,7 @@ const formatNunjucksError = (err, input) => {

class Tag {
constructor() {
this.env = new nunjucks.Environment(null, {
this.env = new Environment(null, {
autoescape: false
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class Hexo extends EventEmitter {
require.extensions = Module._extensions;
require.cache = Module._cache;

script = `(function(exports, require, module, __filename, __dirname, hexo){${script}});`;
script = `(function(exports, require, module, __filename, __dirname, hexo){${script}\n});`;

const fn = runInThisContext(script, path);

Expand Down
9 changes: 1 addition & 8 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ class Post {
return Promise.reject(new Error('No input file or string!')).asCallback(callback);
}

const isSwig = ext === 'swig';

// disable Nunjucks when the renderer spcify that.
const disableNunjucks = ext && ctx.render.renderer.get(ext) && !!ctx.render.renderer.get(ext).disableNunjucks;

Expand All @@ -255,12 +253,7 @@ class Post {
// Run "before_post_render" filters
return ctx.execFilter('before_post_render', data, {context: ctx});
}).then(() => {
if (isSwig) {
// Render with Nunjucks if this is a swig file
return tag.render(data.content, data);
}

// Escape all Swig tags
// Escape all Nunjucks/Swig tags
if (!disableNunjucks) {
data.content = cacheObj.escapeAllSwigTags(data.content);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = ctx => {

renderer.register('json', 'json', require('./json'), true);

renderer.register('swig', 'html', require('./swig'), true);
const nunjucks = require('hexo-renderer-nunjucks');
nunjucks.register(ctx);

const yaml = require('./yaml');

Expand Down
45 changes: 0 additions & 45 deletions lib/plugins/renderer/swig.js

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@
"hexo-fs": "^2.0.0",
"hexo-i18n": "^1.0.0",
"hexo-log": "^1.0.0",
"hexo-renderer-nunjucks": "^2.0.0",
"hexo-util": "^1.9.0",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"micromatch": "^4.0.2",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"nunjucks": "^3.1.3",
"pretty-hrtime": "^1.0.3",
"resolve": "^1.8.1",
"strip-ansi": "^6.0.0",
"swig-extras": "0.0.1",
"swig-templates": "^2.0.3",
"text-table": "^0.2.0",
"tildify": "^2.0.0",
"titlecase": "^1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe('Box', () => {
});

it('watch() - update with simple "ignore" option', async () => {
const box = newBox('test', {ignore: '**/ignore_me'});
const box = newBox('test', {ignore: '**/ignore_me/**'});
const path1 = 'a.txt';
const path2 = 'b.txt';
const src1 = join(box.base, path1);
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('Box', () => {
});

it('watch() - update with complex "ignore" option', async () => {
const box = newBox('test', {ignore: ['**/ignore_me', '**/ignore_me_too.txt']});
const box = newBox('test', {ignore: ['**/ignore_me/**', '**/ignore_me_too.txt']});
const path1 = 'a.txt';
const path2 = 'b.txt';
const path3 = 'ignore_me_too.txt';
Expand Down
4 changes: 2 additions & 2 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ describe('generate', () => {
// Add some source files
writeFile(join(hexo.theme_dir, 'source', 'a.txt'), 'a'),
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'b'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'c')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'c')
]);
await generate();

// Update source file
await Promise.all([
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'bb'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'cc')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'cc')
]);

// Generate again
Expand Down
8 changes: 4 additions & 4 deletions test/scripts/extend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ describe('Renderer', () => {

r.isRenderableSync('yaml').should.be.false;

r.register('swig', 'html', () => {}, true);
r.register('njk', 'html', () => {}, true);

r.isRenderableSync('swig').should.be.true;
r.isRenderableSync('.swig').should.be.true;
r.isRenderableSync('layout.swig').should.be.true;
r.isRenderableSync('njk').should.be.true;
r.isRenderableSync('.njk').should.be.true;
r.isRenderableSync('layout.njk').should.be.true;
r.isRenderableSync('foo.html').should.be.false;
});

Expand Down
16 changes: 8 additions & 8 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('partial', () => {
const hexo = new Hexo(pathFn.join(__dirname, 'partial_test'), {silent: true});
const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test');
const viewDir = pathFn.join(themeDir, 'layout') + pathFn.sep;
const viewName = 'article.swig';
const viewName = 'article.njk';

const ctx = {
site: hexo.locals,
Expand All @@ -32,7 +32,7 @@ describe('partial', () => {
fs.writeFile(hexo.config_path, 'theme: test')
]);
await hexo.init();
hexo.theme.setView('widget/tag.swig', 'tag widget');
hexo.theme.setView('widget/tag.njk', 'tag widget');
});

after(() => fs.rmdir(hexo.base_dir));
Expand All @@ -52,28 +52,28 @@ describe('partial', () => {
});

it('locals', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}).should.eql('bar');
});

it('cache', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar');
partial('test', {}, {cache: true}).should.eql('bar');
});

it('only', () => {
hexo.theme.setView('test.swig', '{{ foo }}{{ bar }}');
hexo.theme.setView('test.njk', '{{ foo }}{{ bar }}');

partial('test', {bar: 'bar'}, {only: true}).should.eql('bar');
});

it('a partial in another partial', () => {
hexo.theme.setView('partial/a.swig', '{{ partial("b") }}');
hexo.theme.setView('partial/b.swig', '{{ partial("c") }}');
hexo.theme.setView('partial/c.swig', 'c');
hexo.theme.setView('partial/a.njk', '{{ partial("b") }}');
hexo.theme.setView('partial/b.njk', '{{ partial("c") }}');
hexo.theme.setView('partial/c.njk', 'c');

partial('partial/a').should.eql('c');
});
Expand Down
22 changes: 11 additions & 11 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe('Hexo', () => {
});

it('_generate() - layout', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ config.title }}',
'{{ page.foo }}',
'{{ layout }}',
Expand All @@ -378,7 +378,7 @@ describe('Hexo', () => {
});

it('_generate() - layout array', () => {
hexo.theme.setView('baz.swig', 'baz');
hexo.theme.setView('baz.njk', 'baz');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand Down Expand Up @@ -418,7 +418,7 @@ describe('Hexo', () => {
it('_generate() - after_route_render filter', () => {
const hook = spy(result => result.replace('foo', 'bar'));
hexo.extend.filter.register('after_route_render', hook);
hexo.theme.setView('test.swig', 'foo');
hexo.theme.setView('test.njk', 'foo');
hexo.extend.generator.register('test', () => ({
path: 'test',
layout: 'test'
Expand All @@ -442,7 +442,7 @@ describe('Hexo', () => {
});

it('_generate() - validate locals', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ path }}',
'{{ url }}',
'{{ view_dir }}'
Expand All @@ -464,7 +464,7 @@ describe('Hexo', () => {
const path = 'bár';
hexo.config.url = 'http://fôo.com';

hexo.theme.setView('test.swig', '{{ url }}');
hexo.theme.setView('test.njk', '{{ url }}');

hexo.extend.generator.register('test', () => ({
path,
Expand All @@ -488,7 +488,7 @@ describe('Hexo', () => {
it('_generate() - reset cache for new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -508,7 +508,7 @@ describe('Hexo', () => {
it('_generate() - cache disabled and use new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -526,7 +526,7 @@ describe('Hexo', () => {
});

it('_generate() - cache disabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -535,12 +535,12 @@ describe('Hexo', () => {

return hexo._generate({cache: false})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '1'));
});

it('_generate() - cache enabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -549,7 +549,7 @@ describe('Hexo', () => {

return hexo._generate({cache: true})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '0')); // should return cached result
});

Expand Down
Loading

0 comments on commit e08cc70

Please sign in to comment.