ronds-metadata 1.1.47 → 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.
@@ -0,0 +1,13 @@
1
+ import './index.less';
2
+ interface IMdNavbarProps {
3
+ source: string;
4
+ ordered?: boolean;
5
+ headingTopOffset?: number;
6
+ updateHashAuto?: boolean;
7
+ declarative?: boolean;
8
+ className?: string;
9
+ onNavItemClick?: (event: any, element: HTMLDivElement, hashValue: string) => void;
10
+ onHashChange?: (newHash: string, oldHash: string) => void;
11
+ }
12
+ declare const MdNavbar: (props: IMdNavbarProps) => JSX.Element;
13
+ export default MdNavbar;
@@ -0,0 +1,180 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+
4
+ /*
5
+ * @Author: wangxian
6
+ * @Date: 2022-10-14 09:05:50
7
+ * @LastEditTime: 2022-10-14 14:25:18
8
+ */
9
+ import React from 'react';
10
+ import { getCurrentHashValue, trimArrZero } from './utils';
11
+ import './index.less';
12
+
13
+ var MdNavbar = function MdNavbar(props) {
14
+ var source = props.source,
15
+ ordered = props.ordered,
16
+ declarative = props.declarative,
17
+ className = props.className,
18
+ onNavItemClick = props.onNavItemClick,
19
+ onHashChange = props.onHashChange;
20
+
21
+ var _React$useState = React.useState([]),
22
+ _React$useState2 = _slicedToArray(_React$useState, 2),
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];
32
+
33
+ var scrollTimeoutRef = React.useRef();
34
+ var addTargetTimeoutRef = React.useRef();
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;
44
+ }
45
+
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
60
+
61
+ for (var i = 0; i < navData.length; i++) {
62
+ var t = navData[i];
63
+ var level = t.level;
64
+
65
+ while (matchStack.length && matchStack[matchStack.length - 1].level > level) {
66
+ matchStack.pop();
67
+ }
68
+
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;
84
+ matchStack.push({
85
+ level: level,
86
+ arr: newArr
87
+ });
88
+ t.listNo = trimArrZero(newArr).join('.');
89
+ }
90
+
91
+ setNavStructure(_toConsumableArray(navData));
92
+ navStructureRef.current = navData;
93
+ }
94
+ }, [source]);
95
+ var initHeadingsId = React.useCallback(function () {
96
+ var headingId = decodeURIComponent(declarative ? window.location.hash.replace(/^#/, '').trim() : (window.location.hash.match(/heading-\d+/g) || [])[0]);
97
+ navStructureRef.current.forEach(function (t) {
98
+ var headings = document.querySelectorAll("h".concat(t.level));
99
+ var curHeading = Array.prototype.slice.apply(headings).find(function (h) {
100
+ return h.innerText.trim() === t.text.trim() && (!h.dataset || !h.dataset.id);
101
+ });
102
+
103
+ if (curHeading) {
104
+ curHeading.dataset.id = declarative ? "".concat(t.listNo, "-").concat(t.text) : "heading-".concat(t.index);
105
+
106
+ if (headingId && headingId === curHeading.dataset.id) {
107
+ scrollToTarget(headingId);
108
+ setCurrentListNo(t.listNo);
109
+ }
110
+ }
111
+ });
112
+ }, []);
113
+ var refreshNav = React.useCallback(function () {
114
+ if (addTargetTimeoutRef.current) {
115
+ clearTimeout(addTargetTimeoutRef.current);
116
+ }
117
+
118
+ addTargetTimeoutRef.current = setTimeout(function () {
119
+ initHeadingsId();
120
+
121
+ if (navStructure.length) {
122
+ var listNo = navStructure[0].listNo;
123
+ setCurrentListNo(listNo);
124
+ }
125
+ }, 500);
126
+ }, [navStructure]);
127
+ React.useEffect(function () {
128
+ if (source) {
129
+ getNavStructure();
130
+ }
131
+ }, [source, getNavStructure]);
132
+ React.useEffect(function () {
133
+ if (source && navStructure && navStructure.length > 0) {
134
+ refreshNav();
135
+ }
136
+ }, [navStructure, source]);
137
+
138
+ var scrollToTarget = function scrollToTarget(dataId) {
139
+ if (scrollTimeoutRef.current) {
140
+ clearTimeout(scrollTimeoutRef.current);
141
+ }
142
+
143
+ scrollTimeoutRef.current = setTimeout(function () {
144
+ var target = document.querySelector("[data-id=\"".concat(dataId, "\"]"));
145
+
146
+ if (target) {
147
+ target.scrollIntoView({
148
+ block: 'start',
149
+ behavior: 'smooth'
150
+ });
151
+ }
152
+ }, 0);
153
+ };
154
+
155
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
156
+ className: "markdown-navigation ".concat(className)
157
+ }, navStructure.map(function (t) {
158
+ var cls = "title-anchor title-level".concat(t.level, " ").concat(currentListNo === t.listNo ? 'active' : '');
159
+ return /*#__PURE__*/React.createElement("div", {
160
+ className: cls,
161
+ onClick: function onClick(evt) {
162
+ var currentHash = declarative ? "".concat(t.listNo, "-").concat(t.text) // 加入listNo确保hash唯一ZZ
163
+ : "heading-".concat(t.index); // Avoid execution the callback `onHashChange` when clicking current nav item
164
+
165
+ if (t.listNo !== currentListNo) {
166
+ // Hash changing callback
167
+ onHashChange && onHashChange(currentHash, getCurrentHashValue());
168
+ } // Nav item clicking callback
169
+
170
+
171
+ onNavItemClick && onNavItemClick(evt, evt.target, currentHash);
172
+ scrollToTarget(currentHash);
173
+ setCurrentListNo(t.listNo);
174
+ },
175
+ key: "title_anchor_".concat(Math.random().toString(36).substring(2))
176
+ }, ordered ? /*#__PURE__*/React.createElement("small", null, t.listNo) : null, t.text);
177
+ })));
178
+ };
179
+
180
+ export default MdNavbar;
@@ -0,0 +1,77 @@
1
+ .markdown-navigation {
2
+ font-size: 14px;
3
+ font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'Helvetica', 'Arial', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
4
+ 'WenQuanYi Micro Hei', sans-serif;
5
+ width: 100%;
6
+ overflow-x: hidden;
7
+ overflow-y: scroll;
8
+ scrollbar-width: 0;
9
+ width: 200px;
10
+ }
11
+ .markdown-navigation::-webkit-scrollbar {
12
+ width: 0;
13
+ }
14
+
15
+ .markdown-navigation .title-anchor {
16
+ display: block;
17
+ color: #bbb;
18
+ transition: all 0.2s;
19
+ margin: 0.8em 0;
20
+ font-weight: lighter;
21
+ line-height: 2em;
22
+ padding-right: 1.8em;
23
+ cursor: pointer;
24
+ }
25
+
26
+ .markdown-navigation .title-anchor:hover,
27
+ .markdown-navigation .title-anchor.active {
28
+ background-color: #f8f8f8;
29
+ text-decoration: inherit;
30
+ }
31
+
32
+ .markdown-navigation .title-anchor.active {
33
+ color: #007fff;
34
+ }
35
+
36
+ .markdown-navigation .title-anchor small {
37
+ margin: 0 0.8em;
38
+ }
39
+
40
+ .markdown-navigation .title-level1 {
41
+ color: #000;
42
+ font-size: 1.2em;
43
+ padding-left: 1em;
44
+ font-weight: normal;
45
+ }
46
+
47
+ .markdown-navigation .title-level2 {
48
+ color: #000;
49
+ font-size: 1em;
50
+ padding-left: 1em;
51
+ font-weight: normal;
52
+ }
53
+
54
+ .markdown-navigation .title-level3 {
55
+ color: #000;
56
+ font-size: 0.8em;
57
+ padding-left: 3em;
58
+ font-weight: normal;
59
+ }
60
+
61
+ .markdown-navigation .title-level4 {
62
+ color: #000;
63
+ font-size: 0.72em;
64
+ padding-left: 5em;
65
+ }
66
+
67
+ .markdown-navigation .title-level5 {
68
+ color: #000;
69
+ font-size: 0.72em;
70
+ padding-left: 7em;
71
+ }
72
+
73
+ .markdown-navigation .title-level6 {
74
+ color: #000;
75
+ font-size: 0.72em;
76
+ padding-left: 9em;
77
+ }
@@ -0,0 +1,3 @@
1
+ export declare const getCurrentHashValue: () => string;
2
+ export declare const trimArrZero: (arr: any[]) => any[];
3
+ export declare const updateHash: (updateHashTimeout: any, value: string) => void;
@@ -0,0 +1,34 @@
1
+ /*
2
+ * @Author: wangxian
3
+ * @Date: 2022-10-14 10:03:03
4
+ * @LastEditTime: 2022-10-14 10:17:18
5
+ */
6
+ export var getCurrentHashValue = function getCurrentHashValue() {
7
+ return decodeURIComponent(window.location.hash.replace(/^#/, ''));
8
+ };
9
+ export var trimArrZero = function trimArrZero(arr) {
10
+ var start, end;
11
+
12
+ for (start = 0; start < arr.length; start++) {
13
+ if (arr[start]) {
14
+ break;
15
+ }
16
+ }
17
+
18
+ for (end = arr.length - 1; end >= 0; end--) {
19
+ if (arr[end]) {
20
+ break;
21
+ }
22
+ }
23
+
24
+ return arr.slice(start, end + 1);
25
+ };
26
+ export var updateHash = function updateHash(updateHashTimeout, value) {
27
+ if (updateHashTimeout) {
28
+ clearTimeout(updateHashTimeout);
29
+ }
30
+
31
+ updateHashTimeout = setTimeout(function () {
32
+ window.history.replaceState({}, '', "".concat(window.location.pathname).concat(window.location.search, "#").concat(value));
33
+ }, 0);
34
+ };
@@ -1,4 +1,3 @@
1
- import 'markdown-navbar/dist/navbar.css';
2
1
  import 'github-markdown-css';
3
2
  import './index.less';
4
3
  interface IMdViewProps {
@@ -4,15 +4,14 @@ import React from "react";
4
4
  /*
5
5
  * @Author: wangxian
6
6
  * @Date: 2022-09-02 16:17:38
7
- * @LastEditTime: 2022-10-13 17:19:32
7
+ * @LastEditTime: 2022-10-14 11:44:51
8
8
  */
9
9
  import ReactMarkdown from 'react-markdown';
10
10
  import remarkGfm from 'remark-gfm';
11
11
  import rehypeRaw from 'rehype-raw';
12
- import MarkNav from 'markdown-navbar';
13
- import 'markdown-navbar/dist/navbar.css';
14
12
  import 'github-markdown-css';
15
13
  import './index.less';
14
+ import MdNavbar from '../MdNavbar';
16
15
 
17
16
  var MdView = function MdView(props) {
18
17
  var source = props.source,
@@ -37,7 +36,7 @@ var MdView = function MdView(props) {
37
36
  padding: '10px',
38
37
  overflowY: 'auto'
39
38
  }
40
- }, /*#__PURE__*/React.createElement(MarkNav, _extends({
39
+ }, /*#__PURE__*/React.createElement(MdNavbar, _extends({
41
40
  className: "h-full w-full",
42
41
  source: source,
43
42
  ordered: true
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.1.47",
4
+ "version": "1.1.49",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",