Skip to content

Commit

Permalink
refactor: make structure more runtime reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
RyoJerryYu committed Apr 30, 2024
1 parent e72990a commit cabfbe1
Show file tree
Hide file tree
Showing 25 changed files with 183 additions and 247 deletions.
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MermaidExcalidraw } from "@/components/ExcalidrawScene/MermaidExcalidraw";
import { CodeBlockProps } from "./types";
import { CodeBlockProps } from "../../../core/parsing/complex-plugins/code-block-escape/types";

export const CodeBlockMermaid = (props: CodeBlockProps) => {
let name = props.lang ?? undefined; // string | undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EmbededExcalidraw from "@/components/ExcalidrawScene/EmbededExcalidraw";
import { ObsidianRichProps } from "./types";
import { ObsidianRichProps } from "../../../core/parsing/complex-plugins/obsidian-rich/types";

export const ObsidianRichExcalidraw = (props: ObsidianRichProps) => {
console.log("ObsidianRichExcalidraw:", props);
Expand Down
19 changes: 19 additions & 0 deletions src/components-parsing/component-parsing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import License from "@/components/License";
import { MDXComponents } from "mdx/types";
import { Bar } from "./chartjs";
import { CodeBlockMermaid } from "./complex-plugin-components/code-block-escape/CodeBlockMermaid";
import { ObsidianRichExcalidraw } from "./complex-plugin-components/obsidian-rich/ObsidianRichExcalidraw";

export const components: MDXComponents = {
Bar,
License,
CodeBlockMermaid,
ObsidianRichExcalidraw,
// code: (props: any) => {
// if (props.className === "language-mermaid") {
// return <Mermaid {...props} />;
// }
// const language = props.className?.replace("language-", "");
// return <CodeBlock language={language} {...props} />;
// },
};
23 changes: 23 additions & 0 deletions src/core/parsing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `core/parsing`

This directory contains the mdx parsing logic.

Mostly contains about:

- `remark` plugins
- `rehype` plugins
- `complex` plugins

## Complex Plugins

Complex plugins are the plugins about defining a new syntax, parsing it and provide a new JSX element for it.
It may mainly contains:

- `type` definition, which is the interface between the parsing plugin and the rendering component.
- `remark` plugin or `rehype` plugin, which is the parsing logic.

Mostly should define a new component, which should receive props of type defined in `type`.

Attention:

All modules in this directory should not be imported by any Component at rendering time, except the `type`.
91 changes: 91 additions & 0 deletions src/core/parsing/rendering-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { CompileOptions } from "@mdx-js/mdx";
import { serialize } from "next-mdx-remote/serialize";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import rehypeKatex, { Options as KatexOptions } from "rehype-katex";
import rehypePrettyCode, {
Options as PrettyCodeOptions,
} from "rehype-pretty-code";
import rehypeSlug from "rehype-slug";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkUnwrapImages from "remark-unwrap-images";
import { ParseMdxProps } from "../types/rendering";
import {
RemarkCodeBlockEscapeOptions,
remarkCodeBlockEscape,
} from "./complex-plugins/code-block-escape/remark-code-block-escape";
import remarkObsidianRich, {
RemarkObsidianRichOptions,
} from "./complex-plugins/obsidian-rich/remark-obsidian-rich";
import { ObsidianRichProps } from "./complex-plugins/obsidian-rich/types";

const genMdxOptions = (props: ParseMdxProps) => {
const defaultMdxOptions: Omit<
CompileOptions,
"outputFormat" | "providerImportSource"
> = {
remarkPlugins: [
remarkGfm,
remarkMath,
// remarkExcalidrawMermaid,
[
remarkCodeBlockEscape,
{
escapes: [["mermaid", "CodeBlockMermaid"]],
} as RemarkCodeBlockEscapeOptions,
],
// [remarkMermaid, { wrap: true, className: ["mermaid"] }],
[
remarkObsidianRich,
{
matchers: [
[
(props: ObsidianRichProps) =>
props.file.endsWith(".excalidraw") ||
props.file.endsWith(".excalidraw.md"),
"ObsidianRichExcalidraw",
],
],
} as RemarkObsidianRichOptions,
],
remarkUnwrapImages,
],
rehypePlugins: [
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: "wrap" }],
[
rehypeKatex,
{
strict: false,
} as KatexOptions,
],
[
rehypePrettyCode,
{
theme: "rose-pine-moon",
keepBackground: true,
onVisitLine: (node: any) => {
if (node.children.length === 0) {
node.children = [{ type: "text", value: " " }];
}
},
onVisitHighlightedLine: (node: any) => {
node.properties.className.push("highlighted");
},
onVisitHighlightedWord: (node: any, id: string | undefined) => {
node.properties.className = ["word"];
},
} as PrettyCodeOptions,
],
],
};
return defaultMdxOptions;
};

export const parseMdx = async (content: string, props: ParseMdxProps) => {
const mdxOptions = genMdxOptions(props);
const source = await serialize(content, {
mdxOptions: mdxOptions,
});
return source;
};
40 changes: 0 additions & 40 deletions src/core/rendering/plugins/backend-plugins.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/core/rendering/plugins/frontend-plugins.ts

This file was deleted.

71 changes: 0 additions & 71 deletions src/core/rendering/plugins/plugins.test.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/core/rendering/rendering-component.tsx

This file was deleted.

82 changes: 0 additions & 82 deletions src/core/rendering/rendering-parse.ts

This file was deleted.

Loading

0 comments on commit cabfbe1

Please sign in to comment.