Skip to content

Commit

Permalink
Merge branch 'master' into improve-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dailyrandomphoto committed Oct 9, 2019
2 parents 9d952e4 + d2662d4 commit f646a41
Show file tree
Hide file tree
Showing 39 changed files with 511 additions and 190 deletions.
39 changes: 0 additions & 39 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

13 changes: 5 additions & 8 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ const placeholder = '\uFFFC';
const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;

function getContextLineNums(min, max, center, amplitude) {
var result = [];
var lbound = Math.max(min, center - amplitude);
var hbound = Math.min(max, center + amplitude);
for (var i = lbound; i <= hbound; i++) {
result.push(i);
}

const result = [];
let lbound = Math.max(min, center - amplitude);
const hbound = Math.min(max, center + amplitude);
while (lbound <= hbound) result.push(lbound++);
return result;
}

Expand All @@ -69,7 +66,7 @@ function getContext(lines, errLine, location, type) {
Array.prototype.push.apply(message,
// get LINES_OF_CONTEXT lines surrounding `errLine`
getContextLineNums(1, lines.length, errLine, LINES_OF_CONTEXT)
.map(function(lnNum) {
.map(lnNum => {
const line = ' ' + lnNum + ' | ' + lines[lnNum - 1];
if (lnNum === errLine) {
return colorize.bold(line);
Expand Down
6 changes: 5 additions & 1 deletion lib/hexo/default_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ module.exports = {
new_post_name: ':title.md',
default_layout: 'post',
titlecase: false,
external_link: true,
external_link: {
enable: true,
field: 'site',
exclude: ''
},
filename_case: 0,
render_drafts: false,
post_asset_folder: false,
Expand Down
10 changes: 8 additions & 2 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ Hexo.prototype._bindLocals = function() {
return db.model('Page').find(query);
});

locals.set('categories', () => db.model('Category'));
locals.set('categories', () => {
// Ignore categories with zero posts
return db.model('Category').filter(category => category.length);
});

locals.set('tags', () => db.model('Tag'));
locals.set('tags', () => {
// Ignore tags with zero posts
return db.model('Tag').filter(tag => tag.length);
});

locals.set('data', () => {
const obj = {};
Expand Down
6 changes: 3 additions & 3 deletions lib/models/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = ctx => {
const Cache = new Schema({
_id: {type: String, required: true},
hash: {type: String, default: ''},
modified: {type: Number, default: Date.now}
modified: {type: Number, default: Date.now() } // UnixTime
});

Cache.static('compareFile', function(id, hashFn, statFn) {
Expand All @@ -19,7 +19,7 @@ module.exports = ctx => {
return Promise.all([hashFn(id), statFn(id)]).spread((hash, stats) => this.insert({
_id: id,
hash,
modified: stats.mtime
modified: stats.mtime.getTime()
})).thenReturn({
type: 'create'
});
Expand All @@ -29,7 +29,7 @@ module.exports = ctx => {

// Get file stats
return statFn(id).then(stats => {
mtime = stats.mtime;
mtime = stats.mtime.getTime();

// Skip the file if the modified time is unchanged
if (cache.modified === mtime) {
Expand Down
9 changes: 4 additions & 5 deletions lib/models/types/moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

const moment = require('moment-timezone');
const { SchemaType } = require('warehouse');
const util = require('util');

function SchemaTypeMoment(name, options) {
Reflect.apply(SchemaType, this, [name, options]);
class SchemaTypeMoment extends SchemaType {
constructor(name, options = {}) {
super(name, options);
}
}

util.inherits(SchemaTypeMoment, SchemaType);

function toMoment(value) {
// FIXME: Something is wrong when using a moment instance. I try to get the
// original date object and create a new moment object again.
Expand Down
42 changes: 32 additions & 10 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
'use strict';

const url = require('url');
const { parse } = require('url');

const isExternal = (url, config) => {
const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude :
[config.external_link.exclude];
const data = parse(url);
const host = data.hostname;
const sitehost = parse(config.url).hostname || config.url;

if (!data.protocol || !sitehost) return false;

if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
}
}

if (host !== sitehost) return true;

return false;
};

function externalLinkFilter(data) {
const { config } = this;
if (!config.external_link) return;

const siteHost = url.parse(config.url).hostname || config.url;
if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object' ||
config.external_link === true) {
config.external_link = Object.assign({
enable: true,
field: 'site',
exclude: ''
}, config.external_link);
}
if (config.external_link === false || config.external_link.enable === false ||
config.external_link.field !== 'post') return;

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

const data = 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 (/target=/gi.test(str) || !isExternal(href, config)) return str;

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand All @@ -26,7 +49,6 @@ function externalLinkFilter(data) {

return str.replace(hrefStr, `${hrefStr} target="_blank" rel="noopener"`);
});

}

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

const { parse } = require('url');

/**
* Check whether the link is external
* @param {String} url The url to check
* @param {Object} config The site config
* @returns {Boolean} True if the link doesn't have protocol or link has same host with config.url
*/
const isExternal = (url, config) => {
const exclude = Array.isArray(config.external_link.exclude) ? config.external_link.exclude :
[config.external_link.exclude];
const data = parse(url);
const host = data.hostname;
const sitehost = parse(config.url).hostname || config.url;

if (!data.protocol || !sitehost) return false;

if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
}
}

if (host !== sitehost) return true;

return false;
};

function externalLinkFilter(data) {
const { config } = this;

if (typeof config.external_link === 'undefined' || typeof config.external_link === 'object' ||
config.external_link === true) {
config.external_link = Object.assign({
enable: true,
field: 'site',
exclude: ''
}, config.external_link);
}
if (config.external_link === false || config.external_link.enable === false ||
config.external_link.field !== 'site') return;

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

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`);
return relStr;
});
return str.replace(hrefStr, `${hrefStr} target="_blank"`);
}

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'));
};
36 changes: 15 additions & 21 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

const urlFn = require('url');
const moment = require('moment');
const { escapeHTML, htmlTag, stripHTML } = require('hexo-util');

function meta(name, content, escape) {
if (escape !== false && typeof content === 'string') {
content = escapeHTML(content);
}
const { encodeURL, htmlTag, stripHTML } = require('hexo-util');

function meta(name, content) {
return `${htmlTag('meta', {
name,
content
})}\n`;
}

function og(name, content, escape) {
if (escape !== false && typeof content === 'string') {
content = escapeHTML(content);
}

function og(name, content) {
return `${htmlTag('meta', {
property: name,
content
Expand Down Expand Up @@ -70,7 +62,7 @@ function openGraphHelper(options = {}) {
let result = '';

if (description) {
result += meta('description', description, false);
result += meta('description', description);
}

if (keywords) {
Expand All @@ -86,19 +78,21 @@ function openGraphHelper(options = {}) {
result += og('og:type', type);
result += og('og:title', title);

if (config.pretty_urls.trailing_index === false) {
url = url.replace(/index\.html$/, '');
if (url) {
if (config.pretty_urls.trailing_index === false) {
url = url.replace(/index\.html$/, '');
}
url = encodeURL(url);
}

result += og('og:url', url, false);
result += og('og:url', url);

result += og('og:site_name', siteName);
if (description) {
result += og('og:description', description, false);
result += og('og:description', description);
}

if (language) {
result += og('og:locale', language, false);
result += og('og:locale', language);
}

images = images.map(path => {
Expand All @@ -112,7 +106,7 @@ function openGraphHelper(options = {}) {
});

images.forEach(path => {
result += og('og:image', path, false);
result += og('og:image', path);
});

if (updated) {
Expand All @@ -124,7 +118,7 @@ function openGraphHelper(options = {}) {
result += meta('twitter:card', twitterCard);

if (images.length) {
result += meta('twitter:image', images[0], false);
result += meta('twitter:image', images[0]);
}

if (options.twitter_id) {
Expand All @@ -135,7 +129,7 @@ function openGraphHelper(options = {}) {
}

if (options.twitter_site) {
result += meta('twitter:site', options.twitter_site, false);
result += meta('twitter:site', options.twitter_site);
}

if (options.google_plus) {
Expand Down
Loading

0 comments on commit f646a41

Please sign in to comment.