mirror of
https://github.com/discourse/discourse.git
synced 2026-08-15 14:06:35 +08:00
Replaces the custom ~1000-line `to-markdown` converter with ProseMirror's schema-based parsing and serialization pipeline, reusing the same extensions that power the rich editor. The old converter duplicated logic the extensions already implement (image formatting, mention/hashtag serialization, table handling, list nesting, etc.) — a single pipeline means one set of rules, fewer divergence bugs, and plugin extensions work in both directions automatically. ### Core change - `toMarkdown()` is now async and lazy-loads ProseMirror on first use. - Cooked→markdown serialization runs through the registered rich-editor extensions, so both conversion directions share one source of truth. ### QuoteState and quote callers Making `toMarkdown()` async changed the quote flow. `QuoteState` now stores the selection's HTML and exposes an async `markdown()`; `buffer` is the plain-text selection. Every caller that needs markdown was updated to await `markdown()`: - Topic controller (`selectText`, `replyToPost` including EmbedMode, `replyAsNewTopic`, `buildQuoteMarkdown`). - Nested controller — forwards the full selection state via the new `copyFrom`. - Fast-edit modal and `computeSupportsFastEdit`. - discourse-ai post-helper menu — now sends markdown (not plain text) to the AI endpoints. `markdown()` snapshots `opts` before awaiting so a concurrent selection change can't mis-pair the result. ### Plugin API - Removed plugin-specific `addTagDecorateCallback`/`addTextDecorateCallback` usage from local-dates and spoiler-alert (the old APIs are kept as deprecated no-ops). local-dates now uses a `transformParsedHTML` rich-editor extension hook. ### Extractions and sharing - Word paste handling moved into a dedicated `word-paste.js` extension. - Quote-selection list-structure preservation moved into `preserve-list-structure.js`. - `normalizeTable` is shared between the paste plugin and the serializer for consistent table output across editor modes. ### Quoting fix - `selectedHTML` drops the empty trailing block a triple-click leaves behind. A triple-click extends the selection's end to the start of the following block, so `cloneContents()` would otherwise clone an empty `<blockquote>` that serializes to a stray `> ` in the quote.
46 lines
1.2 KiB
JavaScript
Vendored
46 lines
1.2 KiB
JavaScript
Vendored
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
import applySpoiler from "discourse/plugins/spoiler-alert/lib/apply-spoiler";
|
|
import richEditorExtension from "../lib/rich-editor-extension";
|
|
|
|
function spoil(element) {
|
|
element.querySelectorAll(".spoiler").forEach((spoiler) => {
|
|
spoiler.classList.remove("spoiler");
|
|
spoiler.classList.add("spoiled");
|
|
applySpoiler(spoiler);
|
|
});
|
|
}
|
|
|
|
export function initializeSpoiler(api) {
|
|
api.decorateCookedElement(spoil, { id: "spoiler-alert" });
|
|
|
|
api.addComposerToolbarPopupMenuOption({
|
|
icon: "wand-magic",
|
|
active: ({ state }) => state.inSpoiler,
|
|
showActiveIcon: true,
|
|
label: "spoiler.title",
|
|
action: (toolbarEvent) => {
|
|
if (toolbarEvent.commands) {
|
|
toolbarEvent.commands.toggleSpoiler();
|
|
} else {
|
|
toolbarEvent.applySurround("[spoiler]", "[/spoiler]", "spoiler_text", {
|
|
multiline: false,
|
|
useBlockMode: true,
|
|
});
|
|
}
|
|
},
|
|
});
|
|
|
|
api.registerRichEditorExtension(richEditorExtension);
|
|
}
|
|
|
|
export default {
|
|
name: "spoiler-alert",
|
|
|
|
initialize(container) {
|
|
const siteSettings = container.lookup("service:site-settings");
|
|
|
|
if (siteSettings.spoiler_enabled) {
|
|
withPluginApi(initializeSpoiler);
|
|
}
|
|
},
|
|
};
|