mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): 🎨 Improve Markdown parsing with markdown-it instead of marked
This commit is contained in:
parent
436e805789
commit
abc8f1ae16
9 changed files with 71 additions and 65 deletions
|
|
@ -24,7 +24,6 @@ import {
|
|||
maybe,
|
||||
oneOrMore,
|
||||
} from "magic-regexp";
|
||||
import { parse } from "marked";
|
||||
import { db } from "~drizzle/db";
|
||||
import {
|
||||
Attachments,
|
||||
|
|
@ -60,6 +59,11 @@ import {
|
|||
userExtrasTemplate,
|
||||
userRelations,
|
||||
} from "./User";
|
||||
import MarkdownIt from "markdown-it";
|
||||
import markdownItTocDoneRight from "markdown-it-toc-done-right";
|
||||
import markdownItContainer from "markdown-it-container";
|
||||
import markdownItAnchor from "markdown-it-anchor";
|
||||
import markdownItTaskLists from "@hackmd/markdown-it-task-lists";
|
||||
|
||||
export type Status = InferSelectModel<typeof Notes>;
|
||||
|
||||
|
|
@ -577,9 +581,9 @@ export const contentToHtml = async (
|
|||
htmlContent = content["text/html"].content;
|
||||
} else if (content["text/markdown"]) {
|
||||
htmlContent = await sanitizeHtml(
|
||||
await parse(content["text/markdown"].content),
|
||||
await markdownParse(content["text/markdown"].content),
|
||||
);
|
||||
} else if (content["text/plain"]) {
|
||||
} else if (content["text/plain"]?.content) {
|
||||
// Split by newline and add <p> tags
|
||||
htmlContent = content["text/plain"].content
|
||||
.split("\n")
|
||||
|
|
@ -605,6 +609,39 @@ export const contentToHtml = async (
|
|||
return htmlContent;
|
||||
};
|
||||
|
||||
export const markdownParse = async (content: string) => {
|
||||
return (await getMarkdownRenderer()).render(content);
|
||||
};
|
||||
|
||||
export const getMarkdownRenderer = async () => {
|
||||
const renderer = MarkdownIt({
|
||||
html: true,
|
||||
linkify: true,
|
||||
});
|
||||
|
||||
renderer.use(markdownItAnchor, {
|
||||
permalink: markdownItAnchor.permalink.ariaHidden({
|
||||
symbol: "",
|
||||
placement: "before",
|
||||
}),
|
||||
});
|
||||
|
||||
renderer.use(markdownItTocDoneRight, {
|
||||
containerClass: "toc",
|
||||
level: [1, 2, 3, 4],
|
||||
listType: "ul",
|
||||
listClass: "toc-list",
|
||||
itemClass: "toc-item",
|
||||
linkClass: "toc-link",
|
||||
});
|
||||
|
||||
renderer.use(markdownItTaskLists);
|
||||
|
||||
renderer.use(markdownItContainer);
|
||||
|
||||
return renderer;
|
||||
};
|
||||
|
||||
export const federateNote = async (note: Note) => {
|
||||
for (const user of await note.getUsersToFederateTo()) {
|
||||
// TODO: Add queue system
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue