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.
- E
- F
- M
- N
- R
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\\*\\*"
fragment_by_unwrapping_raw_markdown_tags(fragment) Link
Returns a copy of fragment with <action-text-markdown> elements replaced by their children, leaving the text to be escaped like any other.
render_attachment wraps already-rendered Markdown in that element so node_to_markdown emits it without escaping. Only Action Text may do that, so ActionText::Content unwraps the element while canonicalizing: anything carrying it at that point came from outside the framework.
markdown_link(title, url, image: false) Link
Returns a Markdown link: [title](url).
Escapes metacharacters in title, and encodes characters in url that would break the link syntax or that Markdown would decode, such as \ and &.
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 85 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**"
NOTE: text inside <action-text-markdown> elements is emitted without escaping, so this method is not safe for untrusted content. Convert user-supplied markup through ActionText::Content, which strips those elements while canonicalizing.
render_attachment(attachment, attachment_links: false) Link
Returns an element holding attachment‘s Markdown, for ActionText::Content#to_markdown to substitute in place of the attachment. node_to_markdown emits the element’s text verbatim rather than escaping it as ordinary Markdown source.
# File actiontext/lib/action_text/markdown_conversion.rb, line 55 def render_attachment(attachment, attachment_links: false) ActionText::HtmlConversion.create_element(RAW_MARKDOWN_TAG_NAME).tap do |node| node.content = attachment.to_markdown(attachment_links: attachment_links) end end