refactor(api): 🎨 Improve Markdown parsing with markdown-it instead of marked

This commit is contained in:
Jesse Wierzbinski 2024-04-22 11:02:09 -10:00
parent 436e805789
commit abc8f1ae16
No known key found for this signature in database
9 changed files with 71 additions and 65 deletions

View file

@ -1,29 +0,0 @@
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 (
text: string,
content_type?: string,
) => {
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");
};