| | |
| | |
| | |
| | |
| |
|
| | import { Marked } from "marked"; |
| | import { markedHighlight } from "marked-highlight"; |
| | import hljs from "highlight.js"; |
| | import frontMatter from "front-matter"; |
| | import type { PostFrontMatter } from "../../shared/types.js"; |
| | import { siteConfig } from "../../shared/config.js"; |
| |
|
| | const marked = new Marked( |
| | markedHighlight({ |
| | langPrefix: "hljs language-", |
| | highlight(code: string, lang: string): string { |
| | const language = hljs.getLanguage(lang) ? lang : "plaintext"; |
| | return hljs.highlight(code, { language }).value; |
| | }, |
| | }) |
| | ); |
| |
|
| | marked.setOptions({ |
| | gfm: true, |
| | breaks: true, |
| | }); |
| |
|
| | export const parseMarkdown = (content: string): string => { |
| | const parsed = frontMatter<PostFrontMatter>(content); |
| | const htmlContent: string = marked.parse(parsed.body) as string; |
| | return htmlContent; |
| | }; |
| |
|
| | export const extractFrontMatter = (content: string): PostFrontMatter => { |
| | const parsed = frontMatter<PostFrontMatter>(content); |
| |
|
| | const defaultFrontMatter: PostFrontMatter = { |
| | title: siteConfig.defaults.postTitle, |
| | date: new Date().toISOString().split("T")[0], |
| | description: siteConfig.defaults.postDescription, |
| | author: siteConfig.defaults.postAuthor, |
| | tags: [], |
| | }; |
| |
|
| | return { |
| | ...defaultFrontMatter, |
| | ...parsed.attributes, |
| | }; |
| | }; |