Class: Markawesome::RandomContentTransformer

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

Overview

Transforms random-content syntax into Web Awesome's experimental element, which shows one or more of its direct element children at random (optionally rotating them) and hides the rest — all in WA's own runtime, so the author writes zero JavaScript. Ideal for a "little bit dynamic" rotating tips / testimonials / featured-content block on an otherwise static site.

Primary syntax:

......mode:random items:2 animation:fade
First option
>>>
Second option
......

Alternative syntax:

:::wa-random-content <params>
First option
>>>
Second option
:::

Each option (split on a >>> line) has its rendered Markdown wrapped in a single

so WA sees exactly one selectable element child per option.

Params (order-independent, on the opening fence line):

- mode:      unique (WA default) | random | sequence — bare enum or mode:VALUE
- animation: none | fade | fade-up | fade-down | fade-left | fade-right —
           bare enum or animation:VALUE
- autoplay:  bare boolean flag
- items:     a bare integer or items:N (how many children to show)
- autoplay-interval:N (ms between rotations)

Unknown/invalid tokens are silently ignored. Deterministic emission order: mode, items, animation, autoplay, autoplay-interval.

The >>> separator is the SHARED glyph already used inside details/dialog/popover/tooltip; it only acts as an item separator inside a ....../:::wa-random-content block. A line of exactly >>> or ...... inside an option is therefore a separator/close-fence — options cannot contain those literal lines (a hard constraint, like carousel's ~~~).

Constant Summary collapse

COMPONENT_ATTRIBUTES =
{
  mode: %w[unique random sequence],
  animation: %w[none fade fade-up fade-down fade-left fade-right],
  autoplay: %w[autoplay]
}.freeze
PRIMARY_REGEX =

Dots MUST be escaped. The body is unstructured (.*?) under /m, so the closing anchor makes an unclosed fence not match and lets multiple blocks match left-to-right. Deliberately NO \s* after the alt fence (unlike carousel): with an unstructured body a greedy \s* would swallow the first newline and capture the first option line as params. ([^\n]*) only — a leading space on the alt form is stripped by the parser.

/^\.{6}([^\n]*)\n(.*?)\n\.{6}/m
ALTERNATIVE_REGEX =
/^:::wa-random-content([^\n]*)\n(.*?)\n:::/m
ITEM_SEPARATOR =
/^>>>$/

Class Method Summary collapse

Class Method Details

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

Degrade to the option bodies joined by blank lines (fences, params, and >>> separators dropped), used for .md endpoints / llms.txt.



73
74
75
76
77
78
79
80
# File 'lib/markawesome/transformers/random_content_transformer.rb', line 73

def self.render_as_markdown(content, _options = {})
  transform_proc = proc do |_params, items_block|
    extract_items(items_block).reject(&:empty?).join("\n\n")
  end

  patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
  apply_multiple_patterns(content, patterns)
end

.transform(content) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/markawesome/transformers/random_content_transformer.rb', line 62

def self.transform(content)
  transform_proc = proc do |params, items_block|
    build_html(extract_items(items_block), parse_params(params))
  end

  patterns = dual_syntax_patterns(PRIMARY_REGEX, ALTERNATIVE_REGEX, transform_proc)
  apply_multiple_patterns(content, patterns)
end