Skip to content

Commit 75055e7

Browse files
committed
fix: emoji markup in code block was corrupted (#57)
When the gem nokogiri >= 1.11.x, any node selected by css selector will not be managed once its parent node is assigned to new inner html, this will introduce problems that changing node properties isn't working.
1 parent 7b6c4e3 commit 75055e7

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

lib/jekyll-spaceship/processors/emoji-processor.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,24 @@ def emoji_filter(content, selector)
4444
# page's layout field of front matter is nil or unavailable
4545
return content if body.nil?
4646

47+
# Count for restoration
48+
escaped_count = 0
49+
4750
# filter nodes (pre, code)
48-
nodes = body.css(selector)
49-
nodes.each do |node|
51+
body.css(selector).each do |node|
52+
count = escaped_count
53+
5054
# handle emoji markup
51-
node.inner_html = node.inner_html.gsub(
52-
/:([\w\d+-]+?):/, '\:\1\:'
53-
)
55+
inner_html = node.inner_html.gsub(
56+
/(?<!\\):([\w\d+-]+?)(?<!\\):/
57+
) do |match|
58+
escaped_count += 1
59+
"\\:#{match[1..-2]}\\:"
60+
end
61+
62+
if escaped_count > count
63+
node.inner_html = inner_html
64+
end
5465
end
5566

5667
# parse the emoji
@@ -73,8 +84,10 @@ def emoji_filter(content, selector)
7384

7485
body.inner_html = content
7586

87+
return doc.to_html if escaped_count.zero?
88+
7689
# restore nodes (pre, code)
77-
nodes.each do |node|
90+
body.css(selector).each do |node|
7891
# handle emoji markup
7992
node.inner_html = node.inner_html.gsub(
8093
/\\:([\w\d+-]+?)\\:/, ':\1:'

0 commit comments

Comments
 (0)