Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds support for superscript in help center articles (#7279)
- Adds support for superscript when rendering article markdown - Chatwoot Markdown Render to render markdown everywhere Co-authored-by: Sojan <sojan@pepalo.com>
- Loading branch information
1 parent
2e79a32
commit d2aa195
Showing
11 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/views/mailers/conversation_reply_mailer/email_reply.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/views/mailers/conversation_reply_mailer/reply_without_summary.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class ChatwootMarkdownRenderer | ||
def initialize(content) | ||
@content = content | ||
end | ||
|
||
def render_message | ||
html = CommonMarker.render_html(@content) | ||
render_as_html_safe(html) | ||
end | ||
|
||
def render_article | ||
superscript_renderer = SuperscriptRenderer.new | ||
doc = CommonMarker.render_doc(@content, :DEFAULT) | ||
html = superscript_renderer.render(doc) | ||
|
||
render_as_html_safe(html) | ||
end | ||
|
||
private | ||
|
||
def render_as_html_safe(html) | ||
# rubocop:disable Rails/OutputSafety | ||
html.html_safe | ||
# rubocop:enable Rails/OutputSafety | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class SuperscriptRenderer < CommonMarker::HtmlRenderer | ||
def text(node) | ||
content = node.string_content | ||
|
||
# Check for presence of '^' in the content | ||
if content.include?('^') | ||
# Split the text and insert <sup> tags where necessary | ||
split_content = parse_sup(content) | ||
# Output the transformed content | ||
out(split_content.join) | ||
else | ||
# Output the original content | ||
out(escape_html(content)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def parse_sup(content) | ||
content.split(/(\^[^\^]+\^)/).map do |segment| | ||
if segment.start_with?('^') && segment.end_with?('^') | ||
"<sup>#{escape_html(segment[1..-2])}</sup>" | ||
else | ||
escape_html(segment) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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(:superscript_renderer) { instance_double(SuperscriptRenderer) } | ||
let(:html_content) { '<p>This is a <em>test</em> content with <sup>markdown</sup></p>' } | ||
|
||
before do | ||
allow(CommonMarker).to receive(:render_doc).with(markdown_content, :DEFAULT).and_return(doc) | ||
allow(SuperscriptRenderer).to receive(:new).and_return(superscript_renderer) | ||
allow(superscript_renderer).to receive(:render).with(doc).and_return(html_content) | ||
end | ||
|
||
describe '#render_article' do | ||
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 | ||
end | ||
|
||
describe '#render_message' do | ||
let(:message_html_content) { '<p>This is a <em>test</em> content with ^markdown^</p>' } | ||
let(:rendered_message) { renderer.render_message } | ||
|
||
before do | ||
allow(CommonMarker).to receive(:render_html).with(markdown_content).and_return(message_html_content) | ||
end | ||
|
||
it 'renders the markdown message to html' do | ||
expect(rendered_message.to_s).to eq(message_html_content) | ||
end | ||
|
||
it 'returns an html safe string' do | ||
expect(rendered_message).to be_html_safe | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rails_helper' | ||
|
||
describe SuperscriptRenderer do | ||
let(:renderer) { described_class.new } | ||
|
||
def render_markdown(markdown) | ||
doc = CommonMarker.render_doc(markdown, :DEFAULT) | ||
renderer.render(doc) | ||
end | ||
|
||
describe '#text' do | ||
it 'converts text wrapped in ^ to superscript' do | ||
markdown = 'This is an example of a superscript: ^superscript^.' | ||
expect(render_markdown(markdown)).to include('<sup>superscript</sup>') | ||
end | ||
|
||
it 'does not convert text not wrapped in ^' do | ||
markdown = 'This is an example without superscript.' | ||
expect(render_markdown(markdown)).not_to include('<sup>') | ||
end | ||
|
||
it 'converts multiple superscripts in the same text' do | ||
markdown = 'This is an example with ^multiple^ ^superscripts^.' | ||
rendered_html = render_markdown(markdown) | ||
expect(rendered_html.scan('<sup>').length).to eq(2) | ||
expect(rendered_html).to include('<sup>multiple</sup>') | ||
expect(rendered_html).to include('<sup>superscripts</sup>') | ||
end | ||
end | ||
|
||
describe 'broken ^ usage' do | ||
it 'does not convert text that only starts with ^' do | ||
markdown = 'This is an example with ^broken superscript.' | ||
expected_output = '<p>This is an example with ^broken superscript.</p>' | ||
expect(render_markdown(markdown)).to include(expected_output) | ||
end | ||
|
||
it 'does not convert text that only ends with ^' do | ||
markdown = 'This is an example with broken^ superscript.' | ||
expected_output = '<p>This is an example with broken^ superscript.</p>' | ||
expect(render_markdown(markdown)).to include(expected_output) | ||
end | ||
|
||
it 'does not convert text with uneven numbers of ^' do | ||
markdown = 'This is an example with ^broken^ superscript^.' | ||
expected_output = '<p>This is an example with <sup>broken</sup> superscript^.</p>' | ||
expect(render_markdown(markdown)).to include(expected_output) | ||
end | ||
end | ||
end |