-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnav-xml-search.js
More file actions
168 lines (136 loc) · 4.82 KB
/
nav-xml-search.js
File metadata and controls
168 lines (136 loc) · 4.82 KB
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
/* ================================
Nav XML Search (nav-only)
================================ */
(() => {
const XML_PATH = "/search-task.xml";
function formatUrlToTitle(url) {
try {
// Remove protocol and domain
let path = url.replace(/^https?:\/\/[^\/]+/, "");
// Special case for root
if (path === "/" || path === "") return "HouseLearning Home";
// Basic cleanup
path = path.replace(/\/$/, "").replace(/\.html?$/, "");
const segments = path.split("/").filter(s => s && s !== "home");
if (segments.length === 0) return "HouseLearning";
// Map common paths to pretty names
const segmentMap = {
"math": "Math",
"computerscience": "Computer Science",
"computer-science-page": "Computer Science",
"about": "About Us",
"auth": "Login / Sign Up",
"games": "Games",
"blog": "Blog"
};
const parts = segments.map(s => {
if (segmentMap[s]) return segmentMap[s];
// Convert kebab-case or underscore to Title Case
return s.split(/[-_]/)
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
});
// Format based on depth
if (parts.length > 1) {
// e.g. Math: Fractions Add Subtract
return `${parts[0]}: ${parts.slice(1).join(" ")}`;
}
return parts[0];
} catch (e) {
return url;
}
}
class NavXMLSearch {
constructor(nav) {
this.nav = nav;
this.data = [];
this.init();
}
async init() {
// Styles are now in style.css
this.container = document.createElement("div");
this.container.className = "nav-xml-search";
this.container.innerHTML = `
<div class="search-wrapper">
<svg class="search-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
<input type="text" placeholder="Search..." autocomplete="off">
</div>
<div class="nav-xml-results" hidden></div>
`;
this.nav.appendChild(this.container);
this.input = this.container.querySelector("input");
this.results = this.container.querySelector(".nav-xml-results");
await this.loadXML();
this.bind();
}
async loadXML() {
try {
const res = await fetch(XML_PATH, { cache: "force-cache" });
const txt = await res.text();
const xml = new DOMParser().parseFromString(txt, "text/xml");
this.data = [...xml.querySelectorAll("loc")]
.map(n => n.textContent.trim());
} catch (e) {
console.error("[NavXMLSearch] Failed to load XML", e);
}
}
bind() {
this.input.addEventListener("input", () => {
const q = this.input.value.toLowerCase().trim();
this.results.innerHTML = "";
if (!q) {
this.results.hidden = true;
return;
}
const matches = this.data
.filter(url => url.toLowerCase().includes(q))
.slice(0, 8);
if (!matches.length) {
const noResults = document.createElement("div");
noResults.className = "no-results";
noResults.textContent = `No matches found for "${q}"`;
this.results.innerHTML = "";
this.results.appendChild(noResults);
this.results.hidden = false;
return;
}
matches.forEach(url => {
const a = document.createElement("a");
const title = formatUrlToTitle(url);
const shortUrl = url.replace(/^https?:\/\/(www\.)?houselearning\.org/, "");
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")) {
a.href = url;
}
const titleSpan = document.createElement("span");
titleSpan.className = "item-title";
titleSpan.textContent = title;
const urlSpan = document.createElement("span");
urlSpan.className = "item-url";
urlSpan.textContent = shortUrl || "/";
a.appendChild(titleSpan);
a.appendChild(urlSpan);
this.results.appendChild(a);
});
this.results.hidden = false;
});
document.addEventListener("click", e => {
if (!this.container.contains(e.target)) {
this.results.hidden = true;
}
});
// Close on escape
this.input.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
this.results.hidden = true;
this.input.blur();
}
});
}
}
document.addEventListener("DOMContentLoaded", () => {
const actions = document.querySelector(".nav-actions");
if (actions) new NavXMLSearch(actions);
});
})();