Skip to content

Commit

Permalink
feat: perfect solution to support non-ASCII file name
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed May 21, 2018
1 parent 613ea7f commit 9e7a3dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/'
Expand Down
2 changes: 1 addition & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 += `,
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9e7a3dc

Please sign in to comment.