Updates code language handling for code blocks

Replaces the previous implementation of updating
the `data-language` attribute on code blocks with a more
robust solution using Lexical's MutationListener. This ensures
that the language attribute is correctly updated when CodeNode
changes occur. Also includes an initial setup to update all
existing code blocks.
This commit is contained in:
dahyeon committed 2025-11-07 13:42:16 +09:00
1 parent b0140fbafb
commit f5520411db
1 file changed
+54 -23
+54 -23
View File
@@ -1,43 +1,74 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CodeNode } from '@lexical/code';
import { $isCodeNode } from '@lexical/code';
export function CodeLanguagePlugin() { export function CodeLanguagePlugin() {
const [editor] = useLexicalComposerContext(); const [editor] = useLexicalComposerContext();
useEffect(() => { useEffect(() => {
// Update data-language attribute on code blocks // Use Lexical's MutationListener to safely update DOM when CodeNode changes
const updateCodeLanguages = () => { const removeMutationListener = editor.registerMutationListener(CodeNode, (mutatedNodes) => {
editor.getEditorState().read(() => { editor.getEditorState().read(() => {
const codeBlocks = document.querySelectorAll('[contenteditable="true"] pre, .lexical-viewer pre'); for (const [nodeKey, mutation] of mutatedNodes) {
codeBlocks.forEach((block) => { // Handle created or updated nodes
if (block instanceof HTMLElement) { if (mutation === 'created' || mutation === 'updated') {
// Get the language from the CodeNode if available const node = editor.getElementByKey(nodeKey);
const codeElement = block.querySelector('code'); if (node instanceof HTMLElement) {
if (codeElement && codeElement.classList.length > 0) {
const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-')); const language = node.getAttribute("data-highlight-language"); //className.replace('data-highlight-language', '');
if (languageClass) { node.setAttribute('data-language', language!);
const language = languageClass.replace('language-', ''); /**
block.setAttribute('data-language', language); * 더 좋은 방법이 있을 때까지..
} else if (!block.hasAttribute('data-language')) { */
block.setAttribute('data-language', 'plain text'); // const codeElement = node.querySelector('code');
// if (codeElement && codeElement.classList.length > 0) {
// const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-'));
// if (languageClass) {
// const language = languageClass.replace('language-', '');
// node.setAttribute('data-language', language);
// } else {
// node.setAttribute('data-language', 'plain text');
// }
// } else {
// node.setAttribute('data-language', 'plain text');
// }
} }
} else if (!block.hasAttribute('data-language')) {
block.setAttribute('data-language', 'plain text');
} }
} }
}); });
}); });
};
// Initial update // Initial setup: update all existing code blocks
setTimeout(updateCodeLanguages, 100); editor.getEditorState().read(() => {
const root = editor.getRootElement();
if (!root) return;
// Update on editor changes editor.getEditorState()._nodeMap.forEach((node) => {
const unregister = editor.registerUpdateListener(() => { if ($isCodeNode(node)) {
setTimeout(updateCodeLanguages, 50); const element = editor.getElementByKey(node.__key);
if (element instanceof HTMLElement) {
console.log(element)
const language = element.getAttribute("data-highlight-language"); //className.replace('data-highlight-language', '');
element.setAttribute('data-language', language!);
// const codeElement = element.querySelector('code');
// if (codeElement && codeElement.classList.length > 0) {
// const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-'));
// if (languageClass) {
// const language = languageClass.replace('language-', '');
// element.setAttribute('data-language', language);
// } else {
// element.setAttribute('data-language', 'plain text');
// }
// } else {
// element.setAttribute('data-language', 'plain text');
// }
}
}
});
}); });
return unregister; return removeMutationListener;
}, [editor]); }, [editor]);
return null; return null;