Skip to content

Commit

Permalink
Add support for relative paths as issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlee committed Feb 5, 2024
1 parent 7eba41b commit 6cd2152
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shaun/alpinejs-router",
"version": "1.2.13",
"version": "1.3.0",
"description": "Easy to use and flexible router for Alpine.js",
"type": "module",
"main": "dist/module.cjs.js",
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ export default function (Alpine) {
return inLoadProgress[url]
}

const tpl = el.getAttribute('template') ?? el.getAttribute('template.preload')
const tpl = RouterURL.resolveTemplatePath(
location.pathname,
el.getAttribute('template') ?? el.getAttribute('template.preload')
)

let loading
if (el.hasAttribute('template.preload')) {
Expand Down
27 changes: 27 additions & 0 deletions src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ export class RouterURL {
this.url = l + (r ? '?' + r : '')
return this
}

static resolveTemplatePath (pathname, tpl) {
if (tpl.startsWith('/')) {
return tpl
}

const dir = pathname.slice(0, pathname.lastIndexOf('/'))

if (tpl.startsWith('./')) {
return dir + tpl.slice(1)
}

if (tpl.startsWith('../')) {
const re = /\.\.\//g
let i = tpl.match(re).length
let arr = dir.split('/')
while (i--) {
arr.pop()
}
if (arr.length === 0) {
arr = ['']
}
return [...arr, tpl.replace(re, '')].join('/')
}

return tpl
}
}
23 changes: 23 additions & 0 deletions tests/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,28 @@ describe('URL', () => {
expect(u.path).toBe('/hello/world')
expect(u.resolve('/xyz', {a: '123', d: 1}).url).toBe('http://localhost/#/xyz?a=123&b=c&d=1')
})

test('resolve template path', () => {
let pathname = new URL('http://localhost/hello/world').pathname
expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/hello/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')

pathname = new URL('http://localhost/hello/world/').pathname
expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/hello/world/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/hello/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')

pathname = new URL('http://localhost/').pathname
expect(RouterURL.resolveTemplatePath(pathname, '/a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, './a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../a.html')).toStrictEqual('/a.html')
expect(RouterURL.resolveTemplatePath(pathname, '../../../a.html')).toStrictEqual('/a.html')
})
})
})

0 comments on commit 6cd2152

Please sign in to comment.