Skip to content

Commit 906a49e

Browse files
Only rewrite folders
1 parent adefd57 commit 906a49e

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

packages/cursorless-org-docs/docusaurus.config.mts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import type { Config } from "@docusaurus/types";
22
import type { Root } from "mdast";
3-
import { dirname, relative, resolve } from "path";
3+
import { createRequire } from "node:module";
4+
import { fileURLToPath } from "node:url";
5+
import { dirname, extname, relative, resolve } from "path";
46
import { themes } from "prism-react-renderer";
57
import type { Transformer } from "unified";
68
import { visit } from "unist-util-visit";
7-
import { createRequire } from "node:module";
8-
import { fileURLToPath } from "node:url";
99

1010
const require = createRequire(import.meta.url);
1111

12-
const userRelative = "packages/cursorless-org-docs/src/docs/user";
13-
const contributingRelative =
14-
"packages/cursorless-org-docs/src/docs/contributing";
12+
const docsRelative = "packages/cursorless-org-docs/src/docs/";
13+
const userRelative = docsRelative + "user";
14+
const contributingRelative = docsRelative + "contributing";
1515
const repoLink = "https://github.com/cursorless-dev/cursorless/tree/main/";
1616

1717
/**
@@ -51,14 +51,16 @@ function remarkPluginFixLinksToRepositoryArtifacts(): Transformer<Root> {
5151
const fileRelative = relative(repoRoot, file.path).replace(/\\/g, "/");
5252

5353
// We host all files under docs. Will resolve as a relative link, but
54-
// relative links passing between user and contributing are not resolved
55-
// correctly by docusaurus. So we need to rewrite them.
56-
if (
57-
(artifactRelative.startsWith(userRelative) &&
58-
fileRelative.startsWith(userRelative)) ||
59-
(artifactRelative.startsWith(contributingRelative) &&
60-
fileRelative.startsWith(contributingRelative))
61-
) {
54+
// relative links pointing to a folder passing between user and
55+
// contributing are not resolved correctly by docusaurus so we need to
56+
// rewrite them.
57+
if (artifactRelative.startsWith(docsRelative)) {
58+
if (
59+
isFolder(url) &&
60+
passingBetweenUserAndContributing(fileRelative, artifactRelative)
61+
) {
62+
node.url = "/docs/" + artifactRelative.slice(docsRelative.length);
63+
}
6264
return;
6365
}
6466

@@ -67,6 +69,19 @@ function remarkPluginFixLinksToRepositoryArtifacts(): Transformer<Root> {
6769
};
6870
}
6971

72+
function isFolder(url: string) {
73+
return !extname(url);
74+
}
75+
76+
function passingBetweenUserAndContributing(
77+
fileRelative: string,
78+
artifactRelative: string,
79+
): boolean {
80+
return fileRelative.startsWith(userRelative)
81+
? !artifactRelative.startsWith(userRelative)
82+
: !artifactRelative.startsWith(contributingRelative);
83+
}
84+
7085
const config: Config = {
7186
title: "Cursorless",
7287
tagline: "Structural voice coding at the speed of thought",

0 commit comments

Comments
 (0)