diff --git a/server/lib/formatters/comments.ts b/server/lib/formatters/comments.ts index 46ae233..05eb9ae 100644 --- a/server/lib/formatters/comments.ts +++ b/server/lib/formatters/comments.ts @@ -1,6 +1,6 @@ import type { NeutralMessage, NeutralAuthor } from "../types"; import { GITHUB_COLORS } from "./colors"; -import { emojiPrefix, type T, buildMessage } from "./helpers"; +import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers"; export function formatIssueComment( payload: Record, @@ -22,7 +22,7 @@ export function formatIssueComment( const al = t("actions." + action) ?? action; const em = (e: string): string => emojiPrefix(e, showEmoji); - const commentBody = comment.body?.slice(0, 500) ?? ""; + const commentBody = htmlToText(comment.body ?? "").slice(0, 500); const truncated = comment.body && comment.body.length > 500; return buildMessage( diff --git a/server/lib/formatters/commit-comment.ts b/server/lib/formatters/commit-comment.ts index 7efb113..f3bfe4e 100644 --- a/server/lib/formatters/commit-comment.ts +++ b/server/lib/formatters/commit-comment.ts @@ -1,6 +1,6 @@ import type { NeutralMessage, NeutralAuthor } from "../types"; import { GITHUB_COLORS } from "./colors"; -import { commitLink, emojiPrefix, type T, buildMessage, repoBaseUrl } from "./helpers"; +import { commitLink, emojiPrefix, type T, buildMessage, repoBaseUrl, htmlToText } from "./helpers"; export function formatCommitComment( payload: Record, @@ -18,7 +18,7 @@ export function formatCommitComment( const al = t("actions." + action) ?? action; const em = (e: string): string => emojiPrefix(e, showEmoji); - const commentBody = comment.body?.slice(0, 500) ?? ""; + const commentBody = htmlToText(comment.body ?? "").slice(0, 500); const truncated = comment.body && comment.body.length > 500; const baseUrl = repoBaseUrl(payload, repo); const shortSha = comment.commit_id?.slice(0, 7) ?? "???????"; diff --git a/server/lib/formatters/discussion.ts b/server/lib/formatters/discussion.ts index e4389e2..4d09587 100644 --- a/server/lib/formatters/discussion.ts +++ b/server/lib/formatters/discussion.ts @@ -1,6 +1,6 @@ import type { NeutralMessage, NeutralAuthor } from "../types"; import { GITHUB_COLORS } from "./colors"; -import { emojiPrefix, type T, buildMessage } from "./helpers"; +import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers"; export function formatDiscussion( payload: Record, @@ -75,7 +75,7 @@ export function formatDiscussionComment( const al = t("actions." + action) ?? action; const em = (e: string): string => emojiPrefix(e, showEmoji); - const commentBody = comment.body?.slice(0, 500) ?? ""; + const commentBody = htmlToText(comment.body ?? "").slice(0, 500); const truncated = comment.body && comment.body.length > 500; return buildMessage( diff --git a/server/lib/formatters/helpers.ts b/server/lib/formatters/helpers.ts index d5abc43..c8ee428 100644 --- a/server/lib/formatters/helpers.ts +++ b/server/lib/formatters/helpers.ts @@ -114,6 +114,31 @@ export function cap(text: string, max: number): string { return text.length > max ? text.slice(0, max) : text; } +export function htmlToText(input: string): string { + if (!input) return ""; + let out = input + .replace(//gi, "\n") + .replace(/]*>/gi, "\n") + .replace(/<\/(p|div|tr|h[1-6]|ul|ol|table|blockquote)>/gi, "\n") + .replace(/<\/t[dh]>/gi, " ") + .replace(/]*>/gi, "\n• ") + .replace(/<[^>]+>/g, ""); + out = out + .replace(/ /gi, " ") + .replace(/</gi, "<") + .replace(/>/gi, ">") + .replace(/"/gi, '"') + .replace(/'/gi, "'") + .replace(/&#x([0-9a-f]+);/gi, (_m, hex: string) => String.fromCodePoint(parseInt(hex, 16))) + .replace(/&#(\d+);/g, (_m, dec: string) => String.fromCodePoint(Number(dec))) + .replace(/&/gi, "&"); + out = out + .replace(/[ \t]+\n/g, "\n") + .replace(/\n{3,}/g, "\n\n") + .trim(); + return out; +} + /** * Normalize a check/workflow status: `queued` / `in_progress` keep their * value (the latter becomes `running`), anything else falls back to the diff --git a/server/lib/formatters/review.ts b/server/lib/formatters/review.ts index fa59b26..de8138c 100644 --- a/server/lib/formatters/review.ts +++ b/server/lib/formatters/review.ts @@ -1,6 +1,6 @@ import type { NeutralMessage, NeutralAuthor } from "../types"; import { GITHUB_COLORS } from "./colors"; -import { emojiPrefix, type T, buildMessage } from "./helpers"; +import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers"; export function formatPullRequestReview( payload: Record, @@ -36,7 +36,7 @@ export function formatPullRequestReview( descriptionParts.push(t("events.pr_review.action_review", { emoji: em(stateEmoji), action: al })); if (review.body) { - const truncated = review.body.slice(0, 500); + const truncated = htmlToText(review.body).slice(0, 500); descriptionParts.push(`\n> ${truncated}${review.body.length > 500 ? "..." : ""}`); } @@ -78,7 +78,7 @@ export function formatPullRequestReviewComment( const al = t("actions." + action) ?? action; const em = (e: string): string => emojiPrefix(e, showEmoji); - const commentBody = comment.body?.slice(0, 400) ?? ""; + const commentBody = htmlToText(comment.body ?? "").slice(0, 400); const truncated = comment.body && comment.body.length > 400; const fields: Array<{ name: string; value: string; inline?: boolean }> = []; diff --git a/tests/html-to-text.test.ts b/tests/html-to-text.test.ts new file mode 100644 index 0000000..37e7550 --- /dev/null +++ b/tests/html-to-text.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from "bun:test"; +import { htmlToText } from "../server/lib/formatters/helpers"; + +describe("htmlToText", () => { + it("strips an HTML table bot comment into clean text", () => { + const html = + "Deploying webhooker with Cloudflare Pages.
Latest commit:526e6c4
Status:✅ Deploy successful!
Preview URL:preview
"; + const out = htmlToText(html); + expect(out).not.toContain(""); + expect(out).not.toContain("
"); + expect(out).not.toContain(" { + const md = "**bold** and `code` and [link](https://example.com)"; + expect(htmlToText(md)).toBe(md); + }); + + it("converts
to a newline", () => { + expect(htmlToText("line one
line two")).toBe("line one\nline two"); + }); + + it("converts
  • to a bullet", () => { + expect(htmlToText("
    • first
    • second
    ")).toBe("• first\n• second"); + }); + + it("decodes entities", () => { + expect(htmlToText("a & b <tag> "q" 'apos'")).toBe( + "a & b \"q\" 'apos'", + ); + }); + + it("returns an empty string for empty input", () => { + expect(htmlToText("")).toBe(""); + }); +});