From de526e1ab288455c0e09b7cb06bff7abe006e05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Fri, 12 Aug 2022 12:50:12 +0200 Subject: [PATCH] feat(plugin-docs): docs sidebar item link: support "autoAddBaseUrl" attribute (#7949) --- .../src/sidebars/types.ts | 1 + .../src/sidebars/validation.ts | 1 + .../src/theme/DocSidebarItem/Link/index.tsx | 3 ++- website/_dogfooding/docs-tests-sidebars.js | 23 ++++++++++++++++--- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts index 1a8746574a5e..555004846ea4 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts @@ -44,6 +44,7 @@ export type SidebarItemLink = SidebarItemBase & { type: 'link'; href: string; label: string; + autoAddBaseUrl?: boolean; }; export type SidebarItemAutogenerated = SidebarItemBase & { diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts index f1cc5a168e3c..0273132360d1 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/validation.ts @@ -59,6 +59,7 @@ const sidebarItemHtmlSchema = sidebarItemBaseSchema.append({ const sidebarItemLinkSchema = sidebarItemBaseSchema.append({ type: 'link', href: URISchema.required(), + autoAddBaseUrl: Joi.boolean(), label: Joi.string() .required() .messages({'any.unknown': '"label" must be a string'}), diff --git a/packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Link/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Link/index.tsx index a61ac7a02793..ffa8b0e89602 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Link/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocSidebarItem/Link/index.tsx @@ -24,7 +24,7 @@ export default function DocSidebarItemLink({ index, ...props }: Props): JSX.Element { - const {href, label, className} = item; + const {href, label, className, autoAddBaseUrl} = item; const isActive = isActiveSidebarItem(item, activePath); const isInternalLink = isInternalUrl(href); return ( @@ -44,6 +44,7 @@ export default function DocSidebarItemLink({ 'menu__link--active': isActive, }, )} + autoAddBaseUrl={autoAddBaseUrl} aria-current={isActive ? 'page' : undefined} to={href} {...(isInternalLink && { diff --git a/website/_dogfooding/docs-tests-sidebars.js b/website/_dogfooding/docs-tests-sidebars.js index 0c2783feeda3..d458e7eb517d 100644 --- a/website/_dogfooding/docs-tests-sidebars.js +++ b/website/_dogfooding/docs-tests-sidebars.js @@ -71,10 +71,27 @@ const sidebars = { ], }, { - type: 'link', - label: 'External link', - href: 'https://github.com/facebook/docusaurus', + type: 'category', + label: 'Link tests', className: 'red', + items: [ + { + type: 'link', + label: 'External link absolute', + href: 'https://github.com/facebook/docusaurus', + }, + { + type: 'link', + label: 'pathname:/// link', + href: 'pathname:///some/local/path', + }, + { + type: 'link', + label: 'pathname:/// link (no baseUrl)', + href: 'pathname:///some/local/path', + autoAddBaseUrl: false, + }, + ], }, { type: 'category',