Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit dfe73ce

Browse files
committed
Pillify permalinks
1 parent 209b652 commit dfe73ce

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/components/views/messages/TextualBody.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,8 @@ export default class TextualBody extends React.Component<IBodyProps, IState> {
9292
const showLineNumbers = SettingsStore.getValue("showCodeLineNumbers");
9393
this.activateSpoilers([content]);
9494

95-
// pillifyLinks BEFORE linkifyElement because plain room/user URLs in the composer
96-
// are still sent as plaintext URLs. If these are ever pillified in the composer,
97-
// we should be pillify them here by doing the linkifying BEFORE the pillifying.
98-
pillifyLinks([content], this.props.mxEvent, this.pills);
9995
HtmlUtils.linkifyElement(content);
96+
pillifyLinks([content], this.props.mxEvent, this.pills);
10097

10198
this.calculateUrlPreview();
10299

src/utils/pillify.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ import { MatrixClientPeg } from "../MatrixClientPeg";
2323
import SettingsStore from "../settings/SettingsStore";
2424
import { Pill, PillType, pillRoomNotifLen, pillRoomNotifPos } from "../components/views/elements/Pill";
2525
import { parsePermalink } from "./permalinks/Permalinks";
26+
import { PermalinkParts } from "./permalinks/PermalinkConstructor";
27+
28+
/**
29+
* A node here is an A element with a href attribute tag.
30+
*
31+
* It should not be pillified if the permalink parser result contains an event id.
32+
*
33+
* It should be pillified if the permalink parser returned a result and one of the following conditions match:
34+
* - Text content equals href. This is the case when sending a plain permalink in a message.
35+
* - The link does not have the "linkified" class.
36+
* Composer completions already create an A tag.
37+
* Linkify will not linkify things again. → There won't be a "linkified" class.
38+
*/
39+
const shouldBePillified = (node: Element, href: string, parts: PermalinkParts | null): boolean => {
40+
if (!parts || parts.eventId) return;
41+
42+
const textContent = node.textContent;
43+
return href === textContent || !node.classList.contains("linkified");
44+
};
2645

2746
/**
2847
* Recurses depth-first through a DOM tree, converting matrix.to links
@@ -51,9 +70,8 @@ export function pillifyLinks(nodes: ArrayLike<Element>, mxEvent: MatrixEvent, pi
5170
} else if (node.tagName === "A" && node.getAttribute("href")) {
5271
const href = node.getAttribute("href")!;
5372
const parts = parsePermalink(href);
54-
// If the link is a (localised) matrix.to link, replace it with a pill
55-
// We don't want to pill event permalinks, so those are ignored.
56-
if (parts && !parts.eventId) {
73+
74+
if (shouldBePillified(node, href, parts)) {
5775
const pillContainer = document.createElement("span");
5876

5977
const pill = (

0 commit comments

Comments
 (0)