mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 20:57:17 +02:00
fix(v2): absolute Links should be automatically prefixed by baseurl (#3143)
* put back the automatic withBaseUrl call, but only for urls starting with / (because useBaseUrl behavior is buggy and forces relative links to absolute links) * fix Link typo
This commit is contained in:
parent
4247fc4bb1
commit
e1b2963f31
3 changed files with 25 additions and 3 deletions
|
@ -11,6 +11,7 @@ import {NavLink, Link as RRLink} from 'react-router-dom';
|
||||||
import isInternalUrl from './isInternalUrl';
|
import isInternalUrl from './isInternalUrl';
|
||||||
import ExecutionEnvironment from './ExecutionEnvironment';
|
import ExecutionEnvironment from './ExecutionEnvironment';
|
||||||
import {useLinksCollector} from '../LinksCollector';
|
import {useLinksCollector} from '../LinksCollector';
|
||||||
|
import {useBaseUrlUtils} from './useBaseUrl';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
|
@ -29,6 +30,13 @@ interface Props {
|
||||||
readonly 'data-noBrokenLinkCheck'?: boolean;
|
readonly 'data-noBrokenLinkCheck'?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO all this wouldn't be necessary if we used ReactRouter basename feature
|
||||||
|
// We don't automatically add base urls to all links,
|
||||||
|
// only the "safe" ones, starting with / (like /docs/introduction)
|
||||||
|
// this is because useBaseUrl() actually transforms relative links
|
||||||
|
// like "introduction" to "/baseUrl/introduction" => bad behavior to fix
|
||||||
|
const shouldAddBaseUrlAutomatically = (to: string) => to.startsWith('/');
|
||||||
|
|
||||||
function Link({
|
function Link({
|
||||||
isNavLink,
|
isNavLink,
|
||||||
to,
|
to,
|
||||||
|
@ -37,13 +45,27 @@ function Link({
|
||||||
'data-noBrokenLinkCheck': noBrokenLinkCheck,
|
'data-noBrokenLinkCheck': noBrokenLinkCheck,
|
||||||
...props
|
...props
|
||||||
}: Props): JSX.Element {
|
}: Props): JSX.Element {
|
||||||
|
const {withBaseUrl} = useBaseUrlUtils();
|
||||||
const linksCollector = useLinksCollector();
|
const linksCollector = useLinksCollector();
|
||||||
|
|
||||||
// IMPORTANT: using to or href should not change anything
|
// IMPORTANT: using to or href should not change anything
|
||||||
// For example, MDX links will ALWAYS give us the href props
|
// For example, MDX links will ALWAYS give us the href props
|
||||||
// Using one prop or the other should not be used to distinguish
|
// Using one prop or the other should not be used to distinguish
|
||||||
// internal links (/docs/myDoc) from external links (https://github.com)
|
// internal links (/docs/myDoc) from external links (https://github.com)
|
||||||
const targetLink = to || href;
|
const targetLinkUnprefixed = to || href;
|
||||||
|
|
||||||
|
function maybeAddBaseUrl(str: string) {
|
||||||
|
return shouldAddBaseUrlAutomatically(str)
|
||||||
|
? withBaseUrl(str)
|
||||||
|
: targetLinkUnprefixed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO we should use ReactRouter basename feature instead!
|
||||||
|
// Automatically apply base url in links that start with /
|
||||||
|
const targetLink =
|
||||||
|
typeof targetLinkUnprefixed !== 'undefined'
|
||||||
|
? maybeAddBaseUrl(targetLinkUnprefixed)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
const isInternal = isInternalUrl(targetLink);
|
const isInternal = isInternalUrl(targetLink);
|
||||||
const preloaded = useRef(false);
|
const preloaded = useRef(false);
|
||||||
|
|
|
@ -44,7 +44,7 @@ The `searchParameters` option used to be named `algoliaOptions` in Docusaurus v1
|
||||||
|
|
||||||
By default, DocSearch comes with a fine-tuned theme that was designed for accessibility, making sure that colors and contrasts respect standards.
|
By default, DocSearch comes with a fine-tuned theme that was designed for accessibility, making sure that colors and contrasts respect standards.
|
||||||
|
|
||||||
Still, you can reuse the [Infima CSS variables](/docs/styling-layout/#styling-your-site-with-infima) from Docusaurus to style DocSearch by editing the `/src/css/custom.css` file.
|
Still, you can reuse the [Infima CSS variables](styling-layout#styling-your-site-with-infima) from Docusaurus to style DocSearch by editing the `/src/css/custom.css` file.
|
||||||
|
|
||||||
```css title="/src/css/custom.css"
|
```css title="/src/css/custom.css"
|
||||||
html[data-theme='light'] .DocSearch {
|
html[data-theme='light'] .DocSearch {
|
||||||
|
|
|
@ -44,7 +44,7 @@ The `searchParameters` option used to be named `algoliaOptions` in Docusaurus v1
|
||||||
|
|
||||||
By default, DocSearch comes with a fine-tuned theme that was designed for accessibility, making sure that colors and contrasts respect standards.
|
By default, DocSearch comes with a fine-tuned theme that was designed for accessibility, making sure that colors and contrasts respect standards.
|
||||||
|
|
||||||
Still, you can reuse the [Infima CSS variables](/docs/styling-layout/#styling-your-site-with-infima) from Docusaurus to style DocSearch by editing the `/src/css/custom.css` file.
|
Still, you can reuse the [Infima CSS variables](styling-layout#styling-your-site-with-infima) from Docusaurus to style DocSearch by editing the `/src/css/custom.css` file.
|
||||||
|
|
||||||
```css title="/src/css/custom.css"
|
```css title="/src/css/custom.css"
|
||||||
html[data-theme='light'] .DocSearch {
|
html[data-theme='light'] .DocSearch {
|
||||||
|
|
Loading…
Add table
Reference in a new issue