0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/plugins/discourse-calendar/test/javascripts/unit/topic-id-from-url-test.js
Kris 2f9f4e8401
FEATURE: render links to event topics as interactive cards (#40953)
When a link to an event topic is oneboxed from another post, it will now
render the event's interactive card instead of a generic quote, which
would sometimes be empty if the only content of the topic was the event.
This covers replies, the composer markdown preview, and the composer
rich text editor. When a card can't render it falls back to the standard
onebox quote.

This mirrors core's `localized_oneboxes` pattern for the event data, and
uses a cooked decorator to swap the quote for `<DiscoursePostEvent>`. In
the rich text editor a ProseMirror NodeView renders the read-only card.

Before:
<img width="828" height="298" alt="image"
src="https://github.com/user-attachments/assets/41df1ece-ca38-4747-8c5a-3dda0b06998a"
/>

After:
<img width="757" height="540" alt="image"
src="https://github.com/user-attachments/assets/6312242d-f7c1-43e3-b641-0d471680c107"
/>

A pared down read-only card is rendered in the composer for preview.

<img width="600" alt="image"
src="https://github.com/user-attachments/assets/c06cd020-6acb-4a45-87be-4621ff2bf308"
/>


<img width="800" alt="image"
src="https://github.com/user-attachments/assets/8250458b-0082-4be8-980a-bfb69a5191d3"
/>
2026-07-17 10:36:54 -04:00

40 lines
1.6 KiB
JavaScript
Vendored

import { module, test } from "qunit";
import { topicIdFromUrl } from "discourse/plugins/discourse-calendar/discourse/components/discourse-post-event/onebox-node-view";
const ORIGIN = window.location.origin;
module("Unit | discourse-post-event | topicIdFromUrl", function () {
test("extracts the topic id for a topic / first-post link", function (assert) {
assert.strictEqual(topicIdFromUrl("/t/some-slug/123"), 123);
assert.strictEqual(topicIdFromUrl(`${ORIGIN}/t/some-slug/123`), 123);
assert.strictEqual(
topicIdFromUrl(`${ORIGIN}/t/some-slug/123/1`),
123,
"treats an explicit first post as the topic"
);
});
test("returns null for a link to a specific reply", function (assert) {
assert.strictEqual(topicIdFromUrl(`${ORIGIN}/t/some-slug/123/4`), null);
});
test("returns null for an off-site Discourse url", function (assert) {
assert.strictEqual(
topicIdFromUrl("https://meta.discourse.org/t/foo/123"),
null,
"does not treat another Discourse site's topic as a local topic"
);
});
test("returns null for slugless urls instead of misreading the post number", function (assert) {
assert.strictEqual(topicIdFromUrl(`${ORIGIN}/t/123/2`), null);
assert.strictEqual(topicIdFromUrl(`${ORIGIN}/t/123`), null);
});
test("returns null for non-topic urls", function (assert) {
assert.strictEqual(topicIdFromUrl(`${ORIGIN}/c/cat/5`), null);
assert.strictEqual(topicIdFromUrl(ORIGIN), null);
assert.strictEqual(topicIdFromUrl(null), null);
assert.strictEqual(topicIdFromUrl(undefined), null);
});
});