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.
Files changed (130) hide show
  1. package/.claude/skills/stellar-theme-dev/SKILL.md +2 -2
  2. package/CHANGELOG.md +70 -0
  3. package/_config.yml +38 -11
  4. package/_data/icons.yml +250 -37
  5. package/languages/en.yml +8 -0
  6. package/languages/zh-CN.yml +8 -0
  7. package/languages/zh-TW.yml +8 -0
  8. package/layout/_partial/comments/artalk/script.ejs +26 -1
  9. package/layout/_partial/comments/beaudar/script.ejs +1 -0
  10. package/layout/_partial/comments/twikoo/script.ejs +1 -0
  11. package/layout/_partial/comments/utterances/script.ejs +1 -0
  12. package/layout/_partial/comments/waline/script.ejs +1 -0
  13. package/layout/_partial/head.ejs +54 -9
  14. package/layout/_partial/main/article/article_tags.ejs +16 -0
  15. package/layout/_partial/main/navbar/article_banner.ejs +17 -0
  16. package/layout/_partial/main/navbar/dateinfo.ejs +1 -1
  17. package/layout/_partial/main/notebook/note_card.ejs +1 -1
  18. package/layout/_partial/main/notebook/paginator.ejs +2 -2
  19. package/layout/_partial/main/pin_slider.ejs +2 -2
  20. package/layout/_partial/main/post_list/paginator.ejs +2 -2
  21. package/layout/_partial/main/post_list/post_card.ejs +11 -0
  22. package/layout/_partial/menubtn.ejs +2 -2
  23. package/layout/_partial/scripts/defines.ejs +26 -2
  24. package/layout/_partial/scripts.ejs +30 -7
  25. package/layout/_partial/sidebar/logo.ejs +1 -1
  26. package/layout/_partial/sidebar/menu.ejs +1 -1
  27. package/layout/_partial/sidebar/search.ejs +1 -1
  28. package/layout/_partial/widgets/author.ejs +1 -1
  29. package/layout/_partial/widgets/components/link.ejs +2 -2
  30. package/layout/_partial/widgets/ghuser.ejs +1 -1
  31. package/layout/_partial/widgets/markdown.ejs +1 -1
  32. package/layout/_partial/widgets/related.ejs +1 -1
  33. package/layout/_partial/widgets/toc.ejs +9 -2
  34. package/layout/_partial/widgets/tree.ejs +1 -1
  35. package/layout/_plugins/fancybox.ejs +4 -2
  36. package/layout/_plugins/mermaid.ejs +2 -1
  37. package/layout/_plugins/search/local_search.ejs +4 -3
  38. package/layout/_plugins/swiper.ejs +1 -0
  39. package/layout/categories.ejs +18 -8
  40. package/layout/page.ejs +8 -1
  41. package/layout/tags.ejs +1 -0
  42. package/package.json +5 -3
  43. package/scripts/commands/new-note.js +2 -2
  44. package/scripts/events/lib/doc_tree.js +12 -208
  45. package/scripts/events/lib/notebooks.js +8 -148
  46. package/scripts/events/lib/utils.js +10 -1
  47. package/scripts/filters/index.js +1 -0
  48. package/scripts/filters/lib/img_lazyload.js +4 -0
  49. package/scripts/filters/lib/img_onerror.js +4 -0
  50. package/scripts/filters/lib/md_table.js +46 -0
  51. package/scripts/generators/search.js +12 -18
  52. package/scripts/generators/stellar-icons.js +47 -0
  53. package/scripts/helpers/ai_label.js +29 -0
  54. package/scripts/helpers/category_tree.js +11 -0
  55. package/scripts/helpers/icon.js +2 -2
  56. package/scripts/helpers/json_ld.js +15 -13
  57. package/scripts/helpers/mdrender.js +29 -0
  58. package/scripts/helpers/reading_time.js +12 -0
  59. package/scripts/helpers/related_posts.js +0 -7
  60. package/scripts/helpers/seo.js +9 -0
  61. package/scripts/lib/ai_label.js +55 -0
  62. package/scripts/lib/category_tree.js +26 -0
  63. package/scripts/lib/doc_tree.js +267 -0
  64. package/scripts/lib/mdrender_html.js +97 -0
  65. package/scripts/lib/notebooks.js +181 -0
  66. package/scripts/lib/reading_time.js +16 -0
  67. package/scripts/lib/search_index.js +82 -0
  68. package/scripts/lib/seo.js +79 -0
  69. package/scripts/lib/wiki_readme.js +57 -0
  70. package/scripts/tags/index.js +2 -0
  71. package/scripts/tags/lib/about.js +2 -2
  72. package/scripts/tags/lib/albums.js +1 -1
  73. package/scripts/tags/lib/banner.js +1 -1
  74. package/scripts/tags/lib/chat.js +49 -49
  75. package/scripts/tags/lib/copy.js +1 -1
  76. package/scripts/tags/lib/friends.js +1 -1
  77. package/scripts/tags/lib/gallery.js +1 -1
  78. package/scripts/tags/lib/hashtag.js +1 -1
  79. package/scripts/tags/lib/image.js +3 -3
  80. package/scripts/tags/lib/md.js +17 -9
  81. package/scripts/tags/lib/posters.js +1 -1
  82. package/scripts/tags/lib/sites.js +2 -2
  83. package/scripts/tags/lib/table.js +22 -0
  84. package/scripts/tags/lib/timeline.js +9 -9
  85. package/scripts/tags/lib/tip.js +23 -0
  86. package/source/css/_common/base.styl +5 -9
  87. package/source/css/_common/device.styl +6 -3
  88. package/source/css/_components/list.styl +11 -5
  89. package/source/css/_components/md-table.styl +23 -0
  90. package/source/css/_components/pages/archives.styl +9 -25
  91. package/source/css/_components/pages/article-story.styl +4 -4
  92. package/source/css/_components/partial/article-tags.styl +10 -0
  93. package/source/css/_components/partial/bread-nav.styl +23 -1
  94. package/source/css/_components/partial/navbar.styl +10 -2
  95. package/source/css/_components/sidebar/search.styl +30 -8
  96. package/source/css/_components/sidebar/sidebar.styl +10 -7
  97. package/source/css/_components/tag-plugins/table.styl +42 -0
  98. package/source/css/_components/tag-plugins/tip.styl +27 -0
  99. package/source/css/_components/widgets/components.styl +6 -0
  100. package/source/css/_components/widgets/list.styl +6 -0
  101. package/source/css/_components/widgets/toc.styl +1 -1
  102. package/source/css/_custom.styl +2 -13
  103. package/source/css/_defines/func.styl +47 -16
  104. package/source/css/_defines/theme_base.styl +3 -0
  105. package/source/css/_plugins/index.styl +0 -18
  106. package/source/css/{_plugins/comments → comments}/artalk.styl +4 -0
  107. package/source/css/{_plugins/comments → comments}/twikoo.styl +4 -0
  108. package/source/css/{_plugins → plugins}/mermaid.styl +3 -0
  109. package/source/css/{_plugins → plugins}/swiper.styl +5 -1
  110. package/source/js/icons.js +60 -0
  111. package/source/js/main.js +206 -1
  112. package/source/js/search/highlight.js +82 -0
  113. package/source/js/search/local-search.js +294 -103
  114. package/source/js/services/mdrender.js +116 -7
  115. package/source/js/services/memos.js +60 -8
  116. package/source/js/services/timeline.js +1 -1
  117. package/source/js/services/waline_latest_comment.js +3 -1
  118. package/source/js/services/weibo.js +3 -3
  119. package/source/js/services.js +179 -0
  120. package/source/js/tagtree.js +29 -0
  121. package/source/js/theme.js +61 -0
  122. package/{layout/_partial/scripts/utils.ejs → source/js/utils.js} +13 -10
  123. package/layout/_partial/scripts/services.ejs +0 -178
  124. package/layout/_partial/scripts/sidebar.ejs +0 -25
  125. package/layout/_partial/scripts/tagtree.ejs +0 -29
  126. package/layout/_partial/scripts/theme.ejs +0 -67
  127. /package/source/css/{_plugins/comments → comments}/beaudar.styl +0 -0
  128. /package/source/css/{_plugins/comments → comments}/utterances.styl +0 -0
  129. /package/source/css/{_plugins/comments → comments}/waline.styl +0 -0
  130. /package/source/css/{_plugins → plugins}/fancybox.styl +0 -0
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * https://github.com/wzpan/hexo-generator-search
3
3
  */
4
- const { stripHTML } = require('hexo-util')
5
4
  const { normalize_path } = require('../lib/path_utils')
5
+ const { buildSearchIndex } = require('../lib/search_index')
6
6
 
7
7
  hexo.extend.generator.register('search_json_generator', function (locals) {
8
8
  if (this.theme.config.search.service != 'local_search') { return {} }
@@ -33,19 +33,11 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
33
33
  temp_post.path = path === '/' ? '/' : path + '/'
34
34
  }
35
35
  if (cfg.content != false && post.content) {
36
- var content = stripHTML(post.content.replace(/<span class="line">\d+<\/span>/g, '')).trim()
37
- // 部分HTML标签
38
- content = content.replace(/<iframe[\s|\S]+iframe>/g, '')
39
- content = content.replace(/<hr>/g, '')
40
- content = content.replace(/<br>/g, '')
41
- // 去除HTML实体
42
- content = content.replace(/&[^\s;]+;/g, "")
43
- // 换行符换成空格
44
- content = content.replace(/\\n/g, ' ')
45
- content = content.replace(/\n/g, ' ')
46
- // 多个连续空格换成单个空格
47
- content = content.replace(/[\s]{2,}/g, ' ')
48
- temp_post.content = content.trim()
36
+ const { content, anchors } = buildSearchIndex(post.content)
37
+ temp_post.content = content
38
+ if (anchors.length > 0) {
39
+ temp_post.anchors = anchors
40
+ }
49
41
  }
50
42
  if (post.tags && post.tags.length > 0) {
51
43
  var tags = []
@@ -64,10 +56,12 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
64
56
  return temp_post
65
57
  }
66
58
 
59
+ // 循环外编译一次 skip_search 正则,避免每个 post/page 重复 new RegExp
60
+ const skipSearchPatterns = (cfg.skip_search || []).map(pattern => new RegExp('^' + pattern.replace(/\*/g, '.*') + '$'));
61
+
67
62
  function matchAndExit(path, patterns) {
68
63
  for (let pattern of patterns) {
69
- const regexPattern = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
70
- if (path.match(regexPattern)) {
64
+ if (path.match(pattern)) {
71
65
  // console.log("Matched pattern:", pattern);
72
66
  return true;
73
67
  }
@@ -79,7 +73,7 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
79
73
  posts.each(function(post) {
80
74
  var layout_list = ["post"]
81
75
  if (!layout_list.includes(post.layout)) return
82
- if (cfg.skip_search && matchAndExit(post.path, cfg.skip_search)) return
76
+ if (cfg.skip_search && matchAndExit(post.path, skipSearchPatterns)) return
83
77
  if (post.indexing == false) return
84
78
  let temp_post = generateJson(post)
85
79
  res.push(temp_post)
@@ -89,7 +83,7 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
89
83
  pages.each(function(page) {
90
84
  var layout_list = ["page", "wiki"]
91
85
  if (!layout_list.includes(page.layout)) return
92
- if (cfg.skip_search && matchAndExit(page.path, cfg.skip_search)) return
86
+ if (cfg.skip_search && matchAndExit(page.path, skipSearchPatterns)) return
93
87
  if (page.indexing == false) return
94
88
  let temp_post = generateJson(page)
95
89
  res.push(temp_post)
@@ -0,0 +1,47 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ // 构建期生成客户端图标白名单文件:避免把 SVG 图标数据内联进每个页面的 defines。
5
+ // 与 layout/_partial/scripts/defines.ejs 中旧的白名单保持一致(客户端通过 ctx.icons 读取)。
6
+ hexo.extend.generator.register('stellar_icons', function () {
7
+ const icons = (this.theme.config && this.theme.config.icons) || {};
8
+ const keys = ['weibo:comment', 'default:loading-spinner', 'default:warning', 'weibo:repeat', 'weibo:like'];
9
+ const out = {};
10
+ // 去除 SVG 注释(避免 <!-- / </script> 解析风险),再转义 <
11
+ for (const k of keys) {
12
+ out[k] = ((icons[k]) || '').replace(/<!--[\s\S]*?-->/g, '');
13
+ }
14
+ const json = JSON.stringify(out).replace(/</g, '\\u003c');
15
+ return {
16
+ path: 'js/stellar-icons.js',
17
+ data: 'window.stellarIcons = ' + json + ';\nif (typeof ctx !== "undefined" && ctx) { ctx.icons = window.stellarIcons; }\n'
18
+ };
19
+ });
20
+
21
+ // 构建期生成按命名空间拆分的图标数据文件(js/icons/{ns}.json):
22
+ // 供客户端加载器 /js/icons.js 对非首屏图标占位符(svg.icon[data-icon])做异步原位替换。
23
+ // 数据源为合并后的 theme.config.icons(主题 _data/icons.yml + 站点 source/_data/icons.yml 覆盖),
24
+ // 仅收集内联 SVG 值(URL 值由服务端直接输出 <img>,无需下发)。
25
+ hexo.extend.generator.register('stellar_icon_sets', function () {
26
+ const icons = (this.theme.config && this.theme.config.icons) || {};
27
+ const sets = {};
28
+ for (const key of Object.keys(icons)) {
29
+ const value = icons[key];
30
+ if (typeof value !== 'string' || !value.trim().startsWith('<svg')) {
31
+ continue;
32
+ }
33
+ const ns = key.split(':')[0];
34
+ if (ns.length === 0) {
35
+ continue;
36
+ }
37
+ if (!sets[ns]) {
38
+ sets[ns] = {};
39
+ }
40
+ // 去除 SVG 注释,避免注释内容在注入 HTML 时引发解析问题
41
+ sets[ns][key] = value.replace(/<!--[\s\S]*?-->/g, '');
42
+ }
43
+ return Object.keys(sets).map((ns) => ({
44
+ path: `js/icons/${ns}.json`,
45
+ data: JSON.stringify({ [ns]: sets[ns] })
46
+ }));
47
+ });
@@ -0,0 +1,29 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ const { buildAiLabel, resolveAiKey } = require('../lib/ai_label');
5
+
6
+ // 文章 AI 成分标签:front-matter 用 ai_label: manual / reviewed / polished / generated 标记,
7
+ // 未设置时取配置 article.ai_label.default(为空则不渲染);
8
+ // 文案由多语言系统提供(languages/*.yml 的 meta.ai_label.*,缺失时不渲染),
9
+ // 颜色与可选图标由配置 article.ai_label 提供(默认在主题 _config.yml,站点可覆盖)。
10
+ hexo.extend.helper.register('ai_label', function(value, extraClass, noColor) {
11
+ const article = hexo.theme.config.article;
12
+ const labelConfig = article && article.ai_label;
13
+ const key = resolveAiKey(value, labelConfig);
14
+ if (key && (!labelConfig || !labelConfig[key])) {
15
+ console.warn('[ai_label] 未知的 ai_label 值: ' + key);
16
+ }
17
+ if (!key) {
18
+ return '';
19
+ }
20
+ const def = labelConfig && labelConfig[key];
21
+ const iconHtml = (def && def.icon) ? hexo.utils.icon(def.icon) : '';
22
+ const __ = (this && typeof this.__ === 'function') ? this.__ : hexo.theme.i18n.__(hexo.config.language);
23
+ const i18nKey = 'meta.ai_label.' + key;
24
+ const text = __(i18nKey);
25
+ if (!text || text === i18nKey) {
26
+ return '';
27
+ }
28
+ return buildAiLabel(key, labelConfig, text, extraClass, iconHtml, noColor);
29
+ });
@@ -0,0 +1,11 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ const { buildCategoryTree } = require('../lib/category_tree');
5
+
6
+ hexo.extend.helper.register('category_tree', function(categories) {
7
+ const list = categories && typeof categories.toArray === 'function'
8
+ ? categories.toArray()
9
+ : (categories || []);
10
+ return buildCategoryTree(list);
11
+ });
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- hexo.extend.helper.register('icon', function(key, args) {
4
- return hexo.utils.icon(key, args)
3
+ hexo.extend.helper.register('icon', function(key, args, inline) {
4
+ return hexo.utils.icon(key, args, inline)
5
5
  })
@@ -6,6 +6,7 @@
6
6
  'use strict';
7
7
 
8
8
  const util = require('hexo-util');
9
+ const { postImages, postDescription } = require('../lib/seo');
9
10
 
10
11
  hexo.extend.helper.register('json_ld', function(args) {
11
12
  const page = this.page;
@@ -37,6 +38,13 @@ hexo.extend.helper.register('json_ld', function(args) {
37
38
  }
38
39
 
39
40
  if (this.is_post()) {
41
+ const images = postImages({
42
+ cover: page.cover,
43
+ banner: page.banner,
44
+ photos: page.photos,
45
+ content: page.content,
46
+ defaultCover: this.theme.default && this.theme.default.cover
47
+ });
40
48
  schema = {
41
49
  '@context': 'https://schema.org',
42
50
  '@type': 'BlogPosting',
@@ -45,7 +53,7 @@ hexo.extend.helper.register('json_ld', function(args) {
45
53
  dateCreated: page.date.format(),
46
54
  dateModified: page.updated.format(),
47
55
  datePublished: page.date.format(),
48
- description: this.strip_html(page.excerpt),
56
+ description: postDescription({ excerpt: page.excerpt, content: page.content }),
49
57
  headline: page.title,
50
58
  mainEntityOfPage: {
51
59
  '@type': 'WebPage',
@@ -59,18 +67,7 @@ hexo.extend.helper.register('json_ld', function(args) {
59
67
  schema.keywords = page.tags.map((tag) => tag.name).join(', ');
60
68
  }
61
69
 
62
- let images = [];
63
- if (page.photos && page.photos.length > 0) {
64
- images = images.concat(page.photos);
65
- }
66
-
67
- if (page.cover?.length > 0) {
68
- images = images.unshift(page.cover);
69
- } else if (page.banner?.length > 0) {
70
- images = images.unshift(page.banner);
71
- }
72
-
73
- schema.thumbnailUrl = page.cover || page.banner;
70
+ schema.thumbnailUrl = images[0] || undefined;
74
71
  schema.image = images;
75
72
 
76
73
  } else if (isPage || this.is_home()) {
@@ -98,6 +95,11 @@ hexo.extend.helper.register('json_ld', function(args) {
98
95
 
99
96
  if (page.excerpt || page.description) {
100
97
  schema.description = this.strip_html(page.description || page.excerpt);
98
+ } else if (page.wiki) {
99
+ const proj = this.theme.wiki.tree[page.wiki];
100
+ if (proj && proj.description) {
101
+ schema.description = proj.description;
102
+ }
101
103
  } else {
102
104
  schema.description = util.truncate(this.strip_html(page.content), {length: 200});
103
105
  }
@@ -0,0 +1,29 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ const { mdrenderHtml, PLACEHOLDER_CLASS } = require('../lib/mdrender_html');
5
+ const { wikiReadmeHtml, isWikiReadmePage } = require('../lib/wiki_readme');
6
+
7
+ // 远程 Markdown 渲染占位标记(底层组件服务端入口,供 EJS 模板复用)
8
+ hexo.extend.helper.register('mdrender_html', function (src, options = {}) {
9
+ if (options.ghraw == null) {
10
+ options.ghraw = hexo.theme.config.api_host.ghraw;
11
+ }
12
+ return mdrenderHtml(src, options);
13
+ });
14
+
15
+ // wiki 项目 README 主页占位(首页正文为空且配置 repo 时输出,否则空串)
16
+ hexo.extend.helper.register('wiki_readme_html', function (proj, page) {
17
+ return wikiReadmeHtml(proj, page, { ghraw: hexo.theme.config.api_host.ghraw });
18
+ });
19
+
20
+ // 页面是否包含客户端渲染的远程 md(供 TOC 预留空容器)
21
+ hexo.extend.helper.register('has_remote_md', function (page) {
22
+ if (!page) {
23
+ return false;
24
+ }
25
+ if (page.content && page.content.indexOf(PLACEHOLDER_CLASS) >= 0) {
26
+ return true;
27
+ }
28
+ return isWikiReadmePage(hexo.theme.config.wiki.tree[page.wiki], page);
29
+ });
@@ -0,0 +1,12 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ const { countWords, readingMinutes } = require('../lib/reading_time');
5
+
6
+ hexo.extend.helper.register('word_count', function(page) {
7
+ return countWords(page && (page.content || page._content || ''));
8
+ });
9
+
10
+ hexo.extend.helper.register('reading_time', function(page) {
11
+ return readingMinutes(page && (page.content || page._content || ''));
12
+ });
@@ -27,16 +27,9 @@ hexo.extend.helper.register('popular_posts_wrapper', function(args){
27
27
  <section class='body'>
28
28
  `;
29
29
 
30
- const posts = this.site.posts;
31
- const root = this.config.root;
32
-
33
30
  function listItem(obj){
34
31
  var el = '';
35
32
  el += '<a class="item" href="' + esc(obj.path) + '" title="' + esc(obj.title) + '">';
36
- var p = posts.filter(p => root + p.path == obj.path)
37
- if (p && p.length > 0) {
38
- p = p.data[0];
39
- }
40
33
  el += '<span class="title">' + esc(obj.title) + '</span>';
41
34
  if (obj.excerpt && obj.excerpt.length > 0) {
42
35
  el += '<span class="excerpt">' + esc(util.truncate(util.stripHTML(obj.excerpt), {length: 120})) + '</span>';
@@ -0,0 +1,9 @@
1
+ /* global hexo */
2
+ 'use strict';
3
+
4
+ const { firstContentImage } = require('../lib/seo');
5
+
6
+ // 正文首图地址:供 og:image 与结构化数据兜底使用(模板内直接调用)。
7
+ hexo.extend.helper.register('first_content_image', function(content) {
8
+ return firstContentImage(content);
9
+ });
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ const { escapeHTML } = require('hexo-util');
4
+
5
+ // 文章 AI 成分标签(ai_label):颜色规范化 + 标签 HTML 生成。
6
+ // 文案由调用方传入(多语言系统提供),经 HTML 转义后输出;
7
+ // 文字使用配置色(无背景),缺 # 自动补全;可选 iconHtml 渲染在文案前;
8
+ // noColor 时输出不带内联样式(继承默认文字色,用于 banner 含图片场景);
9
+ // 样式由调用方通过 extraClass 传入上下文类。
10
+
11
+ function normalizeColor(color) {
12
+ if (color == null) {
13
+ return '';
14
+ }
15
+ let text = String(color).trim();
16
+ if (!text) {
17
+ return '';
18
+ }
19
+ if (!text.startsWith('#')) {
20
+ text = '#' + text;
21
+ }
22
+ return text;
23
+ }
24
+
25
+ // 解析实际生效的标签键:文章设置 ai_label 时优先使用,否则取配置 default(为空则不渲染)
26
+ function resolveAiKey(value, labelConfig) {
27
+ if (value && typeof value === 'string') {
28
+ return value;
29
+ }
30
+ return (labelConfig && labelConfig.default) || '';
31
+ }
32
+
33
+ function buildAiLabel(value, labelConfig, text, extraClass, iconHtml, noColor) {
34
+ if (!value || typeof value !== 'string' || !labelConfig || typeof labelConfig !== 'object') {
35
+ return '';
36
+ }
37
+ const def = labelConfig[value];
38
+ if (!def || typeof def !== 'object' || !text) {
39
+ return '';
40
+ }
41
+ const cls = ['ai-label'];
42
+ if (extraClass) {
43
+ cls.unshift(extraClass);
44
+ }
45
+ const safeText = escapeHTML(String(text));
46
+ const color = normalizeColor(def.color);
47
+ const icon = iconHtml || '';
48
+ const className = cls.join(' ');
49
+ if (!noColor && color) {
50
+ return `<span class="${className}" style="color:${color}">${icon}${safeText}</span>`;
51
+ }
52
+ return `<span class="${className}">${icon}${safeText}</span>`;
53
+ }
54
+
55
+ module.exports = { buildAiLabel, normalizeColor, resolveAiKey };
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ // 分类嵌套树构建(#564):基于 parent 关系递归组装,兼容 2/3/4 级
4
+ function buildCategoryTree(list) {
5
+ const items = (list || []).map(c => ({
6
+ _id: c._id,
7
+ name: c.name,
8
+ path: c.path,
9
+ parentId: c.parent ? (typeof c.parent === 'object' ? c.parent._id : c.parent) : null,
10
+ count: (c.posts && c.posts.length) || 0,
11
+ children: []
12
+ }));
13
+ const byId = {};
14
+ items.forEach(item => { byId[item._id] = item; });
15
+ const roots = [];
16
+ items.forEach(item => {
17
+ if (item.parentId != null && byId[item.parentId]) {
18
+ byId[item.parentId].children.push(item);
19
+ } else {
20
+ roots.push(item);
21
+ }
22
+ });
23
+ return roots;
24
+ }
25
+
26
+ module.exports = { buildCategoryTree };
@@ -0,0 +1,267 @@
1
+ /**
2
+ * doc_tree.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
3
+ *
4
+ * Wiki 文档树构建(纯函数,供 events/lib/doc_tree.js 调用与单测覆盖)。
5
+ * 与旧实现的输出语义保持一致,仅把重复的 filter/some 遍历改为单遍 Map 索引:
6
+ * - 页面按 wiki 分组一次(O(W+P) 替代 O(W*P));
7
+ * - 项目内按 path_key 分组一次,首页解析与 sections 组装改走索引查询;
8
+ * - all_tags / relatedItems 用 Set/Map 去重,输出顺序不变。
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ const { normalize_path } = require('./path_utils');
14
+
15
+ class WikiPage {
16
+ constructor(page) {
17
+ this.id = page._id;
18
+ this.wiki = page.wiki;
19
+ this.title = page.title;
20
+ this.path = page.path;
21
+ this.path_key = normalize_path(page.path);
22
+ this.layout = page.layout;
23
+ this.updated = page.updated;
24
+ }
25
+ }
26
+
27
+ function getWikiObject(data) {
28
+ var wiki = { tree: {} };
29
+ var list = [];
30
+ for (let key of Object.keys(data)) {
31
+ if (key.endsWith('.DS_Store')) {
32
+ continue;
33
+ }
34
+ if (key.includes('wiki/') && key.length > 5) {
35
+ let newKey = key.replace('wiki/', '');
36
+ let obj = data[key];
37
+ if ((typeof obj.tags == 'string') && obj.tags.constructor == String) {
38
+ obj.tags = [obj.tags];
39
+ }
40
+ if ((typeof obj.tree == 'object') && obj.tree.constructor == Array) {
41
+ obj.tree = { '': obj.tree };
42
+ }
43
+ obj.id = newKey;
44
+ if (obj.sort == null) {
45
+ obj.sort = 0;
46
+ }
47
+ if (obj.base_dir) {
48
+ if (obj.base_dir.startsWith('/')) {
49
+ obj.base_dir = obj.base_dir.substring(1);
50
+ }
51
+ if (obj.base_dir.length > 1 && obj.base_dir.endsWith('/') == false) {
52
+ obj.base_dir = obj.base_dir + '/';
53
+ }
54
+ } else {
55
+ obj.base_dir = '';
56
+ }
57
+ list.push(obj);
58
+ }
59
+ }
60
+ list = list.sort((p1, p2) => p2.sort - p1.sort);
61
+ for (let item of list) {
62
+ wiki.tree[item.id] = item;
63
+ }
64
+ return wiki;
65
+ }
66
+
67
+ /**
68
+ * 构建 wiki 文档树。
69
+ * @param {Object} options
70
+ * @param {Object} options.data 站点 _data(含 wiki/ 前缀的 YAML 数据)
71
+ * @param {Array|Object} options.pages 站点 pages(warehouse Query 或数组)
72
+ * @param {Array} options.shelf 上架项目 id 列表
73
+ * @param {Object} options.siteTree 主题配置 site_tree(取 index_wiki.base_dir)
74
+ */
75
+ function buildWikiTree({ data, pages, shelf, siteTree }) {
76
+ // wiki 配置
77
+ var wiki = getWikiObject(data);
78
+ // wiki 所有页面
79
+ const wiki_pages = pages.filter(p => (p.wiki != null)).map(p => new WikiPage(p));
80
+
81
+ // 单遍分组:wiki id → WikiPage[](保持 wiki_pages 原始顺序)
82
+ const pagesByWiki = new Map();
83
+ for (const wp of wiki_pages) {
84
+ let arr = pagesByWiki.get(wp.wiki);
85
+ if (arr == null) {
86
+ arr = [];
87
+ pagesByWiki.set(wp.wiki, arr);
88
+ }
89
+ arr.push(wp);
90
+ }
91
+
92
+ // 上架的项目列表
93
+ wiki.shelf = shelf || [];
94
+
95
+ // 按 tree 键序过滤出有页面的项目(等价于旧实现 wiki_pages.some 判定)
96
+ const wiki_list = Object.keys(wiki.tree).filter(id => pagesByWiki.has(id));
97
+
98
+ // 数据整合:项目标签
99
+ var all_tag_name = [];
100
+ for (let id of wiki_list) {
101
+ let item = wiki.tree[id];
102
+ let tags = item.tags;
103
+ if (tags) {
104
+ tags.forEach((tag, i) => {
105
+ if (all_tag_name.includes(tag) === false) {
106
+ all_tag_name.push(tag);
107
+ }
108
+ });
109
+ wiki.tree[id].tags = tags;
110
+ }
111
+ }
112
+
113
+ // 补充项目名称和首页
114
+ for (let id of wiki_list) {
115
+ let item = wiki.tree[id];
116
+ item.id = id;
117
+ if (item.title == undefined || item.title.length === 0) {
118
+ item.title = id;
119
+ }
120
+ if (item.name == undefined || item.name.length == 0) {
121
+ item.name = id;
122
+ }
123
+ }
124
+
125
+ // 数据整合:每个项目的子页面
126
+ for (let i = 0; i < wiki_list.length; i++) {
127
+ let id = wiki_list[i];
128
+ let item = wiki.tree[id];
129
+ let sub_pages = pagesByWiki.get(id) || [];
130
+
131
+ // 单遍 path_key 索引:同一 path_key 可能对应多个页面,保持数组与顺序
132
+ const pagesByPathKey = new Map();
133
+ for (const p of sub_pages) {
134
+ let arr = pagesByPathKey.get(p.path_key);
135
+ if (arr == null) {
136
+ arr = [];
137
+ pagesByPathKey.set(p.path_key, arr);
138
+ }
139
+ arr.push(p);
140
+ }
141
+
142
+ // 首页
143
+ // 未特别指定首页时,获取TOC第一页作为首页
144
+ var homepage = item.homepage;
145
+ if (homepage == null && item.tree != null) {
146
+ for (let tid of Object.keys(item.tree)) {
147
+ const sec = item.tree[tid];
148
+ for (let key of sec) {
149
+ const hs = pagesByPathKey.get(normalize_path(item.base_dir + key)) || [];
150
+ if (hs.length > 0) {
151
+ homepage = hs[0];
152
+ break;
153
+ }
154
+ }
155
+ if (homepage != null) {
156
+ break;
157
+ }
158
+ }
159
+ }
160
+ if (homepage == null) {
161
+ homepage = sub_pages[0];
162
+ }
163
+ if (typeof homepage == 'string') {
164
+ homepage = { path: homepage };
165
+ }
166
+ homepage.is_homepage = true;
167
+ item.homepage = homepage;
168
+
169
+ // 内页分组
170
+ var sections = [];
171
+ var others = sub_pages;
172
+ if (item.tree) {
173
+ // 根据配置设置顺序
174
+ const assignedPathKeys = new Set();
175
+ for (let title of Object.keys(item.tree)) {
176
+ var sec = { title: title, pages: [] };
177
+ for (let key of item.tree[title]) {
178
+ const pagePathKey = normalize_path(item.base_dir + key);
179
+ const matched = pagesByPathKey.get(pagePathKey) || [];
180
+ sec.pages = sec.pages.concat(matched);
181
+ if (matched.length > 0) {
182
+ assignedPathKeys.add(pagePathKey);
183
+ }
184
+ }
185
+ sections.push(sec);
186
+ }
187
+ // 未被任何 section 收录的页面归入 '...' 分组(顺序同 sub_pages 过滤结果)
188
+ others = sub_pages.filter(p => !assignedPathKeys.has(p.path_key));
189
+ if (others.length > 0 && others.filter(p => p.title?.length > 0).length > 0) {
190
+ sections.push({
191
+ title: '...',
192
+ pages: others.sort((p1, p2) => p1.title - p2.title)
193
+ });
194
+ }
195
+ } else {
196
+ // 自动设置顺序
197
+ sections.push({
198
+ pages: sub_pages.sort((p1, p2) => p1.title - p2.title)
199
+ });
200
+ }
201
+
202
+ // page number
203
+ var page_number = 0;
204
+ for (let sec of sections) {
205
+ for (let page of sec.pages) {
206
+ page.page_number = page_number++;
207
+ }
208
+ }
209
+ item.sections = sections;
210
+ item.pages = sub_pages;
211
+ }
212
+
213
+ // 全站所有的项目标签
214
+ var all_tags = {};
215
+ all_tag_name.forEach((tag_name, i) => {
216
+ var items = [];
217
+ // 与旧实现 items.includes(tag_name) 判定等价(项目 id 恰等于标签名时的去重行为)
218
+ var itemsSet = new Set();
219
+ for (let id of wiki_list) {
220
+ let item = wiki.tree[id];
221
+ // 过滤掉找不到页面的项目
222
+ if (item.homepage == null) {
223
+ continue;
224
+ }
225
+ // 过滤掉未上架的项目
226
+ if (!wiki.shelf.includes(item.id)) {
227
+ continue;
228
+ }
229
+ if (item.tags && item.tags.includes(tag_name) === true && itemsSet.has(tag_name) === false) {
230
+ itemsSet.add(item.id);
231
+ items.push(item.id);
232
+ }
233
+ }
234
+ all_tags[tag_name] = {
235
+ name: tag_name,
236
+ path: (siteTree.index_wiki.base_dir) + '/tags/' + tag_name + '/index.html',
237
+ items: items
238
+ };
239
+ });
240
+
241
+ // 关联相似项目
242
+ for (let id of wiki_list) {
243
+ let item = wiki.tree[id];
244
+ if (item.tags) {
245
+ var relatedItems = [];
246
+ item.tags.forEach((tag_name, i) => {
247
+ let relatedOtherItems = all_tags[tag_name].items.filter(name => name != item.id);
248
+ if (relatedOtherItems.length > 0) {
249
+ relatedItems.push({
250
+ name: tag_name,
251
+ items: relatedOtherItems
252
+ });
253
+ }
254
+ });
255
+ item.relatedItems = relatedItems;
256
+ }
257
+ }
258
+
259
+ wiki.all_tags = all_tags;
260
+ wiki.all_pages = wiki_pages;
261
+ return wiki;
262
+ }
263
+
264
+ module.exports = {
265
+ buildWikiTree,
266
+ WikiPage
267
+ };