Skip to content

Commit

Permalink
fix(#25): Remove cheerio
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyib committed Jan 12, 2020
1 parent d412a73 commit 31d6356
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 108 deletions.
83 changes: 47 additions & 36 deletions scripts/filters/external-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,60 @@

'use strict';

hexo.extend.filter.register('after_post_render', function(data) {
hexo.extend.filter.register('after_post_render', function (data) {
var theme = hexo.theme.config;
if (!(theme.external_link && theme.external_link.icon && theme.external_link.icon.enable)) return;
if (
!theme.external_link ||
!theme.external_link.icon ||
!theme.external_link.icon.enable
) {
return;
}

var url = require('url');
var cheerio;
var config = this.config;

if (!cheerio) cheerio = require('cheerio');

var $ = cheerio.load(data.content, { decodeEntities: false });
var siteHost = url.parse(config.url).hostname || config.url;

$('a').each(function() {
var href = $(this).attr('href');

if (!href) return;

var className = $(this).attr('class');
var classNameWhitelist = [
'friends-plugin__item',
];

if (className && classNameWhitelist.includes(className)) return;

var data = url.parse(href);

if (!data.protocol) return;
if (data.hostname === siteHost) return;

var fa_prefix = theme.fa_prefix || 'fa';

$(this).replaceWith(function() {
return $(
data.content = data.content.replace(
/(<a([^>]*)href="([^"]*)"([^>]*)>([^<]*)<\/a>)/gim,
function (match, all, attrBegin, href, attrEnd, html) {
// Exit if the href attribute doesn't exists.
if (!href) {
return match;
}

// Exit if the url has same host with `config.url`, which means isn't an external link.
var link = url.parse(href);
if (!link.protocol || link.hostname === siteHost) {
return match;
};

var attrOther = attrBegin + attrEnd;
var className = '';

attrOther.split(/\s/gim).forEach(attr => {
var nAttr = attr.replace(/["']*/gim, '');
var aKey = (nAttr.split('=')[0] || '').trim();
var aValue = (nAttr.split('=')[1] || '').trim();

if (aKey === 'class') {
className = aValue;
}
});

// Exit if the class name is in whitelist.
var whiteList = ['friends-plugin__item'];
if (className && whiteList.includes(className)) {
return match;
}

var fa_prefix = theme.fa_prefix || 'fa';
return (
'<span class="external-link">' +
'<a href="' + href + '" target="_blank" rel="noopener">' +
$(this).html() +
'</a>' +
'<i class="' + fa_prefix + ' fa-external-link"></i>' +
`<a ${attrBegin} href="${href}" ${attrEnd}>${html}</a>` +
`<i class="${fa_prefix} fa-external-link"></i>` +
'</span>'
);
});
});

data.content = $.html();
}
);
}, 0);
59 changes: 29 additions & 30 deletions scripts/filters/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@

'use strict';

hexo.extend.filter.register('after_post_render', function(data) {
hexo.extend.filter.register('after_post_render', function (data) {
var theme = hexo.theme.config;
if (!(theme.lazyload && theme.lazyload.enable)) return;

var cheerio;

if (!cheerio) cheerio = require('cheerio');

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

$('img').each(function () {
var $img = $(this);
// 最小 1 * 1 像素的透明 gif 图片
var loadingGIF = `/${(theme.images && theme.images.replace(/^\/+|\/+$/gm,'')) || 'images'}/loading.svg`;
var loadingBlock = 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=';
var placeholder;

if (theme.lazyload.placeholder === 'gif') {
placeholder = loadingGIF;
} else if (theme.lazyload.placeholder === 'block') {
placeholder = loadingBlock;
}

$img.addClass('lazyload');
$img.attr('data-src', $img.attr('src'));
$img.attr('src', placeholder);

if (theme.lazyload.placeholder) {
$img.addClass(theme.lazyload.placeholder);
if (!theme.lazyload || !theme.lazyload.enable) {
return;
}

data.content = data.content.replace(
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match;
}

// Smallest 1 * 1 pixel transparent gif
var loadingBlock = 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=';
var loadingGIF = `/${(theme.images && theme.images.replace(/^\/+|\/+$/gm,'')) || 'images'}/loading.svg`;
var phClassName = theme.lazyload.placeholder;
var placeholder = '';

if (phClassName === 'gif') {
placeholder = loadingGIF;
} else if (phClassName === 'block') {
placeholder = loadingBlock;
}

return `
<img ${attrBegin} class="lazyload lazyload-${phClassName}"
src="${placeholder}" data-src="${src}" ${attrEnd}>`;
}
});
data.content = $.html();
);
}, 1);
34 changes: 22 additions & 12 deletions scripts/filters/post-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
'use strict';

hexo.extend.filter.register('after_post_render', function (data) {
var cheerio;
var tagName = `h[1-6]`;
var attrId = `id="([^"]*)"`;
var tagNotEnd = `([^>]*)`;
var regAttrId = tagNotEnd + attrId + tagNotEnd;
var regHTagInnermost = new RegExp(
`<${tagName}${regAttrId}>((?:(?!<\\/?${tagName}${regAttrId}>)(?:\\s|\\S))*?)<\/${tagName}>`,
'gim'
);

if (!cheerio) cheerio = require('cheerio');
data.content = data.content.replace(
regHTagInnermost,
function (match, attrBegin, id, attrEnd, html) {
if (!id) {
return match;
}

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

$('h1,h2,h3,h4,h5,h6').each(function () {
var $heading = $(this);
var headingTxt = $heading.text().trim();

$heading.html('<span class="heading-link">' + headingTxt + '</span>');
});

data.content = $.html();
var newHtml = (html.replace(/<[^>]+>/gim, '') || '').trim();
return `
<h2 ${attrBegin} id="${id}" ${attrEnd}>
<span class="heading-link">${newHtml}</span>
</h2>
`;
}
);
}, 0);
58 changes: 39 additions & 19 deletions scripts/filters/resize-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,44 @@
'use strict';

hexo.extend.filter.register('after_post_render', function (data) {
var cheerio;

if (!cheerio) cheerio = require('cheerio');

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

$('img').each(function () {
var $img = $(this);

if (!$img.attr('src')) return;
if ($img.attr('src').includes('?size=')) {
var size = $img.attr('src').split('?size=')[1] && $img.attr('src').split('?size=')[1].toLowerCase();
var w = size.split('x')[0] + 'px';
var h = size.split('x')[1] + 'px';

$img.css({ width: w, height: h });
data.content = data.content.replace(
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match;
}

// A flag that sets the size of image.
var RESIZE_IMG_SIGN = '?size=';
var srcLower = src.toLowerCase();
// Exit if the src doesn't include the sign of resize.
if (!srcLower.includes(RESIZE_IMG_SIGN)) {
return match;
}

var size = srcLower.split(RESIZE_IMG_SIGN)[1];
// Exit if hasn't the value of width or height.
if (!size) {
return match;
}

// The sign between width and height.
var MULTIPLY_SIGN = 'x';
var w = size.split(MULTIPLY_SIGN)[0];
var h = size.split(MULTIPLY_SIGN)[1];
var style = '';

if (w) {
style += `width: ${w}px;`;
}
if (h) {
style += `height: ${h}px;`;
}

var attr1 = attrBegin.trim();
var attr2 = attrEnd.trim();
return `<img ${attr1} src="${src}" style="${style}" ${attr2}>`;
}
});

data.content = $.html();
);
}, 0);
6 changes: 4 additions & 2 deletions scripts/filters/shake-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

'use strict';

hexo.extend.filter.register('after_generate', () => {
hexo.extend.filter.register('after_generate', function () {
var theme = hexo.theme.config;

if (!theme.shake_file) return;
if (!theme.shake_file) {
return;
}

if (!(theme.sidebar && theme.sidebar.enable)) {
hexo.route.remove('js/sidebar.js');
Expand Down
17 changes: 9 additions & 8 deletions scripts/filters/wrap-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
'use strict';

hexo.extend.filter.register('after_post_render', function (data) {
var cheerio;
data.content = data.content.replace(
/(<table[^>]*>(?!<\/table).*<\/table>)/gim,
function (match, table) {
if (!table) {
return match;
}

if (!cheerio) cheerio = require('cheerio');

var $ = cheerio.load(data.content, { decodeEntities: false });
var $wrapper = $('<div class="table-container"></div>');

$('table').wrap($wrapper);
data.content = $.html();
return `<div class="table-container">${table}</div>`;
}
);
}, 0);
2 changes: 1 addition & 1 deletion source/css/_common/components/widgets/lazyload.styl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.lazyload {
&.block {
&-block {
box-sizing: content-box;
min-width: 30px;
min-height: 30px;
Expand Down

0 comments on commit 31d6356

Please sign in to comment.