-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathutils.js
119 lines (107 loc) · 2.85 KB
/
utils.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { rules } from '../../../src/utils/rules.ts';
import { readable, writable } from 'svelte/store';
import { page } from '$app/stores';
import { base as baseUrl } from '$app/paths';
export function stripBaseUrl(path) {
if (path.startsWith(baseUrl)) {
return path.slice(baseUrl.length);
}
return path;
}
const svelteRules = rules.filter((rule) => !rule.meta.deprecated);
const categories = [
'Possible Errors',
'Security Vulnerability',
'Best Practices',
'Stylistic Issues',
'Extension Rules',
'SvelteKit',
'Experimental',
'System'
];
svelteRules.forEach((rule) => {
if (!categories.includes(rule.meta.docs.category)) {
throw new Error(`missing categories:${rule.meta.docs.category}`);
}
});
const categoryRules = categories.map((cat) => {
return {
title: cat,
children: svelteRules
.filter((rule) => rule.meta.docs.category === cat)
.map((rule) => {
return {
title: rule.meta.docs.ruleId,
path: `/rules/${rule.meta.docs.ruleName}/`
};
})
};
});
const SIDE_MENU = {
'/rules': [
{ path: '/', title: 'Introduction' },
{ path: '/user-guide/', title: 'User Guide' },
{
path: '/rules/',
title: 'Available Rules',
children: categoryRules
},
{ path: '/playground/', title: 'Playground' },
{ path: '/migration/', title: 'Migration' }
],
'/': [
{ path: '/', title: 'Introduction' },
{ path: '/user-guide/', title: 'User Guide' },
{ path: '/rules/', title: 'Available Rules' },
{ path: '/playground/', title: 'Playground' },
{ path: '/migration/', title: 'Migration' }
]
};
export function isActive(path, $page) {
return markdownPath($page.url.pathname) === markdownPath(path);
}
export function markdownPath(path) {
// eslint-disable-next-line no-param-reassign -- ignore
path = stripBaseUrl(path);
let normalized = !path.trim() || path === '/' ? 'README' : path.replace(/^\/|\/$/g, '');
return `${normalized}.md`;
}
export const tocStore = writable({ children: [] });
export const menuItems = readable([], function start(set) {
let pageData = {};
let tocData = { children: [] };
const pageUnsubscriber = page.subscribe(($page) => {
pageData = $page;
set(generateMenu(pageData, tocData));
});
const tocUnsubscriber = tocStore.subscribe((toc) => {
tocData = toc;
set(generateMenu(pageData, tocData));
});
return function stop() {
pageUnsubscriber();
tocUnsubscriber();
};
});
function generateMenu($page, toc) {
const result = [];
const [, menus] = Object.entries(SIDE_MENU).find(([k]) =>
stripBaseUrl($page.url.pathname).startsWith(k)
) || ['/', SIDE_MENU['/']];
for (const { path, title, children } of menus) {
const active = isActive(path, $page);
if (active) {
for (const item of toc.children) {
result.push({
...item,
path,
title,
children: children || item.children
});
}
} else {
result.push({ path, title, children });
}
}
return result;
}