Skip to content

Commit

Permalink
Allow relative paths in Markdown (#641)
Browse files Browse the repository at this point in the history
* Allow relative paths in Markdown

* Allow relative paths in Markdown

* Fix images as tag

* Ignore non-strings
  • Loading branch information
dan-lee authored Feb 5, 2025
1 parent 12d34ad commit 19995e9
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 32 deletions.
8 changes: 8 additions & 0 deletions docs/pages/static-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ If you want users to download a file like a PDF, you can use an anchor tag like
```html
<a href="/documents/api-spec.pdf" download="/documents/api-spec.pdf">Download API specification</a>
```

## Relative paths

If you want to reference a file that is in the same directory as the current file, you can also use a relative path:

```md title="page.mdx"
![API Architecture](./image.png)
```
Binary file removed examples/cosmo-cargo/pages/burger.webp
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/cosmo-cargo/pages/general.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_icon: heart-handshake
---

<img
src={"./header.png"}
src="./header.png"
alt="Cosmo Cargo Inc."
className="rounded-xl float-right w-full"
/>
Expand Down
Binary file added examples/cosmo-cargo/pages/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/cosmo-cargo/pages/interstellar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_icon: rocket
---

<img
src={"./interstellar.png"}
src="/interstellar.png"
alt="Interstellar Freight Ship"
className="rounded-xl w-full m-0 mb-4"
/>
Expand Down
Binary file removed examples/cosmo-cargo/public/burger.webp
Binary file not shown.
Binary file removed examples/cosmo-cargo/public/header.png
Binary file not shown.
Binary file removed examples/cosmo-cargo/public/header.webp
Binary file not shown.
1 change: 1 addition & 0 deletions packages/zudoku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
"react-hook-form": "7.54.2",
"react-is": "catalog:",
"react-router": "7.1.3",
"rehype-mdx-import-media": "1.2.0",
"rehype-raw": "7.0.0",
"rehype-slug": "6.0.0",
"remark-comment": "1.0.0",
Expand Down
36 changes: 22 additions & 14 deletions packages/zudoku/src/vite/plugin-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import withTocExport from "@stefanprobst/rehype-extract-toc/mdx";
import { toString as hastToString } from "hast-util-to-string";
import type { Root } from "mdast";
import path from "node:path";
import rehypeMdxImportMedia from "rehype-mdx-import-media";
import rehypeSlug from "rehype-slug";
import remarkComment from "remark-comment";
import remarkDirective from "remark-directive";
Expand All @@ -13,6 +14,7 @@ import remarkFrontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
import { EXIT, visit } from "unist-util-visit";
import { type VFile } from "vfile";
import { type Plugin } from "vite";
import { type ZudokuPluginOptions } from "../config/config.js";
import { joinUrl } from "../lib/util/joinUrl.js";
Expand Down Expand Up @@ -50,22 +52,33 @@ const remarkLinkRewritePlugin =
};

const rehypeMediaBase =
(base = "") =>
(rootDir: string, base = "") =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(tree: any) => {
if (!base) return;

(tree: any, vfile: VFile) => {
visit(tree, "element", (node) => {
if (!base) return;
if (node.tagName !== "img" && node.tagName !== "video") return;

if (node.properties.src && !node.properties.src.startsWith("http")) {
node.properties.src = joinUrl(base, node.properties.src);
}
});

// `rehype-mdx-import-media` doesn't handle images in mdxJsxFlowElement so we do it manually
visit(tree, ["mdxJsxFlowElement", "mdxJsxElement"], (node) => {
if (node.name !== "img" && node.name !== "video") return;
const src = node.attributes.find((attr: any) => attr?.name === "src");

if (typeof src?.value !== "string" || /^(http|\/)/i.test(src.value))
return;

const relativePath = path.dirname(path.relative(rootDir, vfile.path));
src.value = "./" + path.join(base, relativePath, src.value);
});
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rehypeExcerptWithMdxExport = () => (tree: any, _vfile: any) => {
const rehypeExcerptWithMdxExport = () => (tree: any) => {
let excerpt: string | undefined;

visit(tree, "element", (node) => {
Expand Down Expand Up @@ -114,13 +127,9 @@ const rehypeExcerptWithMdxExport = () => (tree: any, _vfile: any) => {

const viteMdxPlugin = (getConfig: () => ZudokuPluginOptions): Plugin => {
const config = getConfig();
let base: string | undefined;

return {
enforce: "pre",
configResolved: (v) => {
base = v.base;
},
...mdx({
providerImportSource:
config.mode === "internal" || config.mode === "standalone"
Expand All @@ -139,19 +148,18 @@ const viteMdxPlugin = (getConfig: () => ZudokuPluginOptions): Plugin => {
remarkDirectiveRehype,
[remarkLinkRewritePlugin, config.basePath],
...(config.build?.remarkPlugins ?? []),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any,
],
rehypePlugins: [
rehypeSlug,
rehypeCodeBlockPlugin,
rehypeMetaAsAttributes,
[rehypeMediaBase, base],
rehypeMdxImportMedia,
[rehypeMediaBase, config.rootDir, config.basePath],
withToc,
withTocExport,
rehypeExcerptWithMdxExport,
...(config.build?.rehypePlugins ?? []),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any,
],
}),
name: "zudoku-mdx-plugin",
} as const;
Expand Down
88 changes: 72 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit 19995e9

Please sign in to comment.