From 1356da49aeb4d0337f63236a82ba482c52c65f11 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:08:03 -0800 Subject: [PATCH] Generalize our markdown pipe --- src-ui/src/app/pipes/markdown.pipe.spec.ts | 6 ++ src-ui/src/app/pipes/markdown.pipe.ts | 74 ++++++++++++++-------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src-ui/src/app/pipes/markdown.pipe.spec.ts b/src-ui/src/app/pipes/markdown.pipe.spec.ts index a0655486e7..a9d42c070f 100644 --- a/src-ui/src/app/pipes/markdown.pipe.spec.ts +++ b/src-ui/src/app/pipes/markdown.pipe.spec.ts @@ -87,4 +87,10 @@ describe('MarkdownPipe', () => { ) expect(html).not.toMatch(/"\s*onmouseover=/) }) + + it('should omit link hostnames when requested', () => { + const html = pipe.transform('[PR](https://github.com/x/y/pull/1)', false) + expect(html).toContain('href="https://github.com/x/y/pull/1"') + expect(html).not.toContain('(github.com)') + }) }) diff --git a/src-ui/src/app/pipes/markdown.pipe.ts b/src-ui/src/app/pipes/markdown.pipe.ts index 992714c148..4a33e4e45c 100644 --- a/src-ui/src/app/pipes/markdown.pipe.ts +++ b/src-ui/src/app/pipes/markdown.pipe.ts @@ -12,41 +12,59 @@ function allowedUrl(href: string): URL | null { } } -// Treat chat content as untrusted: no raw HTML, no images, and only -// absolute links. All escaping is done by marked's default renderer, and -// Angular's sanitizer runs on top of this as well. -const renderer: Partial = { - html(this: Renderer, { raw, text }: Tokens.HTML | Tokens.Tag): string { - return this.text({ type: 'text', raw, text }) - }, - image(this: Renderer, { raw, text }: Tokens.Image): string { - return this.text({ type: 'text', raw, text }) - }, - link(this: Renderer, token: Tokens.Link): string { - const url = allowedUrl(token.href) - if (!url) return this.parser.parseInline(token.tokens) - const anchor = Renderer.prototype.link - .call(this, token) - .replace('(${hostname})` - }, +/** + * Treat content as untrusted: no raw HTML, no images, and only + * absolute links. All escaping is done by marked's default renderer, and + * Angular's sanitizer should run on top of this as well. + */ +function buildRenderer(showLinkHosts: boolean): Partial { + return { + html(this: Renderer, { raw, text }: Tokens.HTML | Tokens.Tag): string { + return this.text({ type: 'text', raw, text }) + }, + image(this: Renderer, { raw, text }: Tokens.Image): string { + return this.text({ type: 'text', raw, text }) + }, + link(this: Renderer, token: Tokens.Link): string { + const url = allowedUrl(token.href) + if (!url) return this.parser.parseInline(token.tokens) + const anchor = Renderer.prototype.link + .call(this, token) + .replace( + '(${hostname})` + }, + } } -const markdown = new Marked({ - async: false, - gfm: true, - breaks: true, - renderer, -}) +function buildMarked(showLinkHosts: boolean): Marked { + return new Marked({ + async: false, + gfm: true, + breaks: true, + renderer: buildRenderer(showLinkHosts), + }) +} + +const markdown = buildMarked(true) +const markdownWithoutLinkHosts = buildMarked(false) @Pipe({ name: 'markdown', }) export class MarkdownPipe implements PipeTransform { - transform(value: string | null | undefined): string { + /** + * @param showLinkHosts append each link's hostname, e.g. "(example.com)". + * Can be disabled for trusted content where every link points to the same host. + */ + transform(value: string | null | undefined, showLinkHosts = true): string { if (!value) return '' - return markdown.parse(value) as string + return (showLinkHosts ? markdown : markdownWithoutLinkHosts).parse( + value + ) as string } }