This commit is contained in:
xaoxuu 2022-10-20 21:34:54 +08:00
parent f1e5b3ac46
commit c928c4124e
6 changed files with 81 additions and 65 deletions

View File

@ -24,51 +24,11 @@ sidebar:
# friends: '[友链](/friends/)' # friends: '[友链](/friends/)'
# about: '[关于](/about/)' # about: '[关于](/about/)'
# Sidebar widgets # Sidebar widgets
widget_layout: widgets:
index: [welcome, recent, ghuser_mini] # for home/wiki/categories/tags/archives/404 pages index: [welcome, recent] # for home/wiki/categories/tags/archives/404 pages
page: [welcome, toc] # for pages using 'layout:page' page: [welcome, toc] # for pages using 'layout:page'
post: [toc, repo_info] # for pages using 'layout:post' post: [toc, repo_info] # for pages using 'layout:post'
wiki: [toc, repo_info, wiki_more] # for pages using 'layout:wiki' 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/) 主题,下面是您的入门指南,祝您使用愉快!
<br>
**第一步**
创建 `blog/_config.stellar.yml` 文件,在此文件中填写需要自定义的主题配置。
<br>
**第二步**
创建 `blog/source/_data/widgets.yml` 文件,此文件中填写需要自定义的侧边栏组件,例如 `welcome` 组件。
<br>
如果有任何疑问,请先查阅[文档](https://xaoxuu.com/wiki/stellar/),如果文档中没有提供,请提 [issue](https://github.com/xaoxuu/hexo-theme-stellar/issues/) 向开发中询问。
######## Main ######## ######## Main ########

46
_data/widgets.yml Normal file
View File

@ -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/) 主题,下面是您的入门指南,祝您使用愉快!
<br>
**第一步**
创建 `blog/_config.stellar.yml` 文件,在此文件中填写需要自定义的主题配置。
<br>
**第二步**
创建 `blog/source/_data/widgets.yml` 文件,此文件中填写需要自定义的侧边栏组件,例如 `welcome` 组件。
<br>
如果有任何疑问,请先查阅[文档](https://xaoxuu.com/wiki/stellar/),如果文档中没有提供,请提 [issue](https://github.com/xaoxuu/hexo-theme-stellar/issues/) 向开发中询问。

View File

@ -2,18 +2,18 @@
// 默认组件 // 默认组件
if (page.sidebar == undefined) { if (page.sidebar == undefined) {
if (page.layout == 'post' && page.content) { 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) { } else if (page.layout == 'wiki' && page.content && page.wiki) {
let proj = theme.wiki.projects[page.wiki]; let proj = theme.wiki.projects[page.wiki];
if (proj.sidebar) { if (proj.sidebar) {
page.sidebar = proj.sidebar; page.sidebar = proj.sidebar;
} else { } 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)) { } 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') { } else if (page.layout == 'page') {
page.sidebar = theme.sidebar.widget_layout.page; page.sidebar = theme.sidebar.widgets.page;
} else { } else {
page.sidebar = []; page.sidebar = [];
} }
@ -23,8 +23,8 @@ function layoutWidgets() {
el += '<div class="widgets">'; el += '<div class="widgets">';
if (page.sidebar) { if (page.sidebar) {
page.sidebar.forEach((w, i) => { page.sidebar.forEach((w, i) => {
if (w in theme.sidebar.widgets) { if (w in theme.data.widgets) {
let widget = theme.sidebar.widgets[w]; let widget = theme.data.widgets[w];
if (widget && widget.layout) { if (widget && widget.layout) {
el += partial('widgets/' + widget.layout, {item: widget}); el += partial('widgets/' + widget.layout, {item: widget});
} }

View File

@ -23,12 +23,6 @@ function layoutDiv() {
} }
el += '<div class="widget-wrap" id="github-user">'; el += '<div class="widget-wrap" id="github-user">';
// header
if (item.header && item.header.length > 0) {
el += '<div class="widget-header cap dis-select">';
el += '<span class="name">' + item.header + '</span>';
el += '</div>';
}
// body // body
el += '<div class="widget-body stellar-ghinfo-api" api="' + item.api + '/users/' + item.username + '">'; el += '<div class="widget-body stellar-ghinfo-api" api="' + item.api + '/users/' + item.username + '">';
if (item.avatar) { if (item.avatar) {

View File

@ -92,7 +92,7 @@ function layoutDiv(fallback) {
} }
el += '</div>'; el += '</div>';
} else if (item.fallback) { } 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; return el;
} }

View File

@ -1,10 +1,14 @@
/** /**
* 部分代码借鉴自 NexT: * 部分代码借鉴自 NexT:
* https://github.com/next-theme/hexo-theme-next/blob/master/scripts/events/lib/config.js * 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'; 'use strict';
const path = require('path');
module.exports = hexo => { module.exports = hexo => {
const { cache, language_switcher } = hexo.theme.config; const { cache, language_switcher } = hexo.theme.config;
@ -25,23 +29,35 @@ module.exports = hexo => {
// merge data // merge data
const data = hexo.locals.get('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) { if (data.widgets) {
for (let id of Object.keys(data.widgets)) { for (let i of Object.keys(data.widgets)) {
hexo.theme.config.sidebar.widgets[id] = data.widgets[id]; 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 // default menu
if (hexo.theme.config.sidebar.menu == undefined) { if (hexo.theme.config.sidebar.menu == undefined) {
hexo.theme.config.sidebar.menu = []; 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'};
}
}; };