Skip to content

Added task list ability to GFM parser.#442

Closed
adwylie wants to merge 2 commits into
gettalong:masterfrom
adwylie:gfm-tasklist
Closed

Added task list ability to GFM parser.#442
adwylie wants to merge 2 commits into
gettalong:masterfrom
adwylie:gfm-tasklist

Conversation

@adwylie

@adwylie adwylie commented Jul 14, 2017

Copy link
Copy Markdown
Contributor

Followed most of the suggestions in #172. Never written Ruby before though so it may be a bit sloppy.

Currently renders as li disc followed by the checkbox, was assuming there'd be css to change list-style-type to none if the checkbox exists. If not I can change it.

Thanks, Andrew.

@gettalong gettalong left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for your pull request! Please see the line comments for needed changes.

And please also add tests into the test/testcases_gfm/ directory to verify that everything works as expected.

Thanks!

Comment thread lib/kramdown/parser/gfm.rb Outdated
# elements where necessary (as well as applying classes to the ul/ol and li elements).
def parse_list
super
@tree.children.each do |element|

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Using @tree.children.each would mean that, e.g., parsing the third list involves checking the first two lists. It would be better to restrict the processing to the list at hand which saves time.

Comment thread lib/kramdown/parser/gfm.rb Outdated
def parse_list
super
@tree.children.each do |element|
if [:ul, :ol].include? element.type

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please use parentheses around method arguments.

Comment thread lib/kramdown/parser/gfm.rb Outdated
/\[ \]\s+/, '<input type="checkbox" class="task-list-item-checkbox" disabled="disabled" />')
unchecked = li.children[0].children[0].value.gsub!(
/\[x\]\s+/i, '<input type="checkbox" class="task-list-item-checkbox" disabled="disabled" checked="checked" />')
is_tasklist = (checked != nil or unchecked != nil)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please use || instead of or; and checked.nil? as this avoids a method call.

Since is_tasklist is also used outside this loop, you need to use is_tasklist ||= ... since otherwise only the state of the last list item determines the status (which is probably not wanted).

Comment thread lib/kramdown/parser/gfm.rb Outdated
/\[x\]\s+/i, '<input type="checkbox" class="task-list-item-checkbox" disabled="disabled" checked="checked" />')
is_tasklist = (checked != nil or unchecked != nil)
if is_tasklist
li.attr[:class] = 'task-list-item'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

  • Please use the if-modifier syntax syntax for this since it is a short line and easy to understand.
  • Use attr['class'] since the keys are strings, not symbols.

All this also applies to line 176.

@adwylie

adwylie commented Sep 22, 2017

Copy link
Copy Markdown
Contributor Author

Hey. I've addressed all of the comments and added some tests. Thanks!

@gettalong

Copy link
Copy Markdown
Owner

Thanks - I'm currently rather busy but will have a look at soon as possible!

@gettalong gettalong self-assigned this Nov 26, 2017
@gettalong

Copy link
Copy Markdown
Owner

Thanks for your contribution - I have squashed the commits and integrated your code.

@gettalong gettalong closed this Nov 26, 2017
@robbiejaeger

Copy link
Copy Markdown

@adwylie @gettalong Thanks for this added feature!

Curious what the decision was for making the checkbox disabled by default?

@gettalong

Copy link
Copy Markdown
Owner

@robbiejaeger kramdown generates static content, so if the input would be clickable the visible state would change but nothing else.

@robbiejaeger

Copy link
Copy Markdown

Makes sense - thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants