hexo-theme-stellar 1.39.1 → 1.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/skills/stellar-theme-dev/SKILL.md +2 -2
- package/CHANGELOG.md +70 -0
- package/_config.yml +38 -11
- package/_data/icons.yml +250 -37
- package/languages/en.yml +8 -0
- package/languages/zh-CN.yml +8 -0
- package/languages/zh-TW.yml +8 -0
- package/layout/_partial/comments/artalk/script.ejs +26 -1
- package/layout/_partial/comments/beaudar/script.ejs +1 -0
- package/layout/_partial/comments/twikoo/script.ejs +1 -0
- package/layout/_partial/comments/utterances/script.ejs +1 -0
- package/layout/_partial/comments/waline/script.ejs +1 -0
- package/layout/_partial/head.ejs +54 -9
- package/layout/_partial/main/article/article_tags.ejs +16 -0
- package/layout/_partial/main/navbar/article_banner.ejs +17 -0
- package/layout/_partial/main/navbar/dateinfo.ejs +1 -1
- package/layout/_partial/main/notebook/note_card.ejs +1 -1
- package/layout/_partial/main/notebook/paginator.ejs +2 -2
- package/layout/_partial/main/pin_slider.ejs +2 -2
- package/layout/_partial/main/post_list/paginator.ejs +2 -2
- package/layout/_partial/main/post_list/post_card.ejs +11 -0
- package/layout/_partial/menubtn.ejs +2 -2
- package/layout/_partial/scripts/defines.ejs +26 -2
- package/layout/_partial/scripts.ejs +30 -7
- package/layout/_partial/sidebar/logo.ejs +1 -1
- package/layout/_partial/sidebar/menu.ejs +1 -1
- package/layout/_partial/sidebar/search.ejs +1 -1
- package/layout/_partial/widgets/author.ejs +1 -1
- package/layout/_partial/widgets/components/link.ejs +2 -2
- package/layout/_partial/widgets/ghuser.ejs +1 -1
- package/layout/_partial/widgets/markdown.ejs +1 -1
- package/layout/_partial/widgets/related.ejs +1 -1
- package/layout/_partial/widgets/toc.ejs +9 -2
- package/layout/_partial/widgets/tree.ejs +1 -1
- package/layout/_plugins/fancybox.ejs +4 -2
- package/layout/_plugins/mermaid.ejs +2 -1
- package/layout/_plugins/search/local_search.ejs +4 -3
- package/layout/_plugins/swiper.ejs +1 -0
- package/layout/categories.ejs +18 -8
- package/layout/page.ejs +8 -1
- package/layout/tags.ejs +1 -0
- package/package.json +5 -3
- package/scripts/commands/new-note.js +2 -2
- package/scripts/events/lib/doc_tree.js +12 -208
- package/scripts/events/lib/notebooks.js +8 -148
- package/scripts/events/lib/utils.js +10 -1
- package/scripts/filters/index.js +1 -0
- package/scripts/filters/lib/img_lazyload.js +4 -0
- package/scripts/filters/lib/img_onerror.js +4 -0
- package/scripts/filters/lib/md_table.js +46 -0
- package/scripts/generators/search.js +12 -18
- package/scripts/generators/stellar-icons.js +47 -0
- package/scripts/helpers/ai_label.js +29 -0
- package/scripts/helpers/category_tree.js +11 -0
- package/scripts/helpers/icon.js +2 -2
- package/scripts/helpers/json_ld.js +15 -13
- package/scripts/helpers/mdrender.js +29 -0
- package/scripts/helpers/reading_time.js +12 -0
- package/scripts/helpers/related_posts.js +0 -7
- package/scripts/helpers/seo.js +9 -0
- package/scripts/lib/ai_label.js +55 -0
- package/scripts/lib/category_tree.js +26 -0
- package/scripts/lib/doc_tree.js +267 -0
- package/scripts/lib/mdrender_html.js +97 -0
- package/scripts/lib/notebooks.js +181 -0
- package/scripts/lib/reading_time.js +16 -0
- package/scripts/lib/search_index.js +82 -0
- package/scripts/lib/seo.js +79 -0
- package/scripts/lib/wiki_readme.js +57 -0
- package/scripts/tags/index.js +2 -0
- package/scripts/tags/lib/about.js +2 -2
- package/scripts/tags/lib/albums.js +1 -1
- package/scripts/tags/lib/banner.js +1 -1
- package/scripts/tags/lib/chat.js +49 -49
- package/scripts/tags/lib/copy.js +1 -1
- package/scripts/tags/lib/friends.js +1 -1
- package/scripts/tags/lib/gallery.js +1 -1
- package/scripts/tags/lib/hashtag.js +1 -1
- package/scripts/tags/lib/image.js +3 -3
- package/scripts/tags/lib/md.js +17 -9
- package/scripts/tags/lib/posters.js +1 -1
- package/scripts/tags/lib/sites.js +2 -2
- package/scripts/tags/lib/table.js +22 -0
- package/scripts/tags/lib/timeline.js +9 -9
- package/scripts/tags/lib/tip.js +23 -0
- package/source/css/_common/base.styl +5 -9
- package/source/css/_common/device.styl +6 -3
- package/source/css/_components/list.styl +11 -5
- package/source/css/_components/md-table.styl +23 -0
- package/source/css/_components/pages/archives.styl +9 -25
- package/source/css/_components/pages/article-story.styl +4 -4
- package/source/css/_components/partial/article-tags.styl +10 -0
- package/source/css/_components/partial/bread-nav.styl +23 -1
- package/source/css/_components/partial/navbar.styl +10 -2
- package/source/css/_components/sidebar/search.styl +30 -8
- package/source/css/_components/sidebar/sidebar.styl +10 -7
- package/source/css/_components/tag-plugins/table.styl +42 -0
- package/source/css/_components/tag-plugins/tip.styl +27 -0
- package/source/css/_components/widgets/components.styl +6 -0
- package/source/css/_components/widgets/list.styl +6 -0
- package/source/css/_components/widgets/toc.styl +1 -1
- package/source/css/_custom.styl +2 -13
- package/source/css/_defines/func.styl +47 -16
- package/source/css/_defines/theme_base.styl +3 -0
- package/source/css/_plugins/index.styl +0 -18
- package/source/css/{_plugins/comments → comments}/artalk.styl +4 -0
- package/source/css/{_plugins/comments → comments}/twikoo.styl +4 -0
- package/source/css/{_plugins → plugins}/mermaid.styl +3 -0
- package/source/css/{_plugins → plugins}/swiper.styl +5 -1
- package/source/js/icons.js +60 -0
- package/source/js/main.js +206 -1
- package/source/js/search/highlight.js +82 -0
- package/source/js/search/local-search.js +294 -103
- package/source/js/services/mdrender.js +116 -7
- package/source/js/services/memos.js +60 -8
- package/source/js/services/timeline.js +1 -1
- package/source/js/services/waline_latest_comment.js +3 -1
- package/source/js/services/weibo.js +3 -3
- package/source/js/services.js +179 -0
- package/source/js/tagtree.js +29 -0
- package/source/js/theme.js +61 -0
- package/{layout/_partial/scripts/utils.ejs → source/js/utils.js} +13 -10
- package/layout/_partial/scripts/services.ejs +0 -178
- package/layout/_partial/scripts/sidebar.ejs +0 -25
- package/layout/_partial/scripts/tagtree.ejs +0 -29
- package/layout/_partial/scripts/theme.ejs +0 -67
- /package/source/css/{_plugins/comments → comments}/beaudar.styl +0 -0
- /package/source/css/{_plugins/comments → comments}/utterances.styl +0 -0
- /package/source/css/{_plugins/comments → comments}/waline.styl +0 -0
- /package/source/css/{_plugins → plugins}/fancybox.styl +0 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mdrender_html.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
|
|
3
|
+
*
|
|
4
|
+
* 远程 Markdown 渲染底层组件(通用能力,不包含应用场景逻辑):
|
|
5
|
+
* 统一生成 .data-service.ds-mdrender 占位标记。
|
|
6
|
+
*
|
|
7
|
+
* 行为:
|
|
8
|
+
* - src 为 GitHub raw 类地址时,host 替换为调用方传入的 ghraw
|
|
9
|
+
* (主题配置 api_host.ghraw,代码不设默认值);未传 ghraw 时保留 src 原 host;
|
|
10
|
+
* - 识别后输出 data-base(该 md 所在 raw 目录);
|
|
11
|
+
* - 非 GitHub src 原样输出、不带 data-base;
|
|
12
|
+
* - replace=true 输出 data-replace(客户端原地替换,无外部容器);
|
|
13
|
+
* - heading 缺省 true,输出 data-heading(客户端标题适配本地文章格式)。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
// GitHub raw 规范 host:用于识别用户 src 中的规范 raw 地址(GitHub 固有事实,非配置默认值)
|
|
19
|
+
const GITHUB_RAW_HOST = 'raw.githubusercontent.com';
|
|
20
|
+
|
|
21
|
+
// 占位标记类名(组件契约:服务端生成、services 按需加载、客户端渲染共用)
|
|
22
|
+
const PLACEHOLDER_CLASS = 'ds-mdrender';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 解析 GitHub raw 地址(规范 raw host 或调用方传入的 ghraw 镜像 host)。
|
|
26
|
+
* 返回 { host, owner, repo, ref, rest };无法识别时返回 null。
|
|
27
|
+
*/
|
|
28
|
+
function parseGitHubRaw(url, ghrawHost) {
|
|
29
|
+
if (typeof url !== 'string' || url.length === 0) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
let u;
|
|
33
|
+
try {
|
|
34
|
+
u = new URL(url);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const host = u.hostname || '';
|
|
39
|
+
if (host !== GITHUB_RAW_HOST && host !== ghrawHost) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const segs = u.pathname.split('/').filter(s => s.length > 0);
|
|
43
|
+
if (segs.length < 3) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
host: host,
|
|
48
|
+
owner: segs[0],
|
|
49
|
+
repo: segs[1],
|
|
50
|
+
ref: segs[2],
|
|
51
|
+
rest: segs.slice(3).join('/')
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function escapeAttr(value) {
|
|
56
|
+
return String(value).replace(/&/g, '&').replace(/"/g, '"');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 生成远程 Markdown 渲染占位标记。
|
|
61
|
+
* options: { id, replace, heading, ghraw }
|
|
62
|
+
*/
|
|
63
|
+
function mdrenderHtml(src, options = {}) {
|
|
64
|
+
const parsed = parseGitHubRaw(src, options.ghraw);
|
|
65
|
+
let url = src;
|
|
66
|
+
let base = null;
|
|
67
|
+
if (parsed) {
|
|
68
|
+
// 未传 ghraw 时保留 src 原 host,不硬编码默认值
|
|
69
|
+
const ghraw = options.ghraw || parsed.host;
|
|
70
|
+
const dir = `${parsed.owner}/${parsed.repo}/${parsed.ref}/`;
|
|
71
|
+
url = `https://${ghraw}/${dir}${parsed.rest}`;
|
|
72
|
+
base = `https://${ghraw}/${dir}`;
|
|
73
|
+
}
|
|
74
|
+
let el = '<div class="data-service ds-mdrender"';
|
|
75
|
+
el += ` src="${escapeAttr(url)}"`;
|
|
76
|
+
if (base) {
|
|
77
|
+
el += ` data-base="${escapeAttr(base)}"`;
|
|
78
|
+
}
|
|
79
|
+
if (options.replace === true) {
|
|
80
|
+
el += ' data-replace="true"';
|
|
81
|
+
}
|
|
82
|
+
if (options.heading !== false) {
|
|
83
|
+
el += ' data-heading="true"';
|
|
84
|
+
}
|
|
85
|
+
if (options.id) {
|
|
86
|
+
el += ` id="${escapeAttr(options.id)}"`;
|
|
87
|
+
}
|
|
88
|
+
el += '></div>';
|
|
89
|
+
return el;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
GITHUB_RAW_HOST,
|
|
94
|
+
PLACEHOLDER_CLASS,
|
|
95
|
+
parseGitHubRaw,
|
|
96
|
+
mdrenderHtml
|
|
97
|
+
};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* notebooks.js v1
|
|
3
|
+
*
|
|
4
|
+
* 笔记本系统构建(纯函数部分,供 events/lib/notebooks.js 调用与单测覆盖)。
|
|
5
|
+
* 把「每个笔记本全量 filter 一遍全部页面」改为单遍分组(O(NB+P) 替代 O(NB*P)),
|
|
6
|
+
* 标签树与 noteMap 构建逻辑和元素顺序不变。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
const { normalize_path } = require('./path_utils');
|
|
12
|
+
|
|
13
|
+
class NotePage {
|
|
14
|
+
constructor(page) {
|
|
15
|
+
this.id = page._id;
|
|
16
|
+
this.notebook = page.notebook;
|
|
17
|
+
this.title = page.title;
|
|
18
|
+
this.tags = page.tags;
|
|
19
|
+
this.path = page.path;
|
|
20
|
+
this.path_key = normalize_path(page.path);
|
|
21
|
+
this.layout = page.layout;
|
|
22
|
+
this.date = page.date;
|
|
23
|
+
this.updated = page.updated || page.date;
|
|
24
|
+
|
|
25
|
+
const pin = page.pin ?? page.sticky ?? 0;
|
|
26
|
+
if (pin === true) {
|
|
27
|
+
this.pin = 1;
|
|
28
|
+
} else if (pin === false) {
|
|
29
|
+
this.pin = 0;
|
|
30
|
+
} else {
|
|
31
|
+
this.pin = pin;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function splitTag(tag) {
|
|
37
|
+
return tag.split('/').filter(t => t.length > 0);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 单遍按 notebook id 分组页面。
|
|
42
|
+
* 等价的旧逻辑:对每个笔记本执行 allPages.filter(p => p.notebook === notebook.id)。
|
|
43
|
+
* @param {Array} pages 页面数组(warehouse Query 的 .data)
|
|
44
|
+
* @returns {Map<string, Array>}
|
|
45
|
+
*/
|
|
46
|
+
function groupPagesByNotebook(pages) {
|
|
47
|
+
const pagesByNotebook = new Map();
|
|
48
|
+
for (const page of pages) {
|
|
49
|
+
if (page.notebook == null) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let arr = pagesByNotebook.get(page.notebook);
|
|
53
|
+
if (arr == null) {
|
|
54
|
+
arr = [];
|
|
55
|
+
pagesByNotebook.set(page.notebook, arr);
|
|
56
|
+
}
|
|
57
|
+
arr.push(page);
|
|
58
|
+
}
|
|
59
|
+
return pagesByNotebook;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function prepareNotebook(id, info, ctx, pages) {
|
|
63
|
+
const notebook = info;
|
|
64
|
+
notebook.id = id;
|
|
65
|
+
|
|
66
|
+
if (notebook.base_dir) {
|
|
67
|
+
if (notebook.base_dir.startsWith('/')) {
|
|
68
|
+
notebook.base_dir = notebook.base_dir.substring(1);
|
|
69
|
+
}
|
|
70
|
+
if (notebook.base_dir.length > 1 && !notebook.base_dir.endsWith('/')) {
|
|
71
|
+
notebook.base_dir = notebook.base_dir + '/';
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
const notebooksBaseDir = ctx.theme.config.site_tree.notebooks.base_dir;
|
|
75
|
+
notebook.base_dir = notebooksBaseDir ? `${notebooksBaseDir}/${id}` : id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
notebook.sort ||= 0;
|
|
79
|
+
notebook.auto_excerpt ||= ctx.theme.config.notebook.auto_excerpt || 0;
|
|
80
|
+
notebook.per_page ??= ctx.theme.config.notebook.per_page ?? ctx.config.per_page ?? 10;
|
|
81
|
+
notebook.order_by ||= ctx.theme.config.notebook.order_by || '-updated';
|
|
82
|
+
notebook.menu_id ??= ctx.theme.config.site_tree.notes.menu_id;
|
|
83
|
+
notebook.license ??= ctx.theme.config.notebook.license;
|
|
84
|
+
notebook.share ??= ctx.theme.config.notebook.share;
|
|
85
|
+
|
|
86
|
+
notebook.leftbar ??= ctx.theme.config.site_tree.notes.leftbar;
|
|
87
|
+
notebook.rightbar ??= ctx.theme.config.site_tree.notes.rightbar;
|
|
88
|
+
notebook.note_leftbar ??= ctx.theme.config.site_tree.note.leftbar;
|
|
89
|
+
notebook.note_rightbar ??= ctx.theme.config.site_tree.note.rightbar;
|
|
90
|
+
|
|
91
|
+
const tagMap = new Map(); // tagId: tagInfo
|
|
92
|
+
notebook.tagTree = tagMap;
|
|
93
|
+
|
|
94
|
+
const rootTag = {
|
|
95
|
+
id: '',
|
|
96
|
+
name: '',
|
|
97
|
+
part: '',
|
|
98
|
+
path: notebook.base_dir,
|
|
99
|
+
parent: null, // parent tag id
|
|
100
|
+
childSet: new Set(), // child tag ids
|
|
101
|
+
noteSet: new Set(), // note ids
|
|
102
|
+
};
|
|
103
|
+
tagMap.set(rootTag.id, rootTag);
|
|
104
|
+
|
|
105
|
+
// Iterate through all notes in the notebook, build the tag tree.
|
|
106
|
+
for (const page of pages) {
|
|
107
|
+
rootTag.noteSet.add(page._id);
|
|
108
|
+
|
|
109
|
+
if (!page.tags) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
for (const hierarchyTag of page.tags) {
|
|
114
|
+
const parts = splitTag(hierarchyTag);
|
|
115
|
+
let parent = rootTag;
|
|
116
|
+
for (const part of parts) {
|
|
117
|
+
const tagName = parent.name ? `${parent.name}/${part}` : part;
|
|
118
|
+
const tagId = tagName.toLowerCase();
|
|
119
|
+
let tag = tagMap.get(tagId);
|
|
120
|
+
if (tag == null) {
|
|
121
|
+
tag = {
|
|
122
|
+
id: tagId,
|
|
123
|
+
name: tagName,
|
|
124
|
+
part: part,
|
|
125
|
+
path: `${notebook.base_dir}/tags/${tagId}`,
|
|
126
|
+
parent: parent.id,
|
|
127
|
+
childSet: new Set(),
|
|
128
|
+
noteSet: new Set(),
|
|
129
|
+
};
|
|
130
|
+
tagMap.set(tagId, tag);
|
|
131
|
+
parent.childSet.add(tagId);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
tag.noteSet.add(page._id);
|
|
135
|
+
parent = tag;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
notebook.noteMap = pages.map(p => new NotePage(p)).reduce((map, note) => {
|
|
141
|
+
map.set(note.id, note);
|
|
142
|
+
return map;
|
|
143
|
+
}, new Map());
|
|
144
|
+
|
|
145
|
+
// Sort child tags for each tag.
|
|
146
|
+
for (const [_, tag] of tagMap) {
|
|
147
|
+
tag.children = Array.from(tag.childSet);
|
|
148
|
+
tag.children.sort();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return notebook;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function getNotebooksObject(ctx) {
|
|
155
|
+
const notebooks = {
|
|
156
|
+
tree: {},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const data = ctx.locals.get('data');
|
|
160
|
+
const pagesByNotebook = groupPagesByNotebook(ctx.locals.get('pages').data);
|
|
161
|
+
const list = [];
|
|
162
|
+
for (const [key, info] of Object.entries(data)) {
|
|
163
|
+
if (!key.startsWith('notebooks/') || key.endsWith('.DS_Store')) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const id = key.substring(10);
|
|
167
|
+
list.push(prepareNotebook(id, info, ctx, pagesByNotebook.get(id) || []));
|
|
168
|
+
}
|
|
169
|
+
list.sort((a, b) => a.sort - b.sort);
|
|
170
|
+
for (const info of list) {
|
|
171
|
+
notebooks.tree[info.id] = info;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return notebooks;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = {
|
|
178
|
+
groupPagesByNotebook,
|
|
179
|
+
NotePage,
|
|
180
|
+
getNotebooksObject
|
|
181
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 阅读信息统计(#302):中文字符按 1 字计,英文/数字按空格分词计词
|
|
4
|
+
function countWords(content) {
|
|
5
|
+
const text = String(content || '').replace(/<[^>]*>/g, ' ');
|
|
6
|
+
const cjk = (text.match(/[\u4e00-\u9fff]/g) || []).length;
|
|
7
|
+
const latin = (text.replace(/[\u4e00-\u9fff]/g, ' ').match(/[A-Za-z0-9]+(?:[-'][A-Za-z0-9]+)*/g) || []).length;
|
|
8
|
+
return cjk + latin;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// 混合阅读速度约 300 字/分钟,最少 1 分钟
|
|
12
|
+
function readingMinutes(content, speed = 300) {
|
|
13
|
+
return Math.max(1, Math.ceil(countWords(content) / speed));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { countWords, readingMinutes };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 本地搜索索引正文构建:从渲染后 HTML 提取搜索正文与章节锚点。
|
|
5
|
+
*
|
|
6
|
+
* - 返回的 content 与 scripts/generators/search.js 原有全文归一化结果逐字节一致;
|
|
7
|
+
* - anchors 每项 { id, text, offset },offset 指向标题文本在 content 中的起始位置,
|
|
8
|
+
* 与客户端 content.indexOf() 的偏移严格对齐。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { stripHTML } = require('hexo-util');
|
|
12
|
+
|
|
13
|
+
// 标题标签切分:捕获 level / 开始标签属性 / 内部 HTML
|
|
14
|
+
const HEADING_RE = /<h([1-6])\b([^>]*)>([\s\S]*?)<\/h\1>/gi;
|
|
15
|
+
const HEADING_ID_RE = /id="([^"]*)"/;
|
|
16
|
+
|
|
17
|
+
// 全局预清理:与 scripts/generators/search.js 原有顺序保持一致(先于 stripHTML)
|
|
18
|
+
function preClean(html) {
|
|
19
|
+
return String(html || '')
|
|
20
|
+
.replace(/<span class="line">\d+<\/span>/g, '')
|
|
21
|
+
.replace(/<iframe[\s|\S]+iframe>/g, '')
|
|
22
|
+
.replace(/<hr>/g, '')
|
|
23
|
+
.replace(/<br>/g, '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 单段清洗:stripHTML 之后移除实体、换行转空格、空白折叠(不做全文 trim)
|
|
27
|
+
function cleanChunk(chunk) {
|
|
28
|
+
return stripHTML(chunk)
|
|
29
|
+
.replace(/&[^\s;]+;/g, '')
|
|
30
|
+
.replace(/\\n/g, ' ')
|
|
31
|
+
.replace(/\n/g, ' ')
|
|
32
|
+
.replace(/[\s]{2,}/g, ' ');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 跨段拼接:上一段以空白结尾且本段以空白开头时,合并为单个空白,
|
|
36
|
+
// 与整段式 [\s]{2,} 折叠在边界上的结果一致
|
|
37
|
+
function mergeChunk(buffer, chunk) {
|
|
38
|
+
if (buffer && /\s$/.test(buffer) && /^\s/.test(chunk)) {
|
|
39
|
+
return chunk.replace(/^\s+/, '');
|
|
40
|
+
}
|
|
41
|
+
return chunk;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 构建搜索索引条目:{ content, anchors }
|
|
46
|
+
* @param {string} html 渲染后的页面 HTML
|
|
47
|
+
*/
|
|
48
|
+
function buildSearchIndex(html) {
|
|
49
|
+
const parts = preClean(html).split(HEADING_RE);
|
|
50
|
+
let content = '';
|
|
51
|
+
const anchors = [];
|
|
52
|
+
for (let i = 0; i < parts.length; i += 4) {
|
|
53
|
+
const before = parts[i] || '';
|
|
54
|
+
const level = parts[i + 1];
|
|
55
|
+
const attrs = parts[i + 2] || '';
|
|
56
|
+
const inner = parts[i + 3] || '';
|
|
57
|
+
content += mergeChunk(content, cleanChunk(before));
|
|
58
|
+
if (!level) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const rawText = cleanChunk(inner);
|
|
62
|
+
const text = rawText.trim();
|
|
63
|
+
const merged = mergeChunk(content, rawText);
|
|
64
|
+
const appended = merged.slice(content.length);
|
|
65
|
+
const offset = content.length + (appended.length - appended.trimStart().length);
|
|
66
|
+
const match = attrs.match(HEADING_ID_RE);
|
|
67
|
+
if (match && text.length > 0) {
|
|
68
|
+
anchors.push({ id: match[1], text, offset });
|
|
69
|
+
}
|
|
70
|
+
content += merged;
|
|
71
|
+
}
|
|
72
|
+
const trimmed = content.trim();
|
|
73
|
+
const shift = content.length - content.trimStart().length;
|
|
74
|
+
if (shift > 0) {
|
|
75
|
+
for (const anchor of anchors) {
|
|
76
|
+
anchor.offset -= shift;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { content: trimmed, anchors };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = { buildSearchIndex };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/* global hexo */
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// SEO 相关纯函数:供 JSON-LD 生成与 head 模板共用,避免两处重复逻辑。
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 提取正文第一张图片的真实地址。
|
|
8
|
+
* 优先匹配 data-src(懒加载占位图场景),其次匹配 src。
|
|
9
|
+
* @param {string} content 已渲染的 HTML 内容
|
|
10
|
+
* @returns {string} 图片地址,未找到时返回空字符串
|
|
11
|
+
*/
|
|
12
|
+
function firstContentImage(content) {
|
|
13
|
+
if (!content) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
const html = String(content);
|
|
17
|
+
const dataSrc = html.match(/<img[^>]*data-src=["']([^"']+)["']/i);
|
|
18
|
+
if (dataSrc) {
|
|
19
|
+
return dataSrc[1];
|
|
20
|
+
}
|
|
21
|
+
const src = html.match(/<img[^>]*src=["']([^"']+)["']/i);
|
|
22
|
+
return src ? src[1] : '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 构建文章图片数组(顺序:cover → banner → photos → 正文首图 → 默认封面)。
|
|
27
|
+
* @param {Object} opts
|
|
28
|
+
* @param {string} opts.cover
|
|
29
|
+
* @param {string} opts.banner
|
|
30
|
+
* @param {Array} opts.photos
|
|
31
|
+
* @param {string} opts.content
|
|
32
|
+
* @param {string} opts.defaultCover 主题默认封面(theme.default.cover)
|
|
33
|
+
* @returns {Array<string>}
|
|
34
|
+
*/
|
|
35
|
+
function postImages(opts) {
|
|
36
|
+
const cover = opts.cover || '';
|
|
37
|
+
const banner = opts.banner || '';
|
|
38
|
+
const photos = Array.isArray(opts.photos) ? opts.photos.slice() : [];
|
|
39
|
+
const defaultCover = opts.defaultCover || '';
|
|
40
|
+
const images = photos;
|
|
41
|
+
if (cover) {
|
|
42
|
+
images.unshift(cover);
|
|
43
|
+
} else if (banner) {
|
|
44
|
+
images.unshift(banner);
|
|
45
|
+
}
|
|
46
|
+
if (images.length === 0) {
|
|
47
|
+
const first = firstContentImage(opts.content);
|
|
48
|
+
if (first) {
|
|
49
|
+
images.push(first);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (images.length === 0 && defaultCover) {
|
|
53
|
+
images.push(defaultCover);
|
|
54
|
+
}
|
|
55
|
+
return images;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 构建文章描述:优先摘要,缺失时回退正文并截断。
|
|
60
|
+
* @param {Object} opts
|
|
61
|
+
* @param {string} opts.excerpt
|
|
62
|
+
* @param {string} opts.content
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
function postDescription(opts) {
|
|
66
|
+
const util = require('hexo-util');
|
|
67
|
+
const excerpt = String(opts.excerpt || '').replace(/<[^>]+>/g, '');
|
|
68
|
+
if (excerpt) {
|
|
69
|
+
return excerpt;
|
|
70
|
+
}
|
|
71
|
+
const content = String(opts.content || '').replace(/<[^>]+>/g, '');
|
|
72
|
+
return util.truncate(content, { length: 200 });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = {
|
|
76
|
+
firstContentImage,
|
|
77
|
+
postImages,
|
|
78
|
+
postDescription
|
|
79
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wiki_readme.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
|
|
3
|
+
*
|
|
4
|
+
* Wiki 应用层:项目首页正文为空(剪裁空白后)且配置 repo 时,
|
|
5
|
+
* 以该仓库 README.md 作为主页正文(复用底层远程 md 渲染组件)。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const { mdrenderHtml } = require('./mdrender_html');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* README 地址。host 由调用方从主题配置 api_host.ghraw 传入;
|
|
14
|
+
* branch 缺省用 GitHub HEAD(自动指向默认分支)。
|
|
15
|
+
*/
|
|
16
|
+
function readmeUrl(repo, branch, host) {
|
|
17
|
+
return `https://${host}/${repo}/${branch || 'HEAD'}/README.md`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 页面正文是否为空(剪裁多余空行、空格后判断)
|
|
21
|
+
function isEmptyContent(page) {
|
|
22
|
+
return !page || !String(page.content || '').trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 判定是否 README 主页:配置了 repo 的项目首页且正文为空。
|
|
27
|
+
*/
|
|
28
|
+
function isWikiReadmePage(proj, page) {
|
|
29
|
+
if (!proj || !proj.repo || !page || !proj.homepage) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!isEmptyContent(page)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return proj.homepage.path === page.path;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 生成 README 主页占位(原地替换模式);不适用时返回空串。
|
|
40
|
+
* options: { ghraw }
|
|
41
|
+
*/
|
|
42
|
+
function wikiReadmeHtml(proj, page, options = {}) {
|
|
43
|
+
if (!isWikiReadmePage(proj, page)) {
|
|
44
|
+
return '';
|
|
45
|
+
}
|
|
46
|
+
return mdrenderHtml(readmeUrl(proj.repo, proj.branch, options.ghraw), {
|
|
47
|
+
ghraw: options.ghraw,
|
|
48
|
+
replace: true
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
readmeUrl,
|
|
54
|
+
isEmptyContent,
|
|
55
|
+
isWikiReadmePage,
|
|
56
|
+
wikiReadmeHtml
|
|
57
|
+
};
|
package/scripts/tags/index.js
CHANGED
|
@@ -48,6 +48,8 @@ hexo.extend.tag.register('audio', require('./lib/audio')(hexo))
|
|
|
48
48
|
hexo.extend.tag.register('video', require('./lib/video')(hexo))
|
|
49
49
|
hexo.extend.tag.register('rating', require('./lib/rating')(hexo))
|
|
50
50
|
hexo.extend.tag.register('vote', require('./lib/vote')(hexo))
|
|
51
|
+
hexo.extend.tag.register('table', require('./lib/table')(hexo), {ends: true})
|
|
52
|
+
hexo.extend.tag.register('tip', require('./lib/tip')(hexo), {ends: true})
|
|
51
53
|
|
|
52
54
|
// read 阅读类
|
|
53
55
|
hexo.extend.tag.register('reel', require('./lib/read/reel')(hexo), true)
|
|
@@ -18,7 +18,7 @@ module.exports = ctx => function(args, content) {
|
|
|
18
18
|
el += '<div class="tag-plugin about">'
|
|
19
19
|
if (args.back) {
|
|
20
20
|
el += '<a class="nav-back cap" href="' + url_for(ctx.config.root) + '">'
|
|
21
|
-
el +=
|
|
21
|
+
el += ctx.utils.icon('default:goback')
|
|
22
22
|
el += '</a>'
|
|
23
23
|
}
|
|
24
24
|
// avatar
|
|
@@ -45,4 +45,4 @@ module.exports = ctx => function(args, content) {
|
|
|
45
45
|
|
|
46
46
|
el += '</div>'
|
|
47
47
|
return el
|
|
48
|
-
}
|
|
48
|
+
}
|
|
@@ -32,7 +32,7 @@ module.exports = ctx => function(args) {
|
|
|
32
32
|
el += `<div class="grid-cell album-card">`
|
|
33
33
|
el += `<a class="card-link lazy-box" target="_blank" rel="external nofollow noopener noreferrer" href="${item.url}">`
|
|
34
34
|
el += `<img class="lazy" data-src="${item.cover || item.icon || item.avatar || ctx.theme.config.default.cover}" onerror="javascript:this.removeAttribute("data-src");this.src="${ctx.theme.config.default.cover}";"/>`
|
|
35
|
-
el += `<div class="lazy-icon" style="background-image:url(${ctx.theme.config.default.loading});"></div>`
|
|
35
|
+
el += `<div class="lazy-icon" style="background-image:url(${ctx.theme.config.default.loading || ctx.utils.iconData('default:loading-placeholder')});"></div>`
|
|
36
36
|
el += `<div class="image-meta">`
|
|
37
37
|
if (item.title) {
|
|
38
38
|
el += `<span class="image-caption">${item.title}</span>`
|
|
@@ -25,7 +25,7 @@ module.exports = ctx => function(args, content) {
|
|
|
25
25
|
} else {
|
|
26
26
|
el += `
|
|
27
27
|
<button class="back cap" onclick="window.history.back()">
|
|
28
|
-
|
|
28
|
+
${ctx.utils.icon('default:goback')}
|
|
29
29
|
</button>
|
|
30
30
|
`
|
|
31
31
|
}
|