Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): sanitize derived thread names and mend New divider line

Genera5e7b6f3fe72
diff --git a/src/shared/src/index.ts b/src/shared/src/index.ts
index 403ff6f07..8e1ccdf18 100644
--- a/src/shared/src/index.ts
+++ b/src/shared/src/index.ts
@@ -517,7 +517,7 @@ export { escapeLikePattern } from "./utils/sql-like";
export { isOnline, formatStatus, isPresenceOnline, isPresenceOffline } from "./utils/status";
export { isUniqueConstraintError } from "./utils/db-errors";
export { generateWorkspaceSlug } from "./utils/slug";
-export { truncateTitle } from "./utils/title";
+export { truncateTitle, stripInlineMarkup, deriveThreadName } from "./utils/title";
export {
canManageServer,
isServerOwner,
diff --git a/src/shared/src/utils/title.ts b/src/shared/src/utils/title.ts
index a612a9d7f..42fc02558 100644
--- a/src/shared/src/utils/title.ts
+++ b/src/shared/src/utils/title.ts
@@ -13,3 +13,59 @@ export function truncateTitle(text: string, maxLen = 50): string {
const title = lastSpace > 20 ? cut.slice(0, lastSpace) : cut;
return title + "...";
}
+
+/**
+ * Strip markdown / chat-syntax markup to plain readable text.
+ *
+ * A community thread's name is derived from the first words of the anchor
+ * message body, which is raw markdown and may carry `||spoiler||`, `<spoiler>`
+ * HTML tags, `**bold**`, `# headings`, `[label](url)` links, `@mention`
+ * discriminators, and code spans. Left as-is those tokens render literally in
+ * the Threads panel / breadcrumb and get re-interpreted once the name is
+ * spliced into the `thread_created` system message body. This flattens them to
+ * the human-readable text so the derived name is clean.
+ *
+ * Regex-based on purpose: `src/shared` has no markdown/AST dependency, and a
+ * full remark pipeline is overkill for a six-word title.
+ */
+export function stripInlineMarkup(raw: string): string {
+ return raw
+ // Fenced code blocks → keep inner text, drop the ``` fences.
+ .replace(/```[^\n]*\n?([\s\S]*?)```/g, "$1")
+ .replace(/~~~[^\n]*\n?([\s\S]*?)~~~/g, "$1")
+ // Images ![alt](url) → alt text.
+ .replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1")
+ // Links [label](url) / [label][ref] → label.
+ .replace(/\[([^\]]*)\]\((?:[^)]*)\)/g, "$1")
+ .replace(/\[([^\]]*)\]\[[^\]]*\]/g, "$1")
+ // HTML/XML-style tags (<spoiler>, </spoiler>, <br/>, …).
+ .replace(/<\/?[a-zA-Z][^>]*>/g, "")
+ // `||spoiler||` → inner text.
+ .replace(/\|\|([\s\S]*?)\|\|/g, "$1")
+ // Inline code `code` → inner text.
+ .replace(/`+([^`]*)`+/g, "$1")
+ // Emphasis / strong / strike markers.
+ .replace(/(\*\*|__|~~|\*|_)/g, "")
+ // `@name#0042` discriminator → `@name`.
+ .replace(/(@[\p{L}\p{N}_-]+)#\d{4}(?!\d)/gu, "$1")
+ // Line-leading block markers: heading #, blockquote >, list bullets/numbers.
+ .replace(/^\s{0,3}(?:#{1,6}\s+|>\s?|[-*+]\s+|\d+[.)]\s+)/gm, "");
+}
+
+/**
+ * Derive a clean thread name from a message body: strip markup, collapse
+ * whitespace, keep the first `maxWords` words, cap at `maxChars`. Falls back to
+ * `fallback` when the message yields no readable text.
+ */
+export function deriveThreadName(
+ raw: string | null | undefined,
+ fallback: string,
+ maxWords = 6,
+ maxChars = 60,
+): string {
+ const cleaned = stripInlineMarkup(raw ?? "")
+ .replace(/\s+/g, " ")
+ .trim();
+ const name = cleaned.split(" ").filter(Boolean).slice(0, maxWords).join(" ").slice(0, maxChars).trim();
+ return name || fallback;
+}
diff --git a/src/shared/test/utils/title.test.ts b/src/shared/test/utils/title.test.ts
index 4a11a1b28..3a59f3cf0 100644
--- a/src/shared/test/utils/title.test.ts
+++ b/src/shared/test/utils/title.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
-import { truncateTitle } from "../../src/utils/title";
+import { truncateTitle, stripInlineMarkup, deriveThreadName } from "../../src/utils/title";
describe("truncateTitle", () => {
it("collapses runs of whitespace into single spaces", () => {
@@ -39,3 +39,64 @@ describe("truncateTitle", () => {
expect(truncateTitle("hello world", 5)).toBe("hello...");
});
});
+
+describe("stripInlineMarkup", () => {
+ it("unwraps ||spoiler|| to its inner text", () => {
+ expect(stripInlineMarkup("a ||secret|| b")).toBe("a secret b");
+ });
+
+ it("removes HTML/XML-style tags but keeps their text", () => {
+ expect(stripInlineMarkup("<spoiler>secret</spoiler> plan")).toBe("secret plan");
+ expect(stripInlineMarkup("line<br/>break")).toBe("linebreak");
+ });
+
+ it("strips bold/emphasis/strike markers", () => {
+ expect(stripInlineMarkup("**bold** _em_ ~~gone~~ *i* __u__")).toBe("bold em gone i u");
+ });
+
+ it("keeps inner text of inline and fenced code", () => {
+ expect(stripInlineMarkup("run `npm test` now")).toBe("run npm test now");
+ expect(stripInlineMarkup("```ts\nconst x = 1\n```")).toBe("const x = 1\n");
+ });
+
+ it("strips leading block markers (heading, quote, list)", () => {
+ expect(stripInlineMarkup("# Title")).toBe("Title");
+ expect(stripInlineMarkup("> quote")).toBe("quote");
+ expect(stripInlineMarkup("- item")).toBe("item");
+ expect(stripInlineMarkup("1. first")).toBe("first");
+ });
+
+ it("keeps link/image label text", () => {
+ expect(stripInlineMarkup("see [the docs](https://x.io)")).toBe("see the docs");
+ expect(stripInlineMarkup("![alt text](a.png) after")).toBe("alt text after");
+ expect(stripInlineMarkup("ref [label][1]")).toBe("ref label");
+ });
+
+ it("drops a @mention discriminator but keeps unicode names", () => {
+ expect(stripInlineMarkup("hi @李四#0042 there")).toBe("hi @李四 there");
+ });
+});
+
+describe("deriveThreadName", () => {
+ it("strips markup then keeps the first six words", () => {
+ expect(deriveThreadName("||secret|| plan **for** the big launch today", "fallback")).toBe(
+ "secret plan for the big launch",
+ );
+ });
+
+ it("caps at 60 chars", () => {
+ const long = deriveThreadName("a".repeat(80), "fallback");
+ expect(long.length).toBeLessThanOrEqual(60);
+ });
+
+ it("falls back when the body yields no readable text", () => {
+ expect(deriveThreadName("", "General")).toBe("General");
+ expect(deriveThreadName(" \n\t ", "General")).toBe("General");
+ expect(deriveThreadName(null, "General")).toBe("General");
+ expect(deriveThreadName("<br/>", "General")).toBe("General");
+ });
+
+ it("collapses whitespace across lines", () => {
+ expect(deriveThreadName("first\n\n\nsecond", "fallback")).toBe("first second");
+ });
+});
diff --git a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
index 6a43971bb..8ce97e30b 100644
--- a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
+++ b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
@@ -16,7 +16,7 @@ import { AddMembersDialog } from "@/components/community/add-members-dialog"
import type { RightPanel, Msg, OpenProfile, Role } from "@/components/community/_types"
import { canManageServer } from "@/components/community/_types"
import type { MentionType } from "@alook/shared"
-import { isForum as isForumType } from "@alook/shared"
+import { isForum as isForumType, deriveThreadName } from "@alook/shared"
import { resolveRowPresence } from "@/lib/community/presence"
import { makeUserNameResolver, displayName } from "@/lib/community/display-name"
import { avatarInitial } from "@/lib/community/avatar"
@@ -616,7 +616,7 @@ function ChannelView() {
},
onCreateThread: async (id: string) => {
const m = messages.find((x) => x.id === id)
- const name = (m?.content ?? channelName).split(/\s+/).slice(0, 6).join(" ").slice(0, 60) || channelName
+ const name = deriveThreadName(m?.content, channelName)
try {
const data = await createThreadMut.mutateAsync({ channelId, messageId: id, name })
router.push(`/community/channels/${params.serverId}/${data.id}`)
diff --git a/src/web/src/app/community/preview/page.tsx b/src/web/src/app/community/preview/page.tsx
index 975946c73..0b98445f3 100644
--- a/src/web/src/app/community/preview/page.tsx
+++ b/src/web/src/app/community/preview/page.tsx
@@ -18,7 +18,7 @@
import { useMemo, useState } from "react"
import type React from "react"
import { toast } from "sonner"
-import { isForum as isForumType, isServerOwner } from "@alook/shared"
+import { isForum as isForumType, isServerOwner, deriveThreadName } from "@alook/shared"
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from "@/components/ui/resizable"
import { Dialog, DialogContent } from "@/components/ui/dialog"
@@ -240,7 +240,7 @@ export default function CommunityPreview() {
// to the message's first words.
const createThreadFromMessage = (id: string) => {
const m = messages.find((x) => x.id === id)
- const name = (m?.content ?? activeChannel).split(/\s+/).slice(0, 6).join(" ").slice(0, 60) || activeChannel
+ const name = deriveThreadName(m?.content, activeChannel)
createThread(name, m)
}
diff --git a/src/web/src/components/community/dividers.tsx b/src/web/src/components/community/dividers.tsx
index c6bf3d284..0480d7727 100644
--- a/src/web/src/components/community/dividers.tsx
+++ b/src/web/src/components/community/dividers.tsx
@@ -24,9 +24,11 @@ export function NewDivider({ dateLabel }: { dateLabel?: string }) {
<div data-new-divider className="relative my-1 flex items-center gap-2">
<Separator className="flex-1 bg-destructive/60" />
{dateLabel && (
- <span className="text-xs font-semibold text-destructive" suppressHydrationWarning>{dateLabel}</span>
+ <>
+ <span className="text-xs font-semibold text-destructive" suppressHydrationWarning>{dateLabel}</span>
+ <Separator className="flex-1 bg-destructive/60" />
+ </>
)}
- <Separator className="flex-1 bg-destructive/60" />
<span
className="pointer-events-none absolute right-0 top-1/2 -translate-y-1/2 rounded-sm bg-destructive px-1.5 py-0.5 text-xs font-semibold text-white"
style={{ WebkitTextStroke: "0.4px currentColor" }}