feat: 首页由 navigation 首项决定
- 移除 navigation.active 配置项,默认页以 navigation[0] 为准(生成端/前端一致) - 注入 homePageId;首页渲染用 profile.title/profile.subtitle 覆盖 title/subtitle - 模板按 homePageId 切换首页/非首页标题 DOM 与 data-editable,避免样式错位 - 更新默认配置与文档;书签导入不再写入 active 字段 - 新增/更新单测覆盖首页规则与 profile 覆盖 BREAKING CHANGE: 不再支持 navigation[].active;通过调整 navigation 顺序设置默认页/首页
This commit is contained in:
@@ -794,8 +794,7 @@ function updateNavigationFile(filePath) {
|
||||
navConfig.push({
|
||||
name: '书签',
|
||||
icon: 'fas fa-bookmark',
|
||||
id: 'bookmarks',
|
||||
active: false
|
||||
id: 'bookmarks'
|
||||
});
|
||||
|
||||
// 更新文件
|
||||
|
||||
@@ -462,13 +462,6 @@ function prepareRenderData(config) {
|
||||
// 移除警告日志,数据处理逻辑保留
|
||||
}
|
||||
|
||||
// 添加序列化的配置数据,用于浏览器扩展
|
||||
renderData.configJSON = JSON.stringify({
|
||||
version: process.env.npm_package_version || '1.0.0',
|
||||
timestamp: new Date().toISOString(),
|
||||
data: renderData // 使用经过处理的renderData而不是原始config
|
||||
});
|
||||
|
||||
// 添加导航项的活动状态标记和子菜单
|
||||
if (Array.isArray(renderData.navigation)) {
|
||||
renderData.navigation = renderData.navigation.map((item, index) => {
|
||||
@@ -476,7 +469,7 @@ function prepareRenderData(config) {
|
||||
...item,
|
||||
isActive: index === 0, // 默认第一项为活动项
|
||||
id: item.id || `nav-${index}`,
|
||||
active: index === 0 // 兼容原有逻辑
|
||||
active: index === 0 // 保持旧模板兼容(由顺序决定,不读取配置的 active 字段)
|
||||
};
|
||||
|
||||
// 使用辅助函数获取子菜单
|
||||
@@ -489,6 +482,16 @@ function prepareRenderData(config) {
|
||||
});
|
||||
}
|
||||
|
||||
// 首页(默认页)规则:navigation 顺序第一项即首页
|
||||
renderData.homePageId = renderData.navigation && renderData.navigation[0] ? renderData.navigation[0].id : null;
|
||||
|
||||
// 添加序列化的配置数据,用于浏览器扩展(确保包含 homePageId 等处理结果)
|
||||
renderData.configJSON = JSON.stringify({
|
||||
version: process.env.npm_package_version || '1.0.0',
|
||||
timestamp: new Date().toISOString(),
|
||||
data: renderData // 使用经过处理的renderData而不是原始config
|
||||
});
|
||||
|
||||
// 为Handlebars模板特别准备navigationData数组
|
||||
renderData.navigationData = renderData.navigation;
|
||||
|
||||
@@ -814,6 +817,17 @@ function renderPage(pageId, config) {
|
||||
Object.assign(data, config[pageId]);
|
||||
}
|
||||
|
||||
// 首页标题规则:使用 site.yml 的 profile 覆盖首页(导航第一项)的 title/subtitle 显示
|
||||
const homePageId = config.homePageId
|
||||
|| (Array.isArray(config.navigation) && config.navigation[0] ? config.navigation[0].id : null)
|
||||
|| 'home';
|
||||
// 供模板判断“当前是否首页”
|
||||
data.homePageId = homePageId;
|
||||
if (pageId === homePageId && config.profile) {
|
||||
if (config.profile.title !== undefined) data.title = config.profile.title;
|
||||
if (config.profile.subtitle !== undefined) data.subtitle = config.profile.subtitle;
|
||||
}
|
||||
|
||||
// 检查页面配置中是否指定了模板
|
||||
let templateName = pageId;
|
||||
if (config[pageId] && config[pageId].template) {
|
||||
@@ -844,11 +858,6 @@ function generateAllPagesHTML(config) {
|
||||
});
|
||||
}
|
||||
|
||||
// 确保首页存在
|
||||
if (!pages.home) {
|
||||
pages.home = renderPage('home', config);
|
||||
}
|
||||
|
||||
// 确保搜索结果页存在
|
||||
if (!pages['search-results']) {
|
||||
pages['search-results'] = renderPage('search-results', config);
|
||||
@@ -989,7 +998,9 @@ function main() {
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
|
||||
// 导出供测试使用的函数
|
||||
module.exports = {
|
||||
@@ -1002,4 +1013,3 @@ module.exports = {
|
||||
renderTemplate,
|
||||
generateAllPagesHTML
|
||||
};
|
||||
|
||||
|
||||
@@ -641,7 +641,41 @@ function extractNestedData(element) {
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// 先声明所有状态变量
|
||||
let isSearchActive = false;
|
||||
let currentPageId = 'home';
|
||||
// 首页不再固定为 "home":以导航顺序第一项为准
|
||||
const homePageId = (() => {
|
||||
// 1) 优先从生成端注入的配置数据读取(保持与实际导航顺序一致)
|
||||
try {
|
||||
const config = window.MeNav && typeof window.MeNav.getConfig === 'function'
|
||||
? window.MeNav.getConfig()
|
||||
: null;
|
||||
const injectedHomePageId = config && config.data && config.data.homePageId
|
||||
? String(config.data.homePageId).trim()
|
||||
: '';
|
||||
if (injectedHomePageId) return injectedHomePageId;
|
||||
const nav = config && config.data && Array.isArray(config.data.navigation)
|
||||
? config.data.navigation
|
||||
: null;
|
||||
const firstId = nav && nav[0] && nav[0].id ? String(nav[0].id).trim() : '';
|
||||
if (firstId) return firstId;
|
||||
} catch (error) {
|
||||
// 忽略解析错误,继续使用 DOM 推断
|
||||
}
|
||||
|
||||
// 2) 回退到 DOM:取首个导航项的 data-page
|
||||
const firstNavItem = document.querySelector('.nav-item[data-page]');
|
||||
if (firstNavItem) {
|
||||
const id = String(firstNavItem.getAttribute('data-page') || '').trim();
|
||||
if (id) return id;
|
||||
}
|
||||
|
||||
// 3) 最后兜底:取首个页面容器 id
|
||||
const firstPage = document.querySelector('.page[id]');
|
||||
if (firstPage && firstPage.id) return firstPage.id;
|
||||
|
||||
return 'home';
|
||||
})();
|
||||
|
||||
let currentPageId = homePageId;
|
||||
let isInitialLoad = true;
|
||||
let isSidebarOpen = false;
|
||||
let isSearchOpen = false;
|
||||
@@ -1257,9 +1291,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
} else {
|
||||
// 如果没有激活的导航项,默认显示首页
|
||||
currentPageId = 'home';
|
||||
currentPageId = homePageId;
|
||||
pages.forEach(page => {
|
||||
page.classList.toggle('active', page.id === 'home');
|
||||
page.classList.toggle('active', page.id === homePageId);
|
||||
});
|
||||
}
|
||||
} catch (resetError) {
|
||||
@@ -1489,7 +1523,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
// 立即执行初始化,不再使用requestAnimationFrame延迟
|
||||
// 显示首页
|
||||
showPage('home');
|
||||
showPage(homePageId);
|
||||
|
||||
// 添加载入动画
|
||||
categories.forEach((category, index) => {
|
||||
|
||||
Reference in New Issue
Block a user