Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

2.5 KB
Raw Blame
import { createCodeBlockLowlight } from "./code-block-highlighting";
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
import type { Editor } from "@tiptap/core";
import { ReactNodeViewRenderer } from "@tiptap/react";
import { MermaidCodeBlock } from "@/components/MermaidCodeBlock";
import { NoteProperties } from "@/components/editor/NoteProperties";
import { FRONT_MATTER_LANGUAGE } from "@edgeever/shared";
import { createElement } from "react";

export const codeBlockLowlight = createCodeBlockLowlight();

export const selectCurrentCodeBlockContent = (editor: Editor) => {
  const { $from, $to, from, to } = editor.state.selection;
  if ($from.parent !== $to.parent || $from.parent.type.name !== "codeBlock") {
    return false;
  }

  const blockFrom = $from.start();
  const blockTo = $from.end();
  if (from === blockFrom && to === blockTo) {
    return false;
  }

  return editor.commands.setTextSelection({ from: blockFrom, to: blockTo });
};

export const EdgeEverCodeBlock = CodeBlockLowlight.extend({
  addKeyboardShortcuts() {
    return {
      ...this.parent?.(),
      "Mod-a": () => selectCurrentCodeBlockContent(this.editor),
    };
  },

  addNodeView() {
    return ReactNodeViewRenderer((props) => createElement(
      props.node.attrs.language === FRONT_MATTER_LANGUAGE ? NoteProperties : MermaidCodeBlock, props,
    ));
  },
});

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";
};