diff --git a/server/api/api/v1/accounts/update_credentials/index.ts b/server/api/api/v1/accounts/update_credentials/index.ts index cce41f73..b3675846 100644 --- a/server/api/api/v1/accounts/update_credentials/index.ts +++ b/server/api/api/v1/accounts/update_credentials/index.ts @@ -14,6 +14,7 @@ import ISO6391 from "iso-639-1"; import { parseEmojis } from "~database/entities/Emoji"; import { client } from "~database/datasource"; import type { APISource } from "~types/entities/source"; +import { convertTextToHtml } from "@formatting"; export const meta = applyConfig({ allowedMethods: ["PATCH"], @@ -122,11 +123,9 @@ export default async (req: Request): Promise => { return errorResponse("Bio contains blocked words", 422); } - // Remove emojis - user.emojis = []; - (user.source as unknown as APISource).note = sanitizedNote; - user.note = sanitizedNote; + // TODO: Convert note to HTML + user.note = await convertTextToHtml(sanitizedNote); } if (source_privacy && user.source) { diff --git a/utils/formatting.ts b/utils/formatting.ts new file mode 100644 index 00000000..871c6670 --- /dev/null +++ b/utils/formatting.ts @@ -0,0 +1,29 @@ +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(parse(text))); + } else if (content_type === "text/x.misskeymarkdown") { + // Parse as MFM + // TODO: Implement MFM + return text; + } else { + // Parse as plaintext + return linkifyStr(text) + .split("\n") + .map(line => `

${line}

`) + .join("\n"); + } +};