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

Fix Gitlab filter: differentiate line separators ("\") from lines to make...

Fix Gitlab filter: differentiate line separators ("\") from lines to make markdown parser work properly.
parent 5ad3dace
Loading
Loading
Loading
Loading
+35 −26
Original line number Diff line number Diff line
@@ -133,42 +133,46 @@ module Banzai
          modified_content = modified_content[cell.leading_spaces..]
          if modified_content.lstrip.match?(/^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)/) # List
            cell.list_flag = true
            modified_content = modified_content.gsub(/\\\s*$/, '\n')
            modified_content = modified_content.gsub(/\\\s*$/, '<br>')

            # Add list element end mark to know when the list element ends
            cell.content = "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          elsif cell.list_flag && !content.strip.empty?
            modified_content = modified_content.gsub(/\\\s*$/, '\n')
            modified_content = modified_content.gsub(/\\\s*$/, '<br>')
            cell.content = "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}" # add the list element end mark
          elsif modified_content.empty?
            cell.content = '\n'
            cell.content = '\n\n'
          else
            cell.content = modified_content.gsub(/\\\s*$/, '\n')
            cell.content = modified_content.gsub(/\\\s*$/, '<br>')
          end
        elsif modified_content.lstrip.match?(/^(?:>?\s*(?:[-*+]|\d+\.|\d+\)\s).*)/) # List
        elsif modified_content.lstrip.match?(/^(?:>?\s*(?:[-*+]|\d+\.|\d+\))\s.*)/) # List
          cell.content += '\n' unless cell.list_flag
          cell.list_flag = true
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          modified_content = modified_content.gsub(/\\\s*$/, '<br>')
          cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
        elsif cell.list_flag && !modified_content.empty?
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          modified_content = modified_content.gsub(/\s*>\s*/, "") # remove > from the beginning of the line
          modified_content = modified_content.gsub(/\\\s*$/, '<br>')
          # cell.content = cell.content.strip(nextListElementMark) #remove list element end mark
          cell.content = cell.content.chomp(NEXT_ELEMENT_LIST_MARK.to_s)
          if cell.content.end_with?('\n') # a force linebreak is present
          if cell.content.end_with?('<br>') # a force linebreak is present
            cell.content += "#{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          else # Multiline content is concatenated to the last line with a space
            cell.content += " #{modified_content}#{NEXT_ELEMENT_LIST_MARK}"
          end
        elsif modified_content.empty?
        elsif modified_content.empty? || modified_content.end_with?('>')
          if cell.list_flag
            cell.list_flag = false
            cell.content += '\n\n'
          end

          cell.content += cell.content.end_with?('\n') ? "" : '\n'
          cell.content += cell.content.end_with?('\n') ? "" : "\\n#{modified_content}\\n"
        else
          modified_content = modified_content.gsub(/\\\s*$/, '\n')
          if cell.content.end_with?('\n') # a force linebreak is present
          if cell.content.end_with?('<br>')
            modified_content = modified_content.gsub(/\s*>\s*/, "") # remove > from the beginning of the line
          end
          modified_content = modified_content.gsub(/\\\s*$/, '<br>')
          if cell.content.end_with?('<br>') # a force linebreak is present
            cell.content += modified_content
          else # Multiline content is concatenated to the last line with a space
            cell.content += " #{modified_content}"
@@ -596,21 +600,25 @@ module Banzai
                matches = cell.content&.scan(
                  /\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+?)#{NEXT_ELEMENT_LIST_MARK}\n?/o)

                if matches
                  list = "<ul>"
                  matches.each do |match|
                    list += "<li>#{match[1]}</li>"
                  end
                  list += "</ul>"
                  cell.content = cell.content.gsub(
                    /(\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+#{NEXT_ELEMENT_LIST_MARK}\n?))+/o, list)
                  # Enforce left alignment if cell contains a list
                  cell.alignment = 'left'
                end
                #if matches
                #  list = "<ul>"
                #  matches.each do |match|
                #    list += "<li>#{match[1]}</li>"
                #  end
                #  list += "</ul>"
                #  cell.content = cell.content.gsub(
                #    /(\s*([-*+]|\d+\.)\s+([^#{NEXT_ELEMENT_LIST_MARK}]+#{NEXT_ELEMENT_LIST_MARK}\n?))+/o, list)
                #  # Enforce left alignment if cell contains a list
                #  cell.alignment = 'left'
                #end

                rowspan = cell.rowspan > 1 ? %( rowspan="#{cell.rowspan}") : ""
                colspan = cell.colspan > 1 ? %( colspan="#{cell.colspan}") : ""
                html += %(<th#{rowspan}#{colspan} markdown="1" style="text-align: #{cell.alignment}">\n\n#{cell.content}\n\n</th>)
                content_with_breaklines = cell.content
                #content_with_breaklines = cell.content&.gsub("<br />", "\n") If this line is commented, lists do not render correctly
                content_with_breaklines = content_with_breaklines&.split("<br />")&.join("\n")
                content_with_breaklines = content_with_breaklines&.split(NEXT_ELEMENT_LIST_MARK.to_s)&.join("\n")
                html += %(<th#{rowspan}#{colspan} markdown="1" style="text-align: #{cell.alignment}">\n\n#{content_with_breaklines}\n\n</th>)
              end
              html += '</tr>'
            end
@@ -646,7 +654,8 @@ module Banzai
              rowspan = cell.rowspan > 1 ? %( rowspan="#{cell.rowspan}") : ""
              colspan = cell.colspan > 1 ? %( colspan="#{cell.colspan}") : ""
              content_with_breaklines = cell.content
              #content_with_breaklines = cell.content&.gsub("<br />", "\n")
              #content_with_breaklines = cell.content&.gsub("<br />", "\n") If this line is commented, lists do not render correctly
              content_with_breaklines = content_with_breaklines&.split("<br />")&.join("\n")
              content_with_breaklines = content_with_breaklines&.split(NEXT_ELEMENT_LIST_MARK.to_s)&.join("\n")
              html += %(<td#{rowspan}#{colspan} markdown="1" style="text-align: #{cell.alignment}">\n\n#{content_with_breaklines}\n\n</td>)
            end