Skip to content

Commit

Permalink
Migrate to Nunjucks [3] (theme-next#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Oct 18, 2019
1 parent b8eeeed commit 0a3f3c3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 143 deletions.
4 changes: 2 additions & 2 deletions layout/_macro/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
{#####################}

{%- if theme.related_posts.enable and (theme.related_posts.display_in_home or not is_index) %}
{% include '../_partials/post/post-related.swig' %}
{{ partial('_partials/post/post-related.swig') }}
{%- endif %}

{%- if not is_index %}
Expand All @@ -240,7 +240,7 @@
{%- endif %}

{%- if theme.creative_commons.license and theme.creative_commons.post %}
{% include '../_partials/post/post-copyright.swig' %}
{{ partial('_partials/post/post-copyright.swig') }}
{%- endif %}

<footer class="post-footer">
Expand Down
1 change: 1 addition & 0 deletions layout/_partials/footer.swig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{%- endif %}

<div class="copyright">
{% set copyright_year = date(null, 'YYYY') %}
&copy; {% if theme.footer.since and theme.footer.since != copyright_year %}{{ theme.footer.since }} – {% endif %}
<span itemprop="copyrightYear">{{ copyright_year }}</span>
<span class="with-love">
Expand Down
19 changes: 10 additions & 9 deletions layout/_third-party/comments/disqus.swig
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
{%- endif %}
{%- if page.comments %}
<script>
function disqus_config() {
this.page.url = {{ page.permalink | json }};
this.page.identifier = {{ page.path | json }};
this.page.title = '{{ page.title | addslashes }}';
{%- if __('disqus') !== 'disqus' %}
this.language = '{{ __('disqus') }}';
{%- endif %}
}
function loadComments() {
if (window.DISQUS) {
DISQUS.reset({
reload: true,
config: disqus_config
config: {
{%- if __('disqus') !== 'disqus' -%}
language: '{{ __('disqus') }}',
{% endif -%}
page: {
url: {{ page.permalink | json }},
identifier: {{ page.path | json }},
title: {{ page.title | json }}
}
}
});
} else {
var d = document, s = d.createElement('script');
Expand Down
4 changes: 2 additions & 2 deletions layout/_third-party/math/index.swig
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

{%- if not theme.math.per_page or is_index_has_math or page.mathjax %}
{%- if theme.math.mathjax.enable %}
{% include 'mathjax.swig' %}
{% include '_third-party/math/mathjax.swig' %}
{% elif theme.math.katex.enable %}
{% include 'katex.swig' %}
{% include '_third-party/math/katex.swig' %}
{%- endif %}
{%- endif %}
{%- endif %}
1 change: 1 addition & 0 deletions scripts/events/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = hexo => {
hexo.log.warn('Since caching is turned on, the `relative_link` option in Hexo `_config.yml` is set to `false` to avoid potential hazards.');
hexo.config.relative_link = false;
}
hexo.config.meta_generator = false;

// Custom languages support. Introduced in NexT v6.3.0.
if (data.languages) {
Expand Down
36 changes: 0 additions & 36 deletions scripts/filters/excerpt.js

This file was deleted.

47 changes: 0 additions & 47 deletions scripts/filters/exturl.js

This file was deleted.

23 changes: 0 additions & 23 deletions scripts/filters/lazyload.js

This file was deleted.

20 changes: 9 additions & 11 deletions scripts/filters/locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

const path = require('path');

hexo.extend.filter.register('template_locals', function(locals) {
const { config } = this;
const { __ } = locals;
hexo.extend.filter.register('template_locals', locals => {
const { env, config } = hexo;
const { __, theme } = locals;
// Hexo & NexT version
locals.hexo_version = this.env.version;
locals.hexo_version = env.version;
locals.next_version = require(path.normalize('../../package.json')).version;
// Language & Config
locals.title = __('title') !== 'title' && __('title') || config.title;
locals.subtitle = __('subtitle') !== 'subtitle' && __('subtitle') || config.subtitle;
locals.author = __('author') !== 'author' && __('author') || config.author;
locals.description = __('description') !== 'description' && __('description') || config.description;
// Current year
locals.copyright_year = new Date().getFullYear();
locals.title = __('title') !== 'title' ? __('title') : config.title;
locals.subtitle = __('subtitle') !== 'subtitle' ? __('subtitle') : config.subtitle;
locals.author = __('author') !== 'author' ? __('author') : config.author;
locals.description = __('description') !== 'description' ? __('description') : config.description;
// PJAX
locals.pjax = this.theme.config.pjax ? ' pjax' : '';
locals.pjax = theme.pjax ? ' pjax' : '';
});
77 changes: 77 additions & 0 deletions scripts/filters/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* global hexo */

'use strict';

hexo.extend.filter.register('after_post_render', data => {
const { config } = hexo;
const theme = hexo.theme.config;
const filters = {
excerpt : theme.auto_excerpt && theme.auto_excerpt.enable && data.excerpt === '',
exturl : theme.exturl,
lazyload: theme.lazyload
};
if (!filters.excerpt && !filters.exturl && !filters.lazyload) {
return;
}
const cheerio = require('cheerio');
const $ = cheerio.load(data.content, {
decodeEntities: false
});
if (filters.lazyload) {
$('img').each((i, o) => {
let src = $(o).attr('src');
$(o).attr('data-src', src).removeAttr('src');
});
}
if (filters.exturl) {
const url = require('url');
var siteHost = url.parse(config.url).hostname || config.url;

$('a').each((i, o) => {
var href = $(o).attr('href');
// Exit if the href attribute doesn't exists.
if (!href) return;

var data = url.parse(href);

// Exit if the url has same host with `config.url`, which means it's an internal link
if (!data.protocol || data.hostname === siteHost) return;

// If title atribute filled, set it as title; if not, set url as title.
var title = $(o).attr('title') || href;

var encoded = Buffer.from(href).toString('base64');

$(o).replaceWith(() => {
return $(`<span class="exturl" data-url="${encoded}" title="${title}">${$(o).html()}<i class="fa fa-external-link"></i></span>`);
});
});
}
if (filters.excerpt) {
const elements = $.root().children();
const _$ = cheerio.load('', {
decodeEntities: false
});
var length = 0;
elements.each((i, o) => {
if (length > theme.auto_excerpt.length) {
return;
}
length += $(o).text().length;
_$.root().append($(o).remove());
});

if ($.root().children().length) {
data.excerpt = _$.html();
data.more = $.html();
data.content = data.excerpt + '<a id="more"></a>' + data.more;
} else {
data.excerpt = '';
data.content = _$.html();
data.more = data.content;
}
} else {
data.content = $.html();
}

}, 20);
3 changes: 1 addition & 2 deletions scripts/helpers/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ hexo.extend.helper.register('next_vendors', function(url) {
hexo.extend.helper.register('post_edit', function(src) {
const theme = hexo.theme.config;
if (!theme.post_edit.enable) return '';
const editIcon = '<i class="fa fa-pencil"></i>';
return this.next_url(theme.post_edit.url + src, editIcon, {
return this.next_url(theme.post_edit.url + src, '<i class="fa fa-pencil"></i>', {
class: 'post-edit-link',
title: this.__('post.edit')
});
Expand Down
4 changes: 0 additions & 4 deletions scripts/helpers/next-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* next-url.js | https://theme-next.org/api/helpers/next-url/
*/

/* global hexo */

'use strict';
Expand Down
12 changes: 5 additions & 7 deletions scripts/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ const path = require('path');

function njkCompile(data) {
const templateDir = path.dirname(data.path);
const config = Object.assign({
autoescape: false,
watch: false
}, hexo.config.nunjucks);
const env = nunjucks.configure(templateDir, config);
const env = nunjucks.configure(templateDir, {
autoescape: false
});
env.addFilter('attr', function(dictionary, key, value) {
dictionary[key] = value;
return dictionary;
Expand All @@ -32,8 +30,8 @@ njkRenderer.compile = function(data) {
// Need a closure to keep the compiled template.
return function(locals, callback) {
return compiledTemplate.render(locals, callback);
}
}
};
};

hexo.extend.renderer.register('njk', 'html', njkRenderer);
hexo.extend.renderer.register('swig', 'html', njkRenderer);

0 comments on commit 0a3f3c3

Please sign in to comment.