Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates header links script to handle duplicate header texts. #1263

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const config = args => {
plugins,
},
{
input: "./src/components/EditorContent/navigableHeadings.js",
input: "./src/components/EditorContent/headerLinks.js",
output: {
dir: `${__dirname}/dist`,
format: "cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/components/EditorContent/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export const VARIABLE_SPAN_REGEX =
export const LANGUAGE_LIST = [...lowlight.listLanguages(), "html"];

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

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

if (editorContent) makeHeadingsNavigable(editorContent);
if (editorContent) buildHeaderLinks(editorContent);
});
6 changes: 3 additions & 3 deletions src/components/EditorContent/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
applyLineHighlighting,
applySyntaxHighlighting,
} from "./utils";
import { makeHeadingsNavigable } from "./utils/headers";
import { buildHeaderLinks } from "./utils/headers";

const EditorContent = ({
content = "",
Expand Down Expand Up @@ -81,8 +81,8 @@ const EditorContent = ({
injectCopyButtonToCodeBlocks();
bindImageClickListener();
applyLineHighlighting(editorContentRef.current);
configuration.navigableHeader &&
makeHeadingsNavigable(editorContentRef.current);
configuration.enableHeaderLinks &&
buildHeaderLinks(editorContentRef.current);
}, [content]);

return (
Expand Down
11 changes: 9 additions & 2 deletions src/components/EditorContent/utils/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ const convertTextToId = text =>
.replace(/[^a-z0-9\s]/g, "")
.replace(/\s+/g, "-");

export const makeHeadingsNavigable = editorContentNode => {
export const buildHeaderLinks = editorContentNode => {
const headerTags = editorContentNode.querySelectorAll(
"h1, h2, h3, h4, h5, h6"
);
const usedIds = new Map();

headerTags.forEach(heading => {
const headingId = convertTextToId(heading.textContent);
let headingId = convertTextToId(heading.textContent);
if (usedIds.has(headingId)) {
const count = usedIds.get(headingId);
usedIds.set(headingId, count + 1);
headingId = `${headingId}-${count}`;
} else usedIds.set(headingId, 1);

heading.setAttribute("id", headingId);

const anchor = document.createElement("a");
Expand Down
2 changes: 1 addition & 1 deletion stories/Walkthroughs/Output/EditorContentDemo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const EditorContentDemo = () => {
<EditorContent
{...{ content }}
className="neeto-ui-p-4"
configuration={{ navigableHeader: true }}
configuration={{ enableHeaderLinks: true }}
/>
</div>
<div>
Expand Down
4 changes: 2 additions & 2 deletions stories/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const EDITOR_CONTENT_PROP_TABLE_ROWS = [
],
[
"configuration",
"Accepts an object, navigableHeader can be set to true or false in the configuration, default value is false.",
`{navigableHeader: true}`,
"Accepts an object, enableHeaderLinks can be set to true or false in the configuration, default value is false.",
`{enableHeaderLinks: true}`,
],
];

Expand Down
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ interface AttachmentsProps {
}

type EditorContentConfigType = {
navigableHeader?: boolean;
enableHeaderLinks?: boolean;
}

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