Skip to content

Commit 566e681

Browse files
committed
feat: support filename that contains non-ASCII and unicode chars (#473)
1 parent be2757b commit 566e681

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

lib/app/Content.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { pathToComponentName } from './util'
2-
31
export default {
42
functional: true,
53

@@ -11,7 +9,7 @@ export default {
119
},
1210

1311
render (h, { parent, props, data }) {
14-
return h(pathToComponentName(parent.$page.path), {
12+
return h(parent.$page.key, {
1513
class: [props.custom ? 'custom' : '', data.class, data.staticClass],
1614
style: data.style
1715
})

lib/app/util.js

-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ export function injectMixins (options, mixins) {
55
options.mixins.push(...mixins)
66
}
77

8-
export function pathToComponentName (path) {
9-
if (path.charAt(path.length - 1) === '/') {
10-
return `page${path.replace(/\//g, '-') + 'index'}`
11-
} else {
12-
return `page${path.replace(/\//g, '-').replace(/\.html$/, '')}`
13-
}
14-
}
15-
168
export function findPageForPath (pages, path) {
179
for (let i = 0; i < pages.length; i++) {
1810
const page = pages[i]

lib/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
153153
console.error(chalk.red(`Error rendering ${pagePath}:`))
154154
throw e
155155
}
156-
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
156+
const filename = decodeURIComponent(pagePath.replace(/\/$/, '/index.html').replace(/^\//, ''))
157157
const filePath = path.resolve(outDir, filename)
158158
await fs.ensureDir(path.dirname(filePath))
159159
await fs.writeFile(filePath, html)

lib/default-theme/Layout.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import Home from './Home.vue'
2727
import Navbar from './Navbar.vue'
2828
import Page from './Page.vue'
2929
import Sidebar from './Sidebar.vue'
30-
import { pathToComponentName } from '@app/util'
3130
import { resolveSidebarItems } from './util'
3231
3332
export default {
@@ -86,11 +85,13 @@ export default {
8685
},
8786
8887
mounted () {
88+
window.addEventListener('scroll', this.onScroll)
89+
8990
// configure progress bar
9091
nprogress.configure({ showSpinner: false })
9192
9293
this.$router.beforeEach((to, from, next) => {
93-
if (to.path !== from.path && !Vue.component(pathToComponentName(to.path))) {
94+
if (to.path !== from.path && !Vue.component(to.name)) {
9495
nprogress.start()
9596
}
9697
next()

lib/prepare.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const createMarkdown = require('./markdown')
55
const loadConfig = require('./util/loadConfig')
66
const tempPath = path.resolve(__dirname, 'app/.temp')
77
const {
8+
encodePath,
89
inferTitle,
910
extractHeaders,
1011
parseFrontmatter,
@@ -181,8 +182,10 @@ async function resolveOptions (sourceDir) {
181182
// resolve pagesData
182183
const pagesData = await Promise.all(pageFiles.map(async (file) => {
183184
const filepath = path.resolve(sourceDir, file)
185+
const key = 'v-' + Math.random().toString(16).slice(2)
184186
const data = {
185-
path: fileToPath(file)
187+
key,
188+
path: encodePath(fileToPath(file))
186189
}
187190

188191
if (shouldResolveLastUpdated) {
@@ -296,21 +299,31 @@ async function resolveComponents (sourceDir) {
296299
}
297300

298301
async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
299-
function genRoute ({ path: pagePath }, index) {
302+
function genRoute ({ path: pagePath, key: componentName }, index) {
300303
const file = pageFiles[index]
301304
const filePath = path.resolve(sourceDir, file)
302305
let code = `
303306
{
307+
name: ${JSON.stringify(componentName)},
304308
path: ${JSON.stringify(pagePath)},
305309
component: ThemeLayout,
306310
beforeEnter: (to, from, next) => {
307311
import(${JSON.stringify(filePath)}).then(comp => {
308-
Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
312+
Vue.component(${JSON.stringify(componentName)}, comp.default)
309313
next()
310314
})
311315
}
312316
}`
313317

318+
const dncodedPath = decodeURIComponent(pagePath)
319+
if (dncodedPath !== pagePath) {
320+
code += `,
321+
{
322+
path: ${JSON.stringify(dncodedPath)},
323+
redirect: ${JSON.stringify(pagePath)}
324+
}`
325+
}
326+
314327
if (/\/$/.test(pagePath)) {
315328
code += `,
316329
{

lib/util/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const spawn = require('cross-spawn')
22
const parseHeaders = require('./parseHeaders')
33

4+
exports.encodePath = function (path) {
5+
return path.split('/').map(item => encodeURIComponent(item)).join('/')
6+
}
7+
48
exports.parseHeaders = parseHeaders
59

610
exports.normalizeHeadTag = tag => {

0 commit comments

Comments
 (0)