关于本站

本站修改了 hexo-next-twikoo@1.0.3hexo-theme-next@8.27.0 的部分代码,根据 GPL 要求,公示改动如下,您可以在 GitHub 获取原始代码。

hexo-next-twikoo@1.0.3.patch

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
diff --git a/twikoo.njk b/twikoo.njk
index 59f90cb..2d1743a 100644
--- a/twikoo.njk
+++ b/twikoo.njk
@@ -2,16 +2,67 @@
el: '#twikoo-comments'
}) }}
<script>
-document.addEventListener('page:loaded', () => {
- NexT.utils.loadComments(CONFIG.twikoo.el)
- .then(() => NexT.utils.getScript(
- CONFIG.twikoo.jsUrl || 'https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js',
- { condition: window.twikoo }
- ))
- .then(() => {
- twikoo.init(CONFIG.twikoo);
- });
-});
+(() => {
+ let initializedElement = null;
+ let initializedPath = null;
+
+ const isLockedEncryptedPost = () => {
+ const encrypted = document.getElementById('hexo-blog-encrypt');
+ return encrypted && !encrypted.classList.contains('hbe-decrypted-content');
+ };
+
+ const readEncryptedCommentKey = () => {
+ const decrypted = document.querySelector('#hexo-blog-encrypt.hbe-decrypted-content');
+ if (!decrypted) return '';
+
+ const keyNode = decrypted.querySelector('template#commentKey');
+ if (!keyNode) return '';
+
+ const key = ((keyNode.content ? keyNode.content.textContent : keyNode.textContent) || '').trim();
+ keyNode.remove();
+ return key;
+ };
+
+ const initTwikoo = path => {
+ const config = Object.assign({}, CONFIG.twikoo);
+ if (config.envId && config.envId.charAt(0) === '/') {
+ config.envId = window.location.origin + config.envId;
+ }
+ if (path) config.path = path;
+
+ const currentElement = document.querySelector(config.el);
+ const currentPath = config.path || '';
+ if (currentElement && initializedElement === currentElement && initializedPath === currentPath) return;
+
+ NexT.utils.loadComments(config.el)
+ .then(() => NexT.utils.getScript(
+ config.jsUrl || 'https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js',
+ { condition: window.twikoo }
+ ))
+ .then(() => {
+ initializedElement = document.querySelector(config.el);
+ initializedPath = currentPath;
+ twikoo.init(config);
+ });
+ };
+
+ const initFromCurrentPage = () => {
+ if (isLockedEncryptedPost()) return;
+
+ const decrypted = document.querySelector('#hexo-blog-encrypt.hbe-decrypted-content');
+ if (decrypted) {
+ const commentKey = readEncryptedCommentKey();
+ if (!commentKey) return;
+ initTwikoo(commentKey);
+ return;
+ }
+
+ initTwikoo();
+ };
+
+ document.addEventListener('page:loaded', initFromCurrentPage);
+ window.addEventListener('hexo-blog-decrypt', () => {
+ const commentKey = readEncryptedCommentKey();
+ if (commentKey) initTwikoo(commentKey);
+ });
+})();
</script>
<style>
.post-block, .comments {

hexo-theme-next@8.27.0.patch

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
diff --git a/layout/_macro/sidebar.njk b/layout/_macro/sidebar.njk
index 6f82d627979174dfdefc34ec0c559f70b395442a..c94a289c653a8f85cd85435ff3465e1565d22d86 100644
--- a/layout/_macro/sidebar.njk
+++ b/layout/_macro/sidebar.njk
@@ -1,11 +1,16 @@
{% macro render(display_toc) %}
<aside class="sidebar">
+ {%- set encrypted_toc = page.encrypt %}
{%- if display_toc %}
- {%- set toc = toc(page.content, {class: 'nav', list_number: page.toc.number, max_depth: page.toc.max_depth}) %}
- {%- set display_toc = toc.length > 1 and display_toc %}
+ {%- if encrypted_toc %}
+ {%- set toc = '' %}
+ {%- else %}
+ {%- set toc = toc(page.content, {class: 'nav', list_number: page.toc.number, max_depth: page.toc.max_depth}) %}
+ {%- set display_toc = toc.length > 1 and display_toc %}
+ {%- endif %}
{%- endif %}

- <div class="sidebar-inner {% if display_toc %}sidebar-nav-active sidebar-toc-active{% else %}sidebar-overview-active{% endif %}">
+ <div class="sidebar-inner {% if display_toc and not encrypted_toc %}sidebar-nav-active sidebar-toc-active{% else %}sidebar-overview-active{% endif %}">
<ul class="sidebar-nav">
<li class="sidebar-nav-toc">
{{ __('sidebar.toc') }}
@@ -19,7 +24,7 @@
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
{%- if display_toc %}
- <div class="post-toc animated">{{ toc }}</div>
+ <div class="post-toc animated{% if encrypted_toc %} placeholder-toc{% endif %}"{% if encrypted_toc %} hidden aria-hidden="true" data-toc-number="{{ page.toc.number }}" data-toc-max-depth="{{ page.toc.max_depth }}"{% endif %}>{{ toc }}</div>
{%- endif %}
</div>
<!--/noindex-->
diff --git a/source/js/next-boot.js b/source/js/next-boot.js
index 1f347a0882486eafb0345550039c1dbee58c849e..e37e1c9b44a8533be9f438e4fb8c5dc22d5fd728 100644
--- a/source/js/next-boot.js
+++ b/source/js/next-boot.js
@@ -71,6 +71,99 @@ NexT.boot.refresh = function() {
NexT.utils.registerVideoIframe();
};

+NexT.boot.buildEncryptedTOC = function(toc) {
+ const postBody = document.querySelector('.post-body');
+ if (!postBody) return false;
+
+ const maxDepth = parseInt(toc.dataset.tocMaxDepth, 10) || 6;
+ const numbered = toc.dataset.tocNumber !== 'false';
+ const headings = [...postBody.querySelectorAll('h1, h2, h3, h4, h5, h6')].filter(heading => {
+ const level = parseInt(heading.tagName.substring(1), 10);
+ return level <= maxDepth && heading.textContent.trim();
+ });
+
+ if (headings.length === 0) return false;
+
+ const levels = headings.map(heading => parseInt(heading.tagName.substring(1), 10));
+ const rootLevel = Math.min(...levels) - 1;
+ const root = document.createElement('ol');
+ const counters = Array(7).fill(0);
+ const stack = [{ level: rootLevel, list: root, item: null }];
+
+ root.className = 'nav';
+ toc.textContent = '';
+
+ headings.forEach((heading, index) => {
+ const level = parseInt(heading.tagName.substring(1), 10);
+ if (!heading.id) heading.id = `encrypted-heading-${index + 1}`;
+
+ while (stack.length > 1 && level <= stack[stack.length - 1].level) {
+ stack.pop();
+ }
+
+ let parent = stack[stack.length - 1];
+ let list = parent.list;
+ if (level > parent.level && parent.item) {
+ list = parent.item.querySelector(':scope > .nav-child');
+ if (!list) {
+ list = document.createElement('ol');
+ list.className = 'nav-child';
+ parent.item.appendChild(list);
+ }
+ }
+
+ counters[level]++;
+ for (let i = level + 1; i < counters.length; i++) counters[i] = 0;
+
+ const item = document.createElement('li');
+ const link = document.createElement('a');
+ const text = document.createElement('span');
+ const clone = heading.cloneNode(true);
+
+ clone.querySelectorAll('.headerlink, .anchor').forEach(element => element.remove());
+ item.className = `nav-item nav-level-${level}`;
+ link.className = 'nav-link';
+ link.href = `#${encodeURI(heading.id)}`;
+
+ if (numbered) {
+ const number = document.createElement('span');
+ number.className = 'nav-number';
+ number.textContent = counters.slice(rootLevel + 1, level + 1).filter(Boolean).join('.') + '.';
+ link.appendChild(number);
+ link.appendChild(document.createTextNode(' '));
+ }
+
+ text.className = 'nav-text';
+ text.textContent = clone.textContent.trim();
+ link.appendChild(text);
+ item.appendChild(link);
+ list.appendChild(item);
+ stack.push({ level, list, item });
+ });
+
+ toc.appendChild(root);
+ return true;
+};
+
+NexT.boot.activateEncryptedTOC = function() {
+ const toc = document.querySelector('.post-toc.placeholder-toc');
+ if (!toc) return;
+
+ if (!NexT.boot.buildEncryptedTOC(toc)) return;
+
+ toc.classList.remove('placeholder-toc');
+ toc.hidden = false;
+ toc.removeAttribute('aria-hidden');
+
+ const sidebar = document.querySelector('.sidebar-inner');
+ if (!sidebar) return;
+
+ sidebar.classList.add('sidebar-nav-active');
+ NexT.utils.registerSidebarTOC();
+ NexT.utils.activateSidebarPanel(0);
+ NexT.utils.updateSidebarPosition();
+};
+
NexT.boot.motion = function() {
// Define Motion Sequence & Bootstrap Motion.
if (CONFIG.motion.enable) {
@@ -89,3 +182,5 @@ document.addEventListener('DOMContentLoaded', () => {
NexT.boot.refresh();
NexT.boot.motion();
});
+
+window.addEventListener('hexo-blog-decrypt', NexT.boot.activateEncryptedTOC);
0%