Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

fix: strip table syntax from help center article previews (#16299)

Sivin Varghese972567d7442b
diff --git a/lib/chatwoot_markdown_renderer.rb b/lib/chatwoot_markdown_renderer.rb
index 97de3c93416..045fbd5294a 100644
--- a/lib/chatwoot_markdown_renderer.rb
+++ b/lib/chatwoot_markdown_renderer.rb
@@ -19,7 +19,16 @@ class ChatwootMarkdownRenderer
end
def render_markdown_to_plain_text
- CommonMarker.render_doc(@content, :DEFAULT).to_plaintext
+ doc = CommonMarker.render_doc(@content, :DEFAULT, [:table])
+ doc.walk.select { |node| node.type == :table }.each do |table|
+ text = CommonMarker::Node.new(:text)
+ text.string_content = table.walk.select { |node| node.type.in?([:text, :code]) }.map(&:string_content).join(' ')
+ paragraph = CommonMarker::Node.new(:paragraph)
+ paragraph.append_child(text)
+ table.insert_before(paragraph)
+ table.delete
+ end
+ doc.to_plaintext
end
private
diff --git a/spec/lib/chatwoot_markdown_renderer_spec.rb b/spec/lib/chatwoot_markdown_renderer_spec.rb
index b074e189b6c..67378c6357a 100644
--- a/spec/lib/chatwoot_markdown_renderer_spec.rb
+++ b/spec/lib/chatwoot_markdown_renderer_spec.rb
@@ -2,7 +2,6 @@ require 'rails_helper'
RSpec.describe ChatwootMarkdownRenderer do
let(:markdown_content) { 'This is a *test* content with ^markdown^' }
- let(:plain_text_content) { 'This is a test content with markdown' }
let(:doc) { instance_double(CommonMarker::Node) }
let(:renderer) { described_class.new(markdown_content) }
let(:markdown_renderer) { instance_double(CustomMarkdownRenderer) }
@@ -121,12 +120,19 @@ RSpec.describe ChatwootMarkdownRenderer do
let(:rendered_content) { renderer.render_markdown_to_plain_text }
before do
- allow(CommonMarker).to receive(:render_doc).with(markdown_content, :DEFAULT).and_return(doc)
- allow(doc).to receive(:to_plaintext).and_return(plain_text_content)
+ allow(CommonMarker).to receive(:render_doc).and_call_original
end
it 'renders the markdown content to plain text' do
- expect(rendered_content).to eq(plain_text_content)
+ expect(rendered_content).to eq("This is a test content with ^markdown^\n")
+ end
+
+ context 'with a table' do
+ let(:markdown_content) { "Intro\n\n| Feature | Use |\n| --- | --- |\n| Captain | `replies` |\n\nOutro" }
+
+ it 'keeps the cell text without the table syntax' do
+ expect(rendered_content).to eq("Intro\n\nFeature Use Captain replies\n\nOutro\n")
+ end
end
end
end