From 526e454fd2519edeb88a73561d937518a2d10ad7 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 20 May 2018 23:58:27 +0800 Subject: [PATCH 01/10] feat: use random hash string as component name --- lib/app/Content.js | 4 +--- lib/app/app.js | 6 ++++++ lib/app/util.js | 9 +++------ lib/default-theme/Layout.vue | 2 +- lib/prepare.js | 6 ++++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/app/Content.js b/lib/app/Content.js index 4b3b4c88f5..46628195e6 100644 --- a/lib/app/Content.js +++ b/lib/app/Content.js @@ -1,5 +1,3 @@ -import { pathToComponentName } from './util' - export default { functional: true, @@ -11,7 +9,7 @@ export default { }, render (h, { parent, props, data }) { - return h(pathToComponentName(parent.$page.path), { + return h(parent.$page.key, { class: [props.custom ? 'custom' : '', data.class, data.staticClass], style: data.style }) diff --git a/lib/app/app.js b/lib/app/app.js index ceaa7b905b..8aa3239bb5 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -67,6 +67,12 @@ export function createApp () { // redirect /foo to /foo/ router.beforeEach((to, from, next) => { + const decodedPath = decodeURIComponent(to.path) + if (decodedPath !== to.path) { + next(Object.assign({}, to, { + path: decodedPath + })) + } if (!/(\/|\.html)$/.test(to.path)) { next(Object.assign({}, to, { path: to.path + '/' diff --git a/lib/app/util.js b/lib/app/util.js index ceafb220c6..b8981397ef 100644 --- a/lib/app/util.js +++ b/lib/app/util.js @@ -5,12 +5,9 @@ export function injectMixins (options, mixins) { options.mixins.push(...mixins) } -export function pathToComponentName (path) { - if (path.charAt(path.length - 1) === '/') { - return `page${path.replace(/\//g, '-') + 'index'}` - } else { - return `page${path.replace(/\//g, '-').replace(/\.html$/, '')}` - } +export function pathToComponentName (pages, path) { + const page = findPageForPath(pages, path) + return page && page.key } export function findPageForPath (pages, path) { diff --git a/lib/default-theme/Layout.vue b/lib/default-theme/Layout.vue index c794f6a753..b84199a51e 100644 --- a/lib/default-theme/Layout.vue +++ b/lib/default-theme/Layout.vue @@ -94,7 +94,7 @@ export default { nprogress.configure({ showSpinner: false }) this.$router.beforeEach((to, from, next) => { - if (to.path !== from.path && !Vue.component(pathToComponentName(to.path))) { + if (to.path !== from.path && !Vue.component(pathToComponentName(this.$site.pages, to.path))) { nprogress.start() } next() diff --git a/lib/prepare.js b/lib/prepare.js index fdeeb67c54..1ba2ca7a2e 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -196,7 +196,9 @@ async function resolveOptions (sourceDir) { // resolve pagesData const pagesData = await Promise.all(pageFiles.map(async (file) => { const filepath = path.resolve(sourceDir, file) + const key = 'v-' + Math.random().toString(16).slice(2) const data = { + key, path: fileToPath(file) } @@ -311,7 +313,7 @@ async function resolveComponents (sourceDir) { } async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { - function genRoute ({ path: pagePath }, index) { + function genRoute ({ path: pagePath, key: componentName }, index) { const file = pageFiles[index] const filePath = path.resolve(sourceDir, file) let code = ` @@ -320,7 +322,7 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { component: ThemeLayout, beforeEnter: (to, from, next) => { import(${JSON.stringify(filePath)}).then(comp => { - Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default) + Vue.component(${JSON.stringify(componentName)}, comp.default) next() }) } From 84e74bd93fce67bedea877caaee5021e27dc9ac9 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 15:57:44 +0800 Subject: [PATCH 02/10] fix: unexpected 404 when hit browser back button --- lib/app/app.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/app/app.js b/lib/app/app.js index 8aa3239bb5..7e0d1f0b20 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -70,6 +70,7 @@ export function createApp () { const decodedPath = decodeURIComponent(to.path) if (decodedPath !== to.path) { next(Object.assign({}, to, { + fullPath: decodeURIComponent(to.fullPath), path: decodedPath })) } @@ -82,6 +83,17 @@ export function createApp () { } }) + // Fix when the two adjacent records in the browser's history contain same state, but have the different url, + // which will cause Vue-Router to match 404. + let lastState + window.addEventListener('popstate', event => { + console.log(event.state) + if (event.state !== null && JSON.stringify(event.state) === JSON.stringify(lastState)) { + history.back() + } + lastState = event.state + }, false) + const options = {} themeEnhanceApp({ Vue, options, router, siteData }) From 4b9a2f352828d2fad7ec273fe7bc07011753de10 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 16:03:36 +0800 Subject: [PATCH 03/10] chore: add temporary test MDs --- docs/README.md | 2 ++ docs/guide/using-vue.md | 2 +- "docs/\345\260\244.md" | 0 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 "docs/\345\260\244.md" diff --git a/docs/README.md b/docs/README.md index 3cd3d75032..85e4a68712 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,6 +15,8 @@ footer: MIT Licensed | Copyright © 2018-present Evan You ### As Easy as 1, 2, 3 +[尤](./尤.md) + ``` bash # install yarn global add vuepress # OR npm install -g vuepress diff --git a/docs/guide/using-vue.md b/docs/guide/using-vue.md index fcc5d22968..6973744d90 100644 --- a/docs/guide/using-vue.md +++ b/docs/guide/using-vue.md @@ -1,4 +1,4 @@ -# Using Vue in Markdown +# Using `Vue` in __Markdown__ ## Browser API Access Restrictions diff --git "a/docs/\345\260\244.md" "b/docs/\345\260\244.md" new file mode 100644 index 0000000000..e69de29bb2 From b142e11007f9027b8918a0ac4a6bf7f683f4ec28 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 16:04:46 +0800 Subject: [PATCH 04/10] chore: clean log --- lib/app/app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/app/app.js b/lib/app/app.js index 7e0d1f0b20..605164bcc2 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -87,7 +87,6 @@ export function createApp () { // which will cause Vue-Router to match 404. let lastState window.addEventListener('popstate', event => { - console.log(event.state) if (event.state !== null && JSON.stringify(event.state) === JSON.stringify(lastState)) { history.back() } From 2ac841da05e5f1d63cc8553d9da9018504e432d7 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 16:14:35 +0800 Subject: [PATCH 05/10] chore: tweaks --- docs/guide/using-vue.md | 2 +- lib/app/app.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/guide/using-vue.md b/docs/guide/using-vue.md index 6973744d90..fcc5d22968 100644 --- a/docs/guide/using-vue.md +++ b/docs/guide/using-vue.md @@ -1,4 +1,4 @@ -# Using `Vue` in __Markdown__ +# Using Vue in Markdown ## Browser API Access Restrictions diff --git a/lib/app/app.js b/lib/app/app.js index 605164bcc2..dadadb7697 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -85,12 +85,12 @@ export function createApp () { // Fix when the two adjacent records in the browser's history contain same state, but have the different url, // which will cause Vue-Router to match 404. - let lastState + let lastHistoryState window.addEventListener('popstate', event => { - if (event.state !== null && JSON.stringify(event.state) === JSON.stringify(lastState)) { + if (event.state !== null && JSON.stringify(event.state) === JSON.stringify(lastHistoryState)) { history.back() } - lastState = event.state + lastHistoryState = event.state }, false) const options = {} From ea27b2eed135e373272d535cfa51aa9723997d87 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 20:54:24 +0800 Subject: [PATCH 06/10] chore: remove fixing 404 of hit back button --- docs/README.md | 2 +- "docs/\345\260\244.md" | 3 +++ "docs/\346\272\252.md" | 3 +++ "docs/\351\233\250.md" | 3 +++ lib/app/app.js | 10 ---------- lib/app/cache.js | 17 +++++++++++++++++ 6 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 "docs/\346\272\252.md" create mode 100644 "docs/\351\233\250.md" create mode 100644 lib/app/cache.js diff --git a/docs/README.md b/docs/README.md index 85e4a68712..36f06654eb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,7 +15,7 @@ footer: MIT Licensed | Copyright © 2018-present Evan You ### As Easy as 1, 2, 3 -[尤](./尤.md) +- [尤](./尤.md) ``` bash # install diff --git "a/docs/\345\260\244.md" "b/docs/\345\260\244.md" index e69de29bb2..c25cdf563c 100644 --- "a/docs/\345\260\244.md" +++ "b/docs/\345\260\244.md" @@ -0,0 +1,3 @@ +# 尤 + +- [雨](./雨.md) diff --git "a/docs/\346\272\252.md" "b/docs/\346\272\252.md" new file mode 100644 index 0000000000..e67ecac260 --- /dev/null +++ "b/docs/\346\272\252.md" @@ -0,0 +1,3 @@ +# 溪 + +- [Homepage](./) diff --git "a/docs/\351\233\250.md" "b/docs/\351\233\250.md" new file mode 100644 index 0000000000..ebef4579eb --- /dev/null +++ "b/docs/\351\233\250.md" @@ -0,0 +1,3 @@ +# 雨 + +- [溪](./溪.md) diff --git a/lib/app/app.js b/lib/app/app.js index dadadb7697..406a9c6b52 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -83,16 +83,6 @@ export function createApp () { } }) - // Fix when the two adjacent records in the browser's history contain same state, but have the different url, - // which will cause Vue-Router to match 404. - let lastHistoryState - window.addEventListener('popstate', event => { - if (event.state !== null && JSON.stringify(event.state) === JSON.stringify(lastHistoryState)) { - history.back() - } - lastHistoryState = event.state - }, false) - const options = {} themeEnhanceApp({ Vue, options, router, siteData }) diff --git a/lib/app/cache.js b/lib/app/cache.js new file mode 100644 index 0000000000..c97fc53f3e --- /dev/null +++ b/lib/app/cache.js @@ -0,0 +1,17 @@ +export default { + set (itemName, item) { + sessionStorage.setItem(itemName, JSON.stringify(item)) + }, + get (itemName) { + let item + try { + item = JSON.parse(sessionStorage.getItem(itemName)) + } catch (err) { + item = null + } + return item + }, + clear (itemName) { + sessionStorage.removeItem(itemName) + } +} From 05cb65a5c6725449025d25eb483996dc9d5373ad Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 22:32:05 +0800 Subject: [PATCH 07/10] chore: clean code --- lib/app/cache.js | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 lib/app/cache.js diff --git a/lib/app/cache.js b/lib/app/cache.js deleted file mode 100644 index c97fc53f3e..0000000000 --- a/lib/app/cache.js +++ /dev/null @@ -1,17 +0,0 @@ -export default { - set (itemName, item) { - sessionStorage.setItem(itemName, JSON.stringify(item)) - }, - get (itemName) { - let item - try { - item = JSON.parse(sessionStorage.getItem(itemName)) - } catch (err) { - item = null - } - return item - }, - clear (itemName) { - sessionStorage.removeItem(itemName) - } -} From 613ea7f892530dc366e4a0d7dd0c372831840e75 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 21 May 2018 23:41:16 +0800 Subject: [PATCH 08/10] chore: simplify code --- lib/app/util.js | 5 ----- lib/default-theme/Layout.vue | 3 +-- lib/prepare.js | 1 + 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/app/util.js b/lib/app/util.js index b8981397ef..c11172240e 100644 --- a/lib/app/util.js +++ b/lib/app/util.js @@ -5,11 +5,6 @@ export function injectMixins (options, mixins) { options.mixins.push(...mixins) } -export function pathToComponentName (pages, path) { - const page = findPageForPath(pages, path) - return page && page.key -} - export function findPageForPath (pages, path) { for (let i = 0; i < pages.length; i++) { const page = pages[i] diff --git a/lib/default-theme/Layout.vue b/lib/default-theme/Layout.vue index b84199a51e..509b84c770 100644 --- a/lib/default-theme/Layout.vue +++ b/lib/default-theme/Layout.vue @@ -27,7 +27,6 @@ import Home from './Home.vue' import Navbar from './Navbar.vue' import Page from './Page.vue' import Sidebar from './Sidebar.vue' -import { pathToComponentName } from '@app/util' import store from '@app/store' import { resolveSidebarItems } from './util' import throttle from 'lodash.throttle' @@ -94,7 +93,7 @@ export default { nprogress.configure({ showSpinner: false }) this.$router.beforeEach((to, from, next) => { - if (to.path !== from.path && !Vue.component(pathToComponentName(this.$site.pages, to.path))) { + if (to.path !== from.path && !Vue.component(to.name)) { nprogress.start() } next() diff --git a/lib/prepare.js b/lib/prepare.js index 1ba2ca7a2e..11db0c132c 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -318,6 +318,7 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { const filePath = path.resolve(sourceDir, file) let code = ` { + name: ${JSON.stringify(componentName)}, path: ${JSON.stringify(pagePath)}, component: ThemeLayout, beforeEnter: (to, from, next) => { From 9e7a3dc63f5fb2ab895477f18082d7a30e9812a9 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 22 May 2018 00:12:46 +0800 Subject: [PATCH 09/10] feat: perfect solution to support non-ASCII file name --- lib/app/app.js | 7 ------- lib/build.js | 2 +- lib/prepare.js | 15 ++++++++++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/app/app.js b/lib/app/app.js index 406a9c6b52..ceaa7b905b 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -67,13 +67,6 @@ export function createApp () { // redirect /foo to /foo/ router.beforeEach((to, from, next) => { - const decodedPath = decodeURIComponent(to.path) - if (decodedPath !== to.path) { - next(Object.assign({}, to, { - fullPath: decodeURIComponent(to.fullPath), - path: decodedPath - })) - } if (!/(\/|\.html)$/.test(to.path)) { next(Object.assign({}, to, { path: to.path + '/' diff --git a/lib/build.js b/lib/build.js index 149f9f93a0..b96f1096b4 100644 --- a/lib/build.js +++ b/lib/build.js @@ -153,7 +153,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) { console.error(chalk.red(`Error rendering ${pagePath}:`)) throw e } - const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '') + const filename = decodeURIComponent(pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')) const filePath = path.resolve(outDir, filename) await fs.ensureDir(path.dirname(filePath)) await fs.writeFile(filePath, html) diff --git a/lib/prepare.js b/lib/prepare.js index 11db0c132c..d619dd0f32 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -199,7 +199,7 @@ async function resolveOptions (sourceDir) { const key = 'v-' + Math.random().toString(16).slice(2) const data = { key, - path: fileToPath(file) + path: encodePath(fileToPath(file)) } if (shouldResolveLastUpdated) { @@ -329,6 +329,15 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) { } }` + const dncodedPath = decodeURIComponent(pagePath) + if (dncodedPath !== pagePath) { + code += `, + { + path: ${JSON.stringify(dncodedPath)}, + redirect: ${JSON.stringify(pagePath)} + }` + } + if (/\/$/.test(pagePath)) { code += `, { @@ -365,6 +374,10 @@ function sort (arr) { }) } +function encodePath (path) { + return path.split('/').map(item => encodeURIComponent(item)).join('/') +} + async function parseConfig (file) { const content = await fs.readFile(file, 'utf-8') const [extension] = /.\w+$/.exec(file) From 953c76c4ee2820fe6117b89232a60c8a89abbf6d Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Thu, 24 May 2018 23:31:19 +0800 Subject: [PATCH 10/10] fix: merge conflict --- lib/util/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/util/index.js b/lib/util/index.js index b9266dc85e..b881b5fb9d 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -1,6 +1,10 @@ const spawn = require('cross-spawn') const parseHeaders = require('./parseHeaders') +exports.encodePath = function (path) { + return path.split('/').map(item => encodeURIComponent(item)).join('/') +} + exports.parseHeaders = parseHeaders exports.normalizeHeadTag = tag => {