mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 12:47:14 +02:00
feat(v2): allow prepending of baseUrl to href in navbar and footer (#2746)
This commit is contained in:
parent
dd1ad5d943
commit
be7367b2f8
4 changed files with 30 additions and 7 deletions
|
@ -13,8 +13,10 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||||
import useBaseUrl from '@docusaurus/useBaseUrl';
|
import useBaseUrl from '@docusaurus/useBaseUrl';
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
function FooterLink({to, href, label, ...props}) {
|
function FooterLink({to, href, label, prependBaseUrlToHref, ...props}) {
|
||||||
const toUrl = useBaseUrl(to);
|
const toUrl = useBaseUrl(to);
|
||||||
|
const normalizedHref = useBaseUrl(href, true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
className="footer__link-item"
|
className="footer__link-item"
|
||||||
|
@ -22,7 +24,7 @@ function FooterLink({to, href, label, ...props}) {
|
||||||
? {
|
? {
|
||||||
target: '_blank',
|
target: '_blank',
|
||||||
rel: 'noopener noreferrer',
|
rel: 'noopener noreferrer',
|
||||||
href,
|
href: prependBaseUrlToHref ? normalizedHref : href,
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
to: toUrl,
|
to: toUrl,
|
||||||
|
|
|
@ -26,10 +26,12 @@ function NavLink({
|
||||||
href,
|
href,
|
||||||
label,
|
label,
|
||||||
activeClassName = 'navbar__link--active',
|
activeClassName = 'navbar__link--active',
|
||||||
|
prependBaseUrlToHref,
|
||||||
...props
|
...props
|
||||||
}) {
|
}) {
|
||||||
const toUrl = useBaseUrl(to);
|
const toUrl = useBaseUrl(to);
|
||||||
const activeBaseUrl = useBaseUrl(activeBasePath);
|
const activeBaseUrl = useBaseUrl(activeBasePath);
|
||||||
|
const normalizedHref = useBaseUrl(href, true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
|
@ -37,7 +39,7 @@ function NavLink({
|
||||||
? {
|
? {
|
||||||
target: '_blank',
|
target: '_blank',
|
||||||
rel: 'noopener noreferrer',
|
rel: 'noopener noreferrer',
|
||||||
href,
|
href: prependBaseUrlToHref ? normalizedHref : href,
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
isNavLink: true,
|
isNavLink: true,
|
||||||
|
|
|
@ -7,7 +7,10 @@
|
||||||
|
|
||||||
import useDocusaurusContext from './useDocusaurusContext';
|
import useDocusaurusContext from './useDocusaurusContext';
|
||||||
|
|
||||||
export default function useBaseUrl(url: string): string {
|
export default function useBaseUrl(
|
||||||
|
url: string,
|
||||||
|
forcePrependBaseUrl: boolean = false,
|
||||||
|
): string {
|
||||||
const {siteConfig} = useDocusaurusContext();
|
const {siteConfig} = useDocusaurusContext();
|
||||||
const {baseUrl = '/'} = siteConfig || {};
|
const {baseUrl = '/'} = siteConfig || {};
|
||||||
|
|
||||||
|
@ -15,6 +18,10 @@ export default function useBaseUrl(url: string): string {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (forcePrependBaseUrl) {
|
||||||
|
return baseUrl + url;
|
||||||
|
}
|
||||||
|
|
||||||
const externalRegex = /^(https?:|\/\/)/;
|
const externalRegex = /^(https?:|\/\/)/;
|
||||||
if (externalRegex.test(url)) {
|
if (externalRegex.test(url)) {
|
||||||
return url;
|
return url;
|
||||||
|
|
|
@ -132,13 +132,23 @@ module.exports = {
|
||||||
navbar: {
|
navbar: {
|
||||||
links: [
|
links: [
|
||||||
{
|
{
|
||||||
|
// Client-side routing, used for navigating within the website.
|
||||||
|
// The baseUrl will be automatically prepended to this value.
|
||||||
to: 'docs/introduction',
|
to: 'docs/introduction',
|
||||||
|
// A full-page navigation, used for navigating outside of the website.
|
||||||
|
// You should only use either `to` or `href`.
|
||||||
|
href: 'https://www.facebook.com',
|
||||||
|
// Prepends the baseUrl to href values.
|
||||||
|
prependBaseUrlToHref: true,
|
||||||
|
// The string to be shown.
|
||||||
label: 'Introduction',
|
label: 'Introduction',
|
||||||
|
// Left or right side of the navbar.
|
||||||
position: 'left', // or 'right'
|
position: 'left', // or 'right'
|
||||||
// To apply the active class styling on all
|
// To apply the active class styling on all
|
||||||
// routes starting with this path.
|
// routes starting with this path.
|
||||||
activeBasePath: 'docs',
|
activeBasePath: 'docs',
|
||||||
className: '', // Custom CSS class (for styling any item)
|
// Custom CSS class (for styling any item).
|
||||||
|
className: '',
|
||||||
},
|
},
|
||||||
// ... other links
|
// ... other links
|
||||||
],
|
],
|
||||||
|
@ -148,7 +158,7 @@ module.exports = {
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Outbound links automatically get `target="_blank" rel="noopener noreferrer"` attributes.
|
Outbound (external) links automatically get `target="_blank" rel="noopener noreferrer"` attributes.
|
||||||
|
|
||||||
### Navbar Dropdown
|
### Navbar Dropdown
|
||||||
|
|
||||||
|
@ -200,7 +210,9 @@ module.exports = {
|
||||||
|
|
||||||
## Footer
|
## Footer
|
||||||
|
|
||||||
## `CodeBlock`
|
TODO.
|
||||||
|
|
||||||
|
## CodeBlock
|
||||||
|
|
||||||
Docusaurus uses [Prism React Renderer](https://github.com/FormidableLabs/prism-react-renderer) to highlight code blocks.
|
Docusaurus uses [Prism React Renderer](https://github.com/FormidableLabs/prism-react-renderer) to highlight code blocks.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue