Hashtags: auf die Suche nach dem Tag verlinkt

Hashtags waren bisher bloßer Text. Jetzt führt jeder auf /search?tag=…,
das genau dieses Tag findet (#kv nicht #kver). Was als Hashtag gilt,
entspricht dem Suchfilter im Backend, damit der Link auch trifft. Hashtags
in URLs (#anker) bleiben Teil des Links.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-30 22:38:22 +01:00
co-authored by Claude Opus 5.5
parent 6ff577b424
commit 4d9ae4252d
2 changed files with 29 additions and 4 deletions
+1
View File
@@ -18,6 +18,7 @@
<h3>30.10.2026</h3> <h3>30.10.2026</h3>
<ul> <ul>
<li>Suche: Beiträge nach Text, Nutzer und Hashtags filtern, wahlweise ohne Antworten</li> <li>Suche: Beiträge nach Text, Nutzer und Hashtags filtern, wahlweise ohne Antworten</li>
<li>Hashtags in Beiträgen, Profilbeschreibungen und Chats führen zur Suche nach genau diesem Tag</li>
</ul> </ul>
<h3>13.10.2026</h3> <h3>13.10.2026</h3>
<ul> <ul>
+28 -4
View File
@@ -1,4 +1,5 @@
import type { ReactNode } from "react"; import type { ReactNode } from "react";
import { Link } from "react-router";
// avatarUrl liefert den Bildpfad oder das Platzhalterbild bei leerem Avatar. // avatarUrl liefert den Bildpfad oder das Platzhalterbild bei leerem Avatar.
export function avatarUrl(path: string): string { export function avatarUrl(path: string): string {
@@ -11,8 +12,31 @@ export function formatTime(unix: number): string {
const urlPattern = /https?:\/\/[^\s<]+/g; const urlPattern = /https?:\/\/[^\s<]+/g;
// linkify macht aus URLs im Text Links und aus Zeilenumbrüchen <br>. Den Text // Hashtag wie in der Suche (internal/httpapi/search.go): Buchstaben, Ziffern,
// escaped React selbst. // _, davor keines davon -- "a#b" ist kein Tag.
const tagPattern = /(^|[^\p{L}\p{N}_])#([\p{L}\p{N}_]+)/gu;
// tagify verlinkt die Hashtags eines Textstücks auf die Suche nach genau
// diesem Tag.
function tagify(text: string, key: string): ReactNode[] {
const out: ReactNode[] = [];
let last = 0;
for (const m of text.matchAll(tagPattern)) {
const start = m.index + m[1].length;
if (start > last) out.push(text.slice(last, start));
out.push(
<Link key={`${key}-t${start}`} to={`/search?tag=${encodeURIComponent(m[2])}`}>
#{m[2]}
</Link>,
);
last = m.index + m[0].length;
}
if (last < text.length) out.push(text.slice(last));
return out;
}
// linkify macht aus URLs im Text Links, aus Hashtags Links auf die Suche und
// aus Zeilenumbrüchen <br>. Den Text escaped React selbst.
export function linkify(text: string): ReactNode[] { export function linkify(text: string): ReactNode[] {
const out: ReactNode[] = []; const out: ReactNode[] = [];
text.split("\n").forEach((line, i) => { text.split("\n").forEach((line, i) => {
@@ -20,7 +44,7 @@ export function linkify(text: string): ReactNode[] {
let last = 0; let last = 0;
for (const m of line.matchAll(urlPattern)) { for (const m of line.matchAll(urlPattern)) {
const start = m.index; const start = m.index;
if (start > last) out.push(line.slice(last, start)); if (start > last) out.push(...tagify(line.slice(last, start), `${i}-${last}`));
out.push( out.push(
<a key={`a${i}-${start}`} href={m[0]} target="_blank" rel="noopener"> <a key={`a${i}-${start}`} href={m[0]} target="_blank" rel="noopener">
{m[0]} {m[0]}
@@ -28,7 +52,7 @@ export function linkify(text: string): ReactNode[] {
); );
last = start + m[0].length; last = start + m[0].length;
} }
if (last < line.length) out.push(line.slice(last)); if (last < line.length) out.push(...tagify(line.slice(last), `${i}-${last}`));
}); });
return out; return out;
} }