Skip to content

Commit 80fc1c6

Browse files
authored
hotfix for article image 404 (#31)
1 parent c685d29 commit 80fc1c6

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

src/app/blog/[articleName]/page.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { Metadata } from "next";
88
import sharp from "sharp";
99
import { promises as fs } from "fs";
1010
import ArticleNameParams from "@/utils/ArticleNameParams";
11+
import path from "path";
1112

1213
export async function generateStaticParams() {
1314
const articles = await getBlogArticles();
1415
return articles.map((article) => ({
15-
articleName: article.path,
16+
// get filename without extension from path
17+
articleName: encodeURIComponent(path.basename(path.dirname(article.path))),
1618
}));
1719
}
1820

@@ -25,7 +27,7 @@ export async function generateMetadata({ params }: ArticleNameParams): Promise<M
2527
}
2628

2729
const articles = await getBlogArticles();
28-
const article = articles.find((article) => article.path === articleName)!;
30+
const article = articles.find((article) => path.basename(path.dirname(article.path)) === articleName)!;
2931
if (article.image !== undefined) {
3032
const filePath = `./public${article.image}`;
3133
const svgBuffer = await fs.readFile(filePath);
@@ -57,7 +59,7 @@ export default async function Page({ params }: ArticleNameParams) {
5759
}
5860

5961
const articles = await getBlogArticles();
60-
const article = articles.find((article) => article.path === articleName)!;
62+
const article = articles.find((article) => path.basename(path.dirname(article.path)) === articleName)!;
6163
articles.sort((a, b) => (a.date > b.date ? -1 : 1));
6264
const articleIndex = articles.indexOf(article);
6365
const previousArticle = articles[articleIndex + 1];
@@ -103,7 +105,7 @@ export default async function Page({ params }: ArticleNameParams) {
103105
)}
104106
</div>
105107

106-
<Article markdown={article.markdown} />
108+
<Article markdown={article.markdown} markdownFilePath={article.path} />
107109
<div className="article-footer">
108110
<div className="footer-oldnew">
109111
{previousArticle === undefined ? (

src/app/specs/[articleName]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default async function Page({ params }: ArticleNameParams) {
1515
const spec = specs.find((spec) => spec.name === articleName)!;
1616
return (
1717
<div className={`article-${articleName}`}>
18-
<Article markdown={spec.markdown} />
18+
<Article markdown={spec.markdown} markdownFilePath={null} />
1919
<style>
2020
{`
2121
@media (max-width: 650px) {

src/app/specs/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default async function Page() {
77
const intro = articles.find((article) => article.name === "Introduction")!;
88
return (
99
<>
10-
<Article markdown={intro.markdown} className="article-Introduction" />
10+
<Article markdown={intro.markdown} markdownFilePath={null} className="article-Introduction" />
1111
<style>
1212
{`
1313
@media (max-width: 650px) {

src/components/Article.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import "./Article.css";
44
import { HTMLProps } from "react";
55
import TableOfContentScrollEffect from "./TableOfContentScrollEffect";
66
import "highlight.js/styles/atom-one-dark.css";
7-
8-
export default async function Article(params: HTMLProps<HTMLDivElement> & { markdown: string }) {
9-
const { markdown, className, ...restProps } = params;
7+
import path from "path";
8+
export default function Article(params: HTMLProps<HTMLDivElement> & { markdown: string, markdownFilePath: string|null}) {
9+
const { markdown, className, markdownFilePath, ...restProps } = params;
1010
const headingsMapPoc = {};
1111
const headingsMapArticle = {};
1212
const html = marked(markdown, {
@@ -77,6 +77,12 @@ export default async function Article(params: HTMLProps<HTMLDivElement> & { mark
7777
const str = `<a href="#${hashName}" class="not-a-link"> <h${level} id="${hashName}" class="article-heading heading-${hashName}">${text}</h${level}> </a>`;
7878
return str;
7979
};
80+
renderer.image = (href, title) => {
81+
if (markdownFilePath === null) return `<img src="${href}" title="${title || ''}" />`;
82+
const markdownDir = markdownFilePath.substring(0, markdownFilePath.lastIndexOf('/'));
83+
const resolvedPath = path.join(path.relative('blog/', markdownDir),href);
84+
return `<img src="${resolvedPath}" title="${title || ''}" />`;
85+
};
8086
return renderer;
8187
}
8288

src/utils/blog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export interface BlogArticle {
1919
export async function getBlogArticles(): Promise<BlogArticle[]> {
2020
// list directories in /blog
2121
// for each directory, read the markdown file article.md
22-
const dir = await fs.promises.opendir(path.join(process.cwd(), "public/blog"));
22+
const publicDir = path.join(process.cwd(), "public");
23+
const dir = await fs.promises.opendir(path.join(publicDir, "blog"));
2324
const articles: BlogArticle[] = [];
2425
for await (const dirEntry of dir) {
2526
if (dirEntry.isDirectory()) {
@@ -53,6 +54,8 @@ export async function getBlogArticles(): Promise<BlogArticle[]> {
5354
if (attributes.authors !== undefined && !Array.isArray(attributes.authors)) {
5455
attributes.authors = [attributes.authors];
5556
}
57+
// blogPath relative to public
58+
const blogPath = path.relative(publicDir, articlePath).replace(/\\/g, "/");
5659

5760
articles.push({
5861
title: attributes.title ?? "Untitled",
@@ -64,7 +67,7 @@ export async function getBlogArticles(): Promise<BlogArticle[]> {
6467
image: attributes.image,
6568
imageMargin: attributes.imageMargin,
6669
imageHeight: attributes.imageHeight,
67-
path: encodeURIComponent(dirEntry.name),
70+
path: blogPath,
6871
makeSocialEmbedBig: attributes.makeSocialEmbedBig ?? false,
6972
});
7073
}

0 commit comments

Comments
 (0)