Skip to content

Commit 6f08643

Browse files
authored
Merge pull request #459 from ianks/hotfix/ruby-4-1
Update commonmarker
2 parents 0617bd2 + e047692 commit 6f08643

File tree

3 files changed

+32
-45
lines changed

3 files changed

+32
-45
lines changed

benchmarks/lobsters/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ gem "rqrcode_core", github: "whomwah/rqrcode_core"
4545
gem "pdf-reader"
4646
gem "nokogiri", ">= 1.13.9"
4747
gem "htmlentities"
48-
gem "commonmarker", ">= 0.23.6", "< 1.0" # The v1.0 Rust gem doesn't compile on Ruby master
48+
gem "commonmarker", github: "ianks/commonmarker", branch: "hotfix/ruby-4-1"
4949

5050
# perf - skip for benchmarking
5151
group :development do

benchmarks/lobsters/Gemfile.lock

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
GIT
2+
remote: https://github.com/ianks/commonmarker.git
3+
revision: 6ea4f5a2be0423206dbaeca7108035a70a149ab7
4+
branch: hotfix/ruby-4-1
5+
specs:
6+
commonmarker (2.6.1)
7+
rb_sys (~> 0.9)
8+
19
GIT
210
remote: https://github.com/whomwah/rqrcode.git
311
revision: f9fa33704951158a38b3cf04311e295d3a0e947d
@@ -107,7 +115,6 @@ GEM
107115
xpath (~> 3.2)
108116
cgi (0.5.0)
109117
chunky_png (1.4.0)
110-
commonmarker (0.23.11)
111118
concurrent-ruby (1.3.5)
112119
connection_pool (3.0.2)
113120
crack (1.0.0)
@@ -235,7 +242,10 @@ GEM
235242
zeitwerk (~> 2.6)
236243
rainbow (3.1.1)
237244
rake (13.3.0)
245+
rake-compiler-dock (1.11.0)
238246
rb-readline (0.5.5)
247+
rb_sys (0.9.124)
248+
rake-compiler-dock (= 1.11.0)
239249
rdoc (6.14.2)
240250
erb
241251
psych (>= 4.0.0)
@@ -349,7 +359,7 @@ DEPENDENCIES
349359
bcrypt (~> 3.1.2)
350360
capybara
351361
cgi
352-
commonmarker (>= 0.23.6, < 1.0)
362+
commonmarker!
353363
database_cleaner
354364
factory_bot_rails
355365
faker
@@ -410,7 +420,7 @@ CHECKSUMS
410420
capybara (3.40.0) sha256=42dba720578ea1ca65fd7a41d163dd368502c191804558f6e0f71b391054aeef
411421
cgi (0.5.0) sha256=fe99f65bb2c146e294372ebb27602adbc3b4c008e9ea7038c6bd48c1ec9759da
412422
chunky_png (1.4.0) sha256=89d5b31b55c0cf4da3cf89a2b4ebc3178d8abe8cbaf116a1dba95668502fdcfe
413-
commonmarker (0.23.11) sha256=9d1d35d358740151bce29235aebfecc63314fb57dd89a83e72d4061b4fe3d2bf
423+
commonmarker (2.6.1)
414424
concurrent-ruby (1.3.5) sha256=813b3e37aca6df2a21a3b9f1d497f8cbab24a2b94cab325bffe65ee0f6cbebc6
415425
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
416426
crack (1.0.0) sha256=c83aefdb428cdc7b66c7f287e488c796f055c0839e6e545fec2c7047743c4a49
@@ -475,7 +485,9 @@ CHECKSUMS
475485
railties (8.1.1) sha256=fb0c7038b147bea41bf6697fa443ff1c5c47d3bb1eedd9ecf1bceeb90efcb868
476486
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
477487
rake (13.3.0) sha256=96f5092d786ff412c62fde76f793cc0541bd84d2eb579caa529aa8a059934493
488+
rake-compiler-dock (1.11.0) sha256=eab51f2cd533eb35cea6b624a75281f047123e70a64c58b607471bb49428f8c2
478489
rb-readline (0.5.5) sha256=9e9bd7e198bdef0822c46902f6c592b882c1f9777894a4c3dcf5b320824a8793
490+
rb_sys (0.9.124) sha256=513476557b12eaf73764b3da9f8746024558fe8699bda785fb548c9aa3877ae7
479491
rdoc (6.14.2) sha256=9fdd44df130f856ae70cc9a264dfd659b9b40de369b16581f4ab746e42439226
480492
regexp_parser (2.10.0) sha256=cb6f0ddde88772cd64bff1dbbf68df66d376043fe2e66a9ef77fcb1b0c548c61
481493
reline (0.6.2) sha256=1dad26a6008872d59c8e05244b119347c9f2ddaf4a53dce97856cd5f30a02846

benchmarks/lobsters/extras/markdowner.rb

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
class Markdowner
22
# opts[:allow_images] allows <img> tags
33

4+
COMMONMARKER_OPTIONS = {
5+
parse: { smart: true },
6+
render: { unsafe: true },
7+
extension: { tagfilter: true, autolink: true, strikethrough: true }
8+
}.freeze
9+
410
def self.to_html(text, opts = {})
511
if text.blank?
612
return ""
713
end
814

9-
exts = [:tagfilter, :autolink, :strikethrough]
10-
root = CommonMarker.render_doc(text.to_s, [:SMART], exts)
15+
# Preprocess @mentions before markdown parsing
16+
processed_text = preprocess_mentions(text.to_s)
1117

12-
walk_text_nodes(root) {|n| postprocess_text_node(n) }
18+
html = Commonmarker.to_html(processed_text, options: COMMONMARKER_OPTIONS)
1319

14-
ng = Nokogiri::HTML(root.to_html([:DEFAULT], exts))
20+
ng = Nokogiri::HTML(html)
1521

1622
# change <h1>, <h2>, etc. headings to just bold tags
1723
ng.css("h1, h2, h3, h4, h5, h6").each do |h|
@@ -33,44 +39,13 @@ def self.to_html(text, opts = {})
3339
end
3440
end
3541

36-
def self.walk_text_nodes(node, &block)
37-
return if node.type == :link
38-
return block.call(node) if node.type == :text
39-
40-
node.each do |child|
41-
walk_text_nodes(child, &block)
42-
end
43-
end
44-
45-
def self.postprocess_text_node(node)
46-
while node
47-
return unless node.string_content =~ /\B(@#{User::VALID_USERNAME})/
48-
before, user, after = $`, $1, $'
49-
50-
node.string_content = before
51-
52-
if User.exists?(:username => user[1..-1])
53-
link = CommonMarker::Node.new(:link)
54-
link.url = Rails.application.root_url + "u/#{user[1..-1]}"
55-
node.insert_after(link)
56-
57-
link_text = CommonMarker::Node.new(:text)
58-
link_text.string_content = user
59-
link.append_child(link_text)
60-
61-
node = link
62-
else
63-
node.string_content += user
64-
end
65-
66-
if after.length > 0
67-
remainder = CommonMarker::Node.new(:text)
68-
remainder.string_content = after
69-
node.insert_after(remainder)
70-
71-
node = remainder
42+
def self.preprocess_mentions(text)
43+
text.gsub(/\B(@#{User::VALID_USERNAME})/) do |match|
44+
user = match[1..-1]
45+
if User.exists?(username: user)
46+
"[#{match}](#{Rails.application.root_url}u/#{user})"
7247
else
73-
node = nil
48+
match
7449
end
7550
end
7651
end

0 commit comments

Comments
 (0)