@shelf/table-of-contents 1.2.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).
|
|
@@ -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/package.json
CHANGED
|
@@ -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
|
-
};
|