Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 62 additions & 17 deletions resources/js/components/linkified-text.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link, usePage } from '@inertiajs/react';
import { Root as TooltipRoot } from '@radix-ui/react-tooltip';
import { Fragment } from 'react';
import type { ReactNode, SVGAttributes } from 'react';
import CommitController from '@/actions/App/Http/Controllers/CommitController';
Expand All @@ -12,10 +13,12 @@ import { CodebergIcon } from '@/components/codeberg-icon';
import { GitHubIcon } from '@/components/github-icon';
import { GitLabIcon } from '@/components/gitlab-icon';
import { GuildIcon } from '@/components/guild-icon';
import { TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { UserPreview } from '@/components/user-preview';
import { typedProse, withoutCode } from '@/lib/code';
import type { Color } from '@/lib/colors';
import { colorsIn } from '@/lib/colors';
import flagNames from '@/lib/flag-names.json';
import { hashtagPattern } from '@/lib/hashtags';
import { mentionPattern } from '@/lib/mentions';
import { pasteisParts } from '@/lib/pasteis';
Expand All @@ -34,6 +37,9 @@ import type { GuildBadge, GuildMentions } from '@/types/guild';
// A link to a commit on dev/null itself, nothing after its id, so it can show as the author and the commit's hash.
const commitLinkPattern =
/^https?:\/\/(?:www\.)?dev-null\.sh\/@([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38})\/commits\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i;
const flagPattern = /\p{Regional_Indicator}{2}/gu;
// Current flag names from https://en.wikipedia.org/wiki/Regional_indicator_symbol
const flagNamesByEmoji = new Map(Object.entries(flagNames));
const gitHubHostPattern = /^(?:www\.)?github\.com$/i;
const gitHubOwnerPattern =
/^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$/;
Expand Down Expand Up @@ -739,11 +745,13 @@ export function Hashtag({
);
}

type RenderProse = (text: string, offset: number) => ReactNode[];

function withHashtags(
text: string,
offset: number,
hashtags: RegExpExecArray[],
pasteisMode: boolean,
renderProse: RenderProse,
guildMentions: GuildMentions | undefined,
): ReactNode[] {
const parts: ReactNode[] = [];
Expand All @@ -757,11 +765,7 @@ function withHashtags(
}

parts.push(
...withWordmarks(
text.slice(position, index),
offset + position,
pasteisMode,
),
...renderProse(text.slice(position, index), offset + position),
<Hashtag
key={`hashtag-${hashtag.index}`}
hashtag={hashtag[0]}
Expand All @@ -772,9 +776,7 @@ function withHashtags(
position = index + hashtag[0].length;
}

parts.push(
...withWordmarks(text.slice(position), offset + position, pasteisMode),
);
parts.push(...renderProse(text.slice(position), offset + position));

return parts;
}
Expand All @@ -787,7 +789,7 @@ function withColors(
offset: number,
colors: Color[],
hashtags: RegExpExecArray[],
pasteisMode: boolean,
renderProse: RenderProse,
guildMentions: GuildMentions | undefined,
): ReactNode[] {
const parts: ReactNode[] = [];
Expand All @@ -805,7 +807,7 @@ function withColors(
text.slice(position, index),
offset + position,
hashtags,
pasteisMode,
renderProse,
guildMentions,
),
<span
Expand All @@ -830,7 +832,7 @@ function withColors(
text.slice(position),
offset + position,
hashtags,
pasteisMode,
renderProse,
guildMentions,
),
);
Expand All @@ -845,7 +847,7 @@ function withMentions(
organizationMentions: Commit['organization_mentions'],
colors: Color[],
hashtags: RegExpExecArray[],
pasteisMode: boolean,
renderProse: RenderProse,
isStatic: boolean,
linksEveryMention: boolean,
guildMentions: GuildMentions | undefined,
Expand All @@ -870,7 +872,7 @@ function withMentions(
offset + position,
colors,
hashtags,
pasteisMode,
renderProse,
guildMentions,
),
isStatic ? (
Expand Down Expand Up @@ -926,7 +928,7 @@ function withMentions(
offset + position,
colors,
hashtags,
pasteisMode,
renderProse,
guildMentions,
),
);
Expand Down Expand Up @@ -959,6 +961,49 @@ export function LinkifiedText({
const prose = withoutCode(text);
const colors = colorsIn(prose, startsText);
const hashtags = isStatic ? [] : [...prose.matchAll(hashtagPattern)];
const renderProse: RenderProse = (text, offset) => {
const parts: ReactNode[] = [];
let position = 0;

const slice = prose.slice(offset, offset + text.length);

for (const flag of slice.matchAll(flagPattern)) {
const name = flagNamesByEmoji.get(flag[0]);

if (name === undefined) {
continue;
}

parts.push(
...withWordmarks(
text.slice(position, flag.index),
offset + position,
pasteisMode,
),
// Keep the narrow inline trigger mounted so one pointer entry opens the tooltip.
<TooltipRoot key={`flag-${offset + flag.index}`}>
<TooltipTrigger asChild>
<span role="img" aria-label={name}>
{flag[0]}
</span>
</TooltipTrigger>
<TooltipContent>{name}</TooltipContent>
</TooltipRoot>,
);

position = flag.index + flag[0].length;
}

parts.push(
...withWordmarks(
text.slice(position),
offset + position,
pasteisMode,
),
);

return parts;
};
let position = 0;

for (const match of text.matchAll(urlPattern)) {
Expand All @@ -980,7 +1025,7 @@ export function LinkifiedText({
organizationMentions,
colors,
hashtags,
pasteisMode,
renderProse,
isStatic,
linksEveryMention,
guildMentions,
Expand Down Expand Up @@ -1028,7 +1073,7 @@ export function LinkifiedText({
organizationMentions,
colors,
hashtags,
pasteisMode,
renderProse,
isStatic,
linksEveryMention,
guildMentions,
Expand Down
Loading