diff --git a/app/utils/string.test.ts b/app/utils/string.test.ts index 34c57ce94..61c97ac2c 100644 --- a/app/utils/string.test.ts +++ b/app/utils/string.test.ts @@ -1,5 +1,9 @@ import { describe, expect, test } from "vitest"; -import { pathnameFromPotentialURL, truncateBySentence } from "./strings"; +import { + pathnameFromPotentialURL, + removeMarkdown, + truncateBySentence, +} from "./strings"; describe("pathnameFromPotentialURL()", () => { test("Resolves path name from valid URL", () => { @@ -44,3 +48,30 @@ describe("truncateBySentence()", () => { expect(truncateBySentence(text, 20)).toBe("First line"); }); }); + +describe("removeMarkdown()", () => { + test("Decodes entities and collapses runs", () => { + const text = " Global Gauntlet is an event"; + expect(removeMarkdown(text)).toBe("Global Gauntlet is an event"); + }); + + test("Decodes common named HTML entities", () => { + expect(removeMarkdown("Tom & Jerry <3 "hi"")).toBe( + 'Tom & Jerry <3 "hi"', + ); + }); + + test("Decodes numeric HTML entities", () => { + expect(removeMarkdown("café & tea")).toBe("café & tea"); + }); + + test("Leaves unknown named entities untouched", () => { + expect(removeMarkdown("AT&T &fakeentity; rules")).toBe( + "AT&T &fakeentity; rules", + ); + }); + + test("Strips HTML tags and markdown emphasis", () => { + expect(removeMarkdown("
Hello **world**!
")).toBe("Hello world!"); + }); +}); diff --git a/app/utils/strings.ts b/app/utils/strings.ts index 60aac7e53..c38d5e409 100644 --- a/app/utils/strings.ts +++ b/app/utils/strings.ts @@ -76,12 +76,35 @@ export function truncateBySentence(value: string, max: number) { } // based on https://github.com/zuchka/remove-markdown +const NAMED_HTML_ENTITIES: Record