Skip to content

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

DEV: Fetch the markdown converter as one chunk (#44078)

David Taylor2b7d4b006354
diff --git a/frontend/discourse/app/lib/to-markdown.js b/frontend/discourse/app/lib/to-markdown.js
index e07638ee838..1ead136fd8d 100644
--- a/frontend/discourse/app/lib/to-markdown.js
+++ b/frontend/discourse/app/lib/to-markdown.js
@@ -1,33 +1,5 @@
+import { waitForPromise } from "@ember/test-waiters";
import deprecated from "discourse/lib/deprecated";
-import {
- areDefaultExtensionsRegistered,
- getExtensions,
-} from "./composer/rich-editor-extensions";
-
-async function ensureDefaultExtensions() {
- if (areDefaultExtensionsRegistered()) {
- return;
- }
-
- // The module's side effect registers the defaults and marks them as registered
- await import(
- /* dynamicChunkName: "prosemirror-extensions" */ "discourse/static/prosemirror/extensions/register-default"
- );
-}
-
-// Chrome and Safari copy the space next to an inline element as a non-breaking
-// space, which stops a pasted emoji from cooking. Restore just the copy
-// artifact - a lone nbsp in a bare span - so an author's own nbsp is kept.
-function restoreReplacedSpaces(root) {
- const spans = root.querySelectorAll(
- "span:not([class]):not([style]), span.Apple-converted-space"
- );
- for (const span of spans) {
- if (span.childNodes.length === 1 && span.textContent === "\u00a0") {
- span.replaceWith(root.ownerDocument.createTextNode(" "));
- }
- }
-}
// Deprecated no-ops - kept for backward compatibility
function deprecationWarning(name) {
@@ -61,57 +33,13 @@ export function clearTextDecorateCallbacks() {
export default async function toMarkdown(html) {
try {
- await ensureDefaultExtensions();
-
- const [
- { DOMParser: ProseMirrorDOMParser },
- { createSchema },
- { default: Serializer },
- { transformWordHtml },
- { isBoundary },
- ] = await Promise.all([
- import("prosemirror-model"),
- import(
- /* dynamicChunkName: "prosemirror-schema" */ "discourse/static/prosemirror/core/schema"
- ),
+ const { default: convert } = await waitForPromise(
import(
- /* dynamicChunkName: "prosemirror-serializer" */ "discourse/static/prosemirror/core/serializer"
- ),
- import("discourse/static/prosemirror/extensions/word-paste"),
- import("discourse/static/prosemirror/lib/plugin-utils"),
- ]);
-
- const extensions = getExtensions();
- const schema = createSchema(extensions);
- const domParser = ProseMirrorDOMParser.fromSchema(schema);
- const pluginParams = { utils: { isBoundary } };
- const serializer = new Serializer(extensions, pluginParams);
-
- const processedHtml = transformWordHtml(html);
- const parsedDoc = new DOMParser().parseFromString(
- processedHtml,
- "text/html"
+ /* dynamicChunkName: "to-markdown" */ "discourse/static/prosemirror/to-markdown"
+ )
);
- restoreReplacedSpaces(parsedDoc.body);
-
- for (const ext of extensions) {
- if (typeof ext.transformParsedHTML === "function") {
- try {
- ext.transformParsedHTML(parsedDoc);
- } catch (e) {
- // eslint-disable-next-line no-console
- console.warn(
- `toMarkdown: transformParsedHTML failed for extension ${ext.name || ext.id || "(unknown)"}`,
- e
- );
- }
- }
- }
-
- const doc = domParser.parse(parsedDoc);
-
- return serializer.convert(doc).trim();
+ return convert(html);
} catch (e) {
// eslint-disable-next-line no-console
console.error("toMarkdown failed:", e);
diff --git a/frontend/discourse/app/static/prosemirror/to-markdown.js b/frontend/discourse/app/static/prosemirror/to-markdown.js
new file mode 100644
index 00000000000..0b804bdfaf3
--- /dev/null
+++ b/frontend/discourse/app/static/prosemirror/to-markdown.js
@@ -0,0 +1,51 @@
+// Registers the default extensions as a side effect.
+import "./extensions/register-default";
+import { DOMParser as ProseMirrorDOMParser } from "prosemirror-model";
+import { getExtensions } from "discourse/lib/composer/rich-editor-extensions";
+import { createSchema } from "./core/schema";
+import Serializer from "./core/serializer";
+import { transformWordHtml } from "./extensions/word-paste";
+import { isBoundary } from "./lib/plugin-utils";
+
+// Chrome and Safari copy the space next to an inline element as a non-breaking
+// space, which stops a pasted emoji from cooking. Restore just the copy
+// artifact - a lone nbsp in a bare span - so an author's own nbsp is kept.
+function restoreReplacedSpaces(root) {
+ const spans = root.querySelectorAll(
+ "span:not([class]):not([style]), span.Apple-converted-space"
+ );
+ for (const span of spans) {
+ if (span.childNodes.length === 1 && span.textContent === "\u00a0") {
+ span.replaceWith(root.ownerDocument.createTextNode(" "));
+ }
+ }
+}
+
+export default function toMarkdown(html) {
+ const extensions = getExtensions();
+ const schema = createSchema(extensions);
+ const domParser = ProseMirrorDOMParser.fromSchema(schema);
+ const pluginParams = { utils: { isBoundary } };
+ const serializer = new Serializer(extensions, pluginParams);
+
+ const processedHtml = transformWordHtml(html);
+ const parsedDoc = new DOMParser().parseFromString(processedHtml, "text/html");
+
+ restoreReplacedSpaces(parsedDoc.body);
+
+ for (const ext of extensions) {
+ if (typeof ext.transformParsedHTML === "function") {
+ try {
+ ext.transformParsedHTML(parsedDoc);
+ } catch (e) {
+ // eslint-disable-next-line no-console
+ console.warn(
+ `toMarkdown: transformParsedHTML failed for extension ${ext.name || ext.id || "(unknown)"}`,
+ e
+ );
+ }
+ }
+ }
+
+ return serializer.convert(domParser.parse(parsedDoc)).trim();
+}