ronds-metadata 1.1.48 → 1.1.49

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.
@@ -1,9 +1,10 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
1
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
3
 
3
4
  /*
4
5
  * @Author: wangxian
5
6
  * @Date: 2022-10-14 09:05:50
6
- * @LastEditTime: 2022-10-14 10:55:42
7
+ * @LastEditTime: 2022-10-14 14:25:18
7
8
  */
8
9
  import React from 'react';
9
10
  import { getCurrentHashValue, trimArrZero } from './utils';
@@ -17,73 +18,83 @@ var MdNavbar = function MdNavbar(props) {
17
18
  onNavItemClick = props.onNavItemClick,
18
19
  onHashChange = props.onHashChange;
19
20
 
20
- var _React$useState = React.useState(''),
21
+ var _React$useState = React.useState([]),
21
22
  _React$useState2 = _slicedToArray(_React$useState, 2),
22
- currentListNo = _React$useState2[0],
23
- setCurrentListNo = _React$useState2[1];
23
+ navStructure = _React$useState2[0],
24
+ setNavStructure = _React$useState2[1];
25
+
26
+ var navStructureRef = React.useRef([]);
27
+
28
+ var _React$useState3 = React.useState(''),
29
+ _React$useState4 = _slicedToArray(_React$useState3, 2),
30
+ currentListNo = _React$useState4[0],
31
+ setCurrentListNo = _React$useState4[1];
24
32
 
25
33
  var scrollTimeoutRef = React.useRef();
26
34
  var addTargetTimeoutRef = React.useRef();
27
- var navStructure = React.useMemo(function () {
28
- var contentWithoutCode = source.replace(/^[^#]+\n/g, '').replace(/(?:[^\n#]+)#+\s([^#\n]+)\n*/g, '') // 匹配行内出现 # 号的情况
29
- .replace(/^#\s[^#\n]*\n+/, '').replace(/```[^`\n]*\n+[^```]+```\n+/g, '').replace(/`([^`\n]+)`/g, '$1').replace(/\*\*?([^*\n]+)\*\*?/g, '$1').replace(/__?([^_\n]+)__?/g, '$1').trim();
30
- var pattOfTitle = /#+\s([^#\n]+)\n*/g;
31
- var matchResult = contentWithoutCode.match(pattOfTitle);
32
-
33
- if (!matchResult) {
34
- return [];
35
- }
36
-
37
- var navData = matchResult.map(function (r, i) {
38
- return {
39
- index: i,
40
- level: r.match(/^#+/g)[0].length,
41
- text: r.replace(pattOfTitle, '$1')
42
- };
43
- });
44
- var maxLevel = 0;
45
- navData.forEach(function (t) {
46
- if (t.level > maxLevel) {
47
- maxLevel = t.level;
35
+ var getNavStructure = React.useCallback(function () {
36
+ if (source) {
37
+ var contentWithoutCode = source.replace(/^[^#]+\n/g, '').replace(/(?:[^\n#]+)#+\s([^#\n]+)\n*/g, '') // 匹配行内出现 # 号的情况
38
+ .replace(/^#\s[^#\n]*\n+/, '').replace(/```[^`\n]*\n+[^```]+```\n+/g, '').replace(/`([^`\n]+)`/g, '$1').replace(/\*\*?([^*\n]+)\*\*?/g, '$1').replace(/__?([^_\n]+)__?/g, '$1').trim();
39
+ var pattOfTitle = /#+\s([^#\n]+)\n*/g;
40
+ var matchResult = contentWithoutCode.match(pattOfTitle);
41
+
42
+ if (!matchResult) {
43
+ return;
48
44
  }
49
- });
50
- var matchStack = []; // 此部分重构,原有方法会出现次级标题后再次出现高级标题时,listNo重复的bug
51
45
 
52
- for (var i = 0; i < navData.length; i++) {
53
- var t = navData[i];
54
- var level = t.level;
46
+ var navData = matchResult.map(function (r, i) {
47
+ return {
48
+ index: i,
49
+ level: r.match(/^#+/g)[0].length,
50
+ text: r.replace(pattOfTitle, '$1')
51
+ };
52
+ });
53
+ var maxLevel = 0;
54
+ navData.forEach(function (t) {
55
+ if (t.level > maxLevel) {
56
+ maxLevel = t.level;
57
+ }
58
+ });
59
+ var matchStack = []; // 此部分重构,原有方法会出现次级标题后再次出现高级标题时,listNo重复的bug
55
60
 
56
- while (matchStack.length && matchStack[matchStack.length - 1].level > level) {
57
- matchStack.pop();
58
- }
61
+ for (var i = 0; i < navData.length; i++) {
62
+ var t = navData[i];
63
+ var level = t.level;
59
64
 
60
- if (matchStack.length === 0) {
61
- var _arr = new Array(maxLevel).fill(0);
65
+ while (matchStack.length && matchStack[matchStack.length - 1].level > level) {
66
+ matchStack.pop();
67
+ }
62
68
 
63
- _arr[level - 1] += 1;
69
+ if (matchStack.length === 0) {
70
+ var _arr = new Array(maxLevel).fill(0);
71
+
72
+ _arr[level - 1] += 1;
73
+ matchStack.push({
74
+ level: level,
75
+ arr: _arr
76
+ });
77
+ t.listNo = trimArrZero(_arr).join('.');
78
+ continue;
79
+ }
80
+
81
+ var arr = matchStack[matchStack.length - 1].arr;
82
+ var newArr = arr.slice();
83
+ newArr[level - 1] += 1;
64
84
  matchStack.push({
65
85
  level: level,
66
- arr: _arr
86
+ arr: newArr
67
87
  });
68
- t.listNo = trimArrZero(_arr).join('.');
69
- continue;
88
+ t.listNo = trimArrZero(newArr).join('.');
70
89
  }
71
90
 
72
- var arr = matchStack[matchStack.length - 1].arr;
73
- var newArr = arr.slice();
74
- newArr[level - 1] += 1;
75
- matchStack.push({
76
- level: level,
77
- arr: newArr
78
- });
79
- t.listNo = trimArrZero(newArr).join('.');
91
+ setNavStructure(_toConsumableArray(navData));
92
+ navStructureRef.current = navData;
80
93
  }
81
-
82
- return navData;
83
94
  }, [source]);
84
95
  var initHeadingsId = React.useCallback(function () {
85
96
  var headingId = decodeURIComponent(declarative ? window.location.hash.replace(/^#/, '').trim() : (window.location.hash.match(/heading-\d+/g) || [])[0]);
86
- navStructure.forEach(function (t) {
97
+ navStructureRef.current.forEach(function (t) {
87
98
  var headings = document.querySelectorAll("h".concat(t.level));
88
99
  var curHeading = Array.prototype.slice.apply(headings).find(function (h) {
89
100
  return h.innerText.trim() === t.text.trim() && (!h.dataset || !h.dataset.id);
@@ -112,12 +123,17 @@ var MdNavbar = function MdNavbar(props) {
112
123
  setCurrentListNo(listNo);
113
124
  }
114
125
  }, 500);
115
- }, []);
126
+ }, [navStructure]);
116
127
  React.useEffect(function () {
117
- if (navStructure) {
128
+ if (source) {
129
+ getNavStructure();
130
+ }
131
+ }, [source, getNavStructure]);
132
+ React.useEffect(function () {
133
+ if (source && navStructure && navStructure.length > 0) {
118
134
  refreshNav();
119
135
  }
120
- }, [navStructure]);
136
+ }, [navStructure, source]);
121
137
 
122
138
  var scrollToTarget = function scrollToTarget(dataId) {
123
139
  if (scrollTimeoutRef.current) {
@@ -4,7 +4,7 @@ import React from "react";
4
4
  /*
5
5
  * @Author: wangxian
6
6
  * @Date: 2022-09-02 16:17:38
7
- * @LastEditTime: 2022-10-14 11:06:26
7
+ * @LastEditTime: 2022-10-14 11:44:51
8
8
  */
9
9
  import ReactMarkdown from 'react-markdown';
10
10
  import remarkGfm from 'remark-gfm';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.1.48",
4
+ "version": "1.1.49",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",