0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-quoting-test.js
Renato Atilio d62cc48d9c
DEV: to-markdown prosemirror HTML parsing (#35563)
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.
2026-05-29 18:14:32 -03:00

141 lines
8.3 KiB
JavaScript
Vendored

import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { cloneJSON } from "discourse/lib/object";
import topicFixtures from "discourse/tests/fixtures/topic";
import { acceptance, selectText } from "discourse/tests/helpers/qunit-helpers";
acceptance("quoting", function (needs) {
needs.user();
needs.settings({ discourse_local_dates_enabled: true });
needs.pretender((server, helper) => {
const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]);
const firstPost = topicResponse.post_stream.posts[0];
firstPost.cooked += `<div class='select-local-date-test'>This is a test <span data-date="2022-06-17" data-time="10:00:00" class="discourse-local-date cooked-date past" data-displayed-timezone="Australia/Perth" data-timezone="Australia/Brisbane" data-email-preview="2022-06-17T00:00:00Z UTC" aria-label="Brisbane Friday, June 17, 2022
<br />
<svg class='fa d-icon d-icon-clock svg-icon svg-string'
xmlns=&quot;http://www.w3.org/2000/svg&quot;>
<use href=&quot;#clock&quot; />
</svg> 10:00 AM, Paris Friday, June 17, 2022
<br />
<svg class='fa d-icon d-icon-clock svg-icon svg-string'
xmlns=&quot;http://www.w3.org/2000/svg&quot;>
<use href=&quot;#clock&quot; />
</svg> 2:00 AM, Los Angeles Thursday, June 16, 2022
<br />
<svg class='fa d-icon d-icon-clock svg-icon svg-string'
xmlns=&quot;http://www.w3.org/2000/svg&quot;>
<use href=&quot;#clock&quot; />
</svg> 5:00 PM" data-title="This is a new topic to check on chat quote issues">
<svg class="fa d-icon d-icon-earth-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
<use href="#earth-americas"></use>
</svg>
<span class="relative-time">June 17, 2022 8:00 AM (Perth)</span>
</span></div>`;
server.get("/t/280.json", () => helper.response(topicResponse));
server.get("/t/280/:post_number.json", () => {
helper.response(topicResponse);
});
server.get("/drafts/topic_280.json", () => {
return helper.response({});
});
});
test("quoting single local dates with basic options", async function (assert) {
await visit("/t/internationalization-localization/280");
await selectText("#post_1 .select-local-date-test");
await click(".insert-quote");
assert.dom(".d-editor-input").hasValue(
`[quote=\"uwe_keim, post:1, topic:280\"]
This is a test [date=2022-06-17 time=10:00:00 timezone=Australia/Brisbane displayedTimezone=Australia/Perth]
[/quote]\n\n`,
"converts the date to markdown with all options correctly"
);
});
});
acceptance("quoting range", function (needs) {
needs.user();
needs.settings({ discourse_local_dates_enabled: true });
needs.pretender((server, helper) => {
const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]);
const firstPost = topicResponse.post_stream.posts[0];
firstPost.cooked += `<div class='select-local-date-test'><p dir="ltr">Some text <span data-date="2022-06-17" data-time="09:30:00" class="discourse-local-date cooked-date past" data-format="LL" data-range="true" data-timezones="Africa/Accra|Australia/Brisbane|Europe/Paris" data-timezone="Australia/Brisbane" data-email-preview="2022-06-16T23:30:00Z UTC" aria-label="Brisbane Friday, June 17, 2022 9:30 AM → Saturday, June 18, 2022 10:30 AM, Accra Thursday, June 16, 2022 11:30 PM → Saturday, June 18, 2022 12:30 AM, Paris Friday, June 17, 2022 1:30 AM → Saturday, June 18, 2022 2:30 AM" data-title="This is a new topic to check on chat quote issues">
<svg class="fa d-icon d-icon-earth-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
<use href="#earth-americas"></use>
</svg>
<span class="relative-time">June 17, 2022</span>
</span>→<span data-date="2022-06-18" data-time="10:30:00" class="discourse-local-date cooked-date past" data-format="LL" data-range="true" data-timezones="Africa/Accra|Australia/Brisbane|Europe/Paris" data-timezone="Australia/Brisbane" data-email-preview="2022-06-18T00:30:00Z UTC" aria-label="Brisbane Friday, June 17, 2022 9:30 AM → Saturday, June 18, 2022 10:30 AM, Accra Thursday, June 16, 2022 11:30 PM → Saturday, June 18, 2022 12:30 AM, Paris Friday, June 17, 2022 1:30 AM → Saturday, June 18, 2022 2:30 AM" data-title="This is a new topic to check on chat quote issues">
<svg class="fa d-icon d-icon-earth-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
<use href="#earth-americas"></use>
</svg>
<span class="relative-time">June 18, 2022</span>
</span></p></div>`;
server.get("/t/280.json", () => helper.response(topicResponse));
server.get("/drafts/topic_280.json", () => {
return helper.response({});
});
server.get("/t/280/:post_number.json", () => {
helper.response(topicResponse);
});
});
test("quoting a range of local dates", async function (assert) {
await visit("/t/internationalization-localization/280");
await selectText("#post_1 .select-local-date-test");
await click(".insert-quote");
assert.dom(".d-editor-input").hasValue(
`[quote=\"uwe_keim, post:1, topic:280\"]
Some text [date-range from=2022-06-17T09:30:00 to=2022-06-18T10:30:00 timezone=Australia/Brisbane format=LL timezones=Africa/Accra|Australia/Brisbane|Europe/Paris]
[/quote]\n\n`,
"converts the date range to markdown with all options correctly"
);
});
});
acceptance("quoting with recurring and countdown", function (needs) {
needs.user();
needs.settings({ discourse_local_dates_enabled: true });
needs.pretender((server, helper) => {
const topicResponse = cloneJSON(topicFixtures["/t/280/1.json"]);
const firstPost = topicResponse.post_stream.posts[0];
firstPost.cooked += `<div class='select-local-date-test'><p dir="ltr">Testing countdown <span data-date="2022-06-21" data-time="09:30:00" class="discourse-local-date cooked-date" data-format="LL" data-countdown="true" data-timezone="Australia/Brisbane" data-email-preview="2022-06-20T23:30:00Z UTC" aria-label="Brisbane Tuesday, June 21, 2022 <br /><svg class='fa d-icon d-icon-clock svg-icon svg-string' xmlns=&quot;http://www.w3.org/2000/svg&quot;><use href=&quot;#clock&quot; /></svg> 9:30 AM, Paris Tuesday, June 21, 2022 <br /><svg class='fa d-icon d-icon-clock svg-icon svg-string' xmlns=&quot;http://www.w3.org/2000/svg&quot;><use href=&quot;#clock&quot; /></svg> 1:30 AM, Los Angeles Monday, June 20, 2022 <br /><svg class='fa d-icon d-icon-clock svg-icon svg-string' xmlns=&quot;http://www.w3.org/2000/svg&quot;><use href=&quot;#clock&quot; /></svg> 4:30 PM" data-title="This is a new topic to check on chat quote issues">
<svg class="fa d-icon d-icon-earth-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
<use href="#earth-americas"></use>
</svg>
<span class="relative-time">21 hours</span>
</span></p>
<p dir="ltr">Testing recurring <span data-date="2022-06-22" class="discourse-local-date cooked-date" data-timezone="Australia/Brisbane" data-recurring="2.weeks" data-email-preview="2022-06-21T14:00:00Z UTC" aria-label="Brisbane Wednesday, June 22, 2022 12:00 AM → Thursday, June 23, 2022 12:00 AM, Paris Tuesday, June 21, 2022 4:00 PM → Wednesday, June 22, 2022 4:00 PM, Los Angeles Tuesday, June 21, 2022 7:00 AM → Wednesday, June 22, 2022 7:00 AM" data-title="This is a new topic to check on chat quote issues">
<svg class="fa d-icon d-icon-earth-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
<use href="#earth-americas"></use>
</svg>
<span class="relative-time">Wednesday</span>
</span></p></div>`;
server.get("/t/280.json", () => helper.response(topicResponse));
server.get("/drafts/topic_280.json", () => {
return helper.response({});
});
server.get("/t/280/:post_number.json", () => {
helper.response(topicResponse);
});
});
test("quoting single local dates with recurring and countdown options", async function (assert) {
await visit("/t/internationalization-localization/280");
await selectText("#post_1 .select-local-date-test");
await click(".insert-quote");
assert.dom(".d-editor-input").hasValue(
`[quote=\"uwe_keim, post:1, topic:280\"]
Testing countdown [date=2022-06-21 time=09:30:00 timezone=Australia/Brisbane format=LL countdown=true]
Testing recurring [date=2022-06-22 timezone=Australia/Brisbane recurring=2.weeks]
[/quote]\n\n`,
"converts the dates to markdown with all options correctly"
);
});
});