Skip to content

Commit

Permalink
Use constants for easy maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Jan 16, 2025
1 parent 1bfb39e commit 7623f7b
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions apps/blog/src/plugins/lib/remarkLocalImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { visit } from "unist-util-visit";
import type { Root } from "mdast";
import { POSTS_DIRECTORY, PUBLIC_DIRECTORY } from "#constants";

export default function remarkLocalImages({ slug }: { slug: string }) {
const postsDir = POSTS_DIRECTORY;
const publicDir = PUBLIC_DIRECTORY;
const POSTS_PATH = "posts";

export default function remarkLocalImages({ slug }: { slug: string }) {
return (tree: Root) => {
visit(tree, "image", (node) => {
const imageUrl = node.url || "";
Expand All @@ -18,7 +17,7 @@ export default function remarkLocalImages({ slug }: { slug: string }) {
}

// Absolute path to the MDX file
const mdxFilePath = join(postsDir, slug, "index.mdx");
const mdxFilePath = join(POSTS_DIRECTORY, slug, "index.mdx");
// e.g. /Users/you/your-project/posts/hello-world/index.mdx
const mdxDir = dirname(mdxFilePath);

Expand All @@ -30,10 +29,10 @@ export default function remarkLocalImages({ slug }: { slug: string }) {
// -> We figure out a sub-directory by removing postsDir from sourcePath
// -> For example, if postsDir = /Users/you/your-project/posts
// subPath might be: hello-world/images/sample.png
const subPath = relative(postsDir, sourcePath);
const subPath = relative(POSTS_DIRECTORY, sourcePath);
// e.g. "hello-world/images/sample.png"

const targetPath = join(publicDir, "posts", subPath);
const targetPath = join(PUBLIC_DIRECTORY, POSTS_PATH, subPath);
// e.g. /Users/you/your-project/public/posts/hello-world/images/sample.png

// Ensure the target directory exists
Expand All @@ -44,7 +43,7 @@ export default function remarkLocalImages({ slug }: { slug: string }) {

// Rewrite the Markdown image URL
// so that Next.js serves it from /posts/hello-world/images/sample.png
node.url = "/posts/" + subPath.replace(/\\/g, "/");
node.url = `/${POSTS_PATH}/${subPath.replace(/\\/g, "/")}`;
});
};
}

1 comment on commit 7623f7b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual regression test result - failure

Component Story Success Viewport MisMatch Percentage
typography-typography default phone 0.37%
input-textarea box phone 0.22%
input-textarea line phone 0.22%
input-input box phone 0.22%
input-input line phone 0.22%
input-input default phone 0.50%
components-button string-children phone 0.25%
components-button string-children tablet 0.10%
input-input default tablet 0.10%
input-input line tablet 0.04%
input-input box tablet 0.04%
input-textarea line tablet 0.04%
input-textarea box tablet 0.04%
typography-typography default tablet 0.07%

Please sign in to comment.