Class: Markawesome::TooltipTransformer

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

Overview

Transforms tooltip syntax into wa-tooltip elements attached to a focusable anchor span via an auto-generated for/id pair. Declarative, zero-JS, and static-site-safe — great for glossary terms and inline definitions.

Inline syntax (primary): (((anchor term >>> tip text)))

Alternative block syntax: :::wa-tooltip paramsnanchorn>>>ntipn:

Params: space-separated tokens (order doesn’t matter) Placement: top (default), bottom, left, right Distance: distance:N (e.g., distance:10)

Tip content is plain text (HTML-escaped), with literal ‘n` rendered as
— the same surface as the popover’s inline form. Tooltips hold brief text, so there is no markdown body.

Constant Summary collapse

TOOLTIP_ATTRIBUTES =
{
  placement: %w[top bottom left right]
}.freeze
INLINE_REGEX =

Inline regex (single-line, no newlines allowed): capture 1 = params+anchor, capture 2 = tip text.

/\(\(\([ \t]*([^\r\n]*?)[ \t]*>>>[ \t]*([^\r\n]+?)[ \t]*\)\)\)/
ALTERNATIVE_REGEX =

Block alternative regex (multiline): capture 1 = params, 2 = anchor, 3 = tip.

/^:::wa-tooltip([^\n]*)$\n(.*?)\n^>>>$\n(.*?)\n^:::$/m

Class Method Summary collapse

Class Method Details

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/markawesome/transformers/tooltip_transformer.rb', line 75

def self.render_as_markdown(content, _options = {})
  inline_transform = {
    regex: INLINE_REGEX,
    block: proc do |_match, matchdata|
      combined = matchdata[1]
      tip_text = matchdata[2].strip
      _params, anchor_text = parse_inline_anchor_and_params(combined)
      "**#{anchor_text}** (#{tip_text})"
    end
  }

  alternative_transform = {
    regex: ALTERNATIVE_REGEX,
    block: proc do |_match, matchdata|
      anchor_text = matchdata[2].strip
      tip_text = matchdata[3].strip
      "**#{anchor_text}** (#{tip_text})"
    end
  }

  apply_multiple_patterns(content, [inline_transform, alternative_transform])
end

.transform(content) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/markawesome/transformers/tooltip_transformer.rb', line 33

def self.transform(content)
  # Tracks ID base usage within this transform call so repeated tooltips
  # (same anchor + tip) get disambiguated suffixes instead of colliding on
  # the page.
  seen_ids = Hash.new(0)

  inline_transform = {
    regex: INLINE_REGEX,
    block: proc do |_match, matchdata|
      combined = matchdata[1]
      tip_text = matchdata[2].strip

      params_string, anchor_text = parse_inline_anchor_and_params(combined)
      placement, distance = parse_parameters(params_string)

      tooltip_id = generate_tooltip_id(anchor_text, tip_text, seen_ids)

      build_tooltip_html(tooltip_id, anchor_text, tip_text,
                         { placement: placement, distance: distance })
    end
  }

  alternative_transform = {
    regex: ALTERNATIVE_REGEX,
    block: proc do |_match, matchdata|
      params_string = matchdata[1]
      anchor_text = matchdata[2].strip
      tip_text = matchdata[3].strip

      placement, distance = parse_parameters(params_string)

      tooltip_id = generate_tooltip_id(anchor_text, tip_text, seen_ids)

      build_tooltip_html(tooltip_id, anchor_text, tip_text,
                         { placement: placement, distance: distance })
    end
  }

  # Inline pattern first to avoid conflicts with the block pattern.
  apply_multiple_patterns(content, [inline_transform, alternative_transform])
end