Skip to content

Commit

Permalink
Merge pull request #395 from MODSetter/codeblock-html-fix
Browse files Browse the repository at this point in the history
fix: codeblock-lowlight doesn't render in html output
  • Loading branch information
andrewdoro committed May 19, 2024
2 parents 74ebbc0 + de5c2e5 commit f4a49dc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/web/components/tailwind/advanced-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { uploadFn } from "./image-upload";
import { TextButtons } from "./selectors/text-buttons";
import { slashCommand, suggestionItems } from "./slash-command";

const hljs = require('highlight.js');

const extensions = [...defaultExtensions, slashCommand];

const TailwindAdvancedEditor = () => {
Expand All @@ -37,10 +39,21 @@ const TailwindAdvancedEditor = () => {
const [openLink, setOpenLink] = useState(false);
const [openAI, setOpenAI] = useState(false);

//Apply Codeblock Highlighting on the HTML from editor.getHTML()
const highlightCodeblocks = (content: string) => {
const doc = new DOMParser().parseFromString(content, 'text/html');
doc.querySelectorAll('pre code').forEach((el) => {
// @ts-ignore
// https://highlightjs.readthedocs.io/en/latest/api.html?highlight=highlightElement#highlightelement
hljs.highlightElement(el);
});
return new XMLSerializer().serializeToString(doc);
};

const debouncedUpdates = useDebouncedCallback(async (editor: EditorInstance) => {
const json = editor.getJSON();
setCharsCount(editor.storage.characterCount.words());
window.localStorage.setItem("html-content", editor.getHTML());
window.localStorage.setItem("html-content", highlightCodeblocks(editor.getHTML()));
window.localStorage.setItem("novel-content", JSON.stringify(json));
window.localStorage.setItem("markdown", editor.storage.markdown.getMarkdown());
setSaveStatus("Saved");
Expand Down

0 comments on commit f4a49dc

Please sign in to comment.