instance method visit_h2

Ruby on Rails edge

Since edge Private — implementation detail, not part of the public API

Signature

visit_h2(node, child_values)

No documentation comment.

Parameters

node req
child_values req
Source
# File actiontext/lib/action_text/markdown_conversion.rb, line 200
      def visit_h2(node, child_values) = visit__heading(node, child_values, 2)
      def visit_h3(node, child_values) = visit__heading(node, child_values, 3)
      def visit_h4(node, child_values) = visit__heading(node, child_values, 4)
      def visit_h5(node, child_values) = visit__heading(node, child_values, 5)
      def visit_h6(node, child_values) = visit__heading(node, child_values, 6)

      def visit_blockquote(_node, child_values)
        quoted = join_children(child_values).strip.lines.map { |line| "> #{line}" }.join
        "#{quoted}\n\n"
      end

      def visit_ul(node, child_values)
        items = list_item_lines(node, child_values, prefix: "- ")
        "#{items}\n\n"
      end

      def visit_ol(node, child_values)
        items = list_item_lines(node, child_values, prefix: ->(i) { "#{i + 1}. " })
        "#{items}\n\n"
      end

      def visit_a(node, child_values)
        inner = join_children(child_values)
        if (href = node["href"]) && Rails::HTML::Sanitizer.allowed_uri?(href)
          "[#{flatten_to_inline(inner)}](#{encode_href(href)})"
        else
          inner
        end
      end

      def flatten_to_inline(text)
        return text unless text.match?(/[\r\n]/)

        text.gsub(/[\r\n]+/, " ")
      end

      # Markdown ends a line at a bare CR, but String#lines and String#split("\n") do not, so a
      # CR inside a fence would slip past the indentation #format_list_item and #visit_blockquote
      # add to each line and land outside the block.
      def normalize_line_endings(text)
        text.gsub(/\r\n?/, "\n")
      end

      def visit_tr(node, child_values)
        # lexxy does not emit `thead`, so we need to infer header rows from `tr` contents
        if node.element_children.all? { |cell| cell.name == "th" }
          visit__table_header_row(node, child_values)
        else
          cells = child_values_for_elements(node, child_values).map { |v| stringify(v).strip }
          "| #{cells.join(" | ")} |\n"
        end
      end

      def visit_summary(_node, child_values)
        "**#{join_children(child_values)}**\n\n"
      end

      def visit_br(_node, _child_values)
        "\n"
      end

      def visit_hr(_node, _child_values)
        "---\n\n"
      end

      # Attachment markdown is wrapped in <action-text-markdown> by Content#to_markdown so it passes
      # through without text escaping.
      def visit_action_text_markdown(_node, child_values)
        join_children(child_values)
      end

      # A container contributes no Markdown of its own, and neither does an element with no
      # visitor at all. #join_children reads a trailing blank line as "this value is a block"
      # and uses it to keep the next value off the same line, so a container holding a block
      # has to report one too. Flatten that away and a fence inside the container lands
      # mid-line, releasing the `pre` content #markdown_for_node emits unescaped.
      def visit__container(_node, child_values)
        inner = join_children(child_values)

        if child_values.any? { |value| block_value?(value) }
          "#{inner.rstrip}\n\n"
        else
          inner
        end
      end
      # Trix uses <div> as its default block element and represents newlines as <br> tags (see
      # piece_view.js and block_view.js in the Trix source). Unlike <p>, a div of inline content
      # adds no paragraph-separating newlines: its <br> children already provide the spacing.
      alias_method :visit_div, :visit__container
      alias_method :visit_li, :visit__container
      alias_method :visit_td, :visit__container
      alias_method :visit_th, :visit__container
      alias_method :visit_thead, :visit__container
      alias_method :visit_tbody, :visit__container

      def block_value?(value)
        stringify(value).end_with?("\n\n")
      end

      # Avoid including content from elements that aren't meaningful for markdown output
      def visit__unsupported(_node, _child_values)
        ""
      end
      alias_method :visit_script, :visit__unsupported
      alias_method :visit_style, :visit__unsupported

      def visit__table_header_row(node, child_values)
        cells = child_values_for_elements(node, child_values).map { |v| stringify(v).strip }
        row = "| #{cells.join(" | ")} |\n"
        separator = "| #{Array.new(cells.size, "---").join(" | ")} |\n"
        "#{row}#{separator}"
      end

      def list_item_lines(list_node, child_values, prefix:)
        element_values = child_values_for_elements(list_node, child_values)
        element_values.each_with_index.filter_map do |value, index|
          text = stringify(value)
          lines = text.split("\n").reject(&:blank?)
          next if lines.empty?

          bullet = prefix.respond_to?(:call) ? prefix.call(index) : prefix
          format_list_item(lines, bullet)
        end.join("\n")
      end

      # A list item's later lines have to be indented to the width of its marker. Indent them
      # less and the item ends there, which for a fenced code block means the fence closes
      # early and the rest of the `pre` content #markdown_for_node emits unescaped is released
      # as Markdown source. `- ` happens to be as wide as LIST_INDENT; `1. ` is not.
      def format_list_item(lines, bullet)
        first, *rest = lines
        leader = first.match?(LIST_BULLET) ? LIST_INDENT : bullet
        indent = " " * leader.length
        ([ leader + first ] + rest.map { |line| indent + line }).join("\n")
      end

      def join_children(child_values)
        merged = []

        child_values.each do |value|
          # Merge adjacent bold/italic runs which Lexxy emits
          if value.is_a?(Array) && (value[0] == :bold || value[0] == :italic)
            if merged.last.is_a?(Array) && merged.last[0] == value[0]
              merged.last[1] = merged.last[1] + value[1]
            else
              merged << [ value[0], value[1] ]
            end
          else
            merged << value
          end
        end

        parts = merged.map { |v| stringify(v) }
        result = +""
        parts.each do |part|
          # A block child has to begin its own block. Renderers disagree about whether a fence or
          # a list may interrupt a paragraph, and one that says no releases the content the fence
          # was holding, so separate with a blank line rather than a single newline.
          if !result.empty? && part.end_with?("\n\n")
            result << "\n" until result.end_with?("\n\n")
          end
          result << part
        end
        result
      end

      def child_values_for_elements(node, child_values)
        node.children.zip(child_values).filter_map do |child, value|
          value if child.element?
        end
      end

      def stringify(value)
        case value
        when Array
          case value[0]
          when :bold then wrap_emphasis(value[1], "**")
          when :italic then wrap_emphasis(value[1], "*")
          else value.join
          end
        else
          value.to_s
        end
      end

      # Make sure `<strong> hello </strong>` becomes ` **hello** ` and not `** hello **`
      # (the latter is not valid markdown).
      def wrap_emphasis(text, marker)
        leading = text[/\A\s*/]
        trailing = text[/\s*\z/]
        inner = text.strip
        "#{leading}#{marker}#{inner}#{marker}#{trailing}"
      end

      def code_fence(content)
        max_run = content.scan(/`{3,}/).map(&:length).max || 0
        "`" * [3, max_run + 1].max
      end

      # Two things break a code span's delimiter. A blank line closes the paragraph before the
      # closing backtick string arrives -- Markdown turns the line endings inside a code span
      # into spaces anyway, so collapse them and the span always closes. And a lone backtick
      # followed by whitespace does not open a span in every renderer (kramdown refuses it), so
      # widen the delimiter when the content leads with whitespace.
      def inline_code(content)
        content = flatten_to_inline(content)
        max_run = content.scan(/`+/).map(&:length).max || 0
        fence = "`" * [content.match?(/\A\s/) ? 2 : 1, max_run + 1].max
        if content.start_with?("`") || content.end_with?("`")
          "#{fence} #{content} #{fence}"
        else
          "#{fence}#{content}#{fence}"
        end
      end

      def strip_pretty_print_indentation(node)
        content = node.content
        return content unless content.include?("\n")

        content
          .sub(LEADING_PRETTY_PRINT_WHITESPACE, inline_sibling?(node.previous_sibling) ? " " : "")
          .sub(TRAILING_PRETTY_PRINT_WHITESPACE, inline_sibling?(node.next_sibling) ? " " : "")
      end

      def significant_whitespace?(node)
        inline_sibling?(node.previous_sibling) &&
          inline_sibling?(node.next_sibling)
      end

      def inline_sibling?(sibling)
        sibling&.text? || sibling&.name&.in?(INLINE_ELEMENTS)
      end

      def ancestor_named?(node, names, max_depth:)
        current = node.parent
        max_depth.times do
          break unless current&.element?
          return true if current.name.in?(names)
          current = current.parent
        end
        false
      end

      def encode_href(href)
        URI::RFC2396_PARSER.escape(href, ENCODE_HREF_CHARS)
      end

      # A fenced code block opens only at the start of a line. A link, a heading, a summary
      # and a table row or cell each splice their descendants into a line they have already
      # begun, and Markdown cannot hold a block inside an inline element at all, so under
      # any of them the fence never opens and the `pre` content #markdown_for_node emits
      # unescaped is released as Markdown source. See SKIP_ESCAPING_PARENTS.
      def single_line_context?(node)
        node.ancestors.any? { |ancestor| ancestor.element? && ancestor.name.in?(SINGLE_LINE_ANCESTORS) }
      end

      def skip_markdown_escaping?(node)
        node.parent&.name.in?(SKIP_ESCAPING_PARENTS)
      

Defined in actiontext/lib/action_text/markdown_conversion.rb line 200 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionText::MarkdownConversion

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close