Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
require 'rails_helper'
RSpec.describe ChatwootMarkdownRenderer do
let(:markdown_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) }
let(:html_content) { '<p>This is a <em>test</em> content with <sup>markdown</sup></p>' }
before do
allow(CustomMarkdownRenderer).to receive(:new).and_return(markdown_renderer)
allow(markdown_renderer).to receive(:render).with(doc).and_return(html_content)
end
describe '#render_article' do
before do
allow(CommonMarker).to receive(:render_doc).with(markdown_content, :DEFAULT, [:table]).and_return(doc)
end
let(:rendered_content) { renderer.render_article }
it 'renders the markdown content to html' do
expect(rendered_content.to_s).to eq(html_content)
end
it 'returns an html safe string' do
expect(rendered_content).to be_html_safe
end
context 'when tables in markdown' do
let(:markdown_content) do
<<~MARKDOWN
This is a **bold** text and *italic* text.
| Header1 | Header2 |
| ------------ | ------------ |
| **Bold Cell**| *Italic Cell*|
| Cell3 | Cell4 |
MARKDOWN
end
let(:html_content) do
<<~HTML
<p>This is a <strong>bold</strong> text and <em>italic</em> text.</p>
<table>
<thead>
<tr><th>Header1</th><th>Header2</th></tr>
</thead>
<tbody>
<tr><td><strong>Bold Cell</strong></td><td><em>Italic Cell</em></td></tr>
<tr><td>Cell3</td><td>Cell4</td></tr>
</tbody>
</table>
HTML
end
it 'renders tables in html' do
expect(rendered_content.to_s).to eq(html_content)
end
end
context 'with trusted HTML generated by the article renderer' do
let(:markdown_content) do
<<~MARKDOWN
[video](https://example.com/demo.mp4)
[demo](https://www.youtube.com/watch?v=VIDEO_ID)
[wistia](https://chatwoot.wistia.com/medias/kjwjeq6f9i)
<!--cw-colwidths:120,200-->
| A | B |
| - | - |
| 1 | 2 |
MARKDOWN
end
before do
allow(CommonMarker).to receive(:render_doc).and_call_original
allow(CustomMarkdownRenderer).to receive(:new).and_call_original
end
it 'preserves embed and table markup' do
output = rendered_content.to_s
expect(output).to include('<video width="640" height="360" controls preload="metadata">')
expect(output).to include('style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"')
expect(output).to include('<script src="https://fast.wistia.com/player.js" async></script>')
expect(output).to include('<colgroup><col style="width: 120px;"><col style="width: 200px;"></colgroup>')
end
end
end
describe '#render_message' do
let(:rendered_message) { renderer.render_message }
before do
allow(CommonMarker).to receive(:render_doc).and_call_original
allow(BaseMarkdownRenderer).to receive(:new).and_call_original
end
it 'renders the markdown message to html' do
expect(rendered_message.to_s).to eq("<p>This is a <em>test</em> content with ^markdown^</p>\n")
end
it 'returns an html safe string' do
expect(rendered_message).to be_html_safe
end
context 'with bare URLs' do
let(:markdown_content) { 'Visit https://example.com for details' }
it 'converts bare URLs to links' do
expect(renderer.render_message.to_s).to eq("<p>Visit <a href=\"https://example.com\">https://example.com</a> for details</p>\n")
end
end
end
describe '#render_markdown_to_plain_text' do
let(:rendered_content) { renderer.render_markdown_to_plain_text }
before do
allow(CommonMarker).to receive(:render_doc).and_call_original
end
it 'renders the markdown content to plain text' do
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