This commit is contained in:
45
src/helpers/markdown.ts
Normal file
45
src/helpers/markdown.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import MarkdownIt from "markdown-it";
|
||||
const parser = new MarkdownIt();
|
||||
|
||||
export function readDuration(markdown: string): number | undefined {
|
||||
const words = markdown.split(" ")?.filter(Boolean)?.length;
|
||||
return words && Math.round(words / 250);
|
||||
}
|
||||
|
||||
export function markdownToHtml(markdown: string): string {
|
||||
const md = new MarkdownIt({
|
||||
html: false, // set to true only if you trust the source
|
||||
linkify: true,
|
||||
typographer: true,
|
||||
breaks: true,
|
||||
});
|
||||
|
||||
// Convert -> sanitize
|
||||
const unsafeHtml = md.render(markdown);
|
||||
return unsafeHtml;
|
||||
}
|
||||
|
||||
export function markdownToText(markdown: string): string {
|
||||
if (!markdown) return "";
|
||||
return parser
|
||||
.render(markdown)
|
||||
.split("\n")
|
||||
.map((str) => str.trim())
|
||||
.map((str) => {
|
||||
return str.replace(/<\/?[^>]+(>|$)/g, "").split("\n");
|
||||
})
|
||||
.flat()
|
||||
.filter((str) =>
|
||||
!str.startsWith("import") &&
|
||||
!str.startsWith("export") &&
|
||||
!str.startsWith("#") &&
|
||||
!str.startsWith("const") &&
|
||||
!str.startsWith("function") &&
|
||||
!str.startsWith("export") &&
|
||||
!str.startsWith("import") &&
|
||||
!str.startsWith("<") &&
|
||||
!str.startsWith("let") &&
|
||||
str.length > 0
|
||||
)
|
||||
.join(" ");
|
||||
}
|
||||
Reference in New Issue
Block a user