feat(highlight): add syntax highlighting
- Add syntax highlighting with [highlight.js](https://highlightjs.org/) Signed-off-by: minicx <minicx@disroot.org>
This commit is contained in:
parent
2c5ca94b3c
commit
901c27a3f1
4 changed files with 39 additions and 0 deletions
27
ui/components/CodeBlock.tsx
Normal file
27
ui/components/CodeBlock.tsx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import React, { useEffect, useRef } from 'react';
|
||||
import hljs from 'highlight.js';
|
||||
|
||||
const CodeBlock = ({
|
||||
children,
|
||||
className,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}) => {
|
||||
const ref = useRef<HTMLElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (ref.current && className?.includes('lang-')) {
|
||||
hljs.highlightElement(ref.current);
|
||||
// hljs won't reprocess the element unless this attribute is removed
|
||||
ref.current.removeAttribute('data-highlighted');
|
||||
}
|
||||
}, [children, className]);
|
||||
return (
|
||||
<code className={className} ref={ref}>
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
};
|
||||
|
||||
export default CodeBlock;
|
||||
Loading…
Add table
Add a link
Reference in a new issue