From 46e72d1eb0f7fa23a77cddd7c52419c0e93fba0b Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Sat, 20 Jan 2024 01:51:09 +0800 Subject: [PATCH] [opt] article top area & sidebar --- _config.yml | 34 ++-- languages/zh-CN.yml | 2 +- languages/zh-TW.yml | 2 +- .../_partial/main/navbar/article_top_area.ejs | 35 ++++ layout/_partial/main/navbar/breadcrumb.ejs | 155 ------------------ .../_partial/main/navbar/breadcrumb/blog.ejs | 34 ++++ .../_partial/main/navbar/breadcrumb/page.ejs | 10 ++ .../_partial/main/navbar/breadcrumb/wiki.ejs | 20 +++ layout/_partial/main/navbar/dateinfo.ejs | 32 ++++ layout/_partial/main/navbar/ghinfo.ejs | 25 +++ layout/_partial/main/navbar/nav_tabs_blog.ejs | 6 +- layout/_partial/main/navbar/nav_tabs_wiki.ejs | 4 +- layout/_partial/sidebar/index.ejs | 11 +- layout/page.ejs | 10 +- scripts/events/lib/doc_tree.js | 26 +-- scripts/generators/topic.js | 4 +- scripts/generators/wiki.js | 6 +- 17 files changed, 218 insertions(+), 198 deletions(-) create mode 100644 layout/_partial/main/navbar/article_top_area.ejs delete mode 100644 layout/_partial/main/navbar/breadcrumb.ejs create mode 100644 layout/_partial/main/navbar/breadcrumb/blog.ejs create mode 100644 layout/_partial/main/navbar/breadcrumb/page.ejs create mode 100644 layout/_partial/main/navbar/breadcrumb/wiki.ejs create mode 100644 layout/_partial/main/navbar/dateinfo.ejs create mode 100644 layout/_partial/main/navbar/ghinfo.ejs diff --git a/_config.yml b/_config.yml index 3d144b1..77bd954 100755 --- a/_config.yml +++ b/_config.yml @@ -59,28 +59,38 @@ menubar: ######## Main ######## # 站点主结构树 site_tree: + # -- 列表类页面 -- # # 主页配置 home: - sidebar: recent, timeline - # 博客列表配置 - blog: + sidebar: welcome, recent, timeline + # 博客列表页配置 + index_blog: base_dir: blog # 只影响自动生成的页面路径 menu_id: post # 未在 front-matter 中指定 menu_id 时,layout 为 post 的页面默认使用这里配置的 menu_id - sidebar: recent, timeline # for categories/tags/archives + sidebar: welcome, recent, timeline # for categories/tags/archives nav_tabs: # 近期发布 分类 标签 专栏 归档 and ... # '朋友文章': /friends/rss/ - # 博客文章配置 + # 博客专栏列表页配置 + index_topic: + base_dir: topic # 只影响自动生成的页面路径 + menu_id: post # 未在 front-matter 中指定 menu_id 时,layout 为 topic 的页面默认使用这里配置的 menu_id + # 文档列表页配置 + index_wiki: + base_dir: wiki # 只影响自动生成的页面路径 + menu_id: wiki # 未在 front-matter 中指定 menu_id 时,layout 为 wiki 的页面默认使用这里配置的 menu_id + sidebar: toc, ghissues, related, recent # for wiki + nav_tabs: + # 'more': https://github.com/xaoxuu + # -- 内容类页面 -- # + # 博客文章内页配置 post: menu_id: post # 未在 front-matter 中指定 menu_id 时,layout 为 post 的页面默认使用这里配置的 menu_id sidebar: toc, related, ghrepo, ghissues, recent # for pages using 'layout:post' - # 博客专栏配置 + # 博客专栏文章内页配置 topic: - base_dir: topic # 只影响自动生成的页面路径 - menu_id: post # 未在 front-matter 中指定 menu_id 时,layout 为 topic 的页面默认使用这里配置的 menu_id - sidebar: toc, related # for topic - # wiki配置 + menu_id: post + # 文档内页配置 wiki: - base_dir: wiki # 只影响自动生成的页面路径 menu_id: wiki # 未在 front-matter 中指定 menu_id 时,layout 为 wiki 的页面默认使用这里配置的 menu_id sidebar: toc, ghissues, related, recent # for wiki # 作者信息配置 @@ -95,7 +105,7 @@ site_tree: sidebar: recent, timeline # 其它自定义页面配置 layout: page page: - sidebar: toc, timeline + sidebar: toc, recent, timeline diff --git a/languages/zh-CN.yml b/languages/zh-CN.yml index bdca9a5..9577dc8 100755 --- a/languages/zh-CN.yml +++ b/languages/zh-CN.yml @@ -1,7 +1,7 @@ btn: home: 主页 blog: 文章 - wiki: 项目 + wiki: 文档 topic: 专栏 recent_publish: 近期发布 all_wiki: 所有项目 diff --git a/languages/zh-TW.yml b/languages/zh-TW.yml index 2d43d1e..f72f34d 100755 --- a/languages/zh-TW.yml +++ b/languages/zh-TW.yml @@ -1,7 +1,7 @@ btn: home: 首頁 blog: 網誌 - wiki: Wiki + wiki: 文檔 topic: 專欄 recent_publish: 近期發布 all_wiki: 所有 Wiki diff --git a/layout/_partial/main/navbar/article_top_area.ejs b/layout/_partial/main/navbar/article_top_area.ejs new file mode 100644 index 0000000..fea4ca8 --- /dev/null +++ b/layout/_partial/main/navbar/article_top_area.ejs @@ -0,0 +1,35 @@ +<% +function layoutDiv() { + if (page.breadcrumb === false) { + return '' + } + var el = '' + // 1.main + el += `
` + // 2.left + el += `
` + // 3.left.top: 面包屑导航 + el += `` + // 3.left.bottom + el += partial('dateinfo') + // end 2.left + el += `
` + // 2.right + el += partial('ghinfo') + // end 1.main + el += `
` + return el +} +%> +<%- layoutDiv() %> diff --git a/layout/_partial/main/navbar/breadcrumb.ejs b/layout/_partial/main/navbar/breadcrumb.ejs deleted file mode 100644 index 5686c2f..0000000 --- a/layout/_partial/main/navbar/breadcrumb.ejs +++ /dev/null @@ -1,155 +0,0 @@ -<% -function layoutDiv() { - var el = ''; - if (page.breadcrumb === false) { - return el; - } - var home_title = __("btn.home"); - if (page.layout === "post") { - var firstCat = ""; - if (page.categories && page.categories.length > 0) { - firstCat = page.categories.data[0].name; - } - el += '
'; - el += '
'; - el += ''; - // 作者 - var author = null - if (theme.authors) { - if (page.author?.length > 0 && theme.authors[page.author] != null) { - author = theme.authors[page.author] - } else { - author = theme.default_author - } - } - el += '
'; - if (author) { - let link = `${author.name}` - el += `${__("meta.created_author", link)}` - } else { - el += `${__("meta.created")}` - } - // 发布日期 - el += ` - - `; - // 更新日期 - el += ` - ${__("symbol.comma") + __("meta.updated")}  - `; - el += '
'; - - el += '
'; - el += '
'; - } else if (page.topic?.length > 0) { - el += '
'; - el += '
'; - el += ''; - // 更新日期 - el += '
'; - el += ` - ${__("meta.updated")}  - `; - el += '
'; - el += '
'; - el += '
'; - } else if (page.layout === "wiki" && page.wiki?.length > 0) { - el += '
'; - el += '
'; - el += ''; - // 更新日期 - el += '
'; - el += ` - ${__("meta.updated")}  - `; - el += '
'; - el += '
'; - - const repo = page.repo || proj?.repo - if (repo) { - el += ` - - `; - } - el += '
'; - } else if (page.title) { - el += ` -
- -
- `; - } - return el; -} -%> -<%- layoutDiv() %> diff --git a/layout/_partial/main/navbar/breadcrumb/blog.ejs b/layout/_partial/main/navbar/breadcrumb/blog.ejs new file mode 100644 index 0000000..9ca8c4e --- /dev/null +++ b/layout/_partial/main/navbar/breadcrumb/blog.ejs @@ -0,0 +1,34 @@ +<% +function layoutDiv() { + const topicObject = theme.topic.tree[page.topic] + var el = '' + if (topicObject) { // 专栏文章 + // 专栏列表页 + const topicIndexPageURL = url_for(theme.site_tree.index_topic.base_dir) + el += `` + el += `${__("btn.topic")}` + // 专栏项目名 + const topicHomePageURL = url_for(topicObject.homepage?.path); + if (topicHomePageURL != topicIndexPageURL) { + el += `` + el += `${topicObject.name || topicObject.title}` + } + } else { // 普通文章 + // 文章列表页 + el += `` + el += `${__("btn.blog")}` + // 分类列表页 + if (page.categories?.length > 0) { + el += `` + el += list_categories(page.categories, { + class: "cap breadcrumb", + show_count: false, + separator: ' ', + style: "none" + }) + } + } + return el +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/breadcrumb/page.ejs b/layout/_partial/main/navbar/breadcrumb/page.ejs new file mode 100644 index 0000000..7afa73f --- /dev/null +++ b/layout/_partial/main/navbar/breadcrumb/page.ejs @@ -0,0 +1,10 @@ +<% +function layoutDiv() { + // 普通页面就显示当前页面 + return ` + + ${page.title} + ` +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/breadcrumb/wiki.ejs b/layout/_partial/main/navbar/breadcrumb/wiki.ejs new file mode 100644 index 0000000..0cdc4ad --- /dev/null +++ b/layout/_partial/main/navbar/breadcrumb/wiki.ejs @@ -0,0 +1,20 @@ +<% +function layoutDiv() { + const wikiObject = theme.wiki.tree[page.wiki] + var el = '' + // 文档列表页 + el += `` + const wikiIndexPageURL = url_for(theme.site_tree.index_wiki.base_dir) + el += `${__("btn.wiki")}` + if (wikiObject) { + // 文档项目名 + const wikiHomePageURL = url_for(wikiObject.homepage?.path); + if (wikiHomePageURL != wikiIndexPageURL) { + el += `` + el += `${wikiObject.name || wikiObject.title}` + } + } + return el +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/dateinfo.ejs b/layout/_partial/main/navbar/dateinfo.ejs new file mode 100644 index 0000000..d582d8c --- /dev/null +++ b/layout/_partial/main/navbar/dateinfo.ejs @@ -0,0 +1,32 @@ +<% +function layoutDiv() { + // 作者 + const author = theme.authors ? (theme.authors[page.author] || theme.default_author) : null + var el = '' + el += `
` + if (page.wiki) { + el += `${__("meta.updated") + __("symbol.colon")}` + el += `` + el += `` + } else { + if (author) { + const link = `${author.name}` + el += `${__("meta.created_author", link) + __("symbol.colon")}` + } else { + el += `${__("meta.created") + __("symbol.colon")}` + } + // 发布日期 + el += `` + el += `` + el += `` + // 更新日期 + el += `` + el += `${__("symbol.comma") + __("meta.updated") + __("symbol.colon")}` + el += `` + el += `` + } + el += `
` + return el +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/ghinfo.ejs b/layout/_partial/main/navbar/ghinfo.ejs new file mode 100644 index 0000000..e65118f --- /dev/null +++ b/layout/_partial/main/navbar/ghinfo.ejs @@ -0,0 +1,25 @@ +<% +function layoutDiv() { + const repo = page.repo || theme.wiki.tree[page.wiki]?.repo + if (repo == null) { + return '' + } + return ` +
+ + ${icon('github:repo')} + ${repo} + + + ${icon('github:star')} + 0stars + + + ${icon('github:fork')} + 0forks + +
+ ` +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/nav_tabs_blog.ejs b/layout/_partial/main/navbar/nav_tabs_blog.ejs index 36fa605..5d16a02 100644 --- a/layout/_partial/main/navbar/nav_tabs_blog.ejs +++ b/layout/_partial/main/navbar/nav_tabs_blog.ejs @@ -31,9 +31,9 @@ function layoutDiv() { if (theme.topic?.publish_list?.length > 0) { if (page.layout == 'index_topic') { - el += '' + __("btn.topic") + ''; + el += '' + __("btn.topic") + ''; } else { - el += '' + __("btn.topic") + ''; + el += '' + __("btn.topic") + ''; } } @@ -45,7 +45,7 @@ function layoutDiv() { } } - const { nav_tabs } = theme.site_tree.blog + const { nav_tabs } = theme.site_tree.index_blog if (nav_tabs) { for (let key of Object.keys(nav_tabs)) { // 当 page 的末尾为 index.html 或者处于该 page 的某个子页面时也应该匹配 diff --git a/layout/_partial/main/navbar/nav_tabs_wiki.ejs b/layout/_partial/main/navbar/nav_tabs_wiki.ejs index 2d6f81b..4338749 100644 --- a/layout/_partial/main/navbar/nav_tabs_wiki.ejs +++ b/layout/_partial/main/navbar/nav_tabs_wiki.ejs @@ -8,7 +8,7 @@ function layoutDiv() { if (page.filter === false) { el += ' class="active"'; } - el += ' href="' + url_for(theme.site_tree.wiki.base_dir) + '">' + __("btn.all_wiki") + ''; + el += ' href="' + url_for(theme.site_tree.index_wiki.base_dir) + '">' + __("btn.all_wiki") + ''; el += ''; // 项目分类 const { shelf, all_tags } = theme.wiki; @@ -25,7 +25,7 @@ function layoutDiv() { } } - const { nav_tabs } = theme.site_tree.wiki + const { nav_tabs } = theme.site_tree.index_wiki if (nav_tabs) { for (let key of Object.keys(nav_tabs)) { // 当 page 的末尾为 index.html 或者处于该 page 的某个子页面时也应该匹配 diff --git a/layout/_partial/sidebar/index.ejs b/layout/_partial/sidebar/index.ejs index a063bd9..0cc80b3 100755 --- a/layout/_partial/sidebar/index.ejs +++ b/layout/_partial/sidebar/index.ejs @@ -9,13 +9,16 @@ if (page.sidebar == null) { if (is_home()) { sidebar = site_tree.home.sidebar } else if (is_category() || is_tag() || is_archive() || ['categories', 'tags', 'archives'].includes(page.layout)) { - sidebar = site_tree.blog.sidebar + sidebar = site_tree.index_blog.sidebar } else if (page.layout === 'index_topic') { // 专栏列表页等同于博客列表页 - sidebar = site_tree.blog.sidebar + sidebar = site_tree.index_blog.sidebar } else if (page.topic?.length > 0) { - sidebar = site_tree.topic.sidebar - } else if (page.layout === 'index_wiki' || page.wiki) { + // 专栏文章内页等同于普通文章内页 + sidebar = site_tree.post.sidebar + } else if (page.layout === 'index_wiki') { + sidebar = site_tree.index_wiki.sidebar + } else if (page.wiki?.length > 0) { sidebar = site_tree.wiki.sidebar } else if (page.layout === '404') { sidebar = site_tree.error_page.sidebar diff --git a/layout/page.ejs b/layout/page.ejs index 9d741d8..6403ae8 100755 --- a/layout/page.ejs +++ b/layout/page.ejs @@ -7,10 +7,12 @@ const isUsingTianliGPT = theme.plugins.tianli_gpt.enable && ['all', page.layout] // 默认的 menu_id if (page.menu_id == null) { - if (page.wiki) { - page.menu_id = 'wiki' + if (page.wiki?.length > 0) { + page.menu_id = theme.site_tree.wiki.menu_id + } else if (page.topic?.length > 0) { + page.menu_id = theme.site_tree.topic.menu_id } else { - page.menu_id = 'post' + page.menu_id = theme.site_tree.post.menu_id } } // 默认的 title @@ -46,7 +48,7 @@ function layoutDiv() { el += partial('_partial/main/navbar/nav_tabs_blog') } if (page.h1 || page.title || (page.content && page.content.length > 0)) { - el += partial('_partial/main/navbar/breadcrumb') + el += partial('_partial/main/navbar/article_top_area') } el += `
` el += layoutTitle() diff --git a/scripts/events/lib/doc_tree.js b/scripts/events/lib/doc_tree.js index c158a17..153b322 100644 --- a/scripts/events/lib/doc_tree.js +++ b/scripts/events/lib/doc_tree.js @@ -34,11 +34,15 @@ function getWikiObject(ctx) { if (obj.sort == null) { obj.sort = 0 } - if (obj.path?.startsWith('/')) { - obj.path = obj.path.substring(1) - } - if (obj.path?.endsWith('/') == false) { - obj.path = obj.path + '/' + if (obj.base_dir) { + if (obj.base_dir.startsWith('/')) { + obj.base_dir = obj.base_dir.substring(1) + } + if (obj.base_dir.length > 1 && obj.base_dir.endsWith('/') == false) { + obj.base_dir = obj.base_dir + '/' + } + } else { + obj.base_dir = '' } list.push(obj) } @@ -104,7 +108,7 @@ module.exports = ctx => { for (let id of Object.keys(item.toc)) { const sec = item.toc[id] for (let key of sec) { - let hs = sub_pages.filter(p => p.path_key == item.path + key) + let hs = sub_pages.filter(p => p.path_key == item.base_dir + key) if (hs.length > 0) { item.homepage = hs[0] break @@ -127,21 +131,21 @@ module.exports = ctx => { for (let title of Object.keys(item.toc)) { var sec = { title: title, pages: []} for (let key of item.toc[title]) { - sec.pages = sec.pages.concat(sub_pages.filter(p => p.path_key == item.path + key)) - others = others.filter(p => p.path_key != item.path + key) + sec.pages = sec.pages.concat(sub_pages.filter(p => p.path_key == item.base_dir + key)) + others = others.filter(p => p.path_key != item.base_dir + key) } sections.push(sec) } if (others.length > 0 && others.filter(p => p.title?.length > 0).length > 0) { sections.push({ title: '...', - pages: others.sort((p1, p2) => p1.path - p2.path) + pages: others.sort((p1, p2) => p1.title - p2.title) }) } } else { // 自动设置顺序 sections.push({ - pages: sub_pages.sort((p1, p2) => p1.path - p2.path) + pages: sub_pages.sort((p1, p2) => p1.title - p2.title) }) } @@ -176,7 +180,7 @@ module.exports = ctx => { } all_tags[tag_name] = { name: tag_name, - path: (ctx.theme.config.site_tree.wiki.base_dir) + '/tags/' + tag_name + '/index.html', + path: (ctx.theme.config.site_tree.index_wiki.base_dir) + '/tags/' + tag_name + '/index.html', items: items } }) diff --git a/scripts/generators/topic.js b/scripts/generators/topic.js index 723d425..af133bd 100644 --- a/scripts/generators/topic.js +++ b/scripts/generators/topic.js @@ -10,11 +10,11 @@ hexo.extend.generator.register('index_topic', function (locals) { } var ret = [] ret.push({ - path: site_tree.topic.base_dir + '/index.html', + path: site_tree.index_topic.base_dir + '/index.html', layout: ['index_topic'], data: { layout: 'index_topic', - menu_id: site_tree.topic.menu_id + menu_id: site_tree.index_topic.menu_id } }) return ret diff --git a/scripts/generators/wiki.js b/scripts/generators/wiki.js index 990dd3d..75e58a5 100644 --- a/scripts/generators/wiki.js +++ b/scripts/generators/wiki.js @@ -10,11 +10,11 @@ hexo.extend.generator.register('wiki', function (locals) { } var ret = [] ret.push({ - path: site_tree.wiki.base_dir + '/index.html', + path: site_tree.index_wiki.base_dir + '/index.html', layout: ['index_wiki'], data: { layout: 'index_wiki', - menu_id: site_tree.wiki.menu_id, + menu_id: site_tree.index_wiki.menu_id, filter: false } }) @@ -26,7 +26,7 @@ hexo.extend.generator.register('wiki', function (locals) { layout: ['index_wiki'], data: { layout: 'index_wiki', - menu_id: site_tree.wiki.menu_id, + menu_id: site_tree.index_wiki.menu_id, filter: true, tagName: tag.name, title: tag.name