From b826767d62ed77accc7c2e25401ade6255d6e2e4 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Dec 2019 09:55:57 +0000 Subject: [PATCH 1/4] refactor(permalink): prettyUrls of hexo-util --- lib/hexo/default_config.js | 3 ++- lib/models/category.js | 4 ++-- lib/models/page.js | 3 ++- lib/models/post.js | 3 ++- lib/models/tag.js | 4 ++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/hexo/default_config.js b/lib/hexo/default_config.js index e1cd17d470..cb642e8e92 100644 --- a/lib/hexo/default_config.js +++ b/lib/hexo/default_config.js @@ -14,7 +14,8 @@ module.exports = { permalink: ':year/:month/:day/:title/', permalink_defaults: {}, pretty_urls: { - trailing_index: true + trailing_index: true, + trailing_html: true }, // Directory source_dir: 'source', diff --git a/lib/models/category.js b/lib/models/category.js index 4ebc8e6e7d..3138093062 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -1,7 +1,7 @@ 'use strict'; const { Schema } = require('warehouse'); -const { slugize } = require('hexo-util'); +const { prettyUrls, slugize } = require('hexo-util'); const full_url_for = require('../plugins/helper/full_url_for'); module.exports = ctx => { @@ -41,7 +41,7 @@ module.exports = ctx => { Category.virtual('permalink').get(function() { const { config } = ctx; let url = full_url_for.call(ctx, this.path); - if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, ''); + url = prettyUrls(url, config.pretty_urls); return url; }); diff --git a/lib/models/page.js b/lib/models/page.js index 2477432fc1..d93a509599 100644 --- a/lib/models/page.js +++ b/lib/models/page.js @@ -5,6 +5,7 @@ const { join } = require('path'); const Moment = require('./types/moment'); const moment = require('moment'); const full_url_for = require('../plugins/helper/full_url_for'); +const { prettyUrls } = require('hexo-util'); module.exports = ctx => { const Page = new Schema({ @@ -35,7 +36,7 @@ module.exports = ctx => { Page.virtual('permalink').get(function() { const { config } = ctx; let url = full_url_for.call(ctx, this.path); - if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, ''); + url = prettyUrls(url, config.pretty_urls); return url; }); diff --git a/lib/models/post.js b/lib/models/post.js index 6a6a19ab25..94004ae997 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -6,6 +6,7 @@ const { extname, join, sep } = require('path'); const Promise = require('bluebird'); const Moment = require('./types/moment'); const full_url_for = require('../plugins/helper/full_url_for'); +const { prettyUrls } = require('hexo-util'); function pickID(data) { return data._id; @@ -53,7 +54,7 @@ module.exports = ctx => { Post.virtual('permalink').get(function() { const { config } = ctx; let url = full_url_for.call(ctx, this.path); - if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, ''); + url = prettyUrls(url, config.pretty_urls); return url; }); diff --git a/lib/models/tag.js b/lib/models/tag.js index 7bec85757e..68d8e4b9de 100644 --- a/lib/models/tag.js +++ b/lib/models/tag.js @@ -1,7 +1,7 @@ 'use strict'; const { Schema } = require('warehouse'); -const { slugize } = require('hexo-util'); +const { prettyUrls, slugize } = require('hexo-util'); const { hasOwnProperty: hasOwn } = Object.prototype; const full_url_for = require('../plugins/helper/full_url_for'); @@ -32,7 +32,7 @@ module.exports = ctx => { Tag.virtual('permalink').get(function() { const { config } = ctx; let url = full_url_for.call(ctx, this.path); - if (config.pretty_urls.trailing_index === false) url = url.replace(/index\.html$/, ''); + url = prettyUrls(url, config.pretty_urls); return url; }); From ac0913d7588b8908b9b405bec4daff0991e909c9 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Dec 2019 10:01:12 +0000 Subject: [PATCH 2/4] test(permalink): pretty_urls.trailing_html --- test/scripts/models/category.js | 11 +++++++++++ test/scripts/models/page.js | 24 ++++++++++++++++++++++++ test/scripts/models/post.js | 26 +++++++++++++++++++++++++- test/scripts/models/tag.js | 11 +++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index f13df9f4a7..19cef2b197 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -116,6 +116,17 @@ describe('Category', () => { }); }); + it('permalink - trailing_html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Category.insert({ + name: 'foo' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, '')); + hexo.config.pretty_urls.trailing_html = true; + return Category.removeById(data._id); + }); + }); + it('permalink - should be encoded', () => { hexo.config.url = 'http://fôo.com'; return Category.insert({ diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index ec12dce4e8..264a307e0c 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -73,6 +73,30 @@ describe('Page', () => { }); }); + it('permalink - trailing_html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Page.insert({ + source: 'foo.md', + path: 'bar/foo.html' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, '')); + hexo.config.pretty_urls.trailing_html = true; + return Page.removeById(data._id); + }); + }); + + it('permalink - trailing_html - index.html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Page.insert({ + source: 'foo.md', + path: 'bar/index.html' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path); + hexo.config.pretty_urls.trailing_html = true; + return Page.removeById(data._id); + }); + }); + it('permalink - should be encoded', () => { hexo.config.url = 'http://fôo.com'; const path = 'bár'; diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index 64a5512726..d1e996d2a4 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -140,7 +140,7 @@ describe('Post', () => { hexo.config.pretty_urls.trailing_index = false; return Post.insert({ source: 'foo.md', - slug: 'bar' + slug: 'bar/index.html' }).then(data => { data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, '')); hexo.config.pretty_urls.trailing_index = true; @@ -148,6 +148,30 @@ describe('Post', () => { }); }); + it('permalink - trailing_html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Post.insert({ + source: 'foo.md', + slug: 'bar/foo.html' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, '')); + hexo.config.pretty_urls.trailing_html = true; + return Post.removeById(data._id); + }); + }); + + it('permalink - trailing_html - index.html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Post.insert({ + source: 'foo.md', + slug: 'bar/index.html' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path); + hexo.config.pretty_urls.trailing_html = true; + return Post.removeById(data._id); + }); + }); + it('full_source - virtual', () => Post.insert({ source: 'foo.md', slug: 'bar' diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index c079f9af80..050ac262ef 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -101,6 +101,17 @@ describe('Tag', () => { }); }); + it('permalink - trailing_html', () => { + hexo.config.pretty_urls.trailing_html = false; + return Tag.insert({ + name: 'foo' + }).then(data => { + data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, '')); + hexo.config.pretty_urls.trailing_html = true; + return Tag.removeById(data._id); + }); + }); + it('permalink - should be encoded', () => { hexo.config.url = 'http://fôo.com'; return Tag.insert({ From 4c09f21b94cb72726e3aada700c10316028145a1 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Mon, 9 Dec 2019 22:42:41 +0000 Subject: [PATCH 3/4] refactor(permalink): full_url_for() has built in prettyUrls() --- lib/models/category.js | 7 ++----- lib/models/page.js | 6 +----- lib/models/post.js | 6 +----- lib/models/tag.js | 7 ++----- 4 files changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/models/category.js b/lib/models/category.js index 3138093062..90737bd1ea 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -1,7 +1,7 @@ 'use strict'; const { Schema } = require('warehouse'); -const { prettyUrls, slugize } = require('hexo-util'); +const { slugize } = require('hexo-util'); const full_url_for = require('../plugins/helper/full_url_for'); module.exports = ctx => { @@ -39,10 +39,7 @@ module.exports = ctx => { }); Category.virtual('permalink').get(function() { - const { config } = ctx; - let url = full_url_for.call(ctx, this.path); - url = prettyUrls(url, config.pretty_urls); - return url; + return full_url_for.call(ctx, this.path); }); Category.virtual('posts').get(function() { diff --git a/lib/models/page.js b/lib/models/page.js index d93a509599..78a21ef80e 100644 --- a/lib/models/page.js +++ b/lib/models/page.js @@ -5,7 +5,6 @@ const { join } = require('path'); const Moment = require('./types/moment'); const moment = require('moment'); const full_url_for = require('../plugins/helper/full_url_for'); -const { prettyUrls } = require('hexo-util'); module.exports = ctx => { const Page = new Schema({ @@ -34,10 +33,7 @@ module.exports = ctx => { }); Page.virtual('permalink').get(function() { - const { config } = ctx; - let url = full_url_for.call(ctx, this.path); - url = prettyUrls(url, config.pretty_urls); - return url; + return full_url_for.call(ctx, this.path); }); Page.virtual('full_source').get(function() { diff --git a/lib/models/post.js b/lib/models/post.js index 94004ae997..975da25883 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -6,7 +6,6 @@ const { extname, join, sep } = require('path'); const Promise = require('bluebird'); const Moment = require('./types/moment'); const full_url_for = require('../plugins/helper/full_url_for'); -const { prettyUrls } = require('hexo-util'); function pickID(data) { return data._id; @@ -52,10 +51,7 @@ module.exports = ctx => { }); Post.virtual('permalink').get(function() { - const { config } = ctx; - let url = full_url_for.call(ctx, this.path); - url = prettyUrls(url, config.pretty_urls); - return url; + return full_url_for.call(ctx, this.path); }); Post.virtual('full_source').get(function() { diff --git a/lib/models/tag.js b/lib/models/tag.js index 68d8e4b9de..3ed38e9e33 100644 --- a/lib/models/tag.js +++ b/lib/models/tag.js @@ -1,7 +1,7 @@ 'use strict'; const { Schema } = require('warehouse'); -const { prettyUrls, slugize } = require('hexo-util'); +const { slugize } = require('hexo-util'); const { hasOwnProperty: hasOwn } = Object.prototype; const full_url_for = require('../plugins/helper/full_url_for'); @@ -30,10 +30,7 @@ module.exports = ctx => { }); Tag.virtual('permalink').get(function() { - const { config } = ctx; - let url = full_url_for.call(ctx, this.path); - url = prettyUrls(url, config.pretty_urls); - return url; + return full_url_for.call(ctx, this.path); }); Tag.virtual('posts').get(function() { From e6955d3825eab16989f3250c107331af28e9fb2f Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Tue, 10 Dec 2019 12:03:59 +0000 Subject: [PATCH 4/4] chore(deps): update hexo-util from 1.6.1 to 1.7.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b46ad3ee46..dd8428d152 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "hexo-fs": "^2.0.0", "hexo-i18n": "^1.0.0", "hexo-log": "^1.0.0", - "hexo-util": "^1.6.1", + "hexo-util": "^1.7.0", "js-yaml": "^3.12.0", "lodash": "^4.17.11", "micromatch": "^4.0.2",