@shelf/table-of-contents 1.2.0 → 1.4.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,4 @@
|
|
|
1
|
-
|
|
1
|
+
const DEFAULT_TEMPLATE = '<h<%= level %><%= attrs %>><a href="#<%= anchor %>" name="<%= anchor %>"><wbr /></a><%= header %></h<%= level %>>';
|
|
2
2
|
export const DEFAULT_SETTINGS = {
|
|
3
3
|
// DEFAULTS FOR toc.process()
|
|
4
4
|
//
|
|
@@ -15,7 +15,7 @@ export const DEFAULT_SETTINGS = {
|
|
|
15
15
|
anchorMin: 2,
|
|
16
16
|
anchorMax: 6,
|
|
17
17
|
// Anchorized header template.
|
|
18
|
-
header:
|
|
18
|
+
header: DEFAULT_TEMPLATE,
|
|
19
19
|
// DEFAULTS FOR toc.toc()
|
|
20
20
|
//
|
|
21
21
|
// TOC part templates.
|
|
@@ -28,5 +28,5 @@ export const DEFAULT_SETTINGS = {
|
|
|
28
28
|
TOC: '<div class="toc"><%= toc %></div>',
|
|
29
29
|
};
|
|
30
30
|
export function getSettings(settingsOverride) {
|
|
31
|
-
return
|
|
31
|
+
return { ...DEFAULT_SETTINGS, ...settingsOverride };
|
|
32
32
|
}
|
package/lib/helpers/anchorize.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import template from 'lodash
|
|
1
|
+
import { template } from 'lodash-es';
|
|
2
2
|
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).
|
|
@@ -41,7 +40,7 @@ export function anchorize(src, settingsOverride) {
|
|
|
41
40
|
headers.push(data);
|
|
42
41
|
}
|
|
43
42
|
if (anchorLevel) {
|
|
44
|
-
return
|
|
43
|
+
return headerTemplate(data);
|
|
45
44
|
}
|
|
46
45
|
return all;
|
|
47
46
|
});
|
package/lib/helpers/toc.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shelf/table-of-contents",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Fork of node-toc to reduce bundle size & rewritten in TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -38,33 +38,31 @@
|
|
|
38
38
|
},
|
|
39
39
|
"prettier": "@shelf/prettier-config",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"entities": "
|
|
42
|
-
"lodash
|
|
43
|
-
"
|
|
44
|
-
"slug": "8.2.3"
|
|
41
|
+
"entities": "5.0.0",
|
|
42
|
+
"lodash-es": "4.17.21",
|
|
43
|
+
"slug": "9.1.0"
|
|
45
44
|
},
|
|
46
45
|
"devDependencies": {
|
|
47
|
-
"@shelf/eslint-config": "3.
|
|
46
|
+
"@shelf/eslint-config": "3.15.1",
|
|
48
47
|
"@shelf/prettier-config": "1.0.0",
|
|
49
|
-
"@shelf/tsconfig": "0.0
|
|
50
|
-
"@size-limit/esbuild-why": "11.
|
|
51
|
-
"@size-limit/preset-small-lib": "11.
|
|
52
|
-
"@swc/core": "1.3
|
|
53
|
-
"@swc/jest": "0.2.
|
|
54
|
-
"@types/jest": "29.5.
|
|
55
|
-
"@types/lodash
|
|
56
|
-
"@types/lodash.template": "4.5.3",
|
|
48
|
+
"@shelf/tsconfig": "0.1.0",
|
|
49
|
+
"@size-limit/esbuild-why": "11.1.6",
|
|
50
|
+
"@size-limit/preset-small-lib": "11.1.6",
|
|
51
|
+
"@swc/core": "1.9.3",
|
|
52
|
+
"@swc/jest": "0.2.37",
|
|
53
|
+
"@types/jest": "29.5.14",
|
|
54
|
+
"@types/lodash-es": "4.17.12",
|
|
57
55
|
"@types/node": "20.8.9",
|
|
58
|
-
"@types/slug": "5.0.
|
|
59
|
-
"eslint": "8.
|
|
60
|
-
"husky": "
|
|
56
|
+
"@types/slug": "5.0.9",
|
|
57
|
+
"eslint": "8.57.1",
|
|
58
|
+
"husky": "9.1.7",
|
|
61
59
|
"jest": "29.7.0",
|
|
62
60
|
"jest-environment-jsdom": "^29.7.0",
|
|
63
|
-
"lint-staged": "15.
|
|
64
|
-
"prettier": "3.1
|
|
65
|
-
"size-limit": "11.
|
|
61
|
+
"lint-staged": "15.2.10",
|
|
62
|
+
"prettier": "3.4.1",
|
|
63
|
+
"size-limit": "11.1.6",
|
|
66
64
|
"ts-jest-resolver": "2.0.1",
|
|
67
|
-
"typescript": "5.
|
|
65
|
+
"typescript": "5.7.2"
|
|
68
66
|
},
|
|
69
67
|
"engines": {
|
|
70
68
|
"node": ">=14"
|
|
@@ -1,23 +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 results;
|
|
12
|
-
}
|
|
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;
|
|
23
|
-
};
|