|
1 | 1 | import type { Config } from "@docusaurus/types"; |
2 | 2 | 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"; |
4 | 6 | import { themes } from "prism-react-renderer"; |
5 | 7 | import type { Transformer } from "unified"; |
6 | 8 | import { visit } from "unist-util-visit"; |
7 | | -import { createRequire } from "node:module"; |
8 | | -import { fileURLToPath } from "node:url"; |
9 | 9 |
|
10 | 10 | const require = createRequire(import.meta.url); |
11 | 11 |
|
| 12 | +const docsRelative = "packages/cursorless-org-docs/src/docs/"; |
| 13 | +const userRelative = docsRelative + "user"; |
| 14 | +const contributingRelative = docsRelative + "contributing"; |
| 15 | +const repoLink = "https://github.com/cursorless-dev/cursorless/tree/main/"; |
| 16 | + |
12 | 17 | /** |
13 | 18 | * Files within /docs reference repository directories |
14 | 19 | * and files outside of that folder. They are not served |
@@ -43,23 +48,40 @@ function remarkPluginFixLinksToRepositoryArtifacts(): Transformer<Root> { |
43 | 48 | ); |
44 | 49 | const artifact = resolve(file.dirname!, url); |
45 | 50 | const artifactRelative = relative(repoRoot, artifact).replace(/\\/g, "/"); |
| 51 | + const fileRelative = relative(repoRoot, file.path).replace(/\\/g, "/"); |
46 | 52 |
|
47 | | - // We host all files under docs, will resolve as a relative link |
48 | | - if ( |
49 | | - artifactRelative.startsWith("packages/cursorless-org-docs/src/docs/") |
50 | | - ) { |
| 53 | + // We host all files under docs. Will resolve as a relative link, but |
| 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 | + } |
51 | 64 | return; |
52 | 65 | } |
53 | 66 |
|
54 | | - const repoLink = |
55 | | - "https://github.com/cursorless-dev/cursorless/tree/main/"; |
56 | | - const linkToRepositoryArtifact = repoLink.concat(artifactRelative); |
57 | | - |
58 | | - node.url = linkToRepositoryArtifact; |
| 67 | + node.url = repoLink + artifactRelative; |
59 | 68 | }); |
60 | 69 | }; |
61 | 70 | } |
62 | 71 |
|
| 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 | + |
63 | 85 | const config: Config = { |
64 | 86 | title: "Cursorless", |
65 | 87 | tagline: "Structural voice coding at the speed of thought", |
@@ -99,6 +121,7 @@ const config: Config = { |
99 | 121 | ], |
100 | 122 | onBrokenLinks: "throw", |
101 | 123 | onBrokenMarkdownLinks: "throw", |
| 124 | + onBrokenAnchors: "throw", |
102 | 125 | trailingSlash: true, |
103 | 126 |
|
104 | 127 | presets: [ |
|
0 commit comments