From c57c058c644289a38a51975bde16d1d716e28df2 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sun, 29 Sep 2019 14:22:57 +0800 Subject: [PATCH] fix: remove /index suffix in permalink --- .../lib/utils/__test__/getPermalink.test.js | 43 +++++++++++++++++++ packages/saber/lib/utils/getPermalink.js | 4 +- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/saber/lib/utils/__test__/getPermalink.test.js diff --git a/packages/saber/lib/utils/__test__/getPermalink.test.js b/packages/saber/lib/utils/__test__/getPermalink.test.js new file mode 100644 index 000000000..693b45eaa --- /dev/null +++ b/packages/saber/lib/utils/__test__/getPermalink.test.js @@ -0,0 +1,43 @@ +const getPermalink = require('../getPermalink') + +test('use default permalink', () => { + const samples = [ + { slug: 'index', permalink: '/' }, + { slug: 'foo/index', permalink: '/foo' }, + { slug: 'foo/bar', permalink: '/foo/bar.html' } + ] + for (const sample of samples) { + const receivedPermalink = getPermalink( + [], + { + slug: sample.slug, + type: 'page', + createdAt: new Date('2019-01-01') + }, + {} + ) + expect(receivedPermalink).toBe(sample.permalink) + } +}) + +test('remove .html extension', () => { + const samples = [ + { slug: 'index', permalink: '/' }, + { slug: 'foo/index', permalink: '/foo' }, + { slug: 'foo/bar', permalink: '/foo/bar' } + ] + for (const sample of samples) { + const receivedPermalink = getPermalink( + [], + { + slug: sample.slug, + type: 'page', + createdAt: new Date('2019-01-01') + }, + { + page: '/:slug' + } + ) + expect(receivedPermalink).toBe(sample.permalink) + } +}) diff --git a/packages/saber/lib/utils/getPermalink.js b/packages/saber/lib/utils/getPermalink.js index 4a861f46f..43ff1b5bf 100644 --- a/packages/saber/lib/utils/getPermalink.js +++ b/packages/saber/lib/utils/getPermalink.js @@ -45,8 +45,8 @@ module.exports = (localeNames, page, permalinks) => { .replace(/:i_month/, () => `${date.getMonth() + 1}`) .replace(/:day/, () => `${padZero(date.getDate())}`) .replace(/:i_day/, () => `${date.getDate()}`) - .replace(/^\/index\.html$/, '/') - .replace(/\/index\.html$/, '') + .replace(/^\/index(\.html)?$/, '/') + .replace(/\/index(\.html)?$/, '') } /**