feat: refactor whole bunch of stuff
This commit is contained in:
46
lib/markdown.ts
Normal file
46
lib/markdown.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { render } from "gfm";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-typescript?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-bash?no-check";
|
||||
import "https://esm.sh/prismjs@1.29.0/components/prism-rust?no-check";
|
||||
|
||||
export type Document = {
|
||||
name: string;
|
||||
lastModified: number;
|
||||
contentType: string;
|
||||
content: string | null;
|
||||
size: number;
|
||||
perm: string;
|
||||
};
|
||||
|
||||
function removeFrontmatter(doc: string) {
|
||||
if (doc.trim().startsWith("---")) {
|
||||
return doc.trim().split("---").filter((s) => s.length).slice(1).join("---");
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
export function removeImage(doc: string, imageUrl?: string) {
|
||||
if (!imageUrl) {
|
||||
return doc;
|
||||
}
|
||||
// Remove image from content
|
||||
const first = doc.slice(0, 500);
|
||||
const second = doc.slice(500);
|
||||
|
||||
// Regex pattern to match the image Markdown syntax with the specific URL
|
||||
const pattern = new RegExp(
|
||||
`!\\[.*?\\]\\(${imageUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\)`,
|
||||
"g",
|
||||
);
|
||||
|
||||
// Remove the matched image
|
||||
const updatedMarkdown = first.replace(pattern, "");
|
||||
return updatedMarkdown + second;
|
||||
}
|
||||
|
||||
export function renderMarkdown(doc: string) {
|
||||
return render(removeFrontmatter(doc), {
|
||||
baseUrl: "https://max-richter.dev",
|
||||
allowMath: true,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user