-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
221 lines (187 loc) · 7.25 KB
/
main.js
File metadata and controls
221 lines (187 loc) · 7.25 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".theme-toggle").forEach((btn) => {
btn.addEventListener("click", () => {
const html = document.documentElement;
const next = html.dataset.theme === "light" ? "dark" : "light";
html.dataset.theme = next;
localStorage.setItem("theme", next);
});
});
const yearSpan = document.getElementById("year");
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear().toString();
}
const body = document.body;
if (body.classList.contains("is-blog")) {
const views = document.querySelectorAll(".blog-view");
const buttons = document.querySelectorAll("[data-blog-view]");
const setView = (view) => {
views.forEach((v) => {
v.classList.toggle("is-active", v.dataset.view === view);
});
buttons.forEach((btn) => {
btn.classList.toggle("is-active", btn.dataset.blogView === view);
});
};
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
const view = btn.dataset.blogView;
setView(view);
});
});
setView("newest");
// ── Newest Updated:Pinned 置顶,其余按月分组(最新月在上)──
const newestView = document.querySelector(".blog-view-newest");
if (newestView) {
const grid = newestView.querySelector(".ins-grid");
if (grid) {
const allNodes = Array.from(grid.children);
// 分离 Pinned 和普通文章
const pinned = [];
const dated = [];
allNodes.forEach((node) => {
const article = node.tagName === "ARTICLE" ? node : node.querySelector("article");
if (article && article.dataset.category === "Pinned") {
pinned.push(node);
} else {
const date = article?.dataset.date || "";
dated.push({ node, date });
}
});
// 按月份分组
const monthMap = new Map();
dated.forEach(({ node, date }) => {
const key = date || "Undated";
if (!monthMap.has(key)) monthMap.set(key, []);
monthMap.get(key).push(node);
});
// 月份降序排序(最新在上),Undated 放最后
const sortedMonths = Array.from(monthMap.keys()).sort((a, b) => {
if (a === "Undated") return 1;
if (b === "Undated") return -1;
return b.localeCompare(a);
});
// 格式化月份标题:YYYY-MM → "Mon. YYYY"
const formatMonth = (key) => {
if (key === "Undated") return "Undated";
const [year, month] = key.split("-");
const months = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
"Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];
const m = parseInt(month, 10);
return `${months[m - 1]} ${year}`;
};
// 清空原 grid,重新填入
grid.innerHTML = "";
// 1. Pinned 节点直接放回
pinned.forEach((node) => grid.appendChild(node));
// 2. 按月分组插入:月份标题 + 文章卡片
sortedMonths.forEach((key) => {
const heading = document.createElement("h3");
heading.className = "blog-month-heading";
heading.textContent = formatMonth(key);
grid.appendChild(heading);
monthMap.get(key).forEach((node) => grid.appendChild(node));
});
}
}
const newestContainer = document.querySelector(".blog-view-newest");
const categoryContainer = document.querySelector(
".blog-view-categories"
);
const alphabeticalContainer = document.querySelector(
".blog-view-alphabetical"
);
if (newestContainer && categoryContainer) {
const items = Array.from(
newestContainer.querySelectorAll(".blog-item")
);
const categoryMap = new Map();
items.forEach((item) => {
const category = item.dataset.category || "Uncategorized";
if (!categoryMap.has(category)) {
categoryMap.set(category, []);
}
const node = item.parentElement.tagName === "A" ? item.parentElement : item; // ← 改这里
categoryMap.get(category).push(node);
});
const sortedCategories = Array.from(categoryMap.keys()).sort((a, b) => {
if (a === "Pinned") return -1;
if (b === "Pinned") return 1;
return a.localeCompare(b, "en", { sensitivity: "base" });
});
sortedCategories.forEach((category) => {
const posts = categoryMap.get(category);
const block = document.createElement("section");
block.className = "blog-category";
const header = document.createElement("h3");
header.className = "blog-category-title";
header.textContent = category;
block.appendChild(header);
const list = document.createElement("div");
list.className = "ins-grid";
posts.forEach((post) => {
list.appendChild(post.cloneNode(true));
});
block.appendChild(list);
categoryContainer.appendChild(block);
});
}
// 按字典序(标题 A→Z)视图
if (newestContainer && alphabeticalContainer) {
const items = Array.from(
newestContainer.querySelectorAll(".blog-item")
);
const pinned = [];
const lettered = [];
items.forEach((item) => {
const title = item.querySelector(".ins-title")?.textContent.trim() ?? "";
const node = item.parentElement.tagName === "A" ? item.parentElement : item;
const isPin = item.dataset.category === "Pinned";
if (isPin) {
pinned.push({ title, node });
} else {
lettered.push({ title, node });
}
});
// 按标题字典序 A→Z
lettered.sort((a, b) =>
a.title.localeCompare(b.title, undefined, { sensitivity: "base" })
);
// 按首字母分组
const letterMap = new Map();
lettered.forEach(({ title, node }) => {
const first = title.charAt(0).toUpperCase();
const key = /[A-Z]/.test(first) ? first : "#";
if (!letterMap.has(key)) letterMap.set(key, []);
letterMap.get(key).push(node);
});
// 字母排序,# 放最后
const sortedLetters = Array.from(letterMap.keys()).sort((a, b) => {
if (a === "#") return 1;
if (b === "#") return -1;
return a.localeCompare(b);
});
// 1. Pinned 置顶
if (pinned.length > 0) {
const pinnedGrid = document.createElement("div");
pinnedGrid.className = "ins-grid";
pinned.forEach(({ node }) => pinnedGrid.appendChild(node.cloneNode(true)));
alphabeticalContainer.appendChild(pinnedGrid);
}
// 2. 字母分组
sortedLetters.forEach((key) => {
const block = document.createElement("section");
block.className = "blog-category";
const heading = document.createElement("h3");
heading.className = "blog-category-title";
heading.textContent = key;
block.appendChild(heading);
const grid = document.createElement("div");
grid.className = "ins-grid";
letterMap.get(key).forEach((node) => grid.appendChild(node.cloneNode(true)));
block.appendChild(grid);
alphabeticalContainer.appendChild(block);
});
}
}
});