Skip to content

Commit

Permalink
fix: external_link should use after_render
Browse files Browse the repository at this point in the history
* also move meta_generator to after_render/ folder
  • Loading branch information
curbengh committed Aug 26, 2019
1 parent 01eebda commit c9ee039
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/plugins/filter/after_post_render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
module.exports = ctx => {
const { filter } = ctx.extend;

filter.register('after_post_render', require('./external_link'));
filter.register('after_post_render', require('./excerpt'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function externalLinkFilter(data) {

const siteHost = url.parse(config.url).hostname || config.url;

data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
data = data.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str)) return str;

const data = url.parse(href);
const parsedUrl = url.parse(href);
// Exit if the link doesn't have protocol, which means it's a internal link
// Exit if the url has same host with config.url
if (!data.protocol || data.hostname === siteHost) return str;
if (!parsedUrl.protocol || parsedUrl.hostname === siteHost) return str;

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand All @@ -27,6 +27,8 @@ function externalLinkFilter(data) {
return str.replace(hrefStr, `${hrefStr} target="_blank" rel="noopener"`);
});

return data;

}

module.exports = externalLinkFilter;
8 changes: 8 additions & 0 deletions lib/plugins/filter/after_render/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = ctx => {
const { filter } = ctx.extend;

filter.register('after_render:html', require('./external_link'));
filter.register('after_render:html', require('./meta_generator'));
};
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/plugins/filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = ctx => {
const { filter } = ctx.extend;

require('./after_render')(ctx);
require('./after_post_render')(ctx);
require('./before_post_render')(ctx);
require('./before_exit')(ctx);
Expand All @@ -11,5 +12,4 @@ module.exports = ctx => {

filter.register('new_post_path', require('./new_post_path'));
filter.register('post_permalink', require('./post_permalink'));
filter.register('after_render:html', require('./meta_generator'));
};
9 changes: 4 additions & 5 deletions test/scripts/filters/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
describe('External link', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const externalLink = require('../../../lib/plugins/filter/after_post_render/external_link').bind(hexo);
const externalLink = require('../../../lib/plugins/filter/after_render/external_link').bind(hexo);
console.log(typeof externalLink);

hexo.config.external_link = true;
hexo.config.url = 'https://example.com';
Expand Down Expand Up @@ -46,11 +47,9 @@ describe('External link', () => {
'<a href="https://example.com">Example Domain</a>'
].join('\n');

const data = {content};

externalLink(data);
const result = externalLink(content);

data.content.should.eql([
result.should.eql([
'# External link test',
'1. External link',
'<a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>',
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/filters/meta_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe('Meta Generator', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const metaGenerator = require('../../../lib/plugins/filter/meta_generator').bind(hexo);
const metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo);
const cheerio = require('cheerio');

it('default', () => {
Expand Down

0 comments on commit c9ee039

Please sign in to comment.