Skip to content

Commit 27750b8

Browse files
authored
Updates header links script to handle duplicate header texts. (#1263)
* Updated the buildHeaderLinks to consider duplicate headings. * Renamed navigableHeader to headerLinks to make simple and consistent across the products.
1 parent a3a71a6 commit 27750b8

File tree

8 files changed

+20
-13
lines changed

8 files changed

+20
-13
lines changed

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const config = args => {
141141
plugins,
142142
},
143143
{
144-
input: "./src/components/EditorContent/navigableHeadings.js",
144+
input: "./src/components/EditorContent/headerLinks.js",
145145
output: {
146146
dir: `${__dirname}/dist`,
147147
format: "cjs",

src/components/EditorContent/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ export const VARIABLE_SPAN_REGEX =
1616
export const LANGUAGE_LIST = [...lowlight.listLanguages(), "html"];
1717

1818
export const EDITOR_CONTENT_DEFAULT_CONFIGURATION = {
19-
navigableHeader: false,
19+
enableHeaderLinks: false,
2020
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { makeHeadingsNavigable } from "./utils/headers";
1+
import { buildHeaderLinks } from "./utils/headers";
22

33
document.addEventListener("DOMContentLoaded", () => {
44
const editorContent = document.querySelector(".neeto-editor-content");
55

6-
if (editorContent) makeHeadingsNavigable(editorContent);
6+
if (editorContent) buildHeaderLinks(editorContent);
77
});

src/components/EditorContent/index.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
applyLineHighlighting,
2121
applySyntaxHighlighting,
2222
} from "./utils";
23-
import { makeHeadingsNavigable } from "./utils/headers";
23+
import { buildHeaderLinks } from "./utils/headers";
2424

2525
const EditorContent = ({
2626
content = "",
@@ -81,8 +81,8 @@ const EditorContent = ({
8181
injectCopyButtonToCodeBlocks();
8282
bindImageClickListener();
8383
applyLineHighlighting(editorContentRef.current);
84-
configuration.navigableHeader &&
85-
makeHeadingsNavigable(editorContentRef.current);
84+
configuration.enableHeaderLinks &&
85+
buildHeaderLinks(editorContentRef.current);
8686
}, [content]);
8787

8888
return (

src/components/EditorContent/utils/headers.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ const convertTextToId = text =>
2828
.replace(/[^a-z0-9\s]/g, "")
2929
.replace(/\s+/g, "-");
3030

31-
export const makeHeadingsNavigable = editorContentNode => {
31+
export const buildHeaderLinks = editorContentNode => {
3232
const headerTags = editorContentNode.querySelectorAll(
3333
"h1, h2, h3, h4, h5, h6"
3434
);
35+
const usedIds = new Map();
3536

3637
headerTags.forEach(heading => {
37-
const headingId = convertTextToId(heading.textContent);
38+
let headingId = convertTextToId(heading.textContent);
39+
if (usedIds.has(headingId)) {
40+
const count = usedIds.get(headingId);
41+
usedIds.set(headingId, count + 1);
42+
headingId = `${headingId}-${count}`;
43+
} else usedIds.set(headingId, 1);
44+
3845
heading.setAttribute("id", headingId);
3946

4047
const anchor = document.createElement("a");

stories/Walkthroughs/Output/EditorContentDemo.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const EditorContentDemo = () => {
1919
<EditorContent
2020
{...{ content }}
2121
className="neeto-ui-p-4"
22-
configuration={{ navigableHeader: true }}
22+
configuration={{ enableHeaderLinks: true }}
2323
/>
2424
</div>
2525
<div>

stories/constants.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const EDITOR_CONTENT_PROP_TABLE_ROWS = [
4242
],
4343
[
4444
"configuration",
45-
"Accepts an object, navigableHeader can be set to true or false in the configuration, default value is false.",
46-
`{navigableHeader: true}`,
45+
"Accepts an object, enableHeaderLinks can be set to true or false in the configuration, default value is false.",
46+
`{enableHeaderLinks: true}`,
4747
],
4848
];
4949

types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ interface AttachmentsProps {
150150
}
151151

152152
type EditorContentConfigType = {
153-
navigableHeader?: boolean;
153+
enableHeaderLinks?: boolean;
154154
}
155155

156156
export function Editor(props: EditorProps): JSX.Element;

0 commit comments

Comments
 (0)