# Action Text Markdown Conversion
Converts an HTML fragment into a Markdown string. Used by ‘ActionText::Content#to_markdown` and `ActionText::Fragment#to_markdown` to produce Markdown representations of rich text.
Example: ‘<h1>Release Notes</h1>` => `# Release Notes`, a markdown heading.
Note that this converter escapes text nodes so it won’t render as markdown.
Example: ‘<p># Release Notes</p>` => `# Release Notes`, not a heading.
Instance Public methods
escape_markdown_text(text) Link
Backslash-escapes CommonMark metacharacters in text so they are treated as literal characters by Markdown renderers.
MarkdownConversion.escape_markdown_text("**Important**") # => "\\*\\*Important\\*\\*"
markdown_link(title, url, image: false) Link
Returns a Markdown link: +[title](url)+.
Escapes metacharacters in title, and percent-encodes characters in url that would break the link syntax.
MarkdownConversion.markdown_link("photo", "https://example.com/photo_(large).png") # => "[photo](https://example.com/photo_%28large%29.png)"
Pass image: true to produce an image link (++).
MarkdownConversion.markdown_link("photo", "https://example.com/photo.png", image: true) # => ""
If the URI scheme is not allowed (per Rails::HTML::Sanitizer.allowed_uri?), returns the escaped title wrapped in escaped brackets (+[title]+).
MarkdownConversion.markdown_link("click", "javascript:alert(1)") # => "\\[click\\]"
# File actiontext/lib/action_text/markdown_conversion.rb, line 49 def markdown_link(title, url, image: false) if Rails::HTML::Sanitizer.allowed_uri?(url) "#{"!" if image}[#{escape_markdown_text(title)}](#{encode_href(url)})" else "\\[#{escape_markdown_text(title)}\\]" end end
node_to_markdown(node) Link
Converts a Nokogiri HTML node into a Markdown string.
node = Nokogiri::HTML4.fragment("<p>Hello <strong>world</strong></p>") MarkdownConversion.node_to_markdown(node) # => "Hello **world**"