deno.land / x / lume@v2.1.4 / plugins / nav.ts

نووسراو ببینە
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { merge } from "../core/utils/object.ts";
import type Site from "../core/site.ts";import type Searcher from "../core/searcher.ts";import type { Data } from "../core/file.ts";
export interface Options { /** The helper name */ name?: string;
/** The default order for the children */ order?: string;}
export const defaults: Options = { name: "nav",};
/** Register the plugin to enable the `search` helpers */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { const nav = new Nav(site.search); site.data(options.name, nav); site.addEventListener("beforeUpdate", () => nav.deleteCache()); };}
/** Search helper */export class Nav { #cache = new Map<string, NavData>(); #search: Searcher;
constructor(searcher: Searcher) { this.#search = searcher; }
/** Clear the cache (used after a change in watch mode) */ deleteCache() { this.#cache.clear(); }
menu(url?: "/", query?: string, sort?: string): NavData; menu(url: string, query?: string, sort?: string): NavData | undefined; menu(url = "/", query?: string, sort?: string): NavData | undefined { const id = JSON.stringify([query, sort]); let nav = this.#cache.get(id);
if (!nav) { nav = this.#buildNav(query, sort); this.#cache.set(id, nav); }
const parts = url.split("/").filter((part) => part !== ""); return searchData(parts, nav); }
breadcrumb(url: string, query?: string, sort?: string): NavData[] { let nav = this.menu(url, query, sort); const breadcrumb: NavData[] = [];
while (nav) { breadcrumb.unshift(nav); nav = nav.parent; }
return breadcrumb; }
/* Build the entire navigation tree */ #buildNav(query?: string, sort?: string): NavData { const nav: TempNavData = { slug: "", };
const dataPages = this.#search.pages(query, sort);
for (const data of dataPages) { const url = data.page?.outputPath; const parts = url.split("/").filter((part) => part !== ""); let part = parts.shift(); let current = nav;
while (part) { if (part === "index.html") { current.data = data; break; }
if (!current.children) { current.children = {}; }
if (!current.children[part]) { current = current.children[part] = { slug: part, parent: current, }; } else { current = current.children[part]; }
if (parts.length === 0) { current.data = data; break; }
part = parts.shift(); } }
return convert(nav); }}
export interface TempNavData { slug: string; data?: Data; children?: Record<string, TempNavData>; parent?: TempNavData;}
export interface NavData { slug: string; data?: Data; children?: NavData[]; parent?: NavData;}
function searchData(parts: string[], menu: NavData): NavData | undefined { const part = parts.shift();
if (!part) { return menu; }
if (menu.children?.length) { for (const child of menu.children) { if (child.slug === part) { return searchData(parts, child); } } }}
// Convert TempNavData to NavDatafunction convert(temp: TempNavData, parent?: NavData, order?: string): NavData { const data: NavData = { slug: temp.slug, data: temp.data, parent, };
data.children = temp.children ? Object.values(temp.children) .map((child) => convert(child, data, order)) .sort((a, b) => { if (!order) { return a.slug < b.slug ? -1 : 1; } return a.data?.[order] < b.data?.[order] ? -1 : 1; }) : undefined;
return data;}
/** Extends Data interface */declare global { namespace Lume { export interface Data { /** @see https://lume.land/plugins/nav/ */ nav: Nav; } }}
lume

Version Info

Tagged at
7 months ago