mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-22 16:11:29 +00:00
fix(formatters): strip HTML from comment bodies so bot notifications render cleanly
This commit is contained in:
parent
fa227b1533
commit
3af50496a7
6 changed files with 74 additions and 9 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import type { NeutralMessage, NeutralAuthor } from "../types";
|
import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS } from "./colors";
|
import { GITHUB_COLORS } from "./colors";
|
||||||
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers";
|
||||||
|
|
||||||
export function formatIssueComment(
|
export function formatIssueComment(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
|
|
@ -22,7 +22,7 @@ export function formatIssueComment(
|
||||||
|
|
||||||
const al = t("actions." + action) ?? action;
|
const al = t("actions." + action) ?? action;
|
||||||
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
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 truncated = comment.body && comment.body.length > 500;
|
||||||
|
|
||||||
return buildMessage(
|
return buildMessage(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import type { NeutralMessage, NeutralAuthor } from "../types";
|
import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS } from "./colors";
|
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(
|
export function formatCommitComment(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
|
|
@ -18,7 +18,7 @@ export function formatCommitComment(
|
||||||
|
|
||||||
const al = t("actions." + action) ?? action;
|
const al = t("actions." + action) ?? action;
|
||||||
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
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 truncated = comment.body && comment.body.length > 500;
|
||||||
const baseUrl = repoBaseUrl(payload, repo);
|
const baseUrl = repoBaseUrl(payload, repo);
|
||||||
const shortSha = comment.commit_id?.slice(0, 7) ?? "???????";
|
const shortSha = comment.commit_id?.slice(0, 7) ?? "???????";
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import type { NeutralMessage, NeutralAuthor } from "../types";
|
import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS } from "./colors";
|
import { GITHUB_COLORS } from "./colors";
|
||||||
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers";
|
||||||
|
|
||||||
export function formatDiscussion(
|
export function formatDiscussion(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
|
|
@ -75,7 +75,7 @@ export function formatDiscussionComment(
|
||||||
|
|
||||||
const al = t("actions." + action) ?? action;
|
const al = t("actions." + action) ?? action;
|
||||||
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
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 truncated = comment.body && comment.body.length > 500;
|
||||||
|
|
||||||
return buildMessage(
|
return buildMessage(
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,31 @@ export function cap(text: string, max: number): string {
|
||||||
return text.length > max ? text.slice(0, max) : text;
|
return text.length > max ? text.slice(0, max) : text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function htmlToText(input: string): string {
|
||||||
|
if (!input) return "";
|
||||||
|
let out = input
|
||||||
|
.replace(/<br\s*\/?>/gi, "\n")
|
||||||
|
.replace(/<table[^>]*>/gi, "\n")
|
||||||
|
.replace(/<\/(p|div|tr|h[1-6]|ul|ol|table|blockquote)>/gi, "\n")
|
||||||
|
.replace(/<\/t[dh]>/gi, " ")
|
||||||
|
.replace(/<li[^>]*>/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
|
* Normalize a check/workflow status: `queued` / `in_progress` keep their
|
||||||
* value (the latter becomes `running`), anything else falls back to the
|
* value (the latter becomes `running`), anything else falls back to the
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import type { NeutralMessage, NeutralAuthor } from "../types";
|
import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS } from "./colors";
|
import { GITHUB_COLORS } from "./colors";
|
||||||
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
import { emojiPrefix, type T, buildMessage, htmlToText } from "./helpers";
|
||||||
|
|
||||||
export function formatPullRequestReview(
|
export function formatPullRequestReview(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
|
|
@ -36,7 +36,7 @@ export function formatPullRequestReview(
|
||||||
descriptionParts.push(t("events.pr_review.action_review", { emoji: em(stateEmoji), action: al }));
|
descriptionParts.push(t("events.pr_review.action_review", { emoji: em(stateEmoji), action: al }));
|
||||||
|
|
||||||
if (review.body) {
|
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 ? "..." : ""}`);
|
descriptionParts.push(`\n> ${truncated}${review.body.length > 500 ? "..." : ""}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@ export function formatPullRequestReviewComment(
|
||||||
|
|
||||||
const al = t("actions." + action) ?? action;
|
const al = t("actions." + action) ?? action;
|
||||||
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
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 truncated = comment.body && comment.body.length > 400;
|
||||||
|
|
||||||
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
||||||
|
|
|
||||||
40
tests/html-to-text.test.ts
Normal file
40
tests/html-to-text.test.ts
Normal file
|
|
@ -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 <a href=\"https://pages.dev\">Cloudflare Pages</a>.<table><tr><td>Latest commit:</td><td><code>526e6c4</code></td></tr><tr><td>Status:</td><td>✅ Deploy successful!</td></tr><tr><td>Preview URL:</td><td><a href='https://ab622f32.webhooker-2e3.pages/...'>preview</a></td></tr></table>";
|
||||||
|
const out = htmlToText(html);
|
||||||
|
expect(out).not.toContain("<table>");
|
||||||
|
expect(out).not.toContain("<td>");
|
||||||
|
expect(out).not.toContain("<a ");
|
||||||
|
expect(out).toContain("Latest commit:");
|
||||||
|
expect(out).toContain("526e6c4");
|
||||||
|
expect(out).toContain("✅ Deploy successful!");
|
||||||
|
expect(out).toContain("preview");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes plain markdown through unchanged", () => {
|
||||||
|
const md = "**bold** and `code` and [link](https://example.com)";
|
||||||
|
expect(htmlToText(md)).toBe(md);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("converts <br> to a newline", () => {
|
||||||
|
expect(htmlToText("line one<br>line two")).toBe("line one\nline two");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("converts <li> to a bullet", () => {
|
||||||
|
expect(htmlToText("<ul><li>first</li><li>second</li></ul>")).toBe("• first\n• second");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("decodes entities", () => {
|
||||||
|
expect(htmlToText("a & b <tag> "q" 'apos'")).toBe(
|
||||||
|
"a & b <tag> \"q\" 'apos'",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns an empty string for empty input", () => {
|
||||||
|
expect(htmlToText("")).toBe("");
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue