From 1d08a8cd353eda67633796f57a730072573b766b Mon Sep 17 00:00:00 2001 From: RyoJerryYu Date: Tue, 30 Apr 2024 02:46:57 +0800 Subject: [PATCH] feat: add remark code block escape template --- .../remark-code-block-escape.test.ts | 7 ++++++ .../remark-code-block-escape.ts | 23 +++++++++++++++++++ .../code-block-escape/types.ts | 5 ++++ 3 files changed, 35 insertions(+) create mode 100644 src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.test.ts create mode 100644 src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.ts create mode 100644 src/core/rendering/complex-plugins/code-block-escape/types.ts diff --git a/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.test.ts b/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.test.ts new file mode 100644 index 00000000..79e4cc6b --- /dev/null +++ b/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.test.ts @@ -0,0 +1,7 @@ +import remarkParse from "remark-parse"; +import remarkRehype from "remark-rehype"; +import { unified } from "unified"; + +test("code fields", () => { + unified().use(remarkParse).use(remarkRehype); +}); diff --git a/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.ts b/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.ts new file mode 100644 index 00000000..a78eec35 --- /dev/null +++ b/src/core/rendering/complex-plugins/code-block-escape/remark-code-block-escape.ts @@ -0,0 +1,23 @@ +import { Code, Parent } from "mdast"; +import unified from "unified"; +import { visit } from "unist-util-visit"; + +export type RemarkCodeBlockEscapeOptions = {}; + +export const remarkCodeBlockEscape: unified.Plugin< + [RemarkCodeBlockEscapeOptions?] +> = (options) => { + return (tree) => { + visit(tree, "code", (node: Code, index: number, parent: Parent) => { + node.data; + node.lang; + node.meta; + if (node.lang === "code-block-escape") { + parent.children.splice(index, 1, { + type: "text", + value: node.value, + }); + } + }); + }; +}; diff --git a/src/core/rendering/complex-plugins/code-block-escape/types.ts b/src/core/rendering/complex-plugins/code-block-escape/types.ts new file mode 100644 index 00000000..7af2d380 --- /dev/null +++ b/src/core/rendering/complex-plugins/code-block-escape/types.ts @@ -0,0 +1,5 @@ +export type CodeBlockProps = { + lang: string; + meta: string; + value: string; +};