From c1716096de14637dfa01b198b0d24d3d86c8c402 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sat, 29 Feb 2020 14:27:56 +0300 Subject: [PATCH] feat(v2): allow specifying custom target for logo link (#2344) * feat(v2): allow specify custom target for logo link * refactor: use isInternalUrl --- .../src/theme/Navbar/index.js | 19 ++++++++++++------- website/docs/theme-classic.md | 5 ++++- 2 files changed, 16 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..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'; @@ -77,13 +78,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 (!isInternalUrl(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) }, }, ...