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

Add hide_title metadata that hides the title text on the top of the doc #540

Merged
merged 7 commits into from
Apr 12, 2018

Conversation

MisterTea
Copy link
Contributor

Motivation

Optionally hide the title from the top of the doc so people can customize the title

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Running with this on my test site

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 10, 2018
@yangshun
Copy link
Contributor

@MisterTea Did you run prettier before submitting this PR?

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

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

Hi @MisterTea. Thanks for the PR. I have a couple of questions/suggestions.

It looks like hide_title would be part of the doc header metadata as discussed here: https://docusaurus.io/docs/en/doc-markdown.html

  1. Can you update the docs for this new field? https://github.com/facebook/Docusaurus/blob/master/docs/api-doc-markdown.md
  2. Run npm run prettier to make sure the code is formatted correctly.
  3. Then I have some code comments inline...

@@ -23,6 +23,15 @@ class DocsLayout extends React.Component {
if (this.props.Doc) {
DocComponent = this.props.Doc;
}
let docTitle = i18n
Copy link
Contributor

Choose a reason for hiding this comment

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

Someone might put

hide_title: false

in the doc header.

So, how about instead we do the converse here:

let docTitle = null;
// If the attribute does not exist or is set to false, we set a title.
if (!this.props.metadata.hide_title || this.props.metadata.hide_title.toLowerCase() === 'false') {
  docTitle = ....
}

lib/core/Doc.js Outdated
@@ -60,11 +60,18 @@ class Doc extends React.Component {
</a>
);
}

let title = null;
if (this.props.title) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a comment here saying something like:

// this.props.title could be set to null if hide_title was set in the doc metadata

@JoelMarcey
Copy link
Contributor

@yangshun Can you look at this too? I think it is right, but just wanted to make sure I wasn't missing anything here.

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Nice work @MisterTea ! I left some inline comments on how the code can be simplified. We should try as much not to change too much of the existing logic and it seems like it's possible for this feature to do so.

@@ -13,6 +13,8 @@ Documents use the following markdown header fields that are enclosed by a line `

`title`: The title of your document. If this field is not present, the document's `title` will default to its `id`.

`hide_title`: Whether to hide the title at the top of each documentation page. If this is `null` or set to anything besides `false`, the title will be displayed.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better if this field was restricted to a boolean value for simplicity. That way, we can shorten the description to the first sentence and do less checks in our code.

Copy link
Contributor

Choose a reason for hiding this comment

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

@yangshun How do you control what a user might enter in the metadata for the doc though? Someone could put:

hide_title: 'blahblah'

How do we treat that?

Or are you saying that just the fact that hide_title is explicitly put in the metadata it is always truthy, no matter the associated string?

Copy link
Contributor

@yangshun yangshun Apr 11, 2018

Choose a reason for hiding this comment

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

Yes I mean we will just determine the value based on whether it's truthy/falsy if a non-true/false value was provided. It'll be too much of a hassle to do type checks manually.
I believe Front matter supports boolean values (I will have to check on that though).

If a use puts 'blahblah', it will be treated as true as it is a non-empty string.

lib/core/Doc.js Outdated
@@ -60,11 +60,17 @@ class Doc extends React.Component {
</a>
);
}

let title = null;
// this.props.title could be set to null if hide_title was set in the doc metadata
Copy link
Contributor

Choose a reason for hiding this comment

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

The title value should be kept independent of the showing/hiding logic. What you can do is to pass in the prop hideTitle into this component from the parent.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can also inline the conditional rendering logic into JSX since it's very short:

{this.props.hideTitle && <h1>{this.props.title}</h1>}

@@ -23,6 +23,18 @@ class DocsLayout extends React.Component {
if (this.props.Doc) {
DocComponent = this.props.Doc;
}
let docTitle = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

These stuff can remain. Pass this.props.metadata.hide_title as a prop to <DocComponent>/<Doc> instead to simplify things.

this.props.metadata.title
: this.props.metadata.title
}
title={docTitle}
Copy link
Contributor

Choose a reason for hiding this comment

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

Pass in the hideTitle prop here - hideTitle={metadata.hide_title}

@MisterTea
Copy link
Contributor Author

I think this new way is a lot cleaner, thanks. Let me know what you think!

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Thanks for the quick changes! LGTM, will let @JoelMarcey have a final look and merge

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks.

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

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

I think we are missing a !, aren't we?

lib/core/Doc.js Outdated
return (
<div className="post">
<header className="postHeader">
{editLink}
<h1>{this.props.title}</h1>
{this.props.hideTitle && <h1>{this.props.title}</h1>}
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait. Shouldn't this be

!this.props.hideTitle

We only want to show this if we don't have hideTitle, right?

@yangshun
Copy link
Contributor

yangshun commented Apr 12, 2018 via email

@JoelMarcey
Copy link
Contributor

I went ahead and made the update. I think we are good now.

@yangshun
Copy link
Contributor

yangshun commented Apr 12, 2018 via email

@JoelMarcey JoelMarcey merged commit 6dd6ead into facebook:master Apr 12, 2018
@JoelMarcey
Copy link
Contributor

I was going to test it locally right now ;)

@JoelMarcey
Copy link
Contributor

Seems to work 👍

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.

4 participants