Files
edgeever/apps/web/src/lib/code-block.ts
T

46 lines
1.5 KiB
TypeScript

import { common, createLowlight } from "lowlight";
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
import { ReactNodeViewRenderer } from "@tiptap/react";
import { MermaidCodeBlock } from "@/components/MermaidCodeBlock";
export const codeBlockLowlight = createLowlight(common);
export const EdgeEverCodeBlock = CodeBlockLowlight.extend({
addNodeView() {
return ReactNodeViewRenderer(MermaidCodeBlock);
},
});
export const CODE_BLOCK_LANGUAGES = [
{ value: "plaintext", label: "Plain text" },
{ value: "bash", label: "Bash" },
{ value: "c", label: "C" },
{ value: "csharp", label: "C#" },
{ value: "cpp", label: "C++" },
{ value: "css", label: "CSS" },
{ value: "go", label: "Go" },
{ value: "html", label: "HTML" },
{ value: "java", label: "Java" },
{ value: "javascript", label: "JavaScript" },
{ value: "json", label: "JSON" },
{ value: "kotlin", label: "Kotlin" },
{ value: "markdown", label: "Markdown" },
{ value: "mermaid", label: "Mermaid" },
{ value: "php", label: "PHP" },
{ value: "python", label: "Python" },
{ value: "ruby", label: "Ruby" },
{ value: "rust", label: "Rust" },
{ value: "sql", label: "SQL" },
{ value: "swift", label: "Swift" },
{ value: "typescript", label: "TypeScript" },
{ value: "yaml", label: "YAML" },
] as const;
export const getCodeBlockLanguageValue = (language: unknown) => {
if (typeof language !== "string" || !language) {
return "plaintext";
}
return CODE_BLOCK_LANGUAGES.some((option) => option.value === language) ? language : "plaintext";
};