Module: Clacky::UI2::MarkdownRenderer

Defined in:
lib/clacky/ui2/markdown_renderer.rb

Overview

MarkdownRenderer handles rendering Markdown content with syntax highlighting

Class Method Summary collapse

Class Method Details

.custom_symbolsHash

Get custom symbols for markdown rendering

Returns:

  • (Hash)

    Symbol configuration for tty-markdown



88
89
90
91
92
93
94
95
96
# File 'lib/clacky/ui2/markdown_renderer.rb', line 88

def custom_symbols
  {
    override: {
      # Make horizontal rule simpler - just a line without decorative diamonds
      diamond: "",
      line: "-"
    }
  }
end

.markdown?(content) ⇒ Boolean

Check if content looks like markdown

Parameters:

  • content (String)

    Content to check

Returns:

  • (Boolean)

    true if content appears to be markdown



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clacky/ui2/markdown_renderer.rb', line 36

def markdown?(content)
  return false if content.nil? || content.empty?

  # Check for common markdown patterns
  content.match?(/^#+ /) ||           # Headers
    content.match?(/```/) ||          # Code blocks
    content.match?(/^\s*[-*+] /) ||   # Unordered lists
    content.match?(/^\s*\d+\. /) ||   # Ordered lists
    content.match?(/\[.+\]\(.+\)/) || # Links
    content.match?(/^\s*> /) ||       # Blockquotes
    content.match?(/\*\*.+\*\*/) ||   # Bold
    content.match?(/`.+`/) ||         # Inline code
    content.match?(/^\s*\|.+\|/) ||   # Tables
    content.match?(/^---+$/)          # Horizontal rules
end

.render(content) ⇒ String

Render markdown content with theme-aware colors

Parameters:

  • content (String)

    Markdown content to render

Returns:

  • (String)

    Rendered content with ANSI colors



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clacky/ui2/markdown_renderer.rb', line 14

def render(content)
  return content if content.nil? || content.empty?

  # Get current theme colors
  theme = ThemeManager.current_theme

  # Configure tty-markdown with custom theme and symbols
  parsed = TTY::Markdown.parse(content, 
    theme: theme_colors,
    symbols: custom_symbols,
    width: TTY::Screen.width - 4  # Leave some margin
  )

  parsed
rescue StandardError => e
  # Fallback to plain content if rendering fails
  content
end

.theme_colorsHash

Get theme-aware colors for markdown rendering

Returns:

  • (Hash)

    Color configuration for tty-markdown



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/clacky/ui2/markdown_renderer.rb', line 55

def theme_colors
  theme = ThemeManager.current_theme

  # Map our theme colors to tty-markdown's expected format
  # Note: theme.colors values are already arrays, so we need to flatten when adding styles
  {
    # Headers use info color (cyan/blue)
    h1: Array(theme.colors[:info]) + [:bold],
    h2: Array(theme.colors[:info]) + [:bold],
    h3: Array(theme.colors[:info]),
    h4: Array(theme.colors[:info]),
    h5: Array(theme.colors[:info]),
    h6: Array(theme.colors[:info]),
    # Horizontal rule - make it subtle (dim gray)
    hr: [:bright_black],
    # Code blocks use dim color
    code: Array(theme.colors[:thinking]),
    # Links use success color (green)
    link: Array(theme.colors[:success]),
    # Lists use default text color
    list: [:bright_white],
    # Strong/bold use bright white
    strong: [:bright_white, :bold],
    # Emphasis/italic use white
    em: [:white],
    # Note/blockquote use dim color
    note: Array(theme.colors[:thinking]),
    quote: Array(theme.colors[:thinking]),
  }
end