Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions front_end/src/components/markdown_editor/initialized_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,22 @@ const PrismCodeBlock: FC<{ code?: string; language?: string }> = ({
theme === "dark" ? prismThemes.dracula : prismThemes.github;

const raw = (language as string | undefined) ?? "ts";
const prismLang = CANONICAL_TO_PRISM[normalizeLang(raw)] ?? "tsx";
const normalizedLang = normalizeLang(raw);
const codeTrimmed = (code ?? "").replace(/^\n+|\n+$/g, "");

// Handle plain text without syntax highlighting
if (normalizedLang === "text") {
return (
<div className="my-4">
<pre className="overflow-x-auto rounded border border-gray-300 bg-gray-100 p-3 dark:border-gray-300-dark dark:bg-gray-100-dark">
{codeTrimmed}
</pre>
</div>
);
}

const prismLang = CANONICAL_TO_PRISM[normalizedLang] ?? "tsx";

return (
<div className="my-4">
<Highlight
Expand Down Expand Up @@ -256,7 +269,7 @@ const InitializedMarkdownEditor: FC<
}
return [
...common,
codeBlockPlugin({ defaultCodeBlockLanguage: "ts" }),
codeBlockPlugin({ defaultCodeBlockLanguage: "text" }),
codeMirrorPlugin({
codeBlockLanguages: CANONICAL_TO_LABEL,
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Language } from "prism-react-renderer";

export const CANONICAL_TO_LABEL = {
text: "Plain text",
ts: "TypeScript",
tsx: "TSX",
js: "JavaScript",
Expand All @@ -14,6 +15,10 @@ export const CANONICAL_TO_LABEL = {
export type CanonicalLang = keyof typeof CANONICAL_TO_LABEL;

const ALIAS_TO_CANONICAL: Record<string, CanonicalLang> = {
text: "text",
plaintext: "text",
plain: "text",

typescript: "ts",
ts: "ts",
tsx: "tsx",
Expand Down Expand Up @@ -42,6 +47,7 @@ export function normalizeLang(
}

export const CANONICAL_TO_PRISM: Record<CanonicalLang, Language> = {
text: "text" as Language,
ts: "typescript",
tsx: "tsx",
js: "javascript",
Expand Down
Loading