Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility function for markdown. #65

Open
hastebrot opened this issue Mar 30, 2022 · 1 comment
Open

Utility function for markdown. #65

hastebrot opened this issue Mar 30, 2022 · 1 comment

Comments

@hastebrot
Copy link

hastebrot commented Mar 30, 2022

Similar to jsx() there could be a function mdx() which transforms a markdown or mdx string into html.

@hastebrot
Copy link
Author

hastebrot commented Mar 30, 2022

Simple example how to transform markdown to html.

// deno run --no-check markdown.ts

import { VFile, type VFileCompatible } from "https://esm.sh/[email protected]";
import type { Plugin } from "https://esm.sh/[email protected]";
import type { Node as UnistNode } from "https://esm.sh/@types/[email protected]";
import type { Root as MdastRoot } from "https://esm.sh/@types/[email protected]";
import type { Root as HastRoot } from "https://esm.sh/@types/[email protected]";

// md processor.
import { unified } from "https://esm.sh/[email protected]";
import rehypeSanitize from "https://esm.sh/[email protected]";
import rehypeStringify from "https://esm.sh/[email protected]";
import remarkFrontmatter from "https://esm.sh/[email protected]";
import remarkGfm from "https://esm.sh/[email protected]";
import remarkParse from "https://esm.sh/[email protected]";
import remarkRehype from "https://esm.sh/[email protected]";

// mdx compiler.
import { compile } from "https://esm.sh/@mdx-js/[email protected]";
import { remarkMdxFrontmatter } from "https://esm.sh/[email protected]";

if (import.meta.main) {
  const printFile = (file: VFile) => {
    console.log("file:", file);
    return file;
  };
  const printNode = <T extends UnistNode | MdastRoot | HastRoot>(node: T) => {
    console.log("node:", node);
    return node;
  };

  const processor = unified()
    .use(remarkParse)
    .use(remarkFrontmatter)
    .use(() => printNode)
    .use(remarkGfm)
    .use(remarkRehype)
    .use(rehypeSanitize)
    .use(rehypeStringify)
    .freeze();

  printFile(
    await processor.process(
      new VFile({
        path: "~/example.md",
        value: "Alpha **bravo** charlie.",
      }),
    ),
  );

  const compiler = {
    compile(doc: VFileCompatible): Promise<VFile> {
      return compile(doc, {
        format: "mdx",
        outputFormat: "program",
        jsx: true,
        jsxRuntime: "classic",
        pragma: "React.createElement",
        pragmaFrag: "React.Fragment",
        rehypePlugins: [],
        remarkPlugins: [
          remarkFrontmatter,
          [remarkMdxFrontmatter as Plugin, { name: "attributes" }],
          () => printNode,
        ],
      });
    },
  };

  printFile(
    await compiler.compile(
      new VFile({
        path: "~/example.mdx",
        value: "Alpha **bravo** charlie.",
      }),
    ),
  );
}

There is quite an ecosystem behind the mdx compiler suite:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant