mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-22 16:11:29 +00:00
fix(telegram): render HTML with markdown-it and strip HTML from Discord bodies
This commit is contained in:
parent
ea049e28c9
commit
9899cd01bf
7 changed files with 90 additions and 18 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import MarkdownIt from "markdown-it";
|
||||
import type { NeutralMessage } from "../../types";
|
||||
import { splitMessageTitle } from "../../formatters/helpers";
|
||||
|
||||
|
|
@ -12,17 +13,15 @@ function esc(s: string): string {
|
|||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
const md = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
|
||||
md.disable(["image", "autolink"]);
|
||||
md.renderer.rules.strong_open = (): string => "<b>";
|
||||
md.renderer.rules.strong_close = (): string => "</b>";
|
||||
md.renderer.rules.em_open = (): string => "<i>";
|
||||
md.renderer.rules.em_close = (): string => "</i>";
|
||||
|
||||
function mdToHtml(s: string): string {
|
||||
let out = esc(s);
|
||||
out = out.replace(
|
||||
/\[([^\]]+)\]\(([^)]+)\)/g,
|
||||
(_m, label, url) => `<a href="${esc(url)}">${label}</a>`,
|
||||
);
|
||||
out = out.replace(/\*\*([^*]+)\*\*/g, "<b>$1</b>");
|
||||
out = out.replace(/`([^`]+)`/g, "<code>$1</code>");
|
||||
out = out.replace(/(^|[^*])\*([^*]+)\*/g, "$1<i>$2</i>");
|
||||
out = out.replace(/~~([^~]+)~~/g, "<s>$1</s>");
|
||||
return out;
|
||||
return md.renderInline(s);
|
||||
}
|
||||
|
||||
function formatTimestamp(ts?: string): string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue