Skip to content

Commit

Permalink
Support 404 page, and passing site context as props to mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jan 21, 2025
1 parent fd38283 commit 100e843
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions plain-blog/lib/job.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default async function job({
if (PAGE_EXT_REGEX.test(dirent.name)) {
console.log("Building", dirent.name);
let basename = dirent.name.replace(PAGE_EXT_REGEX, "");
const is404 = basename === "404";
const isIndex = basename === "index";
let order = "";
if (isIndex) {
Expand All @@ -53,7 +54,7 @@ export default async function job({
}
}

const folder = isIndex ? distDir : path.join(distDir, basename);
const folder = isIndex || is404 ? distDir : path.join(distDir, basename);
const url = `${context.baseUrl}${basename}/`;

// if (basename === assetsPathPart) {
Expand Down Expand Up @@ -90,23 +91,25 @@ export default async function job({
"twitter:image": imageUrl,
};

const siteContext = {...context, frontmatter, summary, url, meta, Header, Footer};

const { prelude } = await prerenderToNodeStream(
<SiteContext.Provider value={{...context, frontmatter, summary, url, meta, Header, Footer}}>
<SiteContext.Provider value={siteContext}>
<Article>
<Content />
<Content {...siteContext} />
</Article>
</SiteContext.Provider>
);

if (!isIndex) {
if (!isIndex && !is404) {
await mkdir(folder);
}
const indexHtmlPath = path.join(folder, "index.html");
const indexHtmlPath = is404 ? path.join(folder, "404.html") : path.join(folder, "index.html");
const writableStream = createWriteStream(indexHtmlPath);
prelude.pipe(writableStream);
await finished(writableStream);

if (title) {
if (title && !is404) {
posts.push({ url, title, date: frontmatter?.date, summary: summary, order });
}
}
Expand Down
2 changes: 1 addition & 1 deletion plain-blog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plain-blog",
"version": "0.2.0",
"version": "0.3.0",
"type": "module",
"bin": {
"plain-blog": "./bin/plain-blog.js"
Expand Down
7 changes: 7 additions & 0 deletions website/posts/404.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: 404 Not Found
---

Oops! Something went wrong.

Go back to <a href={props.baseUrl}>homepage</a>?

0 comments on commit 100e843

Please sign in to comment.