Class: Markawesome::PlainMarkdownRenderer
- Inherits:
-
Object
- Object
- Markawesome::PlainMarkdownRenderer
- 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 tooltip date badge button callout card carousel comparison video copy_button details image_dialog dialog icon tag tabs accordion tree random_content ].freeze
- TRANSFORMER_MAP =
{ layout: LayoutTransformer, popover: PopoverTransformer, tooltip: TooltipTransformer, date: DateTransformer, badge: BadgeTransformer, button: ButtonTransformer, callout: CalloutTransformer, card: CardTransformer, carousel: CarouselTransformer, comparison: ComparisonTransformer, video: VideoTransformer, copy_button: CopyButtonTransformer, details: DetailsTransformer, image_dialog: ImageDialogTransformer, dialog: DialogTransformer, icon: IconTransformer, tag: TagTransformer, tabs: TabsTransformer, accordion: AccordionTransformer, tree: TreeTransformer, random_content: RandomContentTransformer }.freeze
Class Attribute Summary collapse
-
.overrides ⇒ Object
readonly
Returns the value of attribute overrides.
Class Method Summary collapse
- .process(content, options = {}) ⇒ Object
-
.register_override(component) {|content, options| ... } ⇒ Object
Register a per-component override.
-
.reset_overrides! ⇒ Object
Clear all registered overrides (useful in tests).
-
.strip_kramdown_attributes(content) ⇒ Object
Strip Kramdown attribute syntax like
{:.class},{:#id},{: .class},{: #id .class}, etc.
Class Attribute Details
.overrides ⇒ Object (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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/markawesome/plain_markdown_renderer.rb', line 94 def self.process(content, = {}) content, tokens = CodeBlockProtector.protect(content) PIPELINE.each do |component| if (override = overrides[component]) content = override.call(content, ) next end transformer = TRANSFORMER_MAP.fetch(component) content = if component == :image_dialog if [:image_dialog] config = [:image_dialog].is_a?(Hash) ? [:image_dialog] : {} transformer.render_as_markdown(content, config) else content end else transformer.render_as_markdown(content, ) 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.
34 35 36 37 38 |
# File 'lib/markawesome/plain_markdown_renderer.rb', line 34 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).
41 42 43 |
# File 'lib/markawesome/plain_markdown_renderer.rb', line 41 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.
122 123 124 |
# File 'lib/markawesome/plain_markdown_renderer.rb', line 122 def self.strip_kramdown_attributes(content) content.gsub(/\s*\{:\s*[^}]*\}/, '') end |