Skip to content

Commit

Permalink
first working example with next-mdx-remote
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Mar 15, 2021
1 parent 935b8c3 commit c383a4a
Show file tree
Hide file tree
Showing 11 changed files with 1,056 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"next-connect": "^0.9.1",
"next-i18next": "^5.1.0",
"next-mdx-enhanced": "^4.0.0",
"next-mdx-remote": "^2.1.3",
"next-sitemap": "^1.4.17",
"passport": "^0.4.1",
"passport-local": "1.0.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions src/pages/docs/[...fileName].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import renderToString from "next-mdx-remote/render-to-string";
import hydrate from "next-mdx-remote/hydrate";
import path from "path";
import { listMdxFiles } from "@vulcanjs/mdx";
import { promises as fsPromises } from "fs";

// Define components to allow them in your mdx files
// import Test from '../components/test'
//
// const components = { Test }

export default function DocPage({ source }) {
const content = hydrate(source); //, { components });
return <div className="wrapper">{content}</div>;
}

export async function getStaticPaths() {
const docsDir = path.resolve("./src/content/docs"); // relative to the project root
// TODO: doesn't handle nesting yet, we suppose the file are locaed at the root
const files = await listMdxFiles({ dir: docsDir });
const pageNames = files.map((f) =>
f.fileName.split(".").slice(0, -1).join(".")
);
// path is the file without the extension
const paths = pageNames.map((name) => ({ params: { fileName: [name] } }));
return {
paths,
fallback: false, // See the "fallback" section below
};
}
export async function getStaticProps({ params }) {
// MDX text - can be from a local file, database, anywhere
// const source = "Some **mdx** text, with a component Test";
const fileName = params.fileName[0];
// TODO: supports only .md at this point
const filePath = path.resolve("./src/content/docs", fileName + ".md"); // get file
const source = await fsPromises.readFile(filePath, { encoding: "utf8" });
const mdxSource = await renderToString(source); //, { components });
return { props: { source: mdxSource } };
}
2 changes: 1 addition & 1 deletion src/pages/docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getStaticProps = async () => {
// we suppose that the page name is always the file name without extension (no frontmatter URL customization)
// NOTE: if frontMatter is needed, an alternative would be using https://github.com/jescalan/babel-plugin-import-glob-array
// to import all frontMatters
const docsDir = path.resolve("./src/pages/docs"); // relative to the project root
const docsDir = path.resolve("./src/content/docs"); // relative to the project root
const files = await listMdxFiles({ dir: docsDir });
const pageNames = files.map((f) =>
f.fileName.split(".").slice(0, -1).join(".")
Expand Down
1,024 changes: 1,014 additions & 10 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit c383a4a

Please sign in to comment.