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 anchors erroring when heading is empty #772

Merged
merged 1 commit into from
Jun 16, 2018
Merged

Fix anchors erroring when heading is empty #772

merged 1 commit into from
Jun 16, 2018

Conversation

Rendez
Copy link
Contributor

@Rendez Rendez commented Jun 14, 2018

Motivation

Found a bug because one of our Markdown docs contained an empty heading

# Heading 1
##

Notice the empty Heading 2, makes the anchors `toSlug()` crash because of the internal `toString()` usage

Here's part of the stacktrace:

TypeError: Cannot read property 'toString' of undefined
    at module.exports (/[...]/node_modules/docusaurus/lib/core/toSlug.js:41:6)
    at Object.md.renderer.rules.heading_open (/[...]/node_modules/docusaurus/lib/core/anchors.js:16:20)
    at Renderer.render (/[...]/node_modules/remarkable/lib/renderer.js:71:39)
    at Remarkable.render (/[...]/node_modules/remarkable/lib/index.js:152:24)
    [...]

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

I added a test to specifically check for these cases.

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@docusaurus-bot
Copy link
Contributor

Deploy preview for docusaurus-preview ready!

Built with commit d60a99e

https://deploy-preview-772--docusaurus-preview.netlify.com

@JoelMarcey
Copy link
Contributor

@Rendez Thank you for the pull request. Can you please sign the CLA so that we can review/merge it?

https://code.facebook.com/cla

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 fixing this. Just have a small question regarding the test.

@@ -109,3 +112,8 @@ test('Anchor index resets on each render', () => {
'id="almost-unique-heading-1"'
);
});

test('Anchor uses default renderer when empty', () => {
expect(render([{hLevel: 1}, {content: null}], 0, {}, {})).toEqual('<h1>');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does it equal <h1> instead of <h1></h1>?

Copy link
Contributor Author

@Rendez Rendez Jun 15, 2018

Choose a reason for hiding this comment

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

Because the render method references the rules.heading_open alone const render = md.renderer.rules.heading_open;. We do not run the remarkable parser, but simply invoke this one function. Am I right?

Until now, the tests' expectations though checked to see if the opening header tag contained an id attribute, etc., so the new test's expectation looks unusual.

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.

Verified that the test results are intended. Thanks @Rendez!

@yangshun yangshun changed the title Fix anchors errorring when heading is empty Fix anchors erroring when heading is empty Jun 16, 2018
@yangshun yangshun merged commit 371fdda into facebook:master Jun 16, 2018
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.

5 participants