Class: Markawesome::PlainMarkdownRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/markawesome/plain_markdown_renderer.rb

Overview

Renders Markawesome-flavored markdown into “clean” plain markdown by degrading each Web Awesome component to its closest GFM equivalent. Used to serve per-page ‘.md` endpoints and to generate llms.txt content that LLM consumers can read without having to understand `<wa-*>` tags.

Mirrors Markawesome::Transformer.process, but dispatches to each transformer’s ‘render_as_markdown` method instead of `transform`.

Constant Summary collapse

PIPELINE =
%i[
  layout
  popover
  badge
  button
  callout
  card
  carousel
  comparison
  copy_button
  details
  image_dialog
  dialog
  icon
  tag
  tabs
].freeze
TRANSFORMER_MAP =
{
  layout: LayoutTransformer,
  popover: PopoverTransformer,
  badge: BadgeTransformer,
  button: ButtonTransformer,
  callout: CalloutTransformer,
  card: CardTransformer,
  carousel: CarouselTransformer,
  comparison: ComparisonTransformer,
  copy_button: CopyButtonTransformer,
  details: DetailsTransformer,
  image_dialog: ImageDialogTransformer,
  dialog: DialogTransformer,
  icon: IconTransformer,
  tag: TagTransformer,
  tabs: TabsTransformer
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.overridesObject (readonly)

Returns the value of attribute overrides.



21
22
23
# File 'lib/markawesome/plain_markdown_renderer.rb', line 21

def overrides
  @overrides
end

Class Method Details

.process(content, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/markawesome/plain_markdown_renderer.rb', line 81

def self.process(content, options = {})
  content, tokens = CodeBlockProtector.protect(content)

  PIPELINE.each do |component|
    if (override = overrides[component])
      content = override.call(content, options)
      next
    end

    transformer = TRANSFORMER_MAP.fetch(component)
    content = if component == :image_dialog
                if options[:image_dialog]
                  config = options[:image_dialog].is_a?(Hash) ? options[:image_dialog] : {}
                  transformer.render_as_markdown(content, config)
                else
                  content
                end
              else
                transformer.render_as_markdown(content, options)
              end
  end

  content = strip_kramdown_attributes(content)
  CodeBlockProtector.restore(content, tokens)
end

.register_override(component) {|content, options| ... } ⇒ Object

Register a per-component override. Consumers can call this from a plugin during boot to override the default rendering for a single component without forking the gem.

Parameters:

  • component (Symbol)

    one of :callout, :badge, :button, :card, :carousel, :comparison, :copy_button, :details, :dialog, :icon, :image_dialog, :layout, :popover, :tabs, :tag.

Yields:

  • (content, options)

    Proc that receives the full source content and the renderer options; returns the content with that component syntax replaced.

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/markawesome/plain_markdown_renderer.rb', line 33

def register_override(component, &block)
  raise ArgumentError, 'block required' unless block_given?

  @overrides[component.to_sym] = block
end

.reset_overrides!Object

Clear all registered overrides (useful in tests).



40
41
42
# File 'lib/markawesome/plain_markdown_renderer.rb', line 40

def reset_overrides!
  @overrides = {}
end

.strip_kramdown_attributes(content) ⇒ Object

Strip Kramdown attribute syntax like ‘::.class`, `::#id`, `.class`, `#id .class`, etc. These are Kramdown-specific and not valid GFM.



109
110
111
# File 'lib/markawesome/plain_markdown_renderer.rb', line 109

def self.strip_kramdown_attributes(content)
  content.gsub(/\s*\{:\s*[^}]*\}/, '')
end