supermemory/apps/web/app/(editor)/lib/debouncedsave.ts
2024-06-11 07:45:21 +05:30

20 lines
No EOL
832 B
TypeScript

import hljs from 'highlight.js'
import { debounce } from 'tldraw';
import { useDebouncedCallback } from "use-debounce";
export const Updates = debounce(({editor, setCharsCount, setSaveStatus})=> {
const json = editor.getJSON();
setCharsCount(editor.storage.characterCount.words());
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");
}, 500)
export const highlightCodeblocks = (content: string) => {
const doc = new DOMParser().parseFromString(content, 'text/html');
doc.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
return new XMLSerializer().serializeToString(doc);
};