1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Mobile,Desktop: Fixes #9971: Fix auto-indentation in some types of code blocks (#9972)

This commit is contained in:
Henry Heino 2024-02-22 07:35:54 -08:00 committed by GitHub
parent de03679be0
commit 2e3783f1c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 41 additions and 15 deletions

View File

@ -588,6 +588,9 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
packages/editor/CodeMirror/editorCommands/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.js
packages/editor/CodeMirror/getScrollFraction.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/lookUpLanguage.js
packages/editor/CodeMirror/markdown/computeSelectionFormatting.test.js
packages/editor/CodeMirror/markdown/computeSelectionFormatting.js
packages/editor/CodeMirror/markdown/decoratorExtension.test.js
@ -600,7 +603,6 @@ packages/editor/CodeMirror/markdown/markdownMathParser.test.js
packages/editor/CodeMirror/markdown/markdownMathParser.js
packages/editor/CodeMirror/markdown/markdownReformatter.test.js
packages/editor/CodeMirror/markdown/markdownReformatter.js
packages/editor/CodeMirror/markdown/syntaxHighlightingLanguages.js
packages/editor/CodeMirror/pluginApi/PluginLoader.js
packages/editor/CodeMirror/pluginApi/codeMirrorRequire.js
packages/editor/CodeMirror/testUtil/createEditorSettings.js

4
.gitignore vendored
View File

@ -568,6 +568,9 @@ packages/editor/CodeMirror/editorCommands/insertLineAfter.js
packages/editor/CodeMirror/editorCommands/supportsCommand.js
packages/editor/CodeMirror/editorCommands/swapLine.js
packages/editor/CodeMirror/getScrollFraction.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/allLanguages.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/defaultLanguage.js
packages/editor/CodeMirror/markdown/codeBlockLanguages/lookUpLanguage.js
packages/editor/CodeMirror/markdown/computeSelectionFormatting.test.js
packages/editor/CodeMirror/markdown/computeSelectionFormatting.js
packages/editor/CodeMirror/markdown/decoratorExtension.test.js
@ -580,7 +583,6 @@ packages/editor/CodeMirror/markdown/markdownMathParser.test.js
packages/editor/CodeMirror/markdown/markdownMathParser.js
packages/editor/CodeMirror/markdown/markdownReformatter.test.js
packages/editor/CodeMirror/markdown/markdownReformatter.js
packages/editor/CodeMirror/markdown/syntaxHighlightingLanguages.js
packages/editor/CodeMirror/pluginApi/PluginLoader.js
packages/editor/CodeMirror/pluginApi/codeMirrorRequire.js
packages/editor/CodeMirror/testUtil/createEditorSettings.js

View File

@ -6,7 +6,7 @@ import { EditorState } from '@codemirror/state';
import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
import { GFM as GitHubFlavoredMarkdownExtension } from '@lezer/markdown';
import { MarkdownMathExtension } from './markdown/markdownMathParser';
import syntaxHighlightingLanguages from './markdown/syntaxHighlightingLanguages';
import lookUpLanguage from './markdown/codeBlockLanguages/lookUpLanguage';
import { html } from '@codemirror/lang-html';
import { defaultKeymap, emacsStyleKeymap } from '@codemirror/commands';
import { vim } from '@replit/codemirror-vim';
@ -26,7 +26,7 @@ const configFromSettings = (settings: EditorSettings) => {
// Don't highlight KaTeX if the user disabled it
settings.katexEnabled ? MarkdownMathExtension : [],
],
codeLanguages: syntaxHighlightingLanguages,
codeLanguages: lookUpLanguage,
}),
markdownLanguage.data.of({ closeBrackets: openingBrackets }),
];

View File

@ -1,7 +1,3 @@
//
// Exports a list of languages that can be used in fenced code blocks.
//
import { LanguageDescription } from '@codemirror/language';
import { languages } from '@codemirror/language-data';
@ -13,13 +9,13 @@ const additionalAliases: Record<string, string[]> = {
// Convert supportedLanguages to a CodeMirror-readable list
// of LanguageDescriptions
const syntaxHighlightingLanguages: LanguageDescription[] = [];
export const allLanguages: LanguageDescription[] = [];
for (const language of languages) {
const languageId = language.name.toLowerCase();
if (additionalAliases.hasOwnProperty(languageId)) {
syntaxHighlightingLanguages.push(LanguageDescription.of({
allLanguages.push(LanguageDescription.of({
name: language.name,
alias: [...language.alias, ...additionalAliases[languageId]],
extensions: language.extensions,
@ -28,8 +24,8 @@ for (const language of languages) {
load: () => language.load.call(language),
}));
} else {
syntaxHighlightingLanguages.push(language);
allLanguages.push(language);
}
}
export default syntaxHighlightingLanguages;
export default allLanguages;

View File

@ -0,0 +1,18 @@
import { LanguageDescription, LanguageSupport, StreamLanguage } from '@codemirror/language';
// To allow auto-indent to work in an unrecognised language, we define an
// empty language. Doing so seems to enable auto-indent in code blocks with
// that language.
const defaultLangauge = StreamLanguage.define({
token: (stream) => {
stream.next();
return null;
},
});
const defaultLanguageDescription = LanguageDescription.of({
name: 'default',
support: new LanguageSupport(defaultLangauge),
});
export default defaultLanguageDescription;

View File

@ -0,0 +1,10 @@
import { LanguageDescription } from '@codemirror/language';
import { languages } from '@codemirror/language-data';
import defaultLanguage from './defaultLanguage';
// Intended for use by the `markdown({ ... })` extension.
const lookUpLanguage = (languageInfo: string) => {
return LanguageDescription.matchLanguageName(languages, languageInfo) ?? defaultLanguage;
};
export default lookUpLanguage;

View File

@ -1,10 +1,8 @@
import syntaxHighlightingLanguages from '../markdown/syntaxHighlightingLanguages';
import allLanguages from '../markdown/codeBlockLanguages/allLanguages';
// Ensure languages we use are loaded. Without this, tests may randomly fail (LanguageDescriptions
// are loaded asyncronously, in the background).
const loadLangauges = async () => {
const allLanguages = syntaxHighlightingLanguages;
for (const lang of allLanguages) {
await lang.load();
}