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

Fix #305 #527

Merged
merged 11 commits into from
Aug 8, 2019
Merged

Fix #305 #527

merged 11 commits into from
Aug 8, 2019

Conversation

iAdramelk
Copy link
Contributor

This PR should fix #305. It contains following changes.

New format for sidebar.json. It removes distinction between sections and files and allows any number of nesting levels.

New format (full):

{
  "slug": "metrics",
  "label": "metrics",
  "source": "metrics/index.md",
  "children": []
}

slug - Required. URL part for the current link, will be also used as URL prefix for all children of the current category. Example: metrics -> /doc/metrics. For the child addof metrics -> /doc/metrics/add.

label - name of the menu item. If not set, it will be generated from slug using startCase from lodash. Generating from slug example: get-started -> Get Started.

source -> name of the corresponding .md file in the filesystem. If not set, it will be generated from slug. Generating from slug example: metrics -> metrics.md. For legacy reasons you can explicitly set this field to false, in this case after user select this item, its first child will be shown instead.

children - optional array of child items.

You can also use short syntax. metrics is the same as { "slug": "metrics" }.

This config is parsed using new src/Documentation/SidebarMenu/helper.js file. It will normalize data from "sidebar.json" and generate all necessary field. It's output structure is:

{
  label: "Add Files or Directories",
  path: "/doc/get-started/add-files",
  source: "/static/docs/get-started/add-files.md",
  prev: "/doc/get-started/configure",
  next: "/doc/get-started/share-data",
  children: []
}

This way we don't need to calculate them in react component anymore.

This file also exports two new helper functions:

getItemByPath - recursively search for items in sidebar, that matches given path. It also resolve some edge cases (like source = false, and /doc/ route).
getParentsListFromPath - returns all parents paths from current path. Example: /doc/commands-reference/destroy will return ['/doc/commands-reference', '/doc/commands-reference/destroy']. Used to make nescessary menu items active.

pages/doc.js, src/Documentation/Markdown/Markdown.js and src/Documentation/SidebarMenu/SidebarMenu.js are updated accordingly, all business logic related to parsing sidebar.json are removed from them and replaced with data from new normalized menu items.

Difference from the current version:

  1. Prev/Next are generated for all pages, not only for pages inside the same section.
  2. Right now each nesting level in menu has different left margin. In this version all nested levels always add 20px.

- Changed sidebar format so it can now have any number of levels
- All json transformations and normalizations moved to src/Documentation/SidebarMenu/helper.js and done on the first init
- Simplified docs.js, Markdown.js and SidebarMenu.js
@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 7, 2019 17:47 Inactive
@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 7, 2019 18:01 Inactive
- Fix bug then parent was added to prev/next order after children
- Fix bug the prev item didn’t have source and was redirecting back to first child
- Add check for items with source = false and without children, they throw erros now
@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 7, 2019 18:19 Inactive
const FILE_ROOT = '/static/docs/'
const FILE_EXTENSION = '.md'

let prevReference // We will save prev item reference here to use for the next and prev fields
Copy link
Member

Choose a reason for hiding this comment

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

it's definitely > 80 symbols. I would recommend to install the pre-commit hook: https://dvc.org/doc/user-guide/contributing-documentation#development-environment

Copy link
Member

Choose a reason for hiding this comment

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

and run the prettier from the command line once again to reformat what has been committed already

Copy link
Contributor Author

@iAdramelk iAdramelk Aug 7, 2019

Choose a reason for hiding this comment

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

I think prettier ignores comments, so it probably didn't help. But I will fix comment lengths.

Copy link
Member

Choose a reason for hiding this comment

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

you might be right - it just does not know what can be done with it.

pages/doc.js Outdated
if (!item) {
this.setState({ pageNotFound: true, currentItem: {} })

return
Copy link
Member

Choose a reason for hiding this comment

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

it's probably better to use else instead of an explicit return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will change it.

Fixed bug with nested intems with osurce false, it required large refactor of the normalization function.
@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 7, 2019 20:19 Inactive
}

function normalizeSidebar(data, parentPath, currentSidebar) {
data.forEach(item => {
Copy link
Member

Choose a reason for hiding this comment

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

ie11? not that it's very important to keep supporting it, but if it's the only that breaks it I would do some workaround

Copy link
Contributor Author

Choose a reason for hiding this comment

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

forEach works in IE starting with IE9 according to https://caniuse.com/#feat=es5 , so it should not be a problem.


const { label, slug, source } = item

// If prev item don't have source we need to recirsively search for it
Copy link
Member

Choose a reason for hiding this comment

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

minor: don't -> doesn't

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

// Global cache vars used in normalization

let prevReference // Save last item here to generate the prev field
const normalizedSidebar = [] // Current state of sidebar to search for prev
Copy link
Member

Choose a reason for hiding this comment

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

Current state of sidebar to search for - a bit misleading. It's not only a "cache" to search for prev. This is what we export then as a final result, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will try to move them inside function and if I wasn't able to, will change text.

}
}

// Global cache vars used in normalization
Copy link
Member

@shcheklein shcheklein Aug 7, 2019

Choose a reason for hiding this comment

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

can we pass them to functions, do we actually need a global prevReference, for example?

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 have an idea how it can be done, but I'm not sure that it will make code easier to understand, but I will try today.

@shcheklein
Copy link
Member

@iAdramelk https://dvc-org-pr-527.herokuapp.com/doc/understanding-dvc/what-is-dvc.md - not found page is broken + the page itself is broken?

- Fix typo in comment text
- Fix error in slug name in sidebar.json
- Move fetch inside else clause
@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 8, 2019 10:52 Inactive
@iAdramelk
Copy link
Contributor Author

@shcheklein Thie problem with https://dvc-org-pr-527.herokuapp.com/doc/understanding-dvc/what-is-dvc.md is that I forgot to remove .md extension form the slug field, my bad. Fixed it and checked other pages to make sure that this is the only one.

@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 8, 2019 11:11 Inactive
@iAdramelk
Copy link
Contributor Author

Yep, removed prevReference and current result from global scope successfully.

@shcheklein shcheklein temporarily deployed to dvc-org-pr-527 August 8, 2019 11:21 Inactive
import { getParentsListFromPath } from './helper'

function SidebarMenuItem({ children, label, path, activePaths, onNavigate }) {
const isActive = activePaths && activePaths.includes(path)
Copy link
Member

Choose a reason for hiding this comment

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

use lodash.includes, otherwise we are breaking compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, will change.

@shcheklein shcheklein requested a deployment to dvc-org-pr-527 August 8, 2019 16:43 Abandoned
Copy link
Member

@shcheklein shcheklein left a comment

Choose a reason for hiding this comment

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

Amazing stuff 🎉 :)

@shcheklein shcheklein merged commit 765ffc5 into iterative:master Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support directories for the third level documentation
2 participants