@valaxyjs/utils 0.27.0 → 0.28.0-beta.2
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.
- package/dist/index.mjs +6 -8
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -5,10 +5,10 @@ function buildTree(data, min, max) {
|
|
|
5
5
|
const stack = [];
|
|
6
6
|
data.forEach((item) => {
|
|
7
7
|
const node = { ...item, children: [] };
|
|
8
|
-
let parent = stack
|
|
8
|
+
let parent = stack.at(-1);
|
|
9
9
|
while (parent && parent.level >= node.level) {
|
|
10
10
|
stack.pop();
|
|
11
|
-
parent = stack
|
|
11
|
+
parent = stack.at(-1);
|
|
12
12
|
}
|
|
13
13
|
if (node.element.classList.contains("ignore-header") || parent && "shouldIgnore" in parent) {
|
|
14
14
|
stack.push({ level: node.level, shouldIgnore: true });
|
|
@@ -31,8 +31,7 @@ function addToParent(currIndex, headers, levelsRange) {
|
|
|
31
31
|
for (let index = currIndex - 1; index >= 0; index--) {
|
|
32
32
|
const header = headers[index];
|
|
33
33
|
if (header.level < currentHeader.level && header.level >= levelsRange[0] && header.level <= levelsRange[1]) {
|
|
34
|
-
|
|
35
|
-
header.children = [];
|
|
34
|
+
header.children ??= [];
|
|
36
35
|
header.children.push(currentHeader);
|
|
37
36
|
return false;
|
|
38
37
|
}
|
|
@@ -46,7 +45,7 @@ function resolveHeaders(headers, range = [2, 4]) {
|
|
|
46
45
|
}
|
|
47
46
|
function serializeHeader(h) {
|
|
48
47
|
let ret = "";
|
|
49
|
-
for (const node of
|
|
48
|
+
for (const node of [...h.childNodes]) {
|
|
50
49
|
if (node.nodeType === 1) {
|
|
51
50
|
if (node.classList.contains("VABadge") || node.classList.contains("header-anchor")) {
|
|
52
51
|
continue;
|
|
@@ -64,15 +63,14 @@ function getHeaders(options = {
|
|
|
64
63
|
}) {
|
|
65
64
|
const mdBodySelector = options.selector || ".markdown-body";
|
|
66
65
|
const markdownBodyElements = document.querySelectorAll(mdBodySelector);
|
|
67
|
-
const markdownBody =
|
|
68
|
-
const headers =
|
|
66
|
+
const markdownBody = [...markdownBodyElements].at(-1);
|
|
67
|
+
const headers = [...markdownBody?.querySelectorAll(`${mdBodySelector} :where(h1,h2,h3,h4,h5,h6)`) || []].filter((el) => options.filter ? options.filter(el) : true).map((el) => {
|
|
69
68
|
const level = Number(el.tagName[1]);
|
|
70
69
|
return {
|
|
71
70
|
element: el,
|
|
72
71
|
title: serializeHeader(el),
|
|
73
72
|
link: `#${el.id}`,
|
|
74
73
|
level,
|
|
75
|
-
// @ts-expect-error lang
|
|
76
74
|
lang: el.lang
|
|
77
75
|
};
|
|
78
76
|
});
|