更新日志

2025

06-20

自动更新节日时间,无需手动修改

01-03

JS路径贴错,已更正

前言

美化之前

美化的时候需要修改网站的源文件、添加样式、js之类的,需要一点基础,可以参考

千万别忘了

在添加完js、css后,一定要记得在_config.butterfly.ymlinject里引用

效果

参考链接

修改方案

1. 添加card_calendar.pugcard_schedule.pug

在目录[blogRoot]\themes\butterfly\layout\includes\widget下添加

card_calendar.pug

1
2
3
4
5
6
7
8
.card-widget#card-calendar
#calendar-area-left
#calendar-week
#calendar-date(style="font-size: 48px;")
#calendar-solar
#calendar-lunar
#calendar-area-right
#calendar-main

card_schedule.pug

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
.card-widget#card-schedule
#schedule-area-left
#schedule-title 距离节日
#schedule-days 0
#schedule-date 0000-00-00
#schedule-area-right
.schedule-r0
.schedule-d0 本年
.schedule-d1
span#p_span_year.aside-span1
span.aside-span2 还剩
a 0
progress#pBar_year(max="365")
.schedule-r1
.schedule-d0 本月
.schedule-d1
span#p_span_month.aside-span1
span.aside-span2 还剩
a 0
progress#pBar_month(max="30")
.schedule-r2
.schedule-d0 本周
.schedule-d1
span#p_span_week.aside-span1
span.aside-span2 还剩
a 0
progress#pBar_week(max="7")

2. 修改index.pug

修改[blogRoot]\themes\butterfly\layout\includes\widget\index.pug

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
#aside-content.aside-content
//- post
if is_post()
- const tocStyle = page.toc_style_simple
- const tocStyleVal = tocStyle === true || tocStyle === false ? tocStyle : theme.toc.style_simple
if showToc && tocStyleVal
.sticky_layout
include ./card_post_toc.pug
else
!=partial('includes/widget/card_author', {}, {cache: true})
!=partial('includes/widget/card_announcement', {}, {cache: true})
!=partial('includes/widget/card_top_self', {}, {cache: true})
.sticky_layout
if showToc
include ./card_post_toc.pug
!=partial('includes/widget/card_ad', {}, {cache: true})
else
//- page
!=partial('includes/widget/card_author', {}, {cache: true})
!=partial('includes/widget/card_announcement', {}, {cache: true})
!=partial('includes/widget/card_top_self', {}, {cache: true})

.sticky_layout
if showToc
include ./card_post_toc.pug
+ !=partial('includes/widget/card_calendar', {}, {cache: true})
+ !=partial('includes/widget/card_schedule', {}, {cache: true})
!=partial('includes/widget/card_recent_post', {}, {cache: true})
!=partial('includes/widget/card_ad', {}, {cache: true})
!=partial('includes/widget/card_newest_comment', {}, {cache: true})
!=partial('includes/widget/card_categories', {}, {cache: true})
!=partial('includes/widget/card_tags', {}, {cache: true})
!=partial('includes/widget/card_archives', {}, {cache: true})
!=partial('includes/widget/card_webinfo', {}, {cache: true})
!=partial('includes/widget/card_bottom_self', {}, {cache: true})

3. 添加 calendar.js

自定义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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
document.addEventListener("DOMContentLoaded", initializeCard);
document.addEventListener("pjax:complete", initializeCard);

let year, month, week, date, dates, weekStr, monthStr, asideTime;

function initializeCard() {
cardTimes();
cardRefreshTimes();
}

function getNextFestivalDate() {
const now = new Date();
const currentYear = now.getFullYear();

// 阳历节日
const solarFestivals = [
{ name: "元旦", month: 1, day: 1 },
{ name: "清明节", month: 4, day: 4 },
{ name: "国庆节", month: 10, day: 1 },
];

// 农历节日
const lunarFestivals = [
{ name: "春节", month: 1, day: 1 },
{ name: "端午节", month: 5, day: 5 },
{ name: "中秋节", month: 8, day: 15 },
];

const upcoming = [];

// 处理阳历节日
solarFestivals.forEach(({ name, month, day }) => {
const festDate = new Date(currentYear, month - 1, day);
if (festDate >= now) upcoming.push({ name, date: festDate });
});

// 处理农历节日,转换成阳历日期
lunarFestivals.forEach(({ name, month, day }) => {
const solarDate = chineseLunar.lunarToSolar(currentYear, month, day);
const festDate = new Date(
solarDate.getFullYear(),
solarDate.getMonth(),
solarDate.getDate()
);
if (festDate >= now) upcoming.push({ name, date: festDate });
});

// 按日期排序,返回最近的节日
return upcoming.sort((a, b) => a.date - b.date)[0];
}

function cardRefreshTimes() {
const e = document.getElementById("card-schedule");
if (!e) return;

const now = new Date();
const asideDay = (now - asideTime) / 86400000;

e.querySelector("#pBar_year").value = asideDay;
e.querySelector("#p_span_year").innerText = ((asideDay / 365) * 100).toFixed(1) + "%";
e.querySelector(".schedule-r0 .aside-span2 a").innerText = (365 - asideDay).toFixed(0);

e.querySelector("#pBar_month").value = date;
e.querySelector("#pBar_month").max = dates;
e.querySelector("#p_span_month").innerText = ((date / dates) * 100).toFixed(1) + "%";
e.querySelector(".schedule-r1 .aside-span2 a").innerText = (dates - date);

const wd = week === 0 ? 7 : week;
e.querySelector("#pBar_week").value = wd;
e.querySelector("#pBar_week").max = 7;
e.querySelector("#p_span_week").innerText = ((wd / 7) * 100).toFixed(1) + "%";
e.querySelector(".schedule-r2 .aside-span2 a").innerText = (7 - wd);
}

function cardTimes() {
const getShanghaiMidnight = (date = new Date()) => {
const shanghaiStr = date.toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" });
return new Date(shanghaiStr.split(" ")[0] + " 00:00:00");
};

const shanghaiNow = getShanghaiMidnight();
year = shanghaiNow.getFullYear();
month = shanghaiNow.getMonth();
week = shanghaiNow.getDay();
date = shanghaiNow.getDate();

monthStr = `${month + 1}月`;
weekStr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][week];
dates = [
31,
(year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 29 : 28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
][month];

asideTime = new Date(`${year}/01/01 00:00:00`);

// 1月1日为第1天
const asideDayNum = Math.ceil((shanghaiNow - asideTime) / 86400000) + 1;

const weekNum = week - asideDayNum % 7 >= 0 ? Math.ceil(asideDayNum / 7) : Math.ceil(asideDayNum / 7) + 1;

const nextFestival = getNextFestivalDate();
const diffDays = Math.ceil((nextFestival.date - shanghaiNow) / 86400000);
const festDateStr = formatDateYMD(nextFestival.date);

const schedule = document.getElementById("card-schedule");
if (schedule) {
schedule.querySelector("#schedule-title").innerText = `距离${nextFestival.name}`;
schedule.querySelector("#schedule-date").innerText = festDateStr;
schedule.querySelector("#schedule-days").innerText = diffDays;
}

const calendar = document.getElementById("card-calendar");
if (calendar) {
calendar.querySelector("#calendar-week").innerHTML = `${year.toString().slice(-2)}${monthStr} ${weekStr}`;
calendar.querySelector("#calendar-date").innerHTML = String(date).padStart(2, "0");
calendar.querySelector("#calendar-solar").innerHTML = `第${weekNum}周 第${asideDayNum}天`;

const lunarDate = chineseLunar.solarToLunar(shanghaiNow);
const ganzhiYear = chineseLunar.format(lunarDate, "T");
const animalYear = chineseLunar.format(lunarDate, "A");
const lunarMonthStr = chineseLunar.format(lunarDate, "M");
const lunarDayStr = chineseLunar.format(lunarDate, "d");

calendar.querySelector(
"#calendar-lunar"
).innerHTML = `${ganzhiYear}${animalYear}${lunarMonthStr}${lunarDayStr}`;
}

renderCalendarGrid();
}

function formatDateYMD(date) {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}-${m}-${d}`;
}

function renderCalendarGrid() {
const container = document.querySelector("#calendar-main");
if (!container) return;

container.innerHTML = "";

const firstDayOfMonth = new Date(year, month, 1).getDay();
const totalCells = firstDayOfMonth + dates;
const totalRows = Math.ceil(totalCells / 7);

let currentDay = 1;

for (let i = 0; i < totalRows; i++) {
const rowDiv = document.createElement("div");
rowDiv.className = `calendar-r${i}`;

for (let j = 0; j < 7; j++) {
const cellIndex = i * 7 + j;
const cellDiv = document.createElement("div");
cellDiv.className = `calendar-d${j}`;
const a = document.createElement("a");

if (cellIndex >= firstDayOfMonth && currentDay <= dates) {
a.textContent = currentDay;
if (currentDay === date) a.classList.add("now");
currentDay++;
} else {
a.innerHTML = "&nbsp;";
}

cellDiv.appendChild(a);
rowDiv.appendChild(cellDiv);
}

container.appendChild(rowDiv);
}
}

4. 添加calendar.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* 日历卡片样式 */
.card-times a, .card-times div {
color: var(--june-fontcolor);
}

#card-calendar {
display: flex;
padding: 18px 10px !important;
}

#calendar-area-left {
width: 45%;
}

#calendar-area-right {
width: 55%;
}

#calendar-area-left, #calendar-area-right {
height: 100%;
padding: 4px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

#calendar-main {
width: 100%;
margin-top: 10px;
}

#calendar-week {
height: 1.2rem;
font-size: 14px;
letter-spacing: 1px;
font-weight: 700;
align-items: center;
display: flex;
}

#calendar-date {
height: 3.5rem;
line-height: 1.3;
font-size: 64px;
letter-spacing: 3px;
color: var(--june-lighttext);
font-weight: 700;
align-items: center;
display: flex;
position: absolute;
top: calc(50% - 2.1rem);
}

#calendar-lunar, #calendar-solar {
height: 1rem;
font-size: 12px;
align-items: center;
display: flex;
position: absolute;
}

#calendar-solar {
bottom: 2.1rem;
}

#calendar-lunar {
bottom: 1rem;
color: var(--june-secondtext);
}

#calendar-main a {
height: 1rem;
width: 1rem;
border-radius: 50%;
font-size: 13px;
line-height: 12px;
display: flex;
justify-content: center;
align-items: center;
}

#calendar-main a.now {
background: var(--june-lighttext);
color: var(--june-card-bg);
}

#calendar-main .calendar-rh a {
color: var(--june-secondtext);
}

.calendar-r0, .calendar-r1, .calendar-r2, .calendar-r3, .calendar-r4, .calendar-r5, .calendar-rh {
height: 1.2rem;
display: flex;
}

.calendar-d0, .calendar-d1, .calendar-d2, .calendar-d3, .calendar-d4, .calendar-d5, .calendar-d6 {
width: calc(100% / 7);
display: flex;
justify-content: center;
align-items: center;
}

#card-schedule {
display: flex;
padding: 18px 10px !important;
}

#schedule-area-right a,
#calendar-area-right a {
pointer-events: none;
color: var(--text-highlight-color);
}


#schedule-area-left, #schedule-area-right {
height: 100px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

#schedule-area-left {
width: 30%;
}

#schedule-area-right {
width: 70%;
padding: 0 5px;
}

.schedule-r0, .schedule-r1, .schedule-r2 {
height: 2rem;
width: 100%;
align-items: center;
display: flex;
}

.schedule-d0 {
width: 30px;
margin-right: 5px;
text-align: center;
font-size: 12px;
}

.schedule-d1 {
width: calc(100% - 35px);
height: 1.5rem;
align-items: center;
display: flex;
}

progress::-webkit-progress-bar {
background: var(--june-secondbg);
border-radius: 5px;
overflow: hidden;
}

progress::-webkit-progress-value {
background: var(--june-lighttext);
border-radius: 5px;
}

.aside-span1, .aside-span2 {
height: 1rem;
font-size: 12px;
z-index: 1;
display: flex;
align-items: center;
position: absolute;
}

.aside-span1 {
margin-left: 5px;
}

.aside-span2 {
right: 20px;
color: var(--june-secondtext);
}

.aside-span2 a {
margin: 0 3px;
}

#pBar_month, #pBar_week, #pBar_year {
width: 100%;
border-radius: 5px;
height: 100%;
}

#schedule-date, #schedule-days, #schedule-title {
display: flex;
align-items: center;
}

#schedule-title {
height: 25px;
line-height: 1;
font-size: 14px;
font-weight: 700;
}

#schedule-days {
height: 55px;
line-height: 1;
font-size: 35px;
font-weight: 900;
color: var(--june-lighttext);
}

#schedule-date {
height: 20px;
line-height: 1;
font-size: 12px;
color: var(--june-secondtext);
}

5. 引入inject

将你的CSSJS文件引入,除此之外还要引入下面这个

```yml
inject:
head:

- <script src="https://cdnjs.cloudflare.com/ajax/libs/lunar-javascript/1.7.3/lunar.min.js"></script>


bottom:

- <script src="https://open.lightxi.com/unpkg/chinese-lunar@0.1.4/lib/chinese-lunar.js"></script>