前言

通过微博热搜的侧边栏学习,将今日新闻也一并接入博客!

效果图

效果

配置方法

创建widget.yml文件

如果没有这个目录需要创建,source/_data/widget.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
# 这里填top表示所有地方都显示,bottom表示只在非文章页面显示,如主页等等
bottom:
- class_name:
id_name: weibo
name: 微博热搜
icon: fa-brands fa-weibo
html: <link rel="stylesheet" href="/css/weibo.css"><div id="weiboContent"></div><script src="/js/weibo.js"></script>

- class_name:
id_name: news
name: 今日新闻
icon: fa-solid fa-newspaper
html: <link rel="stylesheet" href="/css/news.css"><div id="newsContent"></div><script src="/js/news.js"></script>

创建news.css

创建themes/butterfly/source/css/news.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 今日新闻卡片样式 - 适配Hexo侧边栏 */
#news.card-widget {
background: linear-gradient(
to bottom,
rgba(0, 132, 255, 0.788) 0%,
rgba(0,0,0,0.5) 70%
) !important;
border-radius: 10px;
color: white;
border-width: 10px;
padding-bottom: 35px;
}

#newsContent {
width: 100%;
height: 270px;
font-size: 90%;
overflow-y: auto;
-ms-overflow-style: none;
scrollbar-width: none
}

/* 自定义滚动条 */
#newsContent::-webkit-scrollbar {
width: 4px;
}
#newsContent::-webkit-scrollbar-track {
background: rgba(0, 20, 40, 0.3);
border-radius: 2px;
}
#newsContent::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.3);
border-radius: 2px;
}

/* 空数据提示 */
.empty-tip {
text-align: center;
padding: 30px 0;
color: rgba(255, 255, 255, 0.7);
font-size: 14px;
}

/* 列表样式 */
.news-list {
list-style: none;
padding: 0;
margin: 0;
}

/* 列表项 */
.news-item {
display: flex;
align-items: flex-start;
padding: 8px 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
gap: 8px;
}

/* 排名数字 */
.news-rank {
display: inline-flex;
align-items: center;
justify-content: center;
width: 15px;
height: 20px;
border-radius: 3px;
background: rgba(255, 255, 255, 0.3);
color: white;
font-size: 12px;
flex-shrink: 0;
}
.news-rank-top1 { background: #f43530; }
.news-rank-top2 { background: #ff7a45; }
.news-rank-top3 { background: #ffc53d; }

/* 新闻标题 */
.news-title {
flex: 1;
font-size: 13px;
color: rgba(255, 255, 255, 0.9);
text-decoration: none;
line-height: 1.4;
word-break: break-all;
}
.news-title:hover {
color: #fff;
text-decoration: underline;
}

/* 来源和时间 */
/* .news-meta {
display: block;
font-size: 11px;
color: rgba(255, 255, 255, 0.6);
margin-top: 3px;
width: 60px;
} */

/* 适配移动端 */
@media (max-width: 768px) {
#newsContent {
max-height: 400px;
}
.news-list-wrapper {
padding: 10px;
}
.news-item {
padding: 6px 0;
}
.news-title {
font-size: 12px;
}
}

/* 无数据兜底 */
#newsContent:empty::after {
content: "暂无新闻数据";
display: block;
text-align: center;
padding: 20px 0;
color: rgba(255, 255, 255, 0.7);
font-size: 14px;
}

创建news.js文件

创建themes/butterfly/source/js/news.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 今日新闻卡片逻辑 - 移除标题外框,仅保留列表
(function() {
// 配置项
const API_URL = 'https://tools.mgtv100.com/external/v1/toutiao/index';
const CACHE_DURATION = 10 * 60 * 1000; // 10分钟缓存
const CACHE_KEY = 'news_list_cache'; // 缓存键名

// 核心渲染函数(移除标题外框)
function news() {
let html = '<div id="newsContent">'; // 完全去掉标题外框,直接渲染列表
let data = JSON.parse(localStorage.getItem(CACHE_KEY));
let nowTime = Date.now();
let ls;

// 缓存逻辑
if (data == null || nowTime - data.timestamp > CACHE_DURATION) {
getData();
return;
} else {
ls = data.data;
}

// 空数据处理
if (!ls || ls.length === 0) {
html += '<div class="empty-tip">暂无新闻数据~</div>';
} else {
// 直接渲染列表,无任何外框/标题
for (let i = 0; i < ls.length; i++) {
let item = ls[i];
let rankClass = i < 3 ? ` news-rank-top${i+1}` : '';
html += '<li class="news-item">';
html += `<div class="news-rank${rankClass}">${i+1}</div>`;
html += `<span class="news-title"><a title="${item.title || '无标题'}" href="${item.url || '#'}" target="_blank" rel="external nofollow noreferrer">${item.title || '无标题'}</a></span>`;
// html += `<div class="news-meta">${item.date || '未知时间'}</div>`;
html += '</li>';
}
html += '</div>';
}

// 直接渲染到核心容器,无任何外层包裹
document.getElementById('newsContent').innerHTML = html;
}

// 获取数据并缓存
function getData() {
fetch(API_URL, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) throw new Error(`状态码: ${response.status}`);
return response.json();
})
.then(data => {
const hotList = data?.data?.result?.data || [];
const cacheData = {
timestamp: Date.now(),
data: hotList
};
localStorage.setItem(CACHE_KEY, JSON.stringify(cacheData));
news(); // 重新渲染
})
.catch(error => {
console.error('获取新闻数据失败:', error);
document.getElementById('newsContent').innerHTML = '<div class="empty-tip">暂无新闻数据~</div>';
});
}

// 页面加载后执行
try {
if (document.getElementById('news').clientWidth) news();
} catch (error) {
news(); // 容错,直接执行
}
})();

加入卡片

微博加过就不需要了

1
2
3
4
5
aside:
..........
card_webinfo:
enable: true
post_count: true

调整侧边栏顺序

调整sort_order大小即可,数字越大越前,要把公告后的卡片都加上数字(有的资料说是越小越前,但是我测试不是,反正能用就行)