-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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: Page per section upgraded with page not found #993
Conversation
Codecov Report
|
This reverts commit fa145b3.
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.
Thanks!
Can we close #989 then?
src/index.js
Outdated
@@ -10,8 +10,20 @@ let codeRevision = 0; | |||
|
|||
/** Scrolls to origin when current window location hash points to an isolated view. */ | |||
const scrollToOrigin = () => { | |||
if (window.location.hash.indexOf('#!/') === 0) { | |||
window.scrollTo(0, 0); | |||
const hash = window.location.hash; |
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.
This code needs a lot of comments, meaningful variable names and splitting into multiple functions with meaningful names. It's impossible to understand what it does.
root: { | ||
maxWidth, | ||
margin: [[0, 'auto']], | ||
padding: space[4], |
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.
This padding creates extra whitespace, that is visible on the screenshot.
text={` | ||
# 404 | ||
|
||
## Sorry, this page isn't available |
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.
# Page not found
would be enough instead of both headings.
|
||
## Sorry, this page isn't available | ||
|
||
The link you followed may be broken, or the page may have been removed |
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.
Please add .
at the end ;-)
@@ -6,14 +6,15 @@ import Para from 'rsg-components/Para'; | |||
import Slot from 'rsg-components/Slot'; | |||
import PlaygroundRenderer from 'rsg-components/Playground/PlaygroundRenderer'; | |||
import { EXAMPLE_TAB_CODE_EDITOR } from '../slots'; | |||
import { DisplayModes } from '../../consts'; | |||
import { DisplayModes, ExampleMode } from '../../consts'; |
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.
DisplayModes
, but ExampleMode
. Was that intentional?
src/utils/getInfoFromHash.js
Outdated
const index = parseInt(tokens[1], 10); | ||
const shouldIsolate = hash.substr(0, 3) === '#!/'; | ||
if (shouldIsolate || hash.substr(0, 2) === '#/') { | ||
const path = hash.replace(/\?id=[^]*/, ''); |
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.
This is very fragile, what if there will be &id=
? It will also remove all parameters after id
.
src/utils/getInfoFromHash.js
Outdated
const shouldIsolate = hash.substr(0, 3) === '#!/'; | ||
if (shouldIsolate || hash.substr(0, 2) === '#/') { | ||
const path = hash.replace(/\?id=[^]*/, ''); | ||
const tokens = path |
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 can split the first part at the very beginning with a regexp and avoid all substr
calls and magic numbers.
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’m wondering why not introduce a routing solution to the project? It would simplify thing a lot since we won’t need parse location ourselves I guess.
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.
@okonet Like history js?, but its possible that it necessary migrate several things.
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 don't think we need a router like React Router because we render a single component in any case except welcome screen. But using a library to parse URLs could be a good idea to avoid hard-to-read string operations and 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.
Yeah I agree complete router might be an overkill but let’s not do parsing ourselves
src/utils/getInfoFromHash.js
Outdated
if (hash.substr(0, 3) === '#!/') { | ||
const tokens = hash.substr(3).split('/'); | ||
const index = parseInt(tokens[1], 10); | ||
const shouldIsolate = hash.substr(0, 3) === '#!/'; |
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.
This function also needs splitting and comments.
src/utils/getPageTitle.js
Outdated
} else if (displayMode === DisplayModes.section) { | ||
return `${sections[0].name} — ${baseTitle}`; | ||
if (displayMode === DisplayModes.notFound) { | ||
return 'Error 404 - ' + baseTitle; |
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.
Error 404 → Page not found
src/utils/getRouteData.js
Outdated
displayMode = DisplayModes.component; | ||
let filteredSections; | ||
if (pagePerSection) { | ||
tokens.forEach((tokenName, index) => { |
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.
What is happening here?
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 agree with all Artem’s comments
@sapegin @okonet I added a new file to handle the hash. You can see the tests. And his implementation here react-styleguidist/src/utils/getInfoFromHash.js Lines 17 to 29 in ad5b8b2
react-styleguidist/src/index.js Lines 12 to 31 in ad5b8b2
|
I still think we should try something like https://github.com/unshiftio/url-parse instead of doing this manually. |
@sapegin I was thinking where use url-parse but I am not sure if we really need it because url-parse doesn't have a method to get the pages of hash and our router is something particular. I found path-to-regexp, it seems useful but it is more useful to set an URL with params Then found hasher, it's a good library because you can customized the pretend hash and you can get the router as array, but it has a strong dependency with location.hash, so it needs to integrate the router with the library when we only need an util. So I had decided to adapt the part of hasher that we need in handleHash file, it doesn't have a strong dependency, easy to test and isn't necessary a big migration. What do you think? |
Reach Router is tiny so I’m wondering if we should try using it. It should simply a lot of code imo. https://mobile.twitter.com/ryanflorence/status/1002959657235722240 |
If it really simplify code (keep in mind that we’re rendering the same component with different props depending on the URL), then I’m fine with it. But we’ll still need something to parse query strings. |
Let’s try router in a separate PR and finish this one. Do we have a conclusion? |
Yup, let’s keep this PR as is, the code is isolated at least ;-/ |
@sapegin it's strange that Travis told that there is an error when I only modified Markdown files. |
Restart usually helps, any CI can fail for many reasons not related to code changes ;-) |
Yes, yesterday I restarted twice and it had the same result
maybe I have to send another change to work fine, please @sapegin review my changes in docs. |
That should be fixed in master already. |
docs/Configuration.md
Outdated
@@ -282,6 +292,47 @@ module.exports = { | |||
} | |||
``` | |||
|
|||
You can set the number of subpages of each section by adding`sectionDepth` in each first level section. |
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 don't understand what that means ;-(
loaders/utils/getSections.js
Outdated
@@ -48,12 +51,19 @@ function processSection(section, config) { | |||
} | |||
content = requireIt(`!!${examplesLoader}!${contentAbsolutePath}`); | |||
} | |||
sectionDepth = |
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.
This needs some explanation: what is happening here? What is firstDepth
?
src/index.js
Outdated
const hash = window.location.hash; | ||
if (hasInHash(hash, '#/') || hasInHash(hash, '#!/')) { | ||
const element = document.scrollingElement || document.documentElement; // cross-browsers | ||
/** Extracts the id param of hash */ |
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.
Please don't use block comments instead of inline comments, also add an empty line before a comment to separate code paragraphs.
src/index.js
Outdated
window.scrollTo(0, 0); | ||
const hash = window.location.hash; | ||
if (hasInHash(hash, '#/') || hasInHash(hash, '#!/')) { | ||
const element = document.scrollingElement || document.documentElement; // cross-browsers |
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.
What this line is doing? What this comment means? Which browsers? Worth extracting to a method with a meaningful name.
src/utils/getPageTitle.js
Outdated
} else if (displayMode === DisplayModes.section) { | ||
return `${sections[0].name} — ${baseTitle}`; | ||
if (displayMode === DisplayModes.notFound) { | ||
return 'Error 404 - Page not found'; |
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.
Just “Page not found", I think I've mentioned this before.
src/utils/getRouteData.js
Outdated
|
||
if (pagePerSection) { | ||
// Try to filter each section for depth of each hash ["Documentation", "Files", "Button"] | ||
// When sectionDepth is major of 0, their children should be filtered |
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.
What “major of 0” means?
if (!sections.length) { | ||
displayMode = DisplayModes.notFound; | ||
} | ||
// The targetName takes the last of hashArray |
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.
This comment is repeating the code, but doesn't explain it.
src/utils/getUrl.js
Outdated
) { | ||
let url = pathname; | ||
|
||
if (takeHash) { | ||
url += hash.replace(/\?id=[^]*/, ''); |
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've mentioned before that this is very destructive regexp, it will cause issues.
src/utils/handleHash.js
Outdated
const separator = '/'; | ||
const hashValRegexp = /(.*)\?/; | ||
|
||
function escapeRegExp(str) { |
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.
Use escapeRegExp
from Lodash.
@@ -0,0 +1,73 @@ | |||
const defaultPrependHash = '#/'; |
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.
This fine needs a huge “HACK” comment explaining that this is a temporary solution.
It seems that there still is the bug when Travis tries to validate the branch, however, the PR was passed by Travis. |
docs/Configuration.md
Outdated
@@ -292,7 +292,7 @@ module.exports = { | |||
} | |||
``` | |||
|
|||
You can set the number of subpages of each section by adding`sectionDepth` in each first level section. | |||
If you want to isolate section's children as single pages, you can set up it adding `sectionDepth` in each section. Where `sectionDepth` it is the number of depth of routes that it will have each section. |
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.
you can set up it adding sectionDepth
in each section… → add the sectionDepth
parameter to a section, where the value is the number of section levels that will be shown as single pages?
Though I’m still not 100% sure I understand now it works ;-/
docs/Configuration.md
Outdated
} | ||
] | ||
} | ||
``` | ||
|
||
For default `sectionDepth` |
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.
Should this be removed or finished?
src/index.js
Outdated
let scrollTop = 0; | ||
|
||
if (idHashParam) { | ||
// Searches the node with the same id | ||
// Searches the node with the same id and takes his offsetTop |
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.
The comment repeats the code but doesn't explain the intent.
Yeah, that's weird. I don't understand the difference between push and pr anyway ;-/ I think the code is fine now, just a few docs tweaks left. |
Could you please merge master? Is there anything else left or we can make 7.1 release with this? |
@sapegin I think there is no more, I am going to solve the conflict with the master branch |
Merging this to maser wasn't a good idea, I'm going to revert it. |
Apologies I thought you mean that, tell me if I can help in any way |
Maybe I wasn't clear enough, I meant to merge master into your branch to resolve merge conflicts. I've reverted the merge now, but you will need to open a new PR I think (renaming the branch and opening a new PR should work). P. S. You can try to make a new release if you want, there are some docs about the process. Let me know if that's something you'd like to do ;-) |
Perfect, thanks! I will work it :) |
Ping me if you have any questions, there's a milestone for the next release. |
🎉 This PR is included in version 7.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This PR is a feature of this issue:
#892
And started with this branch
#989
Also added a page not found