Class: Banzai::Filter::TaskListFilter

Inherits:
TaskList::Filter
  • Object
show all
Extended by:
Gitlab::Utils::Override
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.

/
  ^
  (?:\s*[-+*]|(?:\d+\.))? # optional list prefix
  \s*                     # optional whitespace prefix
  (                       # checkbox
    #{CompletePattern}|
    #{IncompletePattern}|
    #{INAPPLICABLEPATTERN}
  )
  (?=\s)                  # followed by whitespace
/x

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Instance Method Details

#callObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/banzai/filter/task_list_filter.rb', line 89

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/banzai/filter/task_list_filter.rb', line 55

def inapplicable?(item)
  !!(item.checkbox_text =~ INAPPLICABLEPATTERN)
end

#render_item_checkbox(item) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/banzai/filter/task_list_filter.rb', line 60

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/banzai/filter/task_list_filter.rb', line 69

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