Commit bb26aede authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Ignore code blocks when converting grid tables

parent 0e191107
Loading
Loading
Loading
Loading
+22 −3
Changes for gitlabFilter/grid_table_filter.rb: 22 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,9 @@ module Banzai
      }mx
      # rubocop:enable Lint/MixedRegexpCaptureTypes

      # Fenced code blocks are left unchanged, including any grid table copied inside them.
      MARKDOWN_FENCED_CODE_BLOCK_REGEX = /^([`~]{3,})[^\n]*\n.*?^\1[ \t]*$\n?/m

      require 'logger'

      # Add these regex constants at the top of the file, after the require statement
@@ -723,12 +726,12 @@ module Banzai
      # rubocop:enable Metrics/AbcSize

      def call
        
        current_text = text
        regex = MARKDOWN_GRID_TABLE_BLOCK_REGEX
        return current_text unless regex.match?(current_text)

        updated_text = current_text.gsub(regex) do
        convert_grid_tables = lambda do |segment|
          segment.gsub(regex) do
            match = Regexp.last_match
            # Extract the grid table content from the match
            grid_table = match[:code]
@@ -740,7 +743,23 @@ module Banzai
              match.to_s
            end
          end
        updated_text
        end

        return convert_grid_tables.call(current_text) unless MARKDOWN_FENCED_CODE_BLOCK_REGEX.match?(current_text)

        output = +""
        cursor = 0
        current_text.to_enum(:scan, MARKDOWN_FENCED_CODE_BLOCK_REGEX).each do
          code_block_match = Regexp.last_match
          non_code_segment = current_text[cursor...code_block_match.begin(0)]
          output << convert_grid_tables.call(non_code_segment) if non_code_segment
          output << code_block_match[0]
          cursor = code_block_match.end(0)
        end

        trailing_segment = current_text[cursor..]
        output << convert_grid_tables.call(trailing_segment) if trailing_segment
        output
      end
    end
  end