hexo-auto-toc 1.0.5 → 1.0.6

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 (3) hide show
  1. package/README.md +13 -0
  2. package/index.js +9 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -49,6 +49,19 @@ npm install hexo-auto-toc
49
49
 
50
50
  优化toc放置左/右,自动选择放在哪边
51
51
 
52
+ - New Updates
53
+
54
+ - version 1.0.6
55
+ Except index page, other pages have TOC if they have kinds of headers.
56
+
57
+ - version 1.0.5
58
+ Greater compatibility: Works with a wider variety of themes.
59
+ Performance Boost: Optimized the regular expressions for better matching.
60
+ Reliable Parsing: Integrated the Cheerio library to parse HTML, avoiding the drawbacks and errors common with regular expressions.
61
+ Smarter Positioning: Improved the Table of Contents (TOC) module's location; it will now be placed directly after the
62
+ element if one is present.
63
+ Automatic Layout: Enhanced the left/right placement of the TOC, allowing it to automatically choose the best side.
64
+
52
65
  # Recommendations
53
66
 
54
67
  - hexo-everyday-calendar:This is a practical calendar plugin for hexo bloggers, like contribution statistics on GitHub.
package/index.js CHANGED
@@ -98,7 +98,7 @@ hexo.extend.filter.register('after_render:html', function (html) {
98
98
 
99
99
 
100
100
  (function() {
101
- const Patternstr = "\/\\\\\d{4}\/\\\\\d{2}\/\\\\\d{2}\/\.*"; //本身是字符串插入,所以对符号要转义
101
+ const Patternstr = "\/*\\\\\d{4}\/\\\\\d{2}\/\\\\\d{2}\/\.*"; //本身是字符串插入,所以对符号要转义
102
102
  //"^\//*"->"/"
103
103
  //"\/.*"->".*"
104
104
  //JS引擎中"\"转义
@@ -106,18 +106,21 @@ hexo.extend.filter.register('after_render:html', function (html) {
106
106
  const dataPattern = new RegExp(Patternstr);
107
107
  const path = window.location.pathname;
108
108
  console.log(dataPattern.toString(),path,dataPattern.test(path));//控制台在渲染时省略了反斜杠的可视表示。
109
- if (!dataPattern.test(path)) {
109
+ if (!dataPattern.test(path)&&(path==='/'||path==='index.html')) {
110
+ console.log('no toc')
110
111
  let toc = document.getElementById('fixed-toc-container');
111
112
  if (toc) toc.style.display = 'none';
112
113
  return;
113
114
  }
114
115
  var content = document.querySelector('body');
116
+ console.log('content',content)
115
117
  //var tocList = document.getElementById('fixed-toc-list');
116
118
  if (!content || !tocList) {
117
119
  document.getElementById('fixed-toc-container').style.display = 'none';
118
120
  return;
119
121
  }
120
122
  var headings = content.querySelectorAll('h1, h2, h3, h4');
123
+ console.log('headings',headings)
121
124
  if(headings){
122
125
  const h1Element=headings[0];
123
126
  const rect = h1Element.getBoundingClientRect();
@@ -172,11 +175,11 @@ hexo.extend.injector.register('body_end', () => {//生成静态页面时
172
175
  const article = document.querySelector("article");
173
176
  const toggleBtn = document.getElementById('toggle_btn');
174
177
  if (!toc || !article) return;
175
- if (window.innerWidth < 1024) {
178
+ if (window.innerWidth < 1024 && article.firstElementChild !== toc) {
176
179
  console.log('small screen!!')
177
- if (article.firstElementChild !== toc) {
178
- article.insertBefore(toc, article.firstChild);//移动节点,而非复制
179
- }
180
+
181
+ article.insertBefore(toc, article.firstChild);//移动节点,而非复制
182
+
180
183
  toggleBtn.style.display='none';
181
184
  // 替换样式:取消 fixed,设置适合正文的样式
182
185
  toc.style.position = 'relative';
@@ -195,7 +198,6 @@ hexo.extend.injector.register('body_end', () => {//生成静态页面时
195
198
  toc.style.boxShadow = 'none';
196
199
  toc.style.margin = '1rem';
197
200
  toc.style.paddingTop= '2rem';
198
-
199
201
  }
200
202
  }
201
203
  window.addEventListener("DOMContentLoaded", moveElementIfSmallScreen);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hexo-auto-toc",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Automatically generates a responsive table of contents that fixes to the side of the article page or above the articles, depending on the user's device.",
5
5
  "keywords": [
6
6
  "toc",