@shelf/table-of-contents 1.0.3 → 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.
@@ -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
@@ -1,3 +1,3 @@
1
- import type { TemplateExecutor } from 'lodash';
1
+ import { type TemplateExecutor } from 'lodash';
2
2
  import type { Header } from '../types.js';
3
3
  export declare const getDataWithoutNestedAnchors: (data: Header, headerTemplate: TemplateExecutor) => string;
@@ -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 headerTemplate(data);
11
+ return results;
12
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;
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.0.3",
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
  },
@@ -46,22 +47,22 @@
46
47
  "@shelf/eslint-config": "3.10.0",
47
48
  "@shelf/prettier-config": "1.0.0",
48
49
  "@shelf/tsconfig": "0.0.11",
49
- "@size-limit/esbuild-why": "10.0.1",
50
- "@size-limit/preset-small-lib": "10.0.1",
51
- "@swc/core": "1.3.95",
50
+ "@size-limit/esbuild-why": "11.0.0",
51
+ "@size-limit/preset-small-lib": "11.0.0",
52
+ "@swc/core": "1.3.96",
52
53
  "@swc/jest": "0.2.29",
53
- "@types/jest": "29.5.6",
54
- "@types/lodash.defaults": "4.2.8",
55
- "@types/lodash.template": "4.5.2",
54
+ "@types/jest": "29.5.8",
55
+ "@types/lodash.defaults": "4.2.9",
56
+ "@types/lodash.template": "4.5.3",
56
57
  "@types/node": "20.8.9",
57
- "@types/slug": "5.0.6",
58
- "eslint": "8.52.0",
58
+ "@types/slug": "5.0.7",
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.2",
63
- "prettier": "3.0.3",
64
- "size-limit": "10.0.1",
63
+ "lint-staged": "15.1.0",
64
+ "prettier": "3.1.0",
65
+ "size-limit": "11.0.0",
65
66
  "ts-jest-resolver": "2.0.1",
66
67
  "typescript": "5.2.2"
67
68
  },