Skip to content
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

Closed
wants to merge 2 commits into from
Closed

feat(v2): Allow activeBasePath to be a RegExp #2683

wants to merge 2 commits into from

Conversation

nebrelbug
Copy link
Contributor

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.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 27, 2020
@nebrelbug nebrelbug changed the title (Feat): Allow activeBasePath to be a RegExp feat(v2): Allow activeBasePath to be a RegExp Apr 27, 2020
@docusaurus-bot
Copy link
Contributor

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(
Copy link
Contributor

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.

Copy link
Contributor Author

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';
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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',
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@lex111 lex111 added this to the v2.0.0-alpha.54 milestone Apr 27, 2020
@nebrelbug
Copy link
Contributor Author

@lex111 problem example:

  const {
    siteConfig: {
      themeConfig: {
        navbar: {title, links = [], hideOnScroll = false} = {},
        disableDarkMode = false,
      },
    },
    isClient,
  } = useDocusaurusContext();
  console.log(links[0].activeBasePath)

Where docusaurus.config.js looks like:

    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: Object, an object which, when inspected, has 0 RegExp properties.

Object
__proto__:
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()

@nebrelbug
Copy link
Contributor Author

I have no idea what's wrong, but somehow it seems like useDocusaurusContext can't handle Regular Expressions.

@yangshun
Copy link
Contributor

Look at .docusaurus/docusaurus.config.js and find the corresponding value. I suspect the RegExp cannot be serialized.

@nebrelbug
Copy link
Contributor Author

@yangshun yep, that's the problem. activeBasePath: {}.

Perhaps we create a new string property called activeBaseTest, and, if it exists, use new RegExp() on it?

@yangshun
Copy link
Contributor

Perhaps we create a new string property called activeBaseTest, and, if it exists, use new RegExp() on it?

Yep sounds good!

@nebrelbug
Copy link
Contributor Author

@yangshun sweet. I'll close this PR and open up a new one 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(v2): allow activeBasePath to be a Regular Expression
5 participants