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
|
|
@ -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(/<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
|
||||
* value (the latter becomes `running`), anything else falls back to the
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue