From 38bfa89c0962f37b01b3eee3bfdf63505cf43ec1 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Wed, 6 Dec 2023 13:22:07 +0800 Subject: [PATCH] [opt] tag -> hashtag --- _config.yml | 8 +-- layout/_partial/main/article/ai_abstract.ejs | 4 +- layout/_partial/main/article/read_next.ejs | 27 +++++---- layout/_partial/main/navbar/breadcrumb.ejs | 44 +++++++++++--- layout/_partial/main/navbar/list_wiki.ejs | 7 ++- .../plugins/comments/beaudar/script.ejs | 2 +- .../plugins/comments/giscus/script.ejs | 2 +- .../plugins/comments/utterances/script.ejs | 2 +- layout/_partial/widgets/related.ejs | 14 +++-- layout/_partial/widgets/toc.ejs | 15 ++--- layout/index.ejs | 8 +-- scripts/events/lib/doc_tree.js | 60 +++++++++++++------ scripts/tags/index.js | 2 +- scripts/tags/lib/hashtag.js | 36 +++++++++++ scripts/tags/lib/tag.js | 35 ----------- source/css/_common/highlight.styl | 2 + source/css/_custom.styl | 4 +- source/css/_layout/md.styl | 7 ++- source/css/_layout/partial/bread-nav.styl | 25 ++++++++ source/css/_layout/tag-plugins/common.styl | 4 +- source/css/_layout/tag-plugins/hashtag.styl | 17 ++++++ source/css/_layout/tag-plugins/mark.styl | 5 +- source/css/_layout/tag-plugins/tag.styl | 13 ---- source/js/main.js | 2 +- source/js/plugins/copycode.js | 4 +- source/js/plugins/linkcard.js | 2 +- 26 files changed, 223 insertions(+), 128 deletions(-) create mode 100644 scripts/tags/lib/hashtag.js delete mode 100644 scripts/tags/lib/tag.js create mode 100644 source/css/_layout/tag-plugins/hashtag.styl delete mode 100644 source/css/_layout/tag-plugins/tag.styl diff --git a/_config.yml b/_config.yml index 76995a6..9a58cd4 100755 --- a/_config.yml +++ b/_config.yml @@ -38,7 +38,7 @@ sidebar: # 文章内页 post: search_blog, toc, ghrepo, ghissues # for pages using 'layout:post' # 文档内页 - wiki: search, ghrepo, toc, ghissues, related # for pages using 'layout:wiki' + wiki: search, toc, ghissues, related # for pages using 'layout:wiki' # 其它 layout:page 的页面 page: toc, search # for custom pages using 'layout:page' @@ -248,9 +248,9 @@ tag_plugins: # {% mark %} mark: default_color: dark # light, dark, red, orange, yellow, green, cyan, blue, purple, warning, error - # {% tag %} - tag: - default_color: yellow # red, orange, yellow, green, cyan, blue, purple + # {% hashtag %} + hashtag: + default_color: # red, orange, yellow, green, cyan, blue, purple ######## JS Plugins ######## diff --git a/layout/_partial/main/article/ai_abstract.ejs b/layout/_partial/main/article/ai_abstract.ejs index a6449c3..8b3e35b 100644 --- a/layout/_partial/main/article/ai_abstract.ejs +++ b/layout/_partial/main/article/ai_abstract.ejs @@ -1,8 +1,8 @@ <% if (page.layout === 'post' || (page.layout === 'wiki' && page.wiki && page.wiki.length > 0)) { %> <% } %> diff --git a/layout/_partial/main/article/read_next.ejs b/layout/_partial/main/article/read_next.ejs index ee91a31..79a6124 100644 --- a/layout/_partial/main/article/read_next.ejs +++ b/layout/_partial/main/article/read_next.ejs @@ -12,18 +12,23 @@ function layoutDiv() { } else if (page.layout === 'wiki' && page.wiki && page.wiki.length > 0) { let proj = theme.wiki.tree[page.wiki]; if (proj) { - const current = page.order || 0; - proj.pages.forEach((p, i) => { - if (p.order < current) { - if (prev == undefined || p.order > prev.order) { - prev = p; + let ps = proj.pages.filter(p => p.path == page.path) + if (ps.length > 0) { + const current_page_number = ps[0].page_number || 0; + proj.pages.forEach((p, i) => { + if (p.page_number < current_page_number) { + if (prev == undefined || p.page_number > prev.page_number) { + prev = p; + } + } else if (p.page_number > current_page_number) { + if (next == undefined || p.page_number < next.page_number) { + next = p; + } } - } else if (p.order > current) { - if (next == undefined || p.order < next.order) { - next = p; - } - } - }); + }); + } else { + console.error('未找到当前页'); + } } } let el = ''; diff --git a/layout/_partial/main/navbar/breadcrumb.ejs b/layout/_partial/main/navbar/breadcrumb.ejs index 2899a21..763a6c5 100644 --- a/layout/_partial/main/navbar/breadcrumb.ejs +++ b/layout/_partial/main/navbar/breadcrumb.ejs @@ -20,6 +20,7 @@ function layoutDiv() { firstCat = page.categories.data[0].name; } el += '
'; + el += '
'; el += ''; el += '
'; + el += '
'; } else if (page.layout === "wiki" && page.wiki && page.wiki.length > 0) { el += '
'; + el += '
'; el += ''; el += '
'; + + let repo = page.repo || proj.repo + if (repo) { + el += ` + + `; + } + el += '
'; } else if (page.title || page.seo_title) { - el += '
'; - el += ''; - el += '
'; + el += ` +
+ +
+ `; } return el; } diff --git a/layout/_partial/main/navbar/list_wiki.ejs b/layout/_partial/main/navbar/list_wiki.ejs index 0f6fe71..e2a0ae0 100644 --- a/layout/_partial/main/navbar/list_wiki.ejs +++ b/layout/_partial/main/navbar/list_wiki.ejs @@ -11,9 +11,10 @@ function layoutDiv() { el += ' href="' + url_for(config.wiki_dir || "/wiki/") + '">' + __("btn.all_wiki") + ''; el += ''; // 项目分类 - for (let id of Object.keys(theme.wiki.all_tags)) { - let tag = theme.wiki.all_tags[id]; - let projects = tag.items.filter(item => item.index !== false) + const { shelf, all_tags } = theme.wiki; + for (let id of Object.keys(all_tags)) { + let tag = all_tags[id]; + let projects = tag.items.filter(item => shelf.includes(item)) if (projects && projects.length > 0) { el += ' 0 && page.tagName === tag.name) { diff --git a/layout/_partial/plugins/comments/beaudar/script.ejs b/layout/_partial/plugins/comments/beaudar/script.ejs index e1c66df..53a7ceb 100644 --- a/layout/_partial/plugins/comments/beaudar/script.ejs +++ b/layout/_partial/plugins/comments/beaudar/script.ejs @@ -6,7 +6,7 @@ try { el.innerHTML = ''; } catch (error) { - console.log(error); + console.error(error); } var script = document.createElement('script'); script.src = 'https://beaudar.lipk.org/client.js'; diff --git a/layout/_partial/plugins/comments/giscus/script.ejs b/layout/_partial/plugins/comments/giscus/script.ejs index 3a9aa72..9acc12a 100644 --- a/layout/_partial/plugins/comments/giscus/script.ejs +++ b/layout/_partial/plugins/comments/giscus/script.ejs @@ -6,7 +6,7 @@ try { el.innerHTML = ''; } catch (error) { - console.log(error); + console.error(error); } var script = document.createElement('script'); script.src = 'https://giscus.app/client.js'; diff --git a/layout/_partial/plugins/comments/utterances/script.ejs b/layout/_partial/plugins/comments/utterances/script.ejs index 84409f0..a6febf3 100644 --- a/layout/_partial/plugins/comments/utterances/script.ejs +++ b/layout/_partial/plugins/comments/utterances/script.ejs @@ -6,7 +6,7 @@ try { el.innerHTML = ''; } catch (error) { - console.log(error); + console.error(error); } var script = document.createElement('script'); script.src = 'https://utteranc.es/client.js'; diff --git a/layout/_partial/widgets/related.ejs b/layout/_partial/widgets/related.ejs index 8dd6a39..c87c7f3 100644 --- a/layout/_partial/widgets/related.ejs +++ b/layout/_partial/widgets/related.ejs @@ -4,11 +4,12 @@ function layoutDiv() { return ''; } var related = []; - let proj = theme.wiki.tree[page.wiki]; + const { shelf, tree } = theme.wiki; + let proj = tree[page.wiki]; if (proj?.related?.length > 0) { - proj.related.forEach((pid, i) => { - let p = theme.wiki.tree[pid]; - if (p && p.title !== proj?.title && p.index !== false) { + proj.related.filter(pid => shelf.includes(pid)).forEach((pid, i) => { + let p = tree[pid]; + if (p && p.title !== proj?.title) { related.push(p); } }); @@ -25,8 +26,11 @@ function layoutDiv() { el += ''; el += ''; - }); + } return el; } @@ -74,9 +77,8 @@ function layoutDiv(fallback) { el += ''; } else if (proj) { // wiki 布局 - if (proj.sections && proj.sections.length > 1) { + if (proj.sections && proj.sections.length > 0 && proj.pages.length > 1) { // 多 pages proj.sections.forEach((sec, i) => { - // 多 section if (sec.title?.length > 0) { el += layoutTocHeader(sec.title); } @@ -84,8 +86,7 @@ function layoutDiv(fallback) { el += layoutDocTree(sec.pages); el += ''; }); - } else { - // 单 section + } else { // 单 page if (proj.pages.length == 1) { el += layoutTocHeader(page.toc_title); } diff --git a/layout/index.ejs b/layout/index.ejs index 17c72fc..517ed50 100755 --- a/layout/index.ejs +++ b/layout/index.ejs @@ -51,13 +51,13 @@ function layout_posts(partial) { function layout_wikis(partial) { var el = ''; var wikis = []; - const wiki_tree = theme.wiki.tree; - for (let proj_name of Object.keys(wiki_tree)) { - let proj = wiki_tree[proj_name]; + const { shelf, tree } = theme.wiki; + for (let pid of shelf) { + let proj = tree[pid]; if (proj == null) { continue; } - if (proj.index === false || proj.pages == undefined || proj.pages.length === 0) { + if (proj.pages == undefined || proj.pages.length === 0) { continue; } if (page.filter === false) { diff --git a/scripts/events/lib/doc_tree.js b/scripts/events/lib/doc_tree.js index eb5c7d1..1d5736f 100644 --- a/scripts/events/lib/doc_tree.js +++ b/scripts/events/lib/doc_tree.js @@ -35,10 +35,16 @@ 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 + '/' + } list.push(obj) } } - list = list.sort((p1, p2) => p1.sort - p2.sort) + list = list.sort((p1, p2) => p2.sort - p1.sort) for (let item of list) { wiki.tree[item.id] = item } @@ -81,10 +87,13 @@ module.exports = ctx => { } // 数据整合:每个项目的子页面 - for (let id of wiki_list) { + for (let i = 0; i < wiki_list.length; i++) { + let id = wiki_list[i]; let item = wiki.tree[id] let sub_pages = wiki_pages.filter(p => p.wiki === id) if (!sub_pages || sub_pages.length == 0) { + wiki_list.splice(i, 1) + delete wiki.tree[id] continue } @@ -92,10 +101,15 @@ module.exports = ctx => { // 未特别指定首页时,获取TOC第一页作为首页 if (item.homepage == null && item.toc != null) { for (let id of Object.keys(item.toc)) { - const path_key = item.toc[id] - let hs = sub_pages.filter(p => (p.path_key.endsWith(path_key))) - if (hs.length > 0) { - item.homepage = hs[0] + const sec = item.toc[id] + for (let key of sec) { + let hs = sub_pages.filter(p => p.path_key == item.path + key) + if (hs.length > 0) { + item.homepage = hs[0] + break + } + } + if (item.homepage != null) { break } } @@ -108,28 +122,39 @@ module.exports = ctx => { var sections = [] var others = sub_pages if (item.toc) { + // 根据配置设置顺序 for (let title of Object.keys(item.toc)) { - let key_list = item.toc[title] - var arr = [] - for (let key of key_list) { - arr = arr.concat(sub_pages.filter(p => p.path_key.endsWith(key))) - others = others.filter(p => p.path_key.endsWith(key) == false) + 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) } + sections.push(sec) + } + if (others.length > 0 && others.filter(p => p.title?.length > 0).length > 0) { sections.push({ - title: title, - pages: arr + title: '...', + pages: others.sort((p1, p2) => p1.path - p2.path) }) } - } - if (others.length > 0) { + } else { + // 自动设置顺序 sections.push({ - pages: others + pages: sub_pages.sort((p1, p2) => p1.path - p2.path) }) } + + // page number + var page_number = 0 + for (let sec of sections) { + for (let page of sec.pages) { + page.page_number = page_number++ + } + } item.sections = sections item.pages = sub_pages } - + // 全站所有的项目标签 var all_tags = {} all_tag_name.forEach((tag_name, i) => { @@ -163,6 +188,7 @@ module.exports = ctx => { wiki.all_tags = all_tags wiki.all_pages = wiki_pages + wiki.shelf = ctx.locals.get('data').wiki ctx.theme.config.wiki = wiki } diff --git a/scripts/tags/index.js b/scripts/tags/index.js index 14242a0..560839e 100644 --- a/scripts/tags/index.js +++ b/scripts/tags/index.js @@ -32,6 +32,6 @@ hexo.extend.tag.register('navbar', require('./lib/navbar')(hexo)) hexo.extend.tag.register('note', require('./lib/note')(hexo)) hexo.extend.tag.register('poetry', require('./lib/poetry')(hexo), true) hexo.extend.tag.register('quot', require('./lib/quot')(hexo)) -hexo.extend.tag.register('tag', require('./lib/tag')(hexo)) +hexo.extend.tag.register('hashtag', require('./lib/hashtag')(hexo)) // others diff --git a/scripts/tags/lib/hashtag.js b/scripts/tags/lib/hashtag.js new file mode 100644 index 0000000..bd4ba58 --- /dev/null +++ b/scripts/tags/lib/hashtag.js @@ -0,0 +1,36 @@ +/** + * hashtag.js v1.0 | https://github.com/xaoxuu/hexo-theme-stellar/ + * 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来) + * + * {% hashtag text href [color:color] %} + * + */ + +'use strict' + +const tag_colors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple'] +var tag_index = 0 + +module.exports = ctx => function(args) { + args = ctx.args.map(args, ['color'], ['text', 'href']) + if (args.color == null) { + const default_color = ctx.theme.config.tag_plugins.hashtag?.default_color + if (default_color) { + args.color = default_color + } else { + args.color = tag_colors[tag_index] + tag_index += 1 + if (tag_index >= tag_colors.length) { + tag_index = 0 + } + } + } + var el = '' + el += '' + el += '' + return el +} diff --git a/scripts/tags/lib/tag.js b/scripts/tags/lib/tag.js deleted file mode 100644 index b5ebc96..0000000 --- a/scripts/tags/lib/tag.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * tag.js v1.0 | https://github.com/xaoxuu/hexo-theme-stellar/ - * 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来) - * - * {% tag text href [color:color] %} - * - */ - -'use strict' - -const tag_colors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple'] -var tag_index = 0 - -module.exports = ctx => function(args) { - args = ctx.args.map(args, ['color'], ['text', 'href']) - if (args.color == null) { - const default_color = ctx.theme.config.tag_plugins.tag?.default_color - if (default_color) { - args.color = default_color - } else { - args.color = tag_colors[tag_index] - tag_index += 1 - if (tag_index >= tag_colors.length) { - tag_index = 0 - } - } - } - var el = '' - el += ' { }) async function copyCode(currentCode) { - // console.log(currentCode) - // console.log('复制代码') if (navigator.clipboard) { try { await navigator.clipboard.writeText(currentCode) - } catch (error) { + } catch (error) { // 未获得用户许可 codeCopyBtn.innerText = '未获得用户许可' codeCopyBtn.classList.add('warning') diff --git a/source/js/plugins/linkcard.js b/source/js/plugins/linkcard.js index b7be0de..6f8e99e 100644 --- a/source/js/plugins/linkcard.js +++ b/source/js/plugins/linkcard.js @@ -42,7 +42,7 @@ function setCardLink(nodes) { }).then(function(data) { renderer(el, data); }).catch(function(error) { - console.log(error); + console.error(error); }); }) }