Class: Markawesome::TreeTransformer

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

Overview

Transforms tree syntax into wa-tree / wa-tree-item elements from a nested Markdown bullet list. Primary syntax:

||||||open?
- icon:folder? expanded? Label
- child label
||||||

Alternative syntax: :::wa-tree open? ...same list... :::

Web Awesome's tree is fundamentally a selection control, which is interactive (needs JS). On a static site we deliberately emit a display/navigation-only tree: visual hierarchy via nesting, initial expand state, and leading folder/file icons — all declarative and static-safe. We skip selection, lazy, selected, and selection events entirely.

Fence token:

- open (alias expanded) -> mark every branch (node with children) expanded

Per-node leading tokens (stripped from the label):

- expanded -> force this one branch open
- icon:name -> leading content <wa-icon name="name"> on the item

The remaining text is the (plain-text, HTML-escaped) label.

expanded is emitted only on nodes that HAVE children AND (fence open OR the node's own expanded flag); leaves never get expanded.

WA runtime caveat (verified against the WA 3.9.0 kit): only honors a static expanded on items that are VISIBLE at load, so in practice open expands the TOP-LEVEL branches and deeper branches stay collapsed until their parent is opened (WA strips expanded from nested items at init). We still emit expanded on every branch on purpose: it's harmless (WA ignores the nested ones), records authorial intent, and is forward-compatible if WA ever honors nested initial-expand.

Constant Summary collapse

ICON_SLOTS =

Content slot: emits with no slot attribute.

{
  default: 'content',
  slots: %w[content],
  slot_map: { 'content' => 'content' }
}.freeze
ITEM_FLAGS =
%w[expanded].freeze
FENCE_TOKENS =
%w[open expanded].freeze
TAB_WIDTH =
4
PRIMARY_REGEX =
/^\|{6}[ \t]*([^\n]*)\n(.*?)\n\|{6}/m
ALTERNATIVE_REGEX =
/^:::wa-tree[ \t]*([^\n]*)\n(.*?)\n:::/m
LIST_LINE_REGEX =
/^(\s*)[-*+]\s+(.*)$/

Class Method Summary collapse

Class Method Details

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

Degrade to a clean nested Markdown list (2-space indent per depth, tokens stripped, fence removed).



68
69
70
71
72
73
74
75
76
# File 'lib/markawesome/transformers/tree_transformer.rb', line 68

def self.render_as_markdown(content, _options = {})
  transform_proc = proc do |_params_string, body, _third|
    nodes = parse_lines(body)
    render_list(nodes, -1, { pos: 0 }, 0)
  end

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

.transform(content) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/markawesome/transformers/tree_transformer.rb', line 55

def self.transform(content)
  transform_proc = proc do |params_string, body, _third|
    fence_open = fence_open?(params_string)
    nodes = parse_lines(body)
    "<wa-tree>#{build_items(nodes, fence_open)}</wa-tree>"
  end

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