Skip to content

Commit

Permalink
refactor: post content has markdown instead of html
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Oct 26, 2023
1 parent 1355cfb commit a819d26
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Post = {
};

export type PostContent = {
html: string;
markdown: string;
images?: Map<string, string>;
};

Expand Down
23 changes: 23 additions & 0 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import rehypeKatex from "rehype-katex";
import rehypeMermaid from "rehype-mermaid";
import rehypePrism from "rehype-prism-plus";
import rehypeStringify from "rehype-stringify";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";

export const md2html = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkMath)
.use(remarkRehype)
.use(rehypeKatex)
.use(rehypeMermaid, { strategy: "pre-mermaid" })
.use(rehypePrism)
.use(rehypeStringify);

export async function markdownToHTML(md: string): Promise<string> {
return String(await md2html.process(md));
}
6 changes: 2 additions & 4 deletions src/notion/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { debug } from "../utils";

import { buildDatabase, buildPost, isValidPage } from "./conv";
import {
md2html,
transformMdBlocks,
transformMdImageBlock,
transformMdLinkBlock,
Expand Down Expand Up @@ -105,10 +104,9 @@ export class Client implements ClientType {
(block) => transformMdLinkBlock(block, posts),
);

const mdString = this.n2m.toMarkdownString(transformed);
const file = await md2html.process(mdString.parent);
const markdown = this.n2m.toMarkdownString(transformed);
return {
html: String(file),
markdown: markdown.parent,
images,
};
}
Expand Down
19 changes: 0 additions & 19 deletions src/notion/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
import type { MdBlock } from "notion-to-md/build/types";
import rehypeKatex from "rehype-katex";
import rehypeMermaid from "rehype-mermaid";
import rehypePrism from "rehype-prism-plus";
import rehypeStringify from "rehype-stringify";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";

import type { Post } from "../interfaces";

import { fileUrlToAssetUrl } from "./utils";

export const md2html = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkMath)
.use(remarkRehype)
.use(rehypeKatex)
.use(rehypeMermaid, { strategy: "pre-mermaid" })
.use(rehypePrism)
.use(rehypeStringify);

export function transformMdBlocks(
blocks: MdBlock[],
...transformers: ((block: MdBlock) => MdBlock)[]
Expand Down
4 changes: 3 additions & 1 deletion src/pages/posts/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import config from "../../config";
import PostFooter from "../../customization/PostFooter.astro";
import { downloadImages } from "../../download";
import Layout from "../../layouts/Layout.astro";
import { markdownToHTML } from "../../markdown";
import client from "../../notion";
import { postUrl, formatPostDate } from "../../utils";
Expand All @@ -18,6 +19,7 @@ const post = slug ? await client.getPostBySlug(slug) : undefined;
if (!post) throw new Error(`Post not found: ${slug}`);
const content = post ? await client.getPostContent(post.id) : undefined;
const html = content ? await markdownToHTML(content.markdown) : undefined;
await downloadImages(content?.images, post?.images, database?.images);
---

Expand Down Expand Up @@ -53,7 +55,7 @@ await downloadImages(content?.images, post?.images, database?.images);
"max-w-3xl mx-auto py-6 lg:py-12 markdown post",
config.post?.classes,
]}
set:html={content?.html}
set:html={html}
/>
<PostFooter post={post} database={database} />
</article>
Expand Down

0 comments on commit a819d26

Please sign in to comment.