@mdxeditor/editor 3.52.5 → 3.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -529,16 +529,24 @@ export declare interface CodeBlockEditorProps {
529
529
  }
530
530
 
531
531
  /**
532
- * The codemirror code block languages.
532
+ * A code block language entry with a name and optional aliases/extensions.
533
+ * Compatible with CodeMirror's `LanguageDescription` from `@codemirror/language-data`.
533
534
  * @group CodeMirror
534
535
  */
535
- export declare const codeBlockLanguages$: NodeRef<{
536
- js: string;
537
- ts: string;
538
- tsx: string;
539
- jsx: string;
540
- css: string;
541
- }>;
536
+ export declare interface CodeBlockLanguage {
537
+ /** Display name shown in the language select dropdown. */
538
+ name: string;
539
+ /** Alternative identifiers for this language (e.g. `["js"]` for JavaScript). */
540
+ alias?: readonly string[];
541
+ /** File extensions associated with this language (e.g. `["js", "mjs"]`). */
542
+ extensions?: readonly string[];
543
+ }
544
+
545
+ /**
546
+ * The normalized code block languages used by the CodeMirror editor.
547
+ * @group CodeMirror
548
+ */
549
+ export declare const codeBlockLanguages$: NodeRef<NormalizedCodeBlockLanguages>;
542
550
 
543
551
  /**
544
552
  * A lexical node that represents a fenced code block. Use {@link "$createCodeBlockNode"} to construct one.
@@ -607,7 +615,13 @@ export declare const codeMirrorExtensions$: NodeRef<Extension_2[]>;
607
615
  * @group CodeMirror
608
616
  */
609
617
  export declare const codeMirrorPlugin: (params?: {
610
- codeBlockLanguages: Record<string, string>;
618
+ /**
619
+ * The code block languages to display in the language select dropdown.
620
+ * Accepts either a `Record<string, string>` (key → label) for backwards compatibility,
621
+ * or an array of {@link CodeBlockLanguage} objects (compatible with CodeMirror's `LanguageDescription`
622
+ * from `@codemirror/language-data`), which supports aliases and file extensions.
623
+ */
624
+ codeBlockLanguages: Record<string, string> | CodeBlockLanguage[];
611
625
  /**
612
626
  * Optional, additional CodeMirror extensions to load in the diff/source mode.
613
627
  */
@@ -2501,6 +2515,26 @@ export declare interface NewImageDialogState {
2501
2515
  */
2502
2516
  export declare function noop(): void;
2503
2517
 
2518
+ /**
2519
+ * Normalizes the `codeBlockLanguages` parameter into a canonical form.
2520
+ * Accepts either a `Record<string, string>` (legacy) or a `CodeBlockLanguage[]` array.
2521
+ */
2522
+ export declare function normalizeCodeBlockLanguages(input: Record<string, string> | CodeBlockLanguage[]): NormalizedCodeBlockLanguages;
2523
+
2524
+ /**
2525
+ * Internal normalized representation of code block languages.
2526
+ * @group CodeMirror
2527
+ */
2528
+ export declare interface NormalizedCodeBlockLanguages {
2529
+ /** Items for the language select dropdown. */
2530
+ items: {
2531
+ value: string;
2532
+ label: string;
2533
+ }[];
2534
+ /** Maps any known key (canonical, alias, extension) to the canonical key. */
2535
+ keyMap: Record<string, string>;
2536
+ }
2537
+
2504
2538
  /**
2505
2539
  * Emits when the editor loses focus
2506
2540
  * @group Core
package/dist/index.js CHANGED
@@ -18,7 +18,7 @@ import { insertJsx$, isMdastJsxNode, jsxPlugin } from "./plugins/jsx/index.js";
18
18
  import { GenericJsxEditor } from "./jsx-editors/GenericJsxEditor.js";
19
19
  import { insertSandpack$, sandpackConfig$, sandpackPlugin } from "./plugins/sandpack/index.js";
20
20
  import { SandpackEditor } from "./plugins/sandpack/SandpackEditor.js";
21
- import { codeBlockLanguages$, codeMirrorAutoLoadLanguageSupport$, codeMirrorExtensions$, codeMirrorPlugin, insertCodeMirror$ } from "./plugins/codemirror/index.js";
21
+ import { codeBlockLanguages$, codeMirrorAutoLoadLanguageSupport$, codeMirrorExtensions$, codeMirrorPlugin, insertCodeMirror$, normalizeCodeBlockLanguages } from "./plugins/codemirror/index.js";
22
22
  import { COMMON_STATE_CONFIG_EXTENSIONS, CodeMirrorEditor } from "./plugins/codemirror/CodeMirrorEditor.js";
23
23
  import { appendCodeBlockEditorDescriptor$, codeBlockPlugin, defaultCodeBlockLanguage$, insertCodeBlock$ } from "./plugins/codeblock/index.js";
24
24
  import { directivesPlugin, insertDirective$ } from "./plugins/directives/index.js";
@@ -305,6 +305,7 @@ export {
305
305
  muteChange$,
306
306
  nestedEditorChildren$,
307
307
  noop,
308
+ normalizeCodeBlockLanguages,
308
309
  onBlur$,
309
310
  onClickLinkCallback$,
310
311
  onReadOnlyClickLinkCallback$,
@@ -89,7 +89,7 @@ const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }) => {
89
89
  Select,
90
90
  {
91
91
  disabled: readOnly,
92
- value: language,
92
+ value: codeBlockLanguages.keyMap[language] ?? language,
93
93
  onChange: (language2) => {
94
94
  parentEditor.update(() => {
95
95
  lexicalNode.setLanguage(language2 === EMPTY_VALUE ? "" : language2);
@@ -102,7 +102,7 @@ const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }) => {
102
102
  },
103
103
  triggerTitle: t("codeBlock.selectLanguage", "Select code block language"),
104
104
  placeholder: t("codeBlock.inlineLanguage", "Language"),
105
- items: Object.entries(codeBlockLanguages).map(([value, label]) => ({ value: value ? value : EMPTY_VALUE, label }))
105
+ items: codeBlockLanguages.items
106
106
  }
107
107
  ), /* @__PURE__ */ React__default.createElement(
108
108
  "button",
@@ -2,13 +2,48 @@ import { realmPlugin } from "../../RealmWithPlugins.js";
2
2
  import { Cell, Signal, map } from "@mdxeditor/gurx";
3
3
  import { insertCodeBlock$, appendCodeBlockEditorDescriptor$ } from "../codeblock/index.js";
4
4
  import { CodeMirrorEditor } from "./CodeMirrorEditor.js";
5
- const codeBlockLanguages$ = Cell({
6
- js: "JavaScript",
7
- ts: "TypeScript",
8
- tsx: "TypeScript (React)",
9
- jsx: "JavaScript (React)",
10
- css: "CSS"
11
- });
5
+ function normalizeCodeBlockLanguages(input) {
6
+ var _a;
7
+ const items = [];
8
+ const keyMap = {};
9
+ if (Array.isArray(input)) {
10
+ for (const lang of input) {
11
+ const canonical = ((_a = lang.alias) == null ? void 0 : _a[0]) ?? lang.name.toLowerCase();
12
+ items.push({ value: canonical, label: lang.name });
13
+ keyMap[canonical] = canonical;
14
+ if (lang.alias) {
15
+ for (const alias of lang.alias) {
16
+ keyMap[alias] = canonical;
17
+ }
18
+ }
19
+ if (lang.extensions) {
20
+ for (const ext of lang.extensions) {
21
+ keyMap[ext] = canonical;
22
+ }
23
+ }
24
+ keyMap[lang.name.toLowerCase()] = canonical;
25
+ }
26
+ } else {
27
+ const firstKeyByLabel = {};
28
+ for (const [key, label] of Object.entries(input)) {
29
+ if (!(label in firstKeyByLabel)) {
30
+ firstKeyByLabel[label] = key;
31
+ items.push({ value: key || "__EMPTY_VALUE__", label });
32
+ }
33
+ keyMap[key] = firstKeyByLabel[label] || "__EMPTY_VALUE__";
34
+ }
35
+ }
36
+ return { items, keyMap };
37
+ }
38
+ const codeBlockLanguages$ = Cell(
39
+ normalizeCodeBlockLanguages({
40
+ js: "JavaScript",
41
+ ts: "TypeScript",
42
+ tsx: "TypeScript (React)",
43
+ jsx: "JavaScript (React)",
44
+ css: "CSS"
45
+ })
46
+ );
12
47
  const insertCodeMirror$ = Signal((r) => {
13
48
  r.link(
14
49
  r.pipe(
@@ -29,24 +64,25 @@ const codeMirrorAutoLoadLanguageSupport$ = Cell(true);
29
64
  const codeMirrorPlugin = realmPlugin({
30
65
  update(r, params) {
31
66
  r.pubIn({
32
- [codeBlockLanguages$]: params == null ? void 0 : params.codeBlockLanguages,
67
+ [codeBlockLanguages$]: normalizeCodeBlockLanguages((params == null ? void 0 : params.codeBlockLanguages) ?? {}),
33
68
  [codeMirrorExtensions$]: (params == null ? void 0 : params.codeMirrorExtensions) ?? [],
34
69
  [codeMirrorAutoLoadLanguageSupport$]: (params == null ? void 0 : params.autoLoadLanguageSupport) ?? true
35
70
  });
36
71
  },
37
72
  init(r, params) {
73
+ const normalized = normalizeCodeBlockLanguages((params == null ? void 0 : params.codeBlockLanguages) ?? {});
38
74
  r.pubIn({
39
- [codeBlockLanguages$]: params == null ? void 0 : params.codeBlockLanguages,
75
+ [codeBlockLanguages$]: normalized,
40
76
  [codeMirrorExtensions$]: (params == null ? void 0 : params.codeMirrorExtensions) ?? [],
41
- [appendCodeBlockEditorDescriptor$]: buildCodeBlockDescriptor((params == null ? void 0 : params.codeBlockLanguages) ?? {}),
77
+ [appendCodeBlockEditorDescriptor$]: buildCodeBlockDescriptor(normalized),
42
78
  [codeMirrorAutoLoadLanguageSupport$]: (params == null ? void 0 : params.autoLoadLanguageSupport) ?? true
43
79
  });
44
80
  }
45
81
  });
46
- function buildCodeBlockDescriptor(codeBlockLanguages) {
82
+ function buildCodeBlockDescriptor(normalized) {
47
83
  return {
48
84
  match(language, meta) {
49
- return Object.hasOwn(codeBlockLanguages, language ?? "") && !meta;
85
+ return (language ?? "") in normalized.keyMap && !meta;
50
86
  },
51
87
  priority: 1,
52
88
  Editor: CodeMirrorEditor
@@ -57,5 +93,6 @@ export {
57
93
  codeMirrorAutoLoadLanguageSupport$,
58
94
  codeMirrorExtensions$,
59
95
  codeMirrorPlugin,
60
- insertCodeMirror$
96
+ insertCodeMirror$,
97
+ normalizeCodeBlockLanguages
61
98
  };
@@ -13,7 +13,8 @@ const ChangeCodeMirrorLanguage = () => {
13
13
  if (!codeBlockNode) {
14
14
  return null;
15
15
  }
16
- let currentLanguage = codeBlockNode.getLanguage();
16
+ const rawLanguage = codeBlockNode.getLanguage();
17
+ let currentLanguage = codeBlockLanguages.keyMap[rawLanguage] ?? rawLanguage;
17
18
  if (currentLanguage === "") {
18
19
  currentLanguage = EMPTY_VALUE;
19
20
  }
@@ -33,7 +34,7 @@ const ChangeCodeMirrorLanguage = () => {
33
34
  },
34
35
  triggerTitle: t("codeBlock.selectLanguage", "Select code block language"),
35
36
  placeholder: t("codeBlock.language", "Code block language"),
36
- items: Object.entries(codeBlockLanguages).map(([value, label]) => ({ value: value ? value : EMPTY_VALUE, label }))
37
+ items: codeBlockLanguages.items
37
38
  }
38
39
  ));
39
40
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "*.css"
12
12
  ],
13
13
  "type": "module",
14
- "version": "3.52.5",
14
+ "version": "3.53.0",
15
15
  "description": "React component for rich text markdown editing",
16
16
  "scripts": {
17
17
  "build": "vite build",