Class: Markawesome::ImageDialogTransformer
- Inherits:
-
BaseTransformer
- Object
- BaseTransformer
- Markawesome::ImageDialogTransformer
- Defined in:
- lib/markawesome/transformers/image_dialog_transformer.rb
Overview
Transforms standalone images into clickable images that open in dialogs Images can opt-out by adding “nodialog” to the title attribute Example: 
Class Method Summary collapse
-
.render_as_markdown(content, _config = {}) ⇒ Object
For plain-markdown output image-dialog wrapping is dropped: the raw markdown image syntax is already what we want.
- .transform(content, config = {}) ⇒ Object
Class Method Details
.render_as_markdown(content, _config = {}) ⇒ Object
For plain-markdown output image-dialog wrapping is dropped: the raw markdown image syntax is already what we want.
45 46 47 |
# File 'lib/markawesome/transformers/image_dialog_transformer.rb', line 45 def self.render_as_markdown(content, _config = {}) content end |
.transform(content, config = {}) ⇒ Object
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/markawesome/transformers/image_dialog_transformer.rb', line 11 def self.transform(content, config = {}) # First, protect code blocks, inline code, and comparison blocks from transformation protected_content, fenced_code_blocks = protect_fenced_code_blocks(content) protected_content, inline_code_blocks = protect_inline_code(protected_content) protected_content, comparison_blocks = protect_comparisons(protected_content) # Match markdown images:  or  # Capture alt text, URL, and optional title # URL can contain spaces and special characters image_regex = /!\[([^\]]*)\]\(([^)]+?)(?:\s+"([^"]*)")?\)/ result = protected_content.gsub(image_regex) do |match| alt_text = Regexp.last_match(1) image_url = Regexp.last_match(2).strip title = Regexp.last_match(3) # Skip transformation if title contains "nodialog" if title&.include?('nodialog') # Return original image without dialog match else # Transform to clickable image with dialog transform_to_dialog(alt_text, image_url, title, config) end end # Restore protected blocks in reverse order result = restore_comparisons(result, comparison_blocks) result = restore_inline_code(result, inline_code_blocks) restore_fenced_code_blocks(result, fenced_code_blocks) end |