A platform for community discussion. Free, open, simple.
FIX: Use default_code_lang for code blocks in the rich editor (#44663)
Renato Atilioc525c83d06e3
diff --git a/frontend/discourse/app/static/prosemirror/extensions/code-block.js b/frontend/discourse/app/static/prosemirror/extensions/code-block.jsindex e15842a25fb..51449e3afaf 100644--- a/frontend/discourse/app/static/prosemirror/extensions/code-block.js+++ b/frontend/discourse/app/static/prosemirror/extensions/code-block.js@@ -6,6 +6,8 @@ import { ensureHighlightJs } from "discourse/lib/highlight-syntax";const INDENT = " ";+const PLAIN_TEXT_LANGS = ["auto", "text", "pre", "plain"];+// cached hljs instance with custom plugins/languageslet hljs;@@ -326,19 +328,21 @@ const extension = {},}),async ({ getContext }) => {+ const { session, siteSettings } = getContext();++ // Autodetection can hang the browser (https://github.com/b-kelly/prosemirror-highlightjs/issues/21)+ const { default_code_lang } = siteSettings;+ const defaultCodeLang =+ !default_code_lang || PLAIN_TEXT_LANGS.includes(default_code_lang)+ ? "text"+ : default_code_lang;+return highlightPlugin(- (hljs = await ensureHighlightJs(getContext().session.highlightJsPath)),+ (hljs = await ensureHighlightJs(session.highlightJsPath)),["code_block", "html_block", "preview_source"],-- // NOTE: If the language has not been set with the code block, we default to plain- // text rather than autodetecting. This is to work around an infinite loop issue- // in prosemirror-highlightjs when autodetecting which hangs the browser sometimes- // for > 10 seconds, for example:- //- // https://github.com/b-kelly/prosemirror-highlightjs/issues/21- //- // We can remove this if we find some other workaround.- (node) => node.attrs.params || "text"+ (node) =>+ node.attrs.params ||+ (node.type.name === "code_block" ? defaultCodeLang : "text"));},],diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/code-block-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/code-block-test.jsindex 5ab7ebcc90d..7d923551203 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/code-block-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/code-block-test.js@@ -58,6 +58,17 @@ module(});});+ test("code block without a language is highlighted with default_code_lang", async function (assert) {+ this.siteSettings.default_code_lang = "javascript";++ await testMarkdown(+ assert,+ '```\nconsole.log("Hello, world!");\n```',+ `<pre><code><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"Hello, world!"</span>);</code>${select()}</pre>`,+ '```\nconsole.log("Hello, world!");\n```'+ );+ });+test("pasting code with line wrappers keeps a single code block", async function (assert) {const [editor] = await setupRichEditor(assert, "");diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/preview-node-view-test.gjs b/frontend/discourse/tests/integration/components/prosemirror-editor/preview-node-view-test.gjsindex 8a15733fda3..baefeef7e87 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/preview-node-view-test.gjs+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/preview-node-view-test.gjs@@ -221,5 +221,17 @@ module(assert.dom(".preview-stub").hasText("-after", "the preview catches up");});++ test("source without a language ignores default_code_lang", async function (assert) {+ this.siteSettings.default_code_lang = "javascript";++ const [editorClass] = await setupRichEditor(assert, "");+ await insertBlock(editorClass.view, 'console.log("Hello, world!");');+ await toggleSource(editorClass.view);++ assert+ .dom("pre.preview-source [class^='hljs-']")+ .doesNotExist("the source stays plain text");+ });});