Skip to content

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

FIX: Preserve pasted code block formatting (#43910)

Renato Atilio609208339218
diff --git a/frontend/discourse/app/lib/composer/rich-editor-extensions.ts b/frontend/discourse/app/lib/composer/rich-editor-extensions.ts
index 9987de3941a..266685b8a36 100644
--- a/frontend/discourse/app/lib/composer/rich-editor-extensions.ts
+++ b/frontend/discourse/app/lib/composer/rich-editor-extensions.ts
@@ -149,15 +149,16 @@ export type StateFunction = (
) => Record<string, unknown>;
type RichPluginValue = Plugin | PluginSpec<unknown>;
-export type RichPlugin =
+type RichPluginFactory = (
+ params: PluginParams
+) =>
| RichPluginValue
| RichPluginValue[]
- | ((
- params: PluginParams
- ) =>
- | RichPluginValue
- | RichPluginValue[]
- | Promise<RichPluginValue | RichPluginValue[]>);
+ | Promise<RichPluginValue | RichPluginValue[]>;
+export type RichPlugin =
+ | RichPluginValue
+ | RichPluginFactory
+ | (RichPluginValue | RichPluginFactory)[];
export type ParseFunction = (
state: unknown,
diff --git a/frontend/discourse/app/lib/to-markdown.js b/frontend/discourse/app/lib/to-markdown.js
index e07638ee838..290badd1dfd 100644
--- a/frontend/discourse/app/lib/to-markdown.js
+++ b/frontend/discourse/app/lib/to-markdown.js
@@ -68,6 +68,7 @@ export default async function toMarkdown(html) {
{ createSchema },
{ default: Serializer },
{ transformWordHtml },
+ { normalizeCodeBlockLines },
{ isBoundary },
] = await Promise.all([
import("prosemirror-model"),
@@ -78,6 +79,7 @@ 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"),
]);
@@ -94,6 +96,7 @@ 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 2402322b8b9..9d307e6c68d 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/code-block.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/code-block.js
@@ -195,6 +195,30 @@ function isBlockLevelSelection(selection) {
return hasMultipleBlocks || isFullBlockSelection;
}
+export function normalizeCodeBlockLines(doc) {
+ let changed = false;
+
+ for (const code of doc.querySelectorAll("pre > code")) {
+ const lines = [...code.childNodes];
+ if (
+ !lines.length ||
+ !lines.every(
+ (line) => line.nodeName === "DIV" && line.lastChild?.nodeName === "BR"
+ )
+ ) {
+ continue;
+ }
+
+ // Each line already has an explicit break; block wrappers would split the code block.
+ for (const line of lines) {
+ line.replaceWith(...line.childNodes);
+ }
+ changed = true;
+ }
+
+ return changed;
+}
+
function convertSelectionToCodeBlock(schema) {
return (editorState, dispatch) => {
const { from, to } = editorState.selection;
@@ -263,22 +287,33 @@ const extension = {
};
},
}),
- async plugins({ getContext }) {
- return highlightPlugin(
- (hljs = await ensureHighlightJs(getContext().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"
- );
- },
+ plugins: [
+ ({ pmState: { Plugin } }) =>
+ new Plugin({
+ props: {
+ transformPastedHTML(html) {
+ const doc = new DOMParser().parseFromString(html, "text/html");
+ return normalizeCodeBlockLines(doc) ? doc.body.innerHTML : html;
+ },
+ },
+ }),
+ async ({ getContext }) => {
+ return highlightPlugin(
+ (hljs = await ensureHighlightJs(getContext().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"
+ );
+ },
+ ],
};
export default extension;
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 bab26ed2922..a4afb2ae2e6 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
@@ -1,4 +1,4 @@
-import { triggerKeyEvent } from "@ember/test-helpers";
+import { settled, triggerKeyEvent } from "@ember/test-helpers";
import { TextSelection } from "prosemirror-state";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
@@ -58,6 +58,29 @@ module(
});
});
+ test("pasting code with line wrappers keeps a single code block", async function (assert) {
+ 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>'
+ );
+ await settled();
+
+ assert
+ .dom(".ProseMirror pre")
+ .exists({ count: 1 }, "keeps one code block");
+ assert.strictEqual(
+ editor.view.state.doc.firstChild.textContent,
+ "first\n\n last\n",
+ "preserves line breaks and indentation in the pasted code"
+ );
+ assert.strictEqual(
+ editor.view.state.doc.firstChild.attrs.params,
+ "text",
+ "preserves the code language"
+ );
+ });
+
test("language selector options are alphabetically sorted", async function (assert) {
await testMarkdown(
assert,
diff --git a/frontend/discourse/tests/unit/lib/to-markdown-test.js b/frontend/discourse/tests/unit/lib/to-markdown-test.js
index 7b18f4147f9..d0ac361e6b3 100644
--- a/frontend/discourse/tests/unit/lib/to-markdown-test.js
+++ b/frontend/discourse/tests/unit/lib/to-markdown-test.js
@@ -345,6 +345,42 @@ helloWorld();</code>consectetur.`;
assert.strictEqual(await toMarkdown(html), output);
});
+ 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>`;
+
+ 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"
+ );
+ });
+
+ test("preserves explicit breaks in code blocks", async function (assert) {
+ assert.strictEqual(
+ await toMarkdown("<pre><code>first<br><br> last</code></pre>"),
+ "```\nfirst\n\n last\n```",
+ "explicit breaks remain newlines"
+ );
+ });
+
+ test("leaves other preformatted structures unchanged", async function (assert) {
+ const { normalizeCodeBlockLines } =
+ 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>",
+ "text/html"
+ );
+ const original = doc.body.innerHTML;
+
+ normalizeCodeBlockLines(doc);
+
+ assert.strictEqual(
+ doc.body.innerHTML,
+ original,
+ "only normalizes code composed entirely of div lines with explicit breaks"
+ );
+ });
+
test("converts blockquote tag", async function (assert) {
let html = "<blockquote>Lorem ipsum</blockquote>";
let output = "> Lorem ipsum";