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 ID generation #240

Merged
merged 2 commits into from
Nov 17, 2022
Merged

Fix ID generation #240

merged 2 commits into from
Nov 17, 2022

Conversation

romaricpascal
Copy link
Member

@romaricpascal romaricpascal commented Nov 16, 2022

Temporary fix waiting for a more long term solution to this issue to be implemented in the tech-docs-gem repository: alphagov/tech-docs-gem#310

The broken computation of IDs seems to be due to the the ID generator being reset a bit too loosely. This PR ensures it get reset for the rendering of each page, which makes sure it only considers the IDs on the page itself.

For that it hooks onto the preprocess step of Redcarpet's Markdown rendering to reset the ID generator.

The TechDocsHTMLRenderer is used in two different places, though (for the general rendering and the Sass API ref). This means we cannot extend it and replace with our own renderer.

Instead this PR uses Ruby's ability to add methods to existing classes. To make sure this is done cleanly, the preprocess method is defined in a separate module and then included in the class separately. This:

  • gives us a clear hint in stacktraces that its our method that had an issue if that ever happens (rather than pointing to the class where you wouldn't find the method)
  • allows us to call super if TechDocsHTMLRenderer starts implementing a preprocess itself (though hopefully it'll be to land the fix, so that'll mean we can get rid of this patch)

To match Ruby conventions as we're going to have more than one ruby file in our project
@netlify
Copy link

netlify bot commented Nov 16, 2022

Deploy Preview for govuk-frontend-docs-preview ready!

Name Link
🔨 Latest commit 78f40d4
🔍 Latest deploy log https://app.netlify.com/sites/govuk-frontend-docs-preview/deploys/63764bc973442f00085f6426
😎 Deploy Preview https://deploy-preview-240--govuk-frontend-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@colinrotherham
Copy link
Contributor

Nice fix @romaricpascal, is it something we can open as a PR on https://github.com/alphagov/tech-docs-gem?

@romaricpascal
Copy link
Member Author

romaricpascal commented Nov 17, 2022

Nice fix @romaricpascal, is it something we can open as a PR on alphagov/tech-docs-gem?

I think on the tech-docs-gem repo, I'd like to do things a bit more cleanly and stop the ID generator from being a singleton and properly tie it to the markdown rendering. At the moment, it needs to be a singleton because it is reset and used in a different place, but wouldn't if everything happens in the TechDocsHTMLRenderer. So that would be a different fix. But plan would be to fix it upstream, yup 😄

@romaricpascal romaricpascal marked this pull request as ready for review November 17, 2022 09:37
Copy link
Contributor

@colinrotherham colinrotherham left a comment

Choose a reason for hiding this comment

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

Happy to approve this as a temporary fix (otherwise it'll bite us again)

Some questions, but not blockers:

  1. Shall we wait until it's fixed upstream?
    alphagov/tech-docs-gem?

  2. What is the patch commit going to solve?
    The commit message doesn't say 😊

For 1) I'm not sure how long (or complex) an upstream fix is. I'd wait if it's only a quick one, otherwise it's complex and has breaking changes for other teams maybe not

Add a new `preprocess` hook to the Redcarpet renderer to reset the ID generator.
This ensures headings that may appear on multiple pages are not treated as duplicate
when rendering one of these pages.

See: alphagov/tech-docs-gem#310
@romaricpascal
Copy link
Member Author

Updated the commit message for the patch to quickly describe why and point to the issue in the tech docs repo.

Regarding the first question, this PR is there to have things fixed in our docs waiting for upstream to be fixed, so it wouldn't make sense to wait for it to be fixed before merging 😄

@romaricpascal romaricpascal merged commit e0e79cc into main Nov 17, 2022
@romaricpascal romaricpascal deleted the fix-id-generation branch November 17, 2022 16:54
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.

2 participants