Class: DocsUI::Markdown

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
app/components/docs_ui/markdown.rb

Overview

A Markdown "island" for prose authoring inside a Phlex page. Parses GFM with commonmarker (v2, comrak) and walks the AST emitting Phlex nodes — it never raws commonmarker's HTML. That buys three things:

* Phlex-native escaping — author text is escaped by Phlex, so no html_safe
on free text (Critical Rule 7) and #{} in prose renders literally.
* Fenced code delegated to DocsUI::Code (Rouge, configured aliases,
plaintext fallback) — highlighted identically to a hand-written Code block.
* The exact DocsUI::Prose typography classes on the wrapper, so Markdown
prose is visually identical to hand-authored Prose.

render DocsUI::Markdown.new(<<~MD)
Write **prose** as GFM. Fenced blocks are highlighted:

```ruby
puts "hi"
```
MD

Raw HTML in the source is dropped (the AST's html_block/html_inline nodes are skipped) — there is no config to enable it. Headings render as styled h3/h4; document structure and the TOC stay with DocsUI::Section.

Constant Summary collapse

CLASSES =

Reuse Prose's child-selector typography vocabulary verbatim so Markdown and hand-authored Prose read identically.

Prose::CLASSES
TABLE_WRAPPER =

The kit table wrapper + daisyUI table classes (matches DocsUI's table look).

"not-prose my-4 overflow-x-auto rounded-box border border-base-300"
TABLE_CLASSES =
"table table-sm table-zebra"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, inline: false) ⇒ Markdown

Returns a new instance of Markdown.



44
45
46
47
48
49
50
# File 'app/components/docs_ui/markdown.rb', line 44

def initialize(source, inline: false)
  # commonmarker v2 raises unless the text is UTF-8. Author heredocs already
  # are, but nil.to_s / a US-ASCII string would crash the render — normalize
  # at the boundary so any input parses.
  @source = source.to_s.encode(Encoding::UTF_8)
  @inline = inline
end

Class Method Details

.inline(source) ⇒ Object

Render source as INLINE markdown: no Prose wrapper div, and a single top-level paragraph is unwrapped so its inline children (strong/em/code/ link) sit directly in the surrounding element. Used for a [:md, "…"] table cell — the cell's is the container, so a block

/typography div would be wrong there.



42
# File 'app/components/docs_ui/markdown.rb', line 42

def self.inline(source) = new(source, inline: true)

Instance Method Details

#view_templateObject



52
53
54
55
56
# File 'app/components/docs_ui/markdown.rb', line 52

def view_template
  return visit_inline(document) if @inline

  div(class: CLASSES) { visit(document) }
end