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

Improve headers method #66

Merged

Conversation

benbarnett
Copy link
Contributor

Fixes two issues to do with nested headers not being picked up, and where explicitly assigned ID's were being ignored in favoured of the auto-generated versions.

Please be kind with code review – I'm not a ruby developer 😇

Example:

# Heading one {#explicit-id}

<div markdown="1">
## Heading two
</div>
  • When calling headers, it was only returning the first heading with an incorrect ID (#heading as opposed to #explicit-id).
  • The <h2>Heading two</h2> was not being recognised by the govspeak method, even though it was in the rendered HTML.

Follows the kramdown documentation for Specifying a Header ID and HTML Blocks.

headers.flatten.compact
end

private
Copy link
Contributor

Choose a reason for hiding this comment

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

Our style is to indent this at the class level and drop the whitespace line beneath. See https://github.com/alphagov/styleguides/blob/master/ruby.md

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@benlovell
Copy link
Contributor

LGTM. Any thoughts @fofr, @dsingleton?

@@ -3,11 +3,45 @@ module Govspeak

class HeaderExtractor < Kramdown::Converter::Base
def convert(doc)
headers = []

doc.root.children.map do |el|
Copy link
Contributor

Choose a reason for hiding this comment

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

Because you're now explicitly pushing things into the headers array, each is more appropriate than map here. map is used to create an array of the items that the iterator block yields, whereas each just executes the block once per item.

doc.root.children.map do |el|
if el.type == :header
Header.new(el.options[:raw_text], el.options[:level], generate_id(el.options[:raw_text]))
headers.push(build_header(el))
Copy link
Contributor

Choose a reason for hiding this comment

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

Personal preference maybe, but I'd prefer headers << build_header(el)

@boffbowsh
Copy link
Contributor

Couple of tiny comments, otherwise 👍

Please be kind with code review – I'm not a ruby developer 😇

You are now 😄

@benbarnett
Copy link
Contributor Author

Thanks both, comments should all be addressed now!

Fixes issues to do with nested headers not being found, and where
explicitly assigned ID's were being ignored in favoured of the
auto-generated versions.
@benbarnett
Copy link
Contributor Author

@boffbowsh @benlovell either of you free to push the button?

benlovell added a commit that referenced this pull request Jan 12, 2016
@benlovell benlovell merged commit c515934 into alphagov:master Jan 12, 2016
@benlovell benlovell deleted the nested-headers branch August 10, 2016 15:11
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.

3 participants