1 import {StreamLanguage} from '@codemirror/language';
3 import {css} from '@codemirror/lang-css';
4 import {json} from '@codemirror/lang-json';
5 import {javascript} from '@codemirror/lang-javascript';
6 import {html} from '@codemirror/lang-html';
7 import {markdown} from '@codemirror/lang-markdown';
8 import {php} from '@codemirror/lang-php';
9 import {twig} from '@ssddanbrown/codemirror-lang-twig';
10 import {xml} from '@codemirror/lang-xml';
12 const legacyLoad = async mode => {
13 const modes = await window.importVersioned('legacy-modes');
14 return StreamLanguage.define(modes[mode]);
17 // Mapping of possible languages or formats from user input to their codemirror modes.
18 // Value can be a mode string or a function that will receive the code content & return the mode string.
19 // The function option is used in the event the exact mode could be dynamic depending on the code.
21 bash: () => legacyLoad('shell'),
22 c: () => legacyLoad('c'),
23 css: async () => css(),
24 'c++': () => legacyLoad('cpp'),
25 'c#': () => legacyLoad('csharp'),
26 clj: () => legacyLoad('clojure'),
27 clojure: () => legacyLoad('clojure'),
28 csharp: () => legacyLoad('csharp'),
29 dart: () => legacyLoad('dart'),
30 diff: () => legacyLoad('diff'),
31 for: () => legacyLoad('fortran'),
32 fortran: () => legacyLoad('fortran'),
33 'f#': () => legacyLoad('fSharp'),
34 fsharp: () => legacyLoad('fSharp'),
35 go: () => legacyLoad('go'),
36 groovy: () => legacyLoad('groovy'),
37 haskell: () => legacyLoad('haskell'),
38 hs: () => legacyLoad('haskell'),
39 html: async () => html({selfClosingTags: true}),
40 ini: () => legacyLoad('properties'),
41 java: () => legacyLoad('java'),
42 javascript: async () => javascript(),
43 json: async () => json(),
44 js: async () => javascript(),
45 jl: () => legacyLoad('julia'),
46 julia: () => legacyLoad('julia'),
47 kotlin: () => legacyLoad('kotlin'),
48 latex: () => legacyLoad('stex'),
49 lua: () => legacyLoad('lua'),
50 markdown: async () => markdown(),
51 matlab: () => legacyLoad('octave'),
52 md: async () => markdown(),
53 mdown: async () => markdown(),
54 ml: () => legacyLoad('sml'),
55 mssql: () => legacyLoad('msSQL'),
56 mysql: () => legacyLoad('mySQL'),
57 nginx: () => legacyLoad('nginx'),
58 octave: () => legacyLoad('octave'),
59 pas: () => legacyLoad('pascal'),
60 pascal: () => legacyLoad('pascal'),
61 perl: () => legacyLoad('perl'),
62 pgsql: () => legacyLoad('pgSQL'),
64 const hasTags = code.includes('<?php');
65 return php({plain: !hasTags});
67 pl: () => legacyLoad('perl'),
68 'pl/sql': () => legacyLoad('plSQL'),
69 postgresql: () => legacyLoad('pgSQL'),
70 powershell: () => legacyLoad('powerShell'),
71 properties: () => legacyLoad('properties'),
72 ocaml: () => legacyLoad('oCaml'),
73 py: () => legacyLoad('python'),
74 python: () => legacyLoad('python'),
75 r: () => legacyLoad('r'),
76 rb: () => legacyLoad('ruby'),
77 rs: () => legacyLoad('rust'),
78 ruby: () => legacyLoad('ruby'),
79 rust: () => legacyLoad('rust'),
80 sas: () => legacyLoad('sas'),
81 scala: () => legacyLoad('scala'),
82 scheme: () => legacyLoad('scheme'),
83 shell: () => legacyLoad('shell'),
84 sh: () => legacyLoad('shell'),
85 smarty: () => legacyLoad('smarty'),
86 stext: () => legacyLoad('stex'),
87 swift: () => legacyLoad('swift'),
88 toml: () => legacyLoad('toml'),
89 ts: async () => javascript({typescript: true}),
90 twig: async () => twig(),
91 typescript: async () => javascript({typescript: true}),
92 sql: () => legacyLoad('standardSQL'),
93 sqlite: () => legacyLoad('sqlite'),
94 vbs: () => legacyLoad('vbScript'),
95 vbscript: () => legacyLoad('vbScript'),
96 'vb.net': () => legacyLoad('vb'),
97 vbnet: () => legacyLoad('vb'),
98 xml: async () => xml(),
99 yaml: () => legacyLoad('yaml'),
100 yml: () => legacyLoad('yaml'),
104 * Get the relevant codemirror language extension based upon the given language
105 * suggestion and content.
106 * @param {String} langSuggestion
107 * @param {String} content
108 * @returns {Promise<StreamLanguage|LanguageSupport>}
110 export function getLanguageExtension(langSuggestion, content) {
111 const suggestion = langSuggestion.trim().replace(/^\./g, '').toLowerCase();
113 const language = modeMap[suggestion];
115 if (typeof language === 'undefined') {
119 return language(content);