0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 17:53:55 +08:00
discourse/plugins/discourse-calendar/test/javascripts/integration/components/dates-test.gjs
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

216 lines
5.5 KiB
Text
Vendored

import { render, settled, waitFor } from "@ember/test-helpers";
import { module, skip, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { fakeTime } from "discourse/tests/helpers/qunit-helpers";
import Dates from "../../discourse/components/discourse-post-event/dates";
module("Integration | Component | Dates", function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
moment.tz.guess = () => "UTC";
this.clock = fakeTime("2025-11-01T00:00:00Z", "UTC", true);
});
hooks.afterEach(function () {
this.clock?.restore();
});
const starts = {
id: 123,
startsAt: "2025-10-06T00:00:00Z",
};
const events = {
currentYear: {
starts,
today: {
...starts,
startsAt: "2025-11-01T08:00:00Z",
endsAt: "2025-11-01T09:00:00Z",
},
yesterdayTomorrow: {
...starts,
startsAt: "2025-10-31T08:00:00Z",
endsAt: "2025-11-02T08:00:00Z",
},
weekdays: {
...starts,
startsAt: "2025-11-01T14:00:00Z",
endsAt: "2025-11-02T14:00:00Z",
},
endsSameDay: {
...starts,
endsAt: "2025-10-06T01:00:00Z",
},
endsSameWeek: {
...starts,
endsAt: "2025-10-10T00:00:00Z",
},
endsSameMonth: {
...starts,
endsAt: "2025-10-20T00:00:00Z",
},
endsDiffMonth: {
...starts,
endsAt: "2025-11-06T00:00:00Z",
},
},
endsDiffYear: {
...starts,
endsAt: "2026-01-06T00:00:00Z",
},
};
async function waitForDates() {
await waitFor(".discourse-local-date");
await settled();
}
module("dates without time", function () {
test("formats weekdays within range 1 day before and 2 days after the specified day", async function (assert) {
await render(
<template>
<div data-post-id="123">
<Dates @event={{events.currentYear.weekdays}} />
</div>
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Sunday → Monday",
"`startsAt` should show full weekday names"
);
});
test("formats start date", async function (assert) {
await render(
<template><Dates @event={{events.currentYear.starts}} /></template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6",
"`startsAt` should not show current year and time"
);
});
test("formats same week range", async function (assert) {
await render(
<template>
<Dates @event={{events.currentYear.endsSameWeek}} />
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6 → Fri, Oct 10",
"`endAt` should be formatted with weekday, month and date"
);
});
test("formats same month range", async function (assert) {
await render(
<template>
<Dates @event={{events.currentYear.endsSameMonth}} />
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6 → Mon, Oct 20",
"`endAt` should be formatted with weekday, month and date"
);
});
test("formats different months range", async function (assert) {
await render(
<template>
<Dates @event={{events.currentYear.endsDiffMonth}} />
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6 → Thu, Nov 6",
"`endAt` should be formatted with weekday, month and date"
);
});
test("formats different years range", async function (assert) {
await render(
<template><Dates @event={{events.endsDiffYear}} /></template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6 → Tue, Jan 6, 2026",
"`endAt` should be formatted with year"
);
});
});
module("dates and time", function () {
test("formats yesterday/tomorrow", async function (assert) {
await render(
<template>
<div data-post-id="123">
<Dates @event={{events.currentYear.yesterdayTomorrow}} />
</div>
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Yesterday 6:00 PM → Tomorrow 6:00 PM",
"`startsAt` should not show current year and time"
);
});
test("formats today range", async function (assert) {
await render(
<template>
<div data-post-id="123">
<Dates @event={{events.currentYear.today}} />
</div>
</template>
);
await waitForDates();
assert
.dom(".event-dates")
.hasText(
"Today 6:00 PM → 7:00 PM (Brisbane)",
"`endsAt` should show from today time to time only"
);
});
// this test goes against the current implementation in local-dates
skip("formats same day range", async function (assert) {
await render(
<template><Dates @event={{events.currentYear.endsSameDay}} /></template>
);
assert
.dom(".event-dates")
.hasText(
"Mon, Oct 6 10:00 AM → 11:00 AM (Brisbane)",
"`endsAt` should show time only"
);
});
});
});