MCPcopy Index your code
hub / github.com/ChatGPTNextWeb/NextChat / PreCode

Function PreCode

app/components/markdown.tsx:74–174  ·  view source on GitHub ↗
(props: { children: any })

Source from the content-addressed store, hash-verified

72}
73
74export function PreCode(props: { children: any }) {
75 const ref = useRef<HTMLPreElement>(null);
76 const previewRef = useRef<HTMLPreviewHandler>(null);
77 const [mermaidCode, setMermaidCode] = useState("");
78 const [htmlCode, setHtmlCode] = useState("");
79 const { height } = useWindowSize();
80 const chatStore = useChatStore();
81 const session = chatStore.currentSession();
82
83 const renderArtifacts = useDebouncedCallback(() => {
84 if (!ref.current) return;
85 const mermaidDom = ref.current.querySelector("code.language-mermaid");
86 if (mermaidDom) {
87 setMermaidCode((mermaidDom as HTMLElement).innerText);
88 }
89 const htmlDom = ref.current.querySelector("code.language-html");
90 const refText = ref.current.querySelector("code")?.innerText;
91 if (htmlDom) {
92 setHtmlCode((htmlDom as HTMLElement).innerText);
93 } else if (
94 refText?.startsWith("<!DOCTYPE") ||
95 refText?.startsWith("<svg") ||
96 refText?.startsWith("<?xml")
97 ) {
98 setHtmlCode(refText);
99 }
100 }, 600);
101
102 const config = useAppConfig();
103 const enableArtifacts =
104 session.mask?.enableArtifacts !== false && config.enableArtifacts;
105
106 //Wrap the paragraph for plain-text
107 useEffect(() => {
108 if (ref.current) {
109 const codeElements = ref.current.querySelectorAll(
110 "code",
111 ) as NodeListOf<HTMLElement>;
112 const wrapLanguages = [
113 "",
114 "md",
115 "markdown",
116 "text",
117 "txt",
118 "plaintext",
119 "tex",
120 "latex",
121 ];
122 codeElements.forEach((codeElement) => {
123 let languageClass = codeElement.className.match(/language-(\w+)/);
124 let name = languageClass ? languageClass[1] : "";
125 if (wrapLanguages.includes(name)) {
126 codeElement.style.whiteSpace = "pre-wrap";
127 }
128 });
129 setTimeout(renderArtifacts, 1);
130 }
131 }, []);

Callers

nothing calls this directly

Calls 2

useWindowSizeFunction · 0.90
copyToClipboardFunction · 0.90

Tested by

no test coverage detected