-
Notifications
You must be signed in to change notification settings - Fork 529
feat: add same deployment link util #2546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
625e295
768d2c1
1e2c20b
9c5be3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| * @vinckr @aeneasr @zepatrik @piotrmsc @unatasha8 | ||
| * @vinckr @aeneasr @zepatrik @piotrmsc @unatasha8 @wassimoo | ||
| *.md @vinckr @aeneasr @unatasha8 | ||
| *.mdx @vinckr @aeneasr @unatasha8 | ||
| /code-examples/ @aeneasr @zepatrik @piotrmsc | ||
| /code-examples/ @aeneasr @zepatrik @piotrmsc @wassimoo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import React, { useEffect, type ReactNode } from "react" | ||
| import Link from "@docusaurus/Link" | ||
| import { useLocation } from "@docusaurus/router" | ||
| import { useAllDocsData } from "@docusaurus/plugin-content-docs/client" | ||
|
|
||
| type DocsDeploymentSegment = "network" | "oel" | "oss" | ||
|
|
||
| type SameDeploymentLinkProps = { | ||
| to: string | ||
| // Optional overrides for the link target based on the deployment segment | ||
| network?: string | ||
| oel?: string | ||
| oss?: string | ||
| children: ReactNode | ||
| } | ||
|
|
||
| const DEFAULT_DOCS_PLUGIN_ID = "default" | ||
|
|
||
| const DEPLOYMENT_SEGMENT_PATTERN = /\/(network|oel|oss)(?:\/|$)/ | ||
|
|
||
| const stripLeadingSlashes = (value: string) => value.replace(/^\/+/, "") | ||
| const normalizePath = (value: string) => `/${stripLeadingSlashes(value)}` | ||
|
|
||
| /** | ||
| * Internal docs link that keeps the reader on the current deployment (Network / OEL / OSS). | ||
| */ | ||
| export default function SameDeploymentLink({ | ||
| to, | ||
| network, | ||
| oel, | ||
| oss, | ||
| children, | ||
| }: SameDeploymentLinkProps): JSX.Element { | ||
| const { pathname } = useLocation() | ||
| const segment = | ||
| (pathname.match(DEPLOYMENT_SEGMENT_PATTERN)?.[1] as | ||
| | DocsDeploymentSegment | ||
| | undefined) ?? "network" | ||
|
|
||
| const rewriteToCurrentDeployment = (value: string): string => { | ||
| const path = normalizePath(value) | ||
| const match = path.match(DEPLOYMENT_SEGMENT_PATTERN) | ||
| if (match) { | ||
| // Replace explicit deployment segment with the current one. | ||
| return path.replace(DEPLOYMENT_SEGMENT_PATTERN, `/${segment}/`) | ||
| } | ||
| // Prefix when no deployment segment is present. | ||
| return `/${segment}${path}` | ||
| } | ||
|
|
||
| const overrideForCurrentDeployment = { network, oel, oss }[segment] | ||
| const href = overrideForCurrentDeployment | ||
| ? normalizePath(overrideForCurrentDeployment) | ||
| : rewriteToCurrentDeployment(to) | ||
| const allDocs = useAllDocsData() | ||
|
|
||
| useEffect(() => { | ||
| if (!DEPLOYMENT_SEGMENT_PATTERN.test(href)) return | ||
| const plugin = allDocs[DEFAULT_DOCS_PLUGIN_ID] | ||
| const version = | ||
| plugin?.versions.find((v) => v.isLast) ?? plugin?.versions[0] | ||
| if (!version?.docs?.length) return | ||
|
|
||
| const docId = stripLeadingSlashes(href) | ||
| const found = version.docs.some((d) => d.id === docId) | ||
| if (!found) { | ||
| const overrideMsg = overrideForCurrentDeployment | ||
| ? ` (override prop "${segment}" was used)` | ||
| : "" | ||
| console.warn( | ||
| `[SameDeploymentLink] No doc with id "${docId}". ` + | ||
| `Resolved href="${href}" from to="${to}" under deployment "${segment}" did not match any page in this build${overrideMsg}.`, | ||
| ) | ||
| } | ||
| }, [allDocs, href, overrideForCurrentDeployment, segment, to]) | ||
|
|
||
| return <Link to={href}>{children}</Link> | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |
| QuickstartsDeploymentProvider, | ||
| useQuickstartsDeployment, | ||
| } from "@site/src/contexts/QuickstartsDeploymentContext" | ||
| import { docsDeploymentFromPathname } from "@site/src/utils/docsDeploymentFromPathname" | ||
|
|
||
| const QUICKSTARTS_SIDEBAR = "quickstartsSidebar" | ||
|
|
||
|
|
@@ -79,13 +80,14 @@ export default function DocRootWrapper(props) { | |
| } | ||
| const { docElement, sidebarName, sidebarItems } = currentDocRouteMetadata | ||
| const pathname = props.location?.pathname ?? "" | ||
| const deploymentFromPath = docsDeploymentFromPathname(pathname) | ||
| const versionMetadata = useDocsVersion() ?? {} | ||
| const docsSidebars = versionMetadata.docsSidebars ?? {} | ||
|
|
||
| return ( | ||
| <div id="route-identifier" data-route={pathname}> | ||
| <HtmlClassNameProvider className={clsx(ThemeClassNames.page.docsDocPage)}> | ||
| <QuickstartsDeploymentProvider> | ||
| <QuickstartsDeploymentProvider initialDeployment={deploymentFromPath}> | ||
|
Comment on lines
+83
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit vs implicit “network” context is currently collapsed.
Suggested direction-import { docsDeploymentFromPathname } from "@site/src/utils/docsDeploymentFromPathname"
+import {
+ docsDeploymentFromPathname,
+ isExplicitDocsDeploymentPath,
+} from "@site/src/utils/docsDeploymentFromPathname"
const pathname = props.location?.pathname ?? ""
const deploymentFromPath = docsDeploymentFromPathname(pathname)
+ const explicitDeploymentPath = isExplicitDocsDeploymentPath(pathname)
- <QuickstartsDeploymentProvider initialDeployment={deploymentFromPath}>
+ <QuickstartsDeploymentProvider
+ initialDeployment={deploymentFromPath}
+ syncFromInitialDeployment={explicitDeploymentPath}
+ >Then gate the 🤖 Prompt for AI Agents |
||
| <DocRootContent | ||
| docElement={docElement} | ||
| sidebarName={sidebarName} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export type DocsDeploymentId = "network" | "oel" | "oss" | ||
|
|
||
| /** | ||
| * Infer the docs deployment from a pathname. | ||
| * | ||
| * Note: Some routes still use legacy `/docs/self-hosted/oel/...` paths. | ||
| */ | ||
| export function docsDeploymentFromPathname(pathname: string): DocsDeploymentId { | ||
| if (!pathname) return "network" | ||
|
|
||
| if (pathname.includes("/oel/") || pathname.includes("/self-hosted/oel/")) | ||
| return "oel" | ||
| if (pathname.includes("/oss/")) return "oss" | ||
| if (pathname.includes("/network/")) return "network" | ||
|
|
||
| return "network" | ||
| } | ||
|
|
||
| export function isExplicitDocsDeploymentPath(pathname: string): boolean { | ||
| if (!pathname) return false | ||
| return ( | ||
| pathname.includes("/network/") || | ||
| pathname.includes("/oel/") || | ||
| pathname.includes("/oss/") || | ||
| pathname.includes("/self-hosted/oel/") | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Override targets bypass deployment rewrite and can produce broken doc links.
overrideForCurrentDeploymentis only normalized, not deployment-rewritten. For values likeoel="kratos/self-hosted/intro",hrefbecomes/kratos/self-hosted/introinstead of a deployment-scoped docs route. This can silently navigate to non-existent pages.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents