From c928c4124ec00b6d0a8cc7e40dd918220bfa9445 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 20 Oct 2022 21:34:54 +0800 Subject: [PATCH] widgets --- _config.yml | 44 +-------------------- _data/widgets.yml | 46 ++++++++++++++++++++++ layout/_partial/sidebar/index.ejs | 12 +++--- layout/_partial/sidebar/widgets/ghuser.ejs | 6 --- layout/_partial/sidebar/widgets/toc.ejs | 2 +- scripts/events/lib/config.js | 36 ++++++++++++----- 6 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 _data/widgets.yml diff --git a/_config.yml b/_config.yml index cac1eca..f3a7f0f 100755 --- a/_config.yml +++ b/_config.yml @@ -24,51 +24,11 @@ sidebar: # friends: '[友链](/friends/)' # about: '[关于](/about/)' # Sidebar widgets - widget_layout: - index: [welcome, recent, ghuser_mini] # for home/wiki/categories/tags/archives/404 pages + widgets: + index: [welcome, recent] # for home/wiki/categories/tags/archives/404 pages page: [welcome, toc] # for pages using 'layout:page' post: [toc, repo_info] # for pages using 'layout:post' wiki: [toc, repo_info, wiki_more] # for pages using 'layout:wiki' - widgets: - # Recent update - recent: - layout: recent - rss: # /atom.xml # npm i hexo-generator-feed - limit: 5 # Count of posts - # TOC (valid only in layout:post/wiki) - toc: - layout: toc - list_number: false - min_depth: 2 - max_depth: 5 - fallback: recent # Use a backup widget when toc does not exist. - # github user info - ghuser: - layout: ghuser - api: https://api.github.com - username: github # your github login username - avatar: true - ghuser_mini: - layout: ghuser - header: # GitHub Info - api: https://api.github.com - username: github # your github login username - avatar: false - # welcome - welcome: - layout: markdown - title: Stellar 入门指南 - content: | # support markdown - 欢迎使用 [Stellar](https://github.com/xaoxuu/hexo-theme-stellar/) 主题,下面是您的入门指南,祝您使用愉快! -
- **第一步** - 创建 `blog/_config.stellar.yml` 文件,在此文件中填写需要自定义的主题配置。 -
- **第二步** - 创建 `blog/source/_data/widgets.yml` 文件,此文件中填写需要自定义的侧边栏组件,例如 `welcome` 组件。 -
- 如果有任何疑问,请先查阅[文档](https://xaoxuu.com/wiki/stellar/),如果文档中没有提供,请提 [issue](https://github.com/xaoxuu/hexo-theme-stellar/issues/) 向开发中询问。 - ######## Main ######## diff --git a/_data/widgets.yml b/_data/widgets.yml new file mode 100644 index 0000000..3b95466 --- /dev/null +++ b/_data/widgets.yml @@ -0,0 +1,46 @@ +# 您可以在这里创建属于自己的侧边栏组件库,在需要的位置放置自己的组件 +# layout即组件布局,支持自定义的有: +# - markdown: 渲染 md 文本 +# + +# ---- 内置组件,请勿覆盖 ---- +repo_info: + layout: repo_info +wiki_more: + layout: wiki_more + +# ---- 默认组件,可以覆盖自定义配置 ---- +# 将其覆盖设置为空,可删除一个默认组件,例如 welcome: +# Recent update +recent: + layout: recent + rss: # /atom.xml # npm i hexo-generator-feed + limit: 5 # Count of posts +# TOC (valid only in layout:post/wiki) +toc: + layout: toc + list_number: false + min_depth: 2 + max_depth: 5 + fallback: recent # Use a backup widget when toc does not exist. +# github user info +ghuser: + layout: ghuser + api: https://api.github.com + username: github # your github login username + avatar: true # true / false + + +welcome: + layout: markdown + title: Stellar 入门指南 + content: | # support markdown + 欢迎使用 [Stellar](https://github.com/xaoxuu/hexo-theme-stellar/) 主题,下面是您的入门指南,祝您使用愉快! +
+ **第一步** + 创建 `blog/_config.stellar.yml` 文件,在此文件中填写需要自定义的主题配置。 +
+ **第二步** + 创建 `blog/source/_data/widgets.yml` 文件,此文件中填写需要自定义的侧边栏组件,例如 `welcome` 组件。 +
+ 如果有任何疑问,请先查阅[文档](https://xaoxuu.com/wiki/stellar/),如果文档中没有提供,请提 [issue](https://github.com/xaoxuu/hexo-theme-stellar/issues/) 向开发中询问。 \ No newline at end of file diff --git a/layout/_partial/sidebar/index.ejs b/layout/_partial/sidebar/index.ejs index e016719..d9777aa 100755 --- a/layout/_partial/sidebar/index.ejs +++ b/layout/_partial/sidebar/index.ejs @@ -2,18 +2,18 @@ // 默认组件 if (page.sidebar == undefined) { if (page.layout == 'post' && page.content) { - page.sidebar = theme.sidebar.widget_layout.post; + page.sidebar = theme.sidebar.widgets.post; } else if (page.layout == 'wiki' && page.content && page.wiki) { let proj = theme.wiki.projects[page.wiki]; if (proj.sidebar) { page.sidebar = proj.sidebar; } else { - page.sidebar = theme.sidebar.widget_layout.wiki; + page.sidebar = theme.sidebar.widgets.wiki; } } else if (is_home() || ['categories', 'tags', 'archives', 'index', '404', undefined].includes(page.layout)) { - page.sidebar = theme.sidebar.widget_layout.index; + page.sidebar = theme.sidebar.widgets.index; } else if (page.layout == 'page') { - page.sidebar = theme.sidebar.widget_layout.page; + page.sidebar = theme.sidebar.widgets.page; } else { page.sidebar = []; } @@ -23,8 +23,8 @@ function layoutWidgets() { el += '
'; if (page.sidebar) { page.sidebar.forEach((w, i) => { - if (w in theme.sidebar.widgets) { - let widget = theme.sidebar.widgets[w]; + if (w in theme.data.widgets) { + let widget = theme.data.widgets[w]; if (widget && widget.layout) { el += partial('widgets/' + widget.layout, {item: widget}); } diff --git a/layout/_partial/sidebar/widgets/ghuser.ejs b/layout/_partial/sidebar/widgets/ghuser.ejs index 202e845..ab524d3 100644 --- a/layout/_partial/sidebar/widgets/ghuser.ejs +++ b/layout/_partial/sidebar/widgets/ghuser.ejs @@ -23,12 +23,6 @@ function layoutDiv() { } el += '
'; - // header - if (item.header && item.header.length > 0) { - el += '
'; - el += '' + item.header + ''; - el += '
'; - } // body el += '
'; if (item.avatar) { diff --git a/layout/_partial/sidebar/widgets/toc.ejs b/layout/_partial/sidebar/widgets/toc.ejs index 4a023cb..641089e 100644 --- a/layout/_partial/sidebar/widgets/toc.ejs +++ b/layout/_partial/sidebar/widgets/toc.ejs @@ -92,7 +92,7 @@ function layoutDiv(fallback) { } el += '
'; } else if (item.fallback) { - el += partial(item.fallback, {item: theme.sidebar.widgets[item.fallback]}); + el += partial(item.fallback, {item: theme.data.widgets[item.fallback]}); } return el; } diff --git a/scripts/events/lib/config.js b/scripts/events/lib/config.js index 3451215..d85f9d8 100644 --- a/scripts/events/lib/config.js +++ b/scripts/events/lib/config.js @@ -1,10 +1,14 @@ /** * 部分代码借鉴自 NexT: * https://github.com/next-theme/hexo-theme-next/blob/master/scripts/events/lib/config.js + * Volantis: + * https://github.com/volantis-x/hexo-theme-volantis/blob/master/scripts/events/lib/cdn.js */ 'use strict'; +const path = require('path'); + module.exports = hexo => { const { cache, language_switcher } = hexo.theme.config; @@ -25,23 +29,35 @@ module.exports = hexo => { // merge data const data = hexo.locals.get('data'); + // merge widgets + var widgets = hexo.render.renderSync({ path: path.join(hexo.theme_dir, '_data/widgets.yml'), engine: 'yaml' }); if (data.widgets) { - for (let id of Object.keys(data.widgets)) { - hexo.theme.config.sidebar.widgets[id] = data.widgets[id]; + for (let i of Object.keys(data.widgets)) { + let widget = data.widgets[i]; + if (widget == null || widget.length == 0) { + // delete + delete widgets[i]; + } else { + // create + if (widgets[i] == null) { + widgets[i] = widget; + } else { + // merge + for (let j of Object.keys(widget)) { + widgets[i][j] = widget[j]; + } + } + } } } + if (hexo.theme.config.data == undefined) { + hexo.theme.config.data = {}; + } + hexo.theme.config.data['widgets'] = widgets; // default menu if (hexo.theme.config.sidebar.menu == undefined) { hexo.theme.config.sidebar.menu = []; } - // default widgets - if (hexo.theme.config.sidebar.widgets.repo_info == undefined) { - hexo.theme.config.sidebar.widgets.repo_info = {layout: 'repo_info'}; - } - if (hexo.theme.config.sidebar.widgets.wiki_more == undefined) { - hexo.theme.config.sidebar.widgets.wiki_more = {layout: 'wiki_more'}; - } - };