md-url-path
v2.0.0
Published
Convert Markdown file paths into clean URL paths.
Readme
md-url-path
Convert Markdown and MDX file paths into clean page URLs.
Install
npm install md-url-pathUsage
import { markdownPathToUrlPath } from 'md-url-path';
markdownPathToUrlPath('./docs/README.md');
// './docs/'
markdownPathToUrlPath('./docs/getting-started.md');
// './docs/getting-started'
markdownPathToUrlPath('./docs/getting-started.mdx');
// './docs/getting-started'
markdownPathToUrlPath('./docs/getting-started.zh-CN.md');
// './docs/getting-started'
markdownPathToUrlPath('./docs/README.md#installation');
// './docs/#installation'
markdownPathToUrlPath('https://github.com/guoyunhe/md-url-path/blob/main/README.md');
// 'https://github.com/guoyunhe/md-url-path/blob/main/README.md'
markdownPathToUrlPath('./docs/image.png');
// './docs/image.png'Use with remark-link-rewrite
Use remark-link-rewrite
to rewrite Markdown links as part of a Remark processing pipeline:
npm install md-url-path remark remark-link-rewriteimport { markdownPathToUrlPath } from 'md-url-path';
import { remark } from 'remark';
import remarkLinkRewrite from 'remark-link-rewrite';
const processor = remark().use(remarkLinkRewrite, {
replacer: (url) => markdownPathToUrlPath(url),
});
const result = await processor.process('[Guide](./docs/getting-started.md)');
console.log(String(result));
// [Guide](./docs/getting-started)The replacer runs for each link. markdownPathToUrlPath transforms Markdown and
MDX file paths while leaving absolute URLs and other paths unchanged.
README files point to their containing directory and keep the trailing slash,
because that directory-style URL is the index page. Other files become
extensionless paths without a trailing slash. Locale suffixes such as .ja.md or
.zh-CN.md are dropped, because translations are served under a locale prefix
that the current page already carries.
