@shelf/table-of-contents 1.1.0 → 1.3.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/default-settings.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import defaults from 'lodash.defaults';
|
|
2
|
+
const DEFAULT_TEMPLATE = '<h<%= level %><%= attrs %>><a href="#<%= anchor %>" name="<%= anchor %>"><wbr /></a><%= header %></h<%= level %>>';
|
|
2
3
|
export const DEFAULT_SETTINGS = {
|
|
3
4
|
// DEFAULTS FOR toc.process()
|
|
4
5
|
//
|
|
@@ -15,7 +16,7 @@ export const DEFAULT_SETTINGS = {
|
|
|
15
16
|
anchorMin: 2,
|
|
16
17
|
anchorMax: 6,
|
|
17
18
|
// Anchorized header template.
|
|
18
|
-
header:
|
|
19
|
+
header: DEFAULT_TEMPLATE,
|
|
19
20
|
// DEFAULTS FOR toc.toc()
|
|
20
21
|
//
|
|
21
22
|
// TOC part templates.
|
package/lib/helpers/anchorize.js
CHANGED
|
@@ -3,7 +3,6 @@ import { getSettings } from '../default-settings.js';
|
|
|
3
3
|
import { untag } from './untag.js';
|
|
4
4
|
import { unique } from './unique.js';
|
|
5
5
|
import { anchor } from './anchor.js';
|
|
6
|
-
import { getDataWithoutNestedAnchors } from './getDataWithoutNestedAnchors.js';
|
|
7
6
|
// Parse HTML, returning an array of header objects and anchorized HTML.
|
|
8
7
|
export function anchorize(src, settingsOverride) {
|
|
9
8
|
// Normalize options and compile template(s).
|
|
@@ -12,6 +11,7 @@ export function anchorize(src, settingsOverride) {
|
|
|
12
11
|
// Process HTML, "anchorizing" headers as-specified.
|
|
13
12
|
const headers = [];
|
|
14
13
|
const names = {};
|
|
14
|
+
// @ts-ignore
|
|
15
15
|
const html = src.replace(settingsOverride.headers, function (all, level, attrs, header) {
|
|
16
16
|
level = Number(level);
|
|
17
17
|
// @ts-ignore
|
|
@@ -40,7 +40,7 @@ export function anchorize(src, settingsOverride) {
|
|
|
40
40
|
headers.push(data);
|
|
41
41
|
}
|
|
42
42
|
if (anchorLevel) {
|
|
43
|
-
return
|
|
43
|
+
return headerTemplate(data);
|
|
44
44
|
}
|
|
45
45
|
return all;
|
|
46
46
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shelf/table-of-contents",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.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"
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const parser = new DOMParser();
|
|
2
|
-
const hasAnchorTagsInHeader = (data) => {
|
|
3
|
-
const doc = parser.parseFromString(data.all, 'text/html');
|
|
4
|
-
// Extract any existing anchor tags from the header variable
|
|
5
|
-
const anchorElement = doc.querySelector('a');
|
|
6
|
-
return anchorElement?.outerHTML;
|
|
7
|
-
};
|
|
8
|
-
export const getDataWithoutNestedAnchors = (data, headerTemplate) => {
|
|
9
|
-
const results = headerTemplate(data);
|
|
10
|
-
if (!hasAnchorTagsInHeader(data)) {
|
|
11
|
-
return headerTemplate(data);
|
|
12
|
-
}
|
|
13
|
-
const doc = parser.parseFromString(results, 'text/xml');
|
|
14
|
-
// get Toc pointer anchor node
|
|
15
|
-
const firstAnchorElement = doc.querySelector(`a[href="#${data.anchor}"]`);
|
|
16
|
-
const parentNode = firstAnchorElement?.parentElement;
|
|
17
|
-
// EDGECASE - other anchors in this header
|
|
18
|
-
const otherAnchorElements = doc?.querySelectorAll(`a:not([href="#${data.anchor}"]):empty`);
|
|
19
|
-
if (otherAnchorElements && otherAnchorElements?.length > 0) {
|
|
20
|
-
otherAnchorElements.forEach(node => {
|
|
21
|
-
node.remove();
|
|
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;
|
|
36
|
-
};
|