Skip to content

Commit

Permalink
fix: handle uri encoded _path query
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed Jan 17, 2023
1 parent 30c26e3 commit a64f1f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/runtime/utils/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ export function jsonStringify (value: any) {
* This function is equivalent to `JSON.parse`, but it also handles RegExp objects.
*/
export function jsonParse (value: string) {
return JSON.parse(value, regExpReviver)
return JSON.parse(value, (key, value) => {
const withOperator = (typeof value === 'string' && value.match(/^--([A-Z]+) (.+)$/)) || []

// Transforms RegExp string representation back to RegExp objects.
if (withOperator[1] === 'REGEX') {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags
const regex = withOperator[2].match(/\/(.*)\/([dgimsuy]*)$/)
return regex ? new RegExp(regex[1], regex[2] || '') : value
}

// Decode URI encoded path
if (key === '_path') {
return decodeURI(value)
}

return value
})
}

/**
Expand All @@ -23,18 +39,3 @@ function regExpReplacer (_key: string, value: any) {
}
return value
}

/**
* A function that transforms RegExp string representation back to RegExp objects.
*/
function regExpReviver (_key: string, value: any) {
const withOperator = (typeof value === 'string' && value.match(/^--([A-Z]+) (.+)$/)) || []

if (withOperator[1] === 'REGEX') {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags
const regex = withOperator[2].match(/\/(.*)\/([dgimsuy]*)$/)
return regex ? new RegExp(regex[1], regex[2] || '') : value
}

return value
}
5 changes: 5 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ describe('Basic usage', async () => {
expect(html).contains('Persian')
})

test('Japanese path', async () => {
const html = await $fetch('/こんにちは')
expect(html).contains('🎨 こんにちは')
})

test('Partials specials chars', async () => {
const html = await $fetch('/_partial/content-(v2)')
expect(html).contains('Content (v2)')
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic/content/こんにちは.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
_path: /こんにちは
---

# 🎨 こんにちは

0 comments on commit a64f1f9

Please sign in to comment.