Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Relative link to MDX files in docs partials #50

Merged
merged 7 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion server/mdx-config-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import remarkVariables from "./remark-variables";
import remarkMdxDisableExplicitJsx from "remark-mdx-disable-explicit-jsx";
import remarkCodeSnippet from "./remark-code-snippet";
import remarkImportFiles from "./remark-import-files";
import remarkLintDetails from "./remark-lint-details";
import { getVersion, getVersionRootPath } from "./docs-helpers";
import { loadConfig } from "./config-docs";
import { fetchVideoMeta } from "./youtube-meta";
Expand Down
1 change: 0 additions & 1 deletion server/mdx-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
} from "./types-unist";

import { createEstree } from "./estree-helpers";
import stringifyObject from "stringify-object";

export const createMdxjsEsmNode = (value: string): EsmNode => {
return {
Expand Down
43 changes: 41 additions & 2 deletions server/remark-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import type { Parent } from "unist";
import type { Content, Code, Text } from "mdast";
import type { VFile } from "vfile";
import type { Node } from "mdast-util-from-markdown/lib";
import { dirname, join, relative } from "path";

import { existsSync, readFileSync } from "fs";
import { join } from "path";
import { visitParents } from "unist-util-visit-parents";

import { fromMarkdown } from "mdast-util-from-markdown";

import { mdxjs } from "micromark-extension-mdxjs";
Expand Down Expand Up @@ -66,6 +66,43 @@ const numIncludes = (value: string) => value.match(globalIncludeRegexp).length;
const isInclude = (node: Code | Text): node is Code | Text =>
typeof node.value === "string" && includeRegexp.test(node.value);

/**
* correct relative paths resolving in partial docs
* i.e. start realtive paths from the partial file directory, not from place where it is being inserted
* example:
* main file: docs/page/1.mdx
* partial: docs/partials/headers/1.mdx

Choose a reason for hiding this comment

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

Thanks for adding these comments: What is the syntax for partials? Maybe indicate this in the comment:

* main file: docs/page/1.mdx
* partial: (!docs/partials/headers/1.mdx!)
 * With this utility a partial like
 * (!../image.jpg!)

*
* With this utility path like that
* ../image.jpg
* in partial will be pointing to
* docs/partials/image.jpg
* and without:
* docs/image.jpg
*/
const handlePartialLink = (node: Node, path: string, mdxPath: string) => {
if (node.type === "link") {
const href = node.url;

if (typeof href !== "string" || href[0] === "/" || /^http/.test(href)) {
return href;
}
// root where all documentation pages store
const absStart = "docs/pages";
// find an "abs" (starting with root) directory path of the file in which the partial doc was inserted
const absMdxPath = dirname(absStart + mdxPath.split(absStart).pop());
const absTargetPath = join(dirname(path), href);
// make the reference path relative to the place where the partial doc was inserted
node.url = relative(absMdxPath, absTargetPath);
}

if ("children" in node) {
node.children?.forEach?.((child) =>
handlePartialLink(child, path, mdxPath)
);
}
};

export interface RemarkIncludesOptions {
rootDir?: string | ((vfile: VFile) => string);
lint?: boolean;
Expand Down Expand Up @@ -131,6 +168,8 @@ export default function remarkIncludes({
],
});

handlePartialLink(tree, path, vfile.path);

const grandParent = ancestors[ancestors.length - 2] as Parent;
const parentIndex = grandParent.children.indexOf(parent);

Expand Down
1 change: 0 additions & 1 deletion server/remark-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export default function remarkLinks(): Transformer {
const hrefAttribute = node.attributes.find(
({ name }) => name === "href"
);

hrefAttribute.value = updateHref(
basename,
hrefAttribute.value as Href
Expand Down