Module: MarkdownToolsHelper

Defined in:
app/helpers/markdown_tools_helper.rb

Instance Method Summary collapse

Instance Method Details

#md_button(name = nil, action: nil, label: nil, **attribs) {|context| ... } ⇒ ActiveSupport::SafeBuffer

Create a Markdown tool button.

Examples:

Create a Bold button with icon:

<%= md_button action: 'bold', label: 'Bold', data: { index: 1 } do %>
  <i class="fas fa-bold"></i>
<% end %>

# => <a class="button is-muted is-outlined js-markdown-tool" data-action="bold" data-index="1" aria-label="Bold"
        role="button">
       <i class="fas fa-bold"></i>
     </a>

Parameters:

  • name (String) (defaults to: nil)

    A name to display on the button. If you want to use an icon instead, pass a block and leave name as nil (default).

  • action (String) (defaults to: nil)

    Populates the button’s data-action attribute, which can be processed later via Markdown JS.

  • label (String) (defaults to: nil)

    Populates the button’s title and aria-label attributes.

  • attribs (Hash{#to_s => Object})

    A hash of additional attributes to pass to the tag generator.

Yield Parameters:

  • context (ActionView::Helpers::TagHelper::TagBuilder)

Yield Returns:

  • (String, ActiveSupport::SafeBuffer)

Returns:

  • (ActiveSupport::SafeBuffer)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/markdown_tools_helper.rb', line 21

def md_button(name = nil, action: nil, label: nil, **attribs, &block)
  attribs.merge! href: 'javascript:void(0)',
                 class: "#{attribs[:class] || ''} button is-muted is-outlined js-markdown-tool",
                 data_action: action,
                 aria_label: label,
                 title: label,
                 role: 'button'
  attribs.transform_keys! { |k| k.to_s.tr('_', '-') }.symbolize_keys!
  if name.nil? && block_given?
    tag.a(**attribs, &block)
  else
    tag.a name, **attribs
  end
end

#md_list_item(name = nil, action: nil, label: nil, **attribs) {|context| ... } ⇒ ActiveSupport::SafeBuffer

Create a Markdown tool list item. Identical to

Parameters:

  • name (String) (defaults to: nil)

    A name to display on the button. If you want to use an icon instead, pass a block and leave name as nil (default).

  • action (String) (defaults to: nil)

    Populates the button’s data-action attribute, which can be processed later via Markdown JS.

  • label (String) (defaults to: nil)

    Populates the button’s title and aria-label attributes.

  • attribs (Hash{#to_s => Object})

    A hash of additional attributes to pass to the tag generator.

Yield Parameters:

  • context (ActionView::Helpers::TagHelper::TagBuilder)

Yield Returns:

  • (String, ActiveSupport::SafeBuffer)

Returns:

  • (ActiveSupport::SafeBuffer)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/markdown_tools_helper.rb', line 41

def md_list_item(name = nil, action: nil, label: nil, **attribs, &block)
  attribs.merge! href: 'javascript:void(0)',
                 class: "#{attribs[:class] || ''}js-markdown-tool",
                 data_action: action,
                 aria_label: label,
                 title: label,
                 role: 'button'
  attribs.transform_keys! { |k| k.to_s.tr('_', '-') }.symbolize_keys!
  if name.nil? && block_given?
    tag.a(**attribs, &block)
  else
    tag.a name, **attribs
  end
end