@shelf/table-of-contents 1.1.0 → 1.2.0
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/lib/helpers/anchorize.js
CHANGED
|
@@ -12,6 +12,7 @@ export function anchorize(src, settingsOverride) {
|
|
|
12
12
|
// Process HTML, "anchorizing" headers as-specified.
|
|
13
13
|
const headers = [];
|
|
14
14
|
const names = {};
|
|
15
|
+
// @ts-ignore
|
|
15
16
|
const html = src.replace(settingsOverride.headers, function (all, level, attrs, header) {
|
|
16
17
|
level = Number(level);
|
|
17
18
|
// @ts-ignore
|
|
@@ -8,29 +8,16 @@ const hasAnchorTagsInHeader = (data) => {
|
|
|
8
8
|
export const getDataWithoutNestedAnchors = (data, headerTemplate) => {
|
|
9
9
|
const results = headerTemplate(data);
|
|
10
10
|
if (!hasAnchorTagsInHeader(data)) {
|
|
11
|
-
return
|
|
11
|
+
return results;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
// get all content nodes from Toc pointer anchor node
|
|
25
|
-
const contentArray = Array.from(firstAnchorElement?.childNodes ?? []);
|
|
26
|
-
// remove all content nodes from Toc pointer anchor node
|
|
27
|
-
contentArray.forEach(node => {
|
|
28
|
-
firstAnchorElement?.removeChild(node);
|
|
29
|
-
});
|
|
30
|
-
if (firstAnchorElement) {
|
|
31
|
-
firstAnchorElement.innerHTML = '<wbr/>';
|
|
32
|
-
}
|
|
33
|
-
const newContentArray = [firstAnchorElement, ...contentArray];
|
|
34
|
-
parentNode?.replaceChildren(...newContentArray);
|
|
35
|
-
return parentNode?.outerHTML || results;
|
|
13
|
+
// separately parse the results to get the anchor tag
|
|
14
|
+
const doc = parser.parseFromString(data.all, 'text/html');
|
|
15
|
+
const anchor = document.createElement('a');
|
|
16
|
+
anchor.setAttribute('href', `#${data.anchor}`);
|
|
17
|
+
anchor.setAttribute('name', `${data.anchor}`);
|
|
18
|
+
anchor.innerHTML = '<wbr>';
|
|
19
|
+
const headerNode = doc.querySelector(`h${data.level}`);
|
|
20
|
+
headerNode?.prepend(anchor);
|
|
21
|
+
const html = doc.body.innerHTML;
|
|
22
|
+
return html || results;
|
|
36
23
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shelf/table-of-contents",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Fork of node-toc to reduce bundle size & rewritten in TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"lint:size": "size-limit",
|
|
25
25
|
"prepack": "yarn build",
|
|
26
26
|
"test": "TZ=UTC jest src",
|
|
27
|
+
"test:watch": "TZ=UTC jest src --watch",
|
|
27
28
|
"type-check": "tsc --noEmit",
|
|
28
29
|
"type-check:watch": "npm run type-check -- --watch"
|
|
29
30
|
},
|
|
@@ -55,12 +56,12 @@
|
|
|
55
56
|
"@types/lodash.template": "4.5.3",
|
|
56
57
|
"@types/node": "20.8.9",
|
|
57
58
|
"@types/slug": "5.0.7",
|
|
58
|
-
"eslint": "8.
|
|
59
|
+
"eslint": "8.53.0",
|
|
59
60
|
"husky": "8.0.3",
|
|
60
61
|
"jest": "29.7.0",
|
|
61
62
|
"jest-environment-jsdom": "^29.7.0",
|
|
62
|
-
"lint-staged": "15.0
|
|
63
|
-
"prettier": "3.0
|
|
63
|
+
"lint-staged": "15.1.0",
|
|
64
|
+
"prettier": "3.1.0",
|
|
64
65
|
"size-limit": "11.0.0",
|
|
65
66
|
"ts-jest-resolver": "2.0.1",
|
|
66
67
|
"typescript": "5.2.2"
|