Class: MDPhlex::MD
- Inherits:
-
Phlex::SGML
- Object
- Phlex::SGML
- MDPhlex::MD
- Extended by:
- Phlex::SGML::Elements
- Defined in:
- lib/mdphlex/md.rb
Class Method Summary collapse
-
.register_block_element(method_name, tag: method_name.to_s.tr("_", "-")) ⇒ Object
Register a block-level custom element that ensures proper newlines.
Instance Method Summary collapse
- #a(href: nil, **attributes) ⇒ Object
- #blockquote(content = nil) ⇒ Object
- #br ⇒ Object
- #code(content = nil) ⇒ Object
- #em(content = nil) ⇒ Object
- #h1(content = nil) ⇒ Object
- #h2(content = nil) ⇒ Object
- #h3(content = nil) ⇒ Object
- #h4(content = nil) ⇒ Object
- #h5(content = nil) ⇒ Object
- #h6(content = nil) ⇒ Object
- #hr(**attributes) ⇒ Object
- #img(src: nil, alt: nil, **attributes) ⇒ Object
- #li(content = nil) ⇒ Object
- #ol ⇒ Object
- #p(content = nil) ⇒ Object
- #plain(content) ⇒ Object
- #pre(language: nil) ⇒ Object
- #strong(content = nil) ⇒ Object
-
#to_markdown ⇒ Object
Render the markdown to a String using the Rails 8.1 standard to_markdown alias.
- #ul ⇒ Object
Class Method Details
.register_block_element(method_name, tag: method_name.to_s.tr("_", "-")) ⇒ Object
Register a block-level custom element that ensures proper newlines
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mdphlex/md.rb', line 10 def self.register_block_element(method_name, tag: method_name.to_s.tr("_", "-")) define_method(method_name) do |**attributes, &block| state = @_state buffer = state.buffer unless state.should_render? __yield_content__(&block) if block return nil end # Render the opening tag - always followed by newline buffer << "<#{tag}" if attributes.length > 0 attributes.each do |key, value| buffer << " #{key}=\"#{value}\"" end end buffer << ">\n" # Render content if block given if block __yield_content__(&block) end # Render closing tag - always followed by newline for block elements buffer << "</#{tag}>\n" nil end end |
Instance Method Details
#a(href: nil, **attributes) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/mdphlex/md.rb', line 100 def a(href: nil, **attributes, &) state = @_state return unless state.should_render? buffer = state.buffer buffer << "[" __yield_content__(&) if block_given? buffer << "](" buffer << href.to_s if href buffer << ")" nil end |
#blockquote(content = nil) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mdphlex/md.rb', line 123 def blockquote(content = nil, &) state = @_state return unless state.should_render? buffer = state.buffer if block_given? # Save current position to capture content start_pos = buffer.length __yield_content__(&) # Extract the content that was added captured = buffer[start_pos..] # Remove it from buffer to re-add with > prefix buffer.slice!(start_pos..) # Add > prefix to each line captured.lines.each do |line| buffer << "> " << line end else content.to_s.lines.each do |line| buffer << "> " << line end end buffer << "\n\n" nil end |
#br ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/mdphlex/md.rb', line 266 def br state = @_state return unless state.should_render? state.buffer << " \n" nil end |
#code(content = nil) ⇒ Object
96 97 98 |
# File 'lib/mdphlex/md.rb', line 96 def code(content = nil, &) wrap_inline("`", content, &) end |
#em(content = nil) ⇒ Object
92 93 94 |
# File 'lib/mdphlex/md.rb', line 92 def em(content = nil, &) wrap_inline("*", content, &) end |
#h1(content = nil) ⇒ Object
48 49 50 |
# File 'lib/mdphlex/md.rb', line 48 def h1(content = nil, &) heading(1, content, &) end |
#h2(content = nil) ⇒ Object
52 53 54 |
# File 'lib/mdphlex/md.rb', line 52 def h2(content = nil, &) heading(2, content, &) end |
#h3(content = nil) ⇒ Object
56 57 58 |
# File 'lib/mdphlex/md.rb', line 56 def h3(content = nil, &) heading(3, content, &) end |
#h4(content = nil) ⇒ Object
60 61 62 |
# File 'lib/mdphlex/md.rb', line 60 def h4(content = nil, &) heading(4, content, &) end |
#h5(content = nil) ⇒ Object
64 65 66 |
# File 'lib/mdphlex/md.rb', line 64 def h5(content = nil, &) heading(5, content, &) end |
#h6(content = nil) ⇒ Object
68 69 70 |
# File 'lib/mdphlex/md.rb', line 68 def h6(content = nil, &) heading(6, content, &) end |
#hr(**attributes) ⇒ Object
258 259 260 261 262 263 264 |
# File 'lib/mdphlex/md.rb', line 258 def hr(**attributes) state = @_state return unless state.should_render? state.buffer << "---\n\n" nil end |
#img(src: nil, alt: nil, **attributes) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/mdphlex/md.rb', line 245 def img(src: nil, alt: nil, **attributes) state = @_state return unless state.should_render? buffer = state.buffer buffer << "" nil end |
#li(content = nil) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/mdphlex/md.rb', line 210 def li(content = nil, &) state = @_state return unless state.should_render? buffer = state.buffer @_list_type ||= [] list_depth = @_list_type.length # Add indentation for nested lists buffer << (" " * [list_depth - 1, 0].max) # Add list marker if @_list_type.last == :ol @_ol_counter ||= [] @_ol_counter[-1] += 1 buffer << "#{@_ol_counter.last}. " else buffer << "- " end if block_given? # Mark position before yielding content start_pos = buffer.length __yield_content__(&) # Only add newline if we haven't already ended with one # (nested lists already add their trailing newline) buffer << "\n" unless buffer.end_with?("\n") else buffer << content.to_s if content buffer << "\n" end nil end |
#ol ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/mdphlex/md.rb', line 187 def ol(&) state = @_state return unless state.should_render? buffer = state.buffer @_list_type ||= [] # If we're in a list item and there's already content, add a newline buffer << "\n" if @_list_type.length > 0 && buffer.length > 0 && !buffer.end_with?("\n") @_list_type.push(:ol) @_ol_counter ||= [] @_ol_counter.push(0) __yield_content__(&) if block_given? @_list_type.pop @_ol_counter.pop # Only add extra newline at the end of top-level lists buffer << "\n" if @_list_type.empty? nil end |
#p(content = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mdphlex/md.rb', line 72 def p(content = nil, &) state = @_state return unless state.should_render? buffer = state.buffer if block_given? __yield_content__(&) elsif content buffer << content.to_s end buffer << "\n\n" nil end |
#plain(content) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/mdphlex/md.rb', line 115 def plain(content) state = @_state return unless state.should_render? state.buffer << content.to_s nil end |
#pre(language: nil) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mdphlex/md.rb', line 152 def pre(language: nil, &) state = @_state return unless state.should_render? buffer = state.buffer buffer << "```" buffer << language.to_s if language buffer << "\n" __yield_content__(&) if block_given? buffer << "\n```\n\n" nil end |
#strong(content = nil) ⇒ Object
88 89 90 |
# File 'lib/mdphlex/md.rb', line 88 def strong(content = nil, &) wrap_inline("**", content, &) end |
#to_markdown ⇒ Object
Render the markdown to a String using the Rails 8.1 standard to_markdown alias
44 45 46 |
# File 'lib/mdphlex/md.rb', line 44 def to_markdown call end |
#ul ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/mdphlex/md.rb', line 167 def ul(&) state = @_state return unless state.should_render? buffer = state.buffer @_list_type ||= [] # If we're in a list item and there's already content, add a newline buffer << "\n" if @_list_type.length > 0 && buffer.length > 0 && !buffer.end_with?("\n") @_list_type.push(:ul) __yield_content__(&) if block_given? @_list_type.pop # Only add extra newline at the end of top-level lists buffer << "\n" if @_list_type.empty? nil end |