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>
This commit is contained in:
committed by
GitHub
parent
2e79a32db7
commit
d2aa19579e
26
lib/chatwoot_markdown_renderer.rb
Normal file
26
lib/chatwoot_markdown_renderer.rb
Normal file
@@ -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
|
||||
28
lib/superscript_renderer.rb
Normal file
28
lib/superscript_renderer.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user