Class: Banzai::Filter::TaskListFilter
- Inherits:
-
TaskList::Filter
- Object
- TaskList::Filter
- Banzai::Filter::TaskListFilter
- Extended by:
- Gitlab::Utils::Override
- Includes:
- Concerns::PipelineTimingCheck
- Defined in:
- lib/banzai/filter/task_list_filter.rb
Overview
TaskList filter replaces task list item markers (‘[ ]`, `[x]`, and `[~]`) with checkboxes, marked up with metadata and behavior.
This should be run on the HTML generated by the Markdown filter, after the SanitizationFilter.
Syntax
Task list items must be in a list format:
“‘
-
incomplete
- x
-
complete
- ~
-
inapplicable
“‘
This class overrides TaskList::Filter in the ‘deckar01-task_list` gem to add support for inapplicable task items
Constant Summary collapse
- XPATH =
'descendant-or-self::li[input[@data-inapplicable]] | descendant-or-self::li[p[input[@data-inapplicable]]]'
- INAPPLICABLE =
'[~]'
- INAPPLICABLEPATTERN =
/\[~\]/
- NEWITEMPATTERN =
Pattern used to identify all task list items. Useful when you need iterate over all items.
/ \A (?:\s*[-+*]|(?:\d+\.))? # optional list prefix \s* # optional whitespace prefix ( # checkbox #{CompletePattern}| #{IncompletePattern}| #{INAPPLICABLEPATTERN} ) (?=\s) # followed by whitespace /x
Constants included from Concerns::PipelineTimingCheck
Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS
Instance Method Summary collapse
- #call ⇒ Object
- #inapplicable?(item) ⇒ Boolean
- #render_item_checkbox(item) ⇒ Object
- #render_task_list_item(item) ⇒ Object
Methods included from Gitlab::Utils::Override
extended, extensions, included, method_added, override, prepended, queue_verification, verify!
Methods included from Concerns::PipelineTimingCheck
Instance Method Details
#call ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/banzai/filter/task_list_filter.rb', line 90 def call super # add class to li for any inapplicable checkboxes doc.xpath(XPATH).each do |li| li.add_class('inapplicable') end doc end |
#inapplicable?(item) ⇒ Boolean
56 57 58 |
# File 'lib/banzai/filter/task_list_filter.rb', line 56 def inapplicable?(item) !!(item.checkbox_text =~ INAPPLICABLEPATTERN) end |
#render_item_checkbox(item) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/banzai/filter/task_list_filter.rb', line 61 def render_item_checkbox(item) %(<task-button></task-button><input type="checkbox" class="task-list-item-checkbox" #{'checked="checked"' if item.complete?} #{'data-inapplicable' if inapplicable?(item)} disabled="disabled"/>) end |
#render_task_list_item(item) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/banzai/filter/task_list_filter.rb', line 70 def render_task_list_item(item) source = item.source if inapplicable?(item) # Add a `<s>` tag around the list item text. However because of the # way tasks are built, the source can include an embedded sublist, like # `[~] foobar\n<ol><li....` # The `<s>` should only be added to the main text. source = source.partition("#{INAPPLICABLE} ") text = source.last.partition(/\<(ol|ul)/) text[0] = "<s>#{text[0]}</s>" source[-1] = text.join source = source.join end Nokogiri::HTML.fragment \ source.sub(ItemPattern, render_item_checkbox(item)), 'utf-8' end |