-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathroutable.js
More file actions
33 lines (28 loc) · 798 Bytes
/
routable.js
File metadata and controls
33 lines (28 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import routeMatches from '../shared/routeMatches'
function erase(node) {
node.type = false
delete node.attributes
delete node.children
}
function match(node) {
return node && node.attributes !== undefined && node.attributes.route !== undefined
}
function load({ router }) {
router._routes = {}
}
function transform({ node, depth, router }) {
if (!match(node)) return
const routeDepth = depth.slice(0, depth.lastIndexOf('-'))
if (router._routes[routeDepth] !== undefined) {
erase(node)
} else {
const params = routeMatches(router.url, node.attributes.route)
if (params) {
router._routes[routeDepth] = true
Object.assign(router._segments, params)
} else {
erase(node)
}
}
}
export default { load, transform, client: true, server: true }