mirror of
https://github.com/elk-zone/elk.git
synced 2024-11-19 07:19:58 +00:00
fix: improve permalink handling somewhat
This commit is contained in:
parent
131d1af827
commit
6dfc94e2a5
3 changed files with 31 additions and 2 deletions
|
@ -12,3 +12,5 @@ export const STORAGE_KEY_FIRST_VISIT = 'elk-first-visit'
|
|||
export const STORAGE_KEY_ZEN_MODE = 'elk-zenmode'
|
||||
export const STORAGE_KEY_LANG = 'elk-lang'
|
||||
export const STORAGE_KEY_FEATURE_FLAGS = 'elk-feature-flags'
|
||||
|
||||
export const HANDLED_MASTO_URLS = /^(https?:\/\/)?([\w\d-]+\.)+\w+\/(@[@\w\d-\.]+)(\/objects)?(\/\d+)?$/
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
<script setup lang="ts">
|
||||
import { parseURL } from 'ufo'
|
||||
import { HANDLED_MASTO_URLS } from '~/constants'
|
||||
|
||||
definePageMeta({
|
||||
name: 'permalink',
|
||||
middleware: async (to) => {
|
||||
const HANDLED_MASTO_URL = /^(https?:\/\/)?(\w+\.)+\w+\/(@[@\w\d\.]+)(\/\d+)?$/
|
||||
try {
|
||||
let permalink = Array.isArray(to.params.permalink)
|
||||
? to.params.permalink.join('/')
|
||||
: to.params.permalink
|
||||
|
||||
if (!HANDLED_MASTO_URL.test(permalink))
|
||||
if (!HANDLED_MASTO_URLS.test(permalink))
|
||||
return '/home'
|
||||
|
||||
if (!permalink.startsWith('http'))
|
||||
|
@ -19,6 +19,10 @@ definePageMeta({
|
|||
if (!currentUser.value) {
|
||||
const { host, pathname } = parseURL(permalink)
|
||||
await loginTo({ server: host! })
|
||||
|
||||
if (pathname.match(/^\/@[^/]+$/))
|
||||
return `${pathname}@${host}`
|
||||
|
||||
return pathname
|
||||
}
|
||||
|
||||
|
|
23
tests/permalinks.test.ts
Normal file
23
tests/permalinks.test.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { describe, expect, it } from 'vitest'
|
||||
import { HANDLED_MASTO_URLS } from '../constants'
|
||||
|
||||
const validPermalinks = [
|
||||
'https://m1as-social34.to.social/@elk',
|
||||
'https://m1as-social34.to.social/@elk22/123',
|
||||
'https://m1as-social34.to.social/@elk22/objects/123',
|
||||
'mas.to/@elk',
|
||||
]
|
||||
|
||||
const invalidPermalinks = [
|
||||
'https://mas.to',
|
||||
'https://mas.to/elk/123',
|
||||
]
|
||||
|
||||
describe('permalinks', () => {
|
||||
it.each(validPermalinks)('should recognise %s', (url) => {
|
||||
expect(HANDLED_MASTO_URLS.test(url)).toBe(true)
|
||||
})
|
||||
it.each(invalidPermalinks)('should not recognise %s', (url) => {
|
||||
expect(HANDLED_MASTO_URLS.test(url)).toBe(false)
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue