Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep the language and drop the extra line in pasted code (#43970)

Renato Atilioe7e3aeabad95
diff --git a/frontend/discourse/app/lib/to-markdown.js b/frontend/discourse/app/lib/to-markdown.js
index 290badd1dfd..e07638ee838 100644
--- a/frontend/discourse/app/lib/to-markdown.js
+++ b/frontend/discourse/app/lib/to-markdown.js
@@ -68,7 +68,6 @@ export default async function toMarkdown(html) {
{ createSchema },
{ default: Serializer },
{ transformWordHtml },
- { normalizeCodeBlockLines },
{ isBoundary },
] = await Promise.all([
import("prosemirror-model"),
@@ -79,7 +78,6 @@ export default async function toMarkdown(html) {
/* dynamicChunkName: "prosemirror-serializer" */ "discourse/static/prosemirror/core/serializer"
),
import("discourse/static/prosemirror/extensions/word-paste"),
- import("discourse/static/prosemirror/extensions/code-block"),
import("discourse/static/prosemirror/lib/plugin-utils"),
]);
@@ -96,7 +94,6 @@ export default async function toMarkdown(html) {
);
restoreReplacedSpaces(parsedDoc.body);
- normalizeCodeBlockLines(parsedDoc);
for (const ext of extensions) {
if (typeof ext.transformParsedHTML === "function") {
diff --git a/frontend/discourse/app/static/prosemirror/extensions/code-block.js b/frontend/discourse/app/static/prosemirror/extensions/code-block.js
index 9d307e6c68d..e15842a25fb 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/code-block.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/code-block.js
@@ -195,10 +195,34 @@ function isBlockLevelSelection(selection) {
return hasMultipleBlocks || isFullBlockSelection;
}
-export function normalizeCodeBlockLines(doc) {
+const LANGUAGE_CLASS = /^lang(?:uage)?-(.+)$/;
+
+function codeBlockLanguage(...elements) {
+ for (const element of elements) {
+ for (const className of element.classList) {
+ const language = className.match(LANGUAGE_CLASS)?.[1];
+ // `auto` is a request to autodetect, not a language
+ if (language && language !== "auto") {
+ return language;
+ }
+ }
+ }
+}
+
+export function normalizeCodeBlocks(doc) {
let changed = false;
for (const code of doc.querySelectorAll("pre > code")) {
+ const pre = code.parentElement;
+
+ if (!pre.hasAttribute("data-params")) {
+ const language = codeBlockLanguage(code, pre, pre.parentElement);
+ if (language) {
+ pre.setAttribute("data-params", language);
+ changed = true;
+ }
+ }
+
const lines = [...code.childNodes];
if (
!lines.length ||
@@ -209,6 +233,9 @@ export function normalizeCodeBlockLines(doc) {
continue;
}
+ // The final break ends the last line; keeping it would add a blank one.
+ lines.at(-1).lastChild.remove();
+
// Each line already has an explicit break; block wrappers would split the code block.
for (const line of lines) {
line.replaceWith(...line.childNodes);
@@ -254,6 +281,7 @@ const extension = {
},
},
nodeViews: { code_block: CodeBlockWithLangSelectorNodeView },
+ transformParsedHTML: normalizeCodeBlocks,
keymap: () => ({
Tab: indentCodeBlock(),
"Shift-Tab": indentCodeBlock(true),
@@ -293,7 +321,7 @@ const extension = {
props: {
transformPastedHTML(html) {
const doc = new DOMParser().parseFromString(html, "text/html");
- return normalizeCodeBlockLines(doc) ? doc.body.innerHTML : html;
+ return normalizeCodeBlocks(doc) ? doc.body.innerHTML : html;
},
},
}),
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.js
index a4afb2ae2e6..5ab7ebcc90d 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
@@ -62,7 +62,7 @@ module(
const [editor] = await setupRichEditor(assert, "");
editor.view.pasteHTML(
- '<pre data-params="text"><code><div><span>first</span><br></div><div><br></div><div> last<br></div></code></pre>'
+ '<pre class="language-bash"><code><div><span>first</span><br></div><div><br></div><div> last<br></div></code></pre>'
);
await settled();
@@ -71,12 +71,12 @@ module(
.exists({ count: 1 }, "keeps one code block");
assert.strictEqual(
editor.view.state.doc.firstChild.textContent,
- "first\n\n last\n",
+ "first\n\n last",
"preserves line breaks and indentation in the pasted code"
);
assert.strictEqual(
editor.view.state.doc.firstChild.attrs.params,
- "text",
+ "bash",
"preserves the code language"
);
});
diff --git a/frontend/discourse/tests/unit/lib/to-markdown-test.js b/frontend/discourse/tests/unit/lib/to-markdown-test.js
index d0ac361e6b3..6cdb93d6fce 100644
--- a/frontend/discourse/tests/unit/lib/to-markdown-test.js
+++ b/frontend/discourse/tests/unit/lib/to-markdown-test.js
@@ -346,12 +346,52 @@ helloWorld();</code>consectetur.`;
});
test("keeps highlighted code lines in a single code block", async function (assert) {
- const html = `<p>Before <code>id</code>.</p><pre><code><div class="token-line"><span class="token plain">website </span><span class="token comment"># Root directory</span><br></div><div class="token-line"><span> └── docs</span><br></div><div class="token-line"><span></span><br></div><div class="token-line"><span> └── hello.md</span><br></div></code></pre><p>After.</p>`;
+ const html = `<p>Before <code>id</code>.</p><pre class="language-bash"><code><div class="token-line"><span class="token plain">website </span><span class="token comment"># Root directory</span><br></div><div class="token-line"><span> └── docs</span><br></div><div class="token-line"><span></span><br></div><div class="token-line"><span> └── hello.md</span><br></div></code></pre><p>After.</p>`;
assert.strictEqual(
await toMarkdown(html),
- "Before `id`.\n\n```\nwebsite # Root directory\n └── docs\n\n └── hello.md\n\n```\n\nAfter.",
- "preserves code boundaries, indentation, and blank lines"
+ "Before `id`.\n\n```bash\nwebsite # Root directory\n └── docs\n\n └── hello.md\n```\n\nAfter.",
+ "preserves code boundaries, indentation, blank lines, and the language"
+ );
+ });
+
+ test("reads the code language from a wrapper around the pre", async function (assert) {
+ const html = `<div class="language-ruby"><pre><code><div>puts :hi<br></div></code></pre></div>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "```ruby\nputs :hi\n```",
+ "a language class on the container is used when the pre has none"
+ );
+ });
+
+ test("reads the code language from the code element", async function (assert) {
+ const html = `<pre><code class="lang-ruby">puts :hi</code></pre>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "```ruby\nputs :hi\n```",
+ "the short class prefix is recognized"
+ );
+ });
+
+ test("ignores the autodetect language marker", async function (assert) {
+ const html = `<pre><code class="lang-auto">puts :hi</code></pre>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "```\nputs :hi\n```",
+ "autodetection is not a language"
+ );
+ });
+
+ test("keeps an explicit language over a class", async function (assert) {
+ const html = `<pre data-params="ruby" class="language-bash"><code><div>puts :hi<br></div></code></pre>`;
+
+ assert.strictEqual(
+ await toMarkdown(html),
+ "```ruby\nputs :hi\n```",
+ "data-params wins over a language class"
);
});
@@ -364,7 +404,7 @@ helloWorld();</code>consectetur.`;
});
test("leaves other preformatted structures unchanged", async function (assert) {
- const { normalizeCodeBlockLines } =
+ const { normalizeCodeBlocks } =
await import("discourse/static/prosemirror/extensions/code-block");
const doc = new DOMParser().parseFromString(
"<pre><code>first\n last</code></pre><pre><code><div>first</div><div>last</div></code></pre><pre><code>first<div>last<br></div></code></pre><div>outside<br></div>",
@@ -372,7 +412,7 @@ helloWorld();</code>consectetur.`;
);
const original = doc.body.innerHTML;
- normalizeCodeBlockLines(doc);
+ normalizeCodeBlocks(doc);
assert.strictEqual(
doc.body.innerHTML,