2023-01-07 22:33:50 +01:00
|
|
|
import generated_pygments_data from "../generated/pygments_data.json";
|
2023-02-22 23:03:47 +01:00
|
|
|
import * as typeahead from "../shared/src/typeahead";
|
2021-05-08 08:04:23 +02:00
|
|
|
|
|
|
|
import {$t} from "./i18n";
|
2021-04-24 10:00:28 +02:00
|
|
|
import * as typeahead_helper from "./typeahead_helper";
|
|
|
|
|
2021-04-13 08:43:03 +02:00
|
|
|
const map_language_to_playground_info = new Map();
|
2021-05-08 07:46:15 +02:00
|
|
|
const map_pygments_pretty_name_to_aliases = new Map();
|
2021-04-13 08:43:03 +02:00
|
|
|
|
|
|
|
export function update_playgrounds(playgrounds_data) {
|
|
|
|
map_language_to_playground_info.clear();
|
|
|
|
|
|
|
|
for (const data of Object.values(playgrounds_data)) {
|
|
|
|
const element_to_push = {
|
|
|
|
id: data.id,
|
|
|
|
name: data.name,
|
|
|
|
url_prefix: data.url_prefix,
|
|
|
|
};
|
|
|
|
if (map_language_to_playground_info.has(data.pygments_language)) {
|
|
|
|
map_language_to_playground_info.get(data.pygments_language).push(element_to_push);
|
|
|
|
} else {
|
|
|
|
map_language_to_playground_info.set(data.pygments_language, [element_to_push]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function get_playground_info_for_languages(lang) {
|
2021-04-17 11:39:02 +02:00
|
|
|
return map_language_to_playground_info.get(lang);
|
2021-04-13 08:43:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 10:00:28 +02:00
|
|
|
export function sort_pygments_pretty_names_by_priority(generated_pygments_data) {
|
|
|
|
const priority_sorted_pygments_data = Object.keys(generated_pygments_data.langs).sort(
|
2023-01-09 00:43:32 +01:00
|
|
|
typeahead_helper.compare_language,
|
2021-04-24 10:00:28 +02:00
|
|
|
);
|
2021-05-08 07:46:15 +02:00
|
|
|
for (const alias of priority_sorted_pygments_data) {
|
|
|
|
const pretty_name = generated_pygments_data.langs[alias].pretty_name;
|
|
|
|
// JS Map remembers the original order of insertion of keys.
|
|
|
|
if (map_pygments_pretty_name_to_aliases.has(pretty_name)) {
|
|
|
|
map_pygments_pretty_name_to_aliases.get(pretty_name).push(alias);
|
|
|
|
} else {
|
|
|
|
map_pygments_pretty_name_to_aliases.set(pretty_name, [alias]);
|
2021-04-24 10:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 22:33:50 +01:00
|
|
|
// This gets the candidate list for showing autocomplete for a code block in
|
|
|
|
// the composebox. The candidate list will include pygments data as well as any
|
|
|
|
// Code Playgrounds.
|
|
|
|
//
|
|
|
|
// May return duplicates, since it's common for playground languages
|
|
|
|
// to also be pygments languages! retain_unique_language_aliases will
|
|
|
|
// deduplicate them.
|
|
|
|
export function get_pygments_typeahead_list_for_composebox() {
|
|
|
|
const playground_pygment_langs = [...map_language_to_playground_info.keys()];
|
|
|
|
const generated_pygment_langs = Object.keys(generated_pygments_data.langs);
|
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
return [...playground_pygment_langs, ...generated_pygment_langs];
|
2023-01-07 22:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This gets the candidate list for showing autocomplete in settings when
|
2023-01-10 08:25:35 +01:00
|
|
|
// adding a new Code Playground.
|
2023-01-07 20:45:48 +01:00
|
|
|
export function get_pygments_typeahead_list_for_settings(query) {
|
2021-05-10 06:22:35 +02:00
|
|
|
const language_labels = new Map();
|
2021-05-08 07:46:15 +02:00
|
|
|
|
2021-05-08 08:04:23 +02:00
|
|
|
// Adds a typeahead that allows selecting a custom language, by adding a
|
|
|
|
// "Custom language" label in the first position of the typeahead list.
|
|
|
|
const clean_query = typeahead.clean_query_lowercase(query);
|
2021-05-08 11:12:15 +02:00
|
|
|
if (clean_query !== "") {
|
2021-05-10 06:22:35 +02:00
|
|
|
language_labels.set(
|
|
|
|
clean_query,
|
|
|
|
$t({defaultMessage: "Custom language: {query}"}, {query: clean_query}),
|
2021-05-08 11:12:15 +02:00
|
|
|
);
|
|
|
|
}
|
2021-05-08 08:04:23 +02:00
|
|
|
|
2023-01-10 08:25:35 +01:00
|
|
|
const playground_pygment_langs = [...map_language_to_playground_info.keys()];
|
|
|
|
for (const lang of playground_pygment_langs) {
|
|
|
|
language_labels.set(lang, $t({defaultMessage: "Custom language: {query}"}, {query: lang}));
|
|
|
|
}
|
|
|
|
|
2021-05-08 07:46:15 +02:00
|
|
|
for (const [key, values] of map_pygments_pretty_name_to_aliases) {
|
2023-03-02 01:58:25 +01:00
|
|
|
language_labels.set(key, key + " (" + values.join(", ") + ")");
|
2021-05-08 07:46:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 06:22:35 +02:00
|
|
|
return language_labels;
|
2021-04-24 10:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initialize(playground_data, generated_pygments_data) {
|
2021-04-13 08:43:03 +02:00
|
|
|
update_playgrounds(playground_data);
|
2021-04-24 10:00:28 +02:00
|
|
|
sort_pygments_pretty_names_by_priority(generated_pygments_data);
|
2021-04-13 08:43:03 +02:00
|
|
|
}
|