-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): Allow activeBasePath to be a RegExp #2683
Conversation
Deploy preview for docusaurus-2 failed. Built with commit fa899cd https://app.netlify.com/sites/docusaurus-2/deploys/5ea71dce98a45a00067b735e |
return location.pathname.startsWith(activeBaseUrl); | ||
} | ||
|
||
throw new Error( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not use throw errors on invalid type checking, I think this should be removed for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, will do
@@ -29,7 +29,8 @@ function NavLink({ | |||
...props | |||
}) { | |||
const toUrl = useBaseUrl(to); | |||
const activeBaseUrl = useBaseUrl(activeBasePath); | |||
const activeBaseUrl = useBaseUrl(activeBasePath.toString()); | |||
const activeBasePathIsRegex = typeof activeBasePath === 'object'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof activeBasePath === 'object';
Why not activeBasePath instanceof RegExp
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, there's a bug. For some reason, none of the methods I've used to check if activeBaseUrl
is a Regular Expression work. It's almost like React Context breaks RegExps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, activeBasePath
shows up (when it should be a RegExp) as an empty object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But currently it looks rather unreliable, we need to find out what exactly is the matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost think there's a bug, where React Context can't handle Regular Expressions correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to investigate this issue.
@@ -137,6 +137,8 @@ module.exports = { | |||
position: 'left', // or 'right' | |||
// To apply the active class styling on all | |||
// routes starting with this path. | |||
// This can also be a Regular Expression. | |||
// If so, it will be tested against the current URL | |||
activeBasePath: 'docs', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a real-word example regex for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@lex111 problem example: const {
siteConfig: {
themeConfig: {
navbar: {title, links = [], hideOnScroll = false} = {},
disableDarkMode = false,
},
},
isClient,
} = useDocusaurusContext();
console.log(links[0].activeBasePath) Where navbar: {
hideOnScroll: true,
title: 'Docusaurus',
logo: {
alt: 'Docusaurus Logo',
src: 'img/docusaurus.svg',
srcDark: 'img/docusaurus_keytar.svg',
},
links: [
{
label: 'Docs',
to: 'docs/introduction', // "fake" link
position: 'left',
activeBasePath: /docs/,
... Logs:
|
I have no idea what's wrong, but somehow it seems like |
Look at |
@yangshun yep, that's the problem. Perhaps we create a new string property called |
Yep sounds good! |
@yangshun sweet. I'll close this PR and open up a new one 😃 |
Motivation
Closes #2574. Allows
activeBasePath
to be a Regular Expression as well as a string.Have you read the Contributing Guidelines on pull requests?
Yep
Test Plan
Run website and make sure that nav links are highlighted correctly. Change
activeBasePath
to a Regular Expression and make sure nav links are highlighted correctly.Related PRs
No related PRs. I included a slight edit to the docs in this PR -- I can move that to a separate PR if necessary.