From 75644904ddd28ce23cb05bd9f56450e33d74de50 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 29 Feb 2020 13:42:45 +0300 Subject: [PATCH 1/2] feat(v2): allow specify custom target for logo link --- .../src/theme/Navbar/index.js | 18 +++++++++++------- website/docs/theme-classic.md | 5 ++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/Navbar/index.js b/packages/docusaurus-theme-classic/src/theme/Navbar/index.js index 5f9f0c0a60a2..5f3b3de77e99 100644 --- a/packages/docusaurus-theme-classic/src/theme/Navbar/index.js +++ b/packages/docusaurus-theme-classic/src/theme/Navbar/index.js @@ -77,13 +77,17 @@ function Navbar() { ); const logoLink = logo.href || baseUrl; - const isExternalLogoLink = /http/.test(logoLink); - const logoLinkProps = isExternalLogoLink - ? { - rel: 'noopener noreferrer', - target: '_blank', - } - : null; + let logoLinkProps = {}; + + if (logo.target) { + logoLinkProps = {target: logo.target}; + } else if (/http/.test(logoLink)) { + logoLinkProps = { + rel: 'noopener noreferrer', + target: '_blank', + }; + } + const logoSrc = logo.srcDark && isDarkTheme ? logo.srcDark : logo.src; const logoImageUrl = useBaseUrl(logoSrc); diff --git a/website/docs/theme-classic.md b/website/docs/theme-classic.md index c2d088041d98..174217d0dddb 100644 --- a/website/docs/theme-classic.md +++ b/website/docs/theme-classic.md @@ -47,7 +47,9 @@ module.exports = { ### Navbar Title & Logo -You can add a logo and title to the navbar via `themeConfig.navbar`. Logo can be placed in [static folder](static-assets.md). Logo URL is set to base URL of your site by default. Although you can specify your own URL for the logo, if it is an external link, it will open in a new tab. You can also set a different logo for dark mode. +You can add a logo and title to the navbar via `themeConfig.navbar`. Logo can be placed in [static folder](static-assets.md). Logo URL is set to base URL of your site by default. Although you can specify your own URL for the logo, if it is an external link, it will open in a new tab. In addition, you can override a value for the target attribute of logo link, it can come in handy if you are hosting docs website in a subdirectory of your main website, and in which case you probably do not need a link in the logo to the main website will open in a new tab. + +To improve dark mode support, you can also set a different logo for this mode. ```js {6-12} // docusaurus.config.js @@ -61,6 +63,7 @@ module.exports = { src: 'img/logo.svg', srcDark: 'img/logo_dark.svg', // default to logo.src href: 'https://v2.docusaurus.io/', // default to siteConfig.baseUrl + target: '_self', // by default, this value is calculated based on the `href` attribute (the external link will open in a new tab, all others in the current one) }, }, ... From e6768afcafd08992a2e2adb6b8728ee6fa094f02 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 29 Feb 2020 14:06:01 +0300 Subject: [PATCH 2/2] refactor: use isInternalUrl --- packages/docusaurus-theme-classic/src/theme/Navbar/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-classic/src/theme/Navbar/index.js b/packages/docusaurus-theme-classic/src/theme/Navbar/index.js index 5f3b3de77e99..b4a2e3c3edd3 100644 --- a/packages/docusaurus-theme-classic/src/theme/Navbar/index.js +++ b/packages/docusaurus-theme-classic/src/theme/Navbar/index.js @@ -9,6 +9,7 @@ import React, {useCallback, useState} from 'react'; import Link from '@docusaurus/Link'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import useBaseUrl from '@docusaurus/useBaseUrl'; +import isInternalUrl from '@docusaurus/isInternalUrl'; import SearchBar from '@theme/SearchBar'; import Toggle from '@theme/Toggle'; @@ -81,7 +82,7 @@ function Navbar() { if (logo.target) { logoLinkProps = {target: logo.target}; - } else if (/http/.test(logoLink)) { + } else if (!isInternalUrl(logoLink)) { logoLinkProps = { rel: 'noopener noreferrer', target: '_blank',