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

Sets the editor content decorator script helpers in the window object. #1290

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
57 changes: 34 additions & 23 deletions src/components/EditorContent/codeBlockHighlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,45 @@ const addCopyToClipboardButton = codeElement => {
codeElement.parentNode.appendChild(copyButton);
};

function applyCodeblockDecorations(codeElement) {
const preElement = codeElement.closest("pre");
hljs.highlightElement(codeElement);
addCopyToClipboardButton(codeElement);

const linesToHighlight = getHighlightedLines(preElement, codeElement);
const highlightLinesOptions = linesToHighlight
.filter(line => line > 0)
.map(line => ({
start: line,
end: line,
color: "rgba(255, 255, 0, 0.2)",
}));
hljs.highlightLinesElement(codeElement, highlightLinesOptions);
}

const fetchHighlightLinesScript = () => {
const applyDecorations = () => {
document.querySelectorAll("pre code").forEach(codeElement => {
const preElement = codeElement.closest("pre");
hljs.highlightElement(codeElement);
addCopyToClipboardButton(codeElement);

const linesToHighlight = getHighlightedLines(preElement, codeElement);
const highlightLinesOptions = linesToHighlight
.filter(line => line > 0)
.map(line => ({
start: line,
end: line,
color: "rgba(255, 255, 0, 0.2)",
}));
hljs.highlightLinesElement(codeElement, highlightLinesOptions);
});
};

const checkContextAndApplyDecorations = () => {
// Don't have to load the script if the `hljs.highlightLinesElement` is in the context.
if (hljs.highlightLinesElement) return applyDecorations();

const script = document.createElement("script");
script.src = highlightLinesScriptCDNPath;
script.async = true;
document.head.appendChild(script);
script.addEventListener("load", () => {
document.querySelectorAll("pre code").forEach(applyCodeblockDecorations);
});

return script.addEventListener("load", applyDecorations);
};

(function () {
(() => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fetchHighlightLinesScript);
} else fetchHighlightLinesScript();
document.addEventListener(
"DOMContentLoaded",
checkContextAndApplyDecorations
);
} else checkContextAndApplyDecorations();

window.neetoEditor = window.neetoEditor ?? {};
window.neetoEditor.applyCodeblockDecorations =
checkContextAndApplyDecorations;
})();
14 changes: 12 additions & 2 deletions src/components/EditorContent/headerLinks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// eslint-disable-next-line @bigbinary/neeto/file-name-and-export-name-standards
import { buildHeaderLinks } from "./utils/headers";

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

if (editorContent) buildHeaderLinks(editorContent);
});
};

(() => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", applyDecorations);
} else applyDecorations();

window.neetoEditor = window.neetoEditor ?? {};
window.neetoEditor.applyHeaderDecorations = applyDecorations;
})();
14 changes: 14 additions & 0 deletions stories/Walkthroughs/Output/Output.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ file.
></script>
```

This script exposes the helper function `applyCodeblockDecorations` via the window object. You can use it to apply syntax highlighting.
```react
useEffect(() => {
window.neetoEditor?.applyCodeblockDecorations?.()
}, [])
```

#### Make headings navigable in NextJS websites

Add the following lines to the SSG/SSR pages. The pages need not be converted to client side rendering for this to work.
Expand All @@ -122,3 +129,10 @@ Add the following lines to the SSG/SSR pages. The pages need not be converted to
strategy="beforeInteractive"
/>
```

This script exposes the helper function `applyHeaderDecorations` via the window object. You can use it to make the headers navigable.
```react
useEffect(() => {
window.neetoEditor?.applyHeaderDecorations?.()
}, [])
```
Loading