Skip to content

Commit

Permalink
Fix bugs caused by this and optimize the loading of cheerio (theme-…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored and 1v9 committed Aug 1, 2019
1 parent 68196e0 commit d5ad1d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
14 changes: 8 additions & 6 deletions scripts/filters/exturl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ hexo.extend.filter.register('after_post_render', data => {
if (!cheerio) cheerio = require('cheerio');

const $ = cheerio.load(data.content, {decodeEntities: false});
const links = $('a');
if (!links.length) return data;

var config = this.config;
var config = hexo.config;
var siteHost = url.parse(config.url).hostname || config.url;

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

Expand All @@ -32,12 +34,12 @@ hexo.extend.filter.register('after_post_render', data => {
if (data.hostname === siteHost) return;

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

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

$(this).replaceWith(() => {
return $(`<span class="exturl" data-url="${encoded}" title="${title}">${$(this).html()}<i class="fa fa-external-link"></i></span>`);
$(o).replaceWith(() => {
return $(`<span class="exturl" data-url="${encoded}" title="${title}">${$(o).html()}<i class="fa fa-external-link"></i></span>`);
});

});
Expand Down
7 changes: 3 additions & 4 deletions scripts/filters/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ hexo.extend.filter.register('after_post_render', data => {
if (!cheerio) cheerio = require('cheerio');

const $ = cheerio.load(data.content, {decodeEntities: false});

const images = $('img');
if (!images.length) return data;

Array.from(images).forEach(img => {
let src = $(img).attr('src');
$(img).attr('data-src', src).removeAttr('src');
images.each((i, o) => {
let src = $(o).attr('src');
$(o).attr('data-src', src).removeAttr('src');
});

data.content = $.html();
Expand Down
4 changes: 2 additions & 2 deletions scripts/hexo-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ hexo.on('generateBefore', () => {

// Custom languages support. Introduced in NexT v6.3.0.
if (data && data.languages) {
var lang = this.config.language;
var i18n = this.theme.i18n;
var lang = hexo.config.language;
var i18n = hexo.theme.i18n;

var mergeLang = lang => {
i18n.set(lang, merge(i18n.get([lang]), data.languages[lang]));
Expand Down

0 comments on commit d5ad1d6

Please sign in to comment.