class Qiita::Markdown::Filters::Checkbox::List

Constants

CHECKBOX_CLOSE_MARK
CHECKBOX_OPEN_MARK

Public Class Methods

new(node) click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 25
def initialize(node)
  @node = node
end

Public Instance Methods

convert() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 33
def convert
  first_text_node.content = first_text_node.content.sub(checkbox_mark, "").lstrip
  first_text_node.add_previous_sibling(checkbox_node)
  @node["class"] = "task-list-item"
end
has_checkbox?() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 29
def has_checkbox?
  has_open_checkbox? || has_close_checkbox?
end

Private Instance Methods

checkbox_mark() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 41
def checkbox_mark
  case
  when has_close_checkbox?
    CHECKBOX_CLOSE_MARK
  when has_open_checkbox?
    CHECKBOX_OPEN_MARK
  end
end
checkbox_node() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 50
def checkbox_node
  node = Nokogiri::HTML.fragment('<input type="checkbox" class="task-list-item-checkbox">')
  node.children.first["checked"] = true if has_close_checkbox?
  node.children.first["disabled"] = true
  node
end
first_text_node() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 57
def first_text_node
  is_loose_list_node = @node.children.first&.text == "\n" && @node.children[1]&.name == "p"

  if is_loose_list_node
    @node.children[1].children.first
  elsif @node.children.first && @node.children.first.name == "p"
    @node.children.first.children.first
  else
    @node.children.first
  end
end
has_close_checkbox?() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 70
def has_close_checkbox?
  !!first_text_node && first_text_node.text? && first_text_node.content.start_with?(CHECKBOX_CLOSE_MARK)
end
has_open_checkbox?() click to toggle source
# File lib/qiita/markdown/filters/checkbox.rb, line 75
def has_open_checkbox?
  !!first_text_node && first_text_node.text? && first_text_node.content.start_with?(CHECKBOX_OPEN_MARK)
end