server/utils/formatting.ts

30 lines
830 B
TypeScript
Raw Normal View History

2023-12-07 00:46:46 +01:00
import { sanitizeHtml } from "@sanitization";
import linkifyHtml from "linkify-html";
import linkifyStr from "linkify-string";
import { parse } from "marked";
/**
* Converts plaintext, MFM or Markdown to HTML
* @param text Text to convert
* @param content_type Content type of the text (optional, defaults to plaintext)
* @returns HTML
*/
export const convertTextToHtml = async (
2024-04-07 07:30:49 +02:00
text: string,
content_type?: string,
2023-12-07 00:46:46 +01:00
) => {
2024-04-07 07:30:49 +02:00
if (content_type === "text/markdown") {
return linkifyHtml(await sanitizeHtml(await parse(text)));
}
if (content_type === "text/x.misskeymarkdown") {
// Parse as MFM
// TODO: Implement MFM
return text;
}
// Parse as plaintext
return linkifyStr(text)
.split("\n")
.map((line) => `<p>${line}</p>`)
.join("\n");
2023-12-07 00:46:46 +01:00
};