Class: Markawesome::DialogTransformer

Inherits:
BaseTransformer show all
Defined in:
lib/markawesome/transformers/dialog_transformer.rb

Overview

Transforms dialog syntax into wa-dialog elements with trigger buttons Primary syntax: ???paramsnbutton textn>>>ncontentn???

Alternative syntax: :::wa-dialog paramsnbutton textn>>>ncontentn:

Params: space-separated tokens (order doesn’t matter) Flags: light-dismiss Width: CSS unit value (e.g., 500px, 50vw, 40em) Note: Header with close X button is always enabled for accessibility

Constant Summary collapse

DIALOG_ATTRIBUTES =
{
  light_dismiss: %w[light-dismiss]
}.freeze

Class Method Summary collapse

Class Method Details

.render_as_markdown(content, _options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/markawesome/transformers/dialog_transformer.rb', line 55

def self.render_as_markdown(content, _options = {})
  primary_regex = /^\?\?\?([^\n]*)$\n(.*?)\n^>>>$\n(.*?)\n^\?\?\?$/m
  alternative_regex = /^:::wa-dialog([^\n]*)$\n(.*?)\n^>>>$\n(.*?)\n^:::$/m

  transform_proc = proc do |_params_string, button_text, dialog_content|
    trigger = button_text.to_s.strip
    body = dialog_content.to_s.strip
    # If dialog body already starts with a heading, use it verbatim.
    if body.match?(/^#\s+/)
      "_#{trigger}:_\n\n#{body}"
    else
      "_#{trigger}:_\n\n#{body}"
    end
  end

  patterns = dual_syntax_patterns(primary_regex, alternative_regex, transform_proc)
  apply_multiple_patterns(content, patterns)
end

.transform(content) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/markawesome/transformers/dialog_transformer.rb', line 20

def self.transform(content)
  # Define both regex patterns - capture parameter string, button text, and content
  # Params are on the same line as the opening delimiter
  # Button text is on the next line(s) until >>>
  # Content is everything after >>> until the closing delimiter
  primary_regex = /^\?\?\?([^\n]*)$\n(.*?)\n^>>>$\n(.*?)\n^\?\?\?$/m
  alternative_regex = /^:::wa-dialog([^\n]*)$\n(.*?)\n^>>>$\n(.*?)\n^:::$/m

  # Define shared transformation logic
  transform_proc = proc do |params_string, button_text, dialog_content|
    button_text = button_text.strip
    dialog_content = dialog_content.strip

    # Parse parameters
    light_dismiss, width = parse_parameters(params_string)

    # Extract label from first heading or use button text
    label, content_without_label = extract_label(dialog_content, button_text)

    # Generate unique ID based on content
    dialog_id = generate_dialog_id(button_text, dialog_content)

    # Convert markdown to HTML
    content_html = markdown_to_html(content_without_label)

    # Build the dialog HTML
    build_dialog_html(dialog_id, button_text, label, content_html,
                      light_dismiss, width)
  end

  # Apply both patterns
  patterns = dual_syntax_patterns(primary_regex, alternative_regex, transform_proc)
  apply_multiple_patterns(content, patterns)
end