Skip to content

Commit c7bf1ca

Browse files
authored
Merge pull request #465 from jhawthorn/c_commonmarker
Go back to C commonmarker
2 parents ada5ca9 + 2a06517 commit c7bf1ca

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
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", github: "ianks/commonmarker", branch: "hotfix/ruby-4-1"
48+
gem "commonmarker", github: "jhawthorn/commonmarker", branch: "c-api-stable" # The v1.0 Rust gem is perpetually broken on Ruby master
4949

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

benchmarks/lobsters/Gemfile.lock

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
GIT
2-
remote: https://github.com/ianks/commonmarker.git
3-
revision: 6ea4f5a2be0423206dbaeca7108035a70a149ab7
4-
branch: hotfix/ruby-4-1
2+
remote: https://github.com/jhawthorn/commonmarker.git
3+
revision: 1469d28ad133cb5b9d834714e7c285a45d30da6c
4+
branch: c-api-stable
55
specs:
6-
commonmarker (2.6.1)
7-
rb_sys (~> 0.9)
6+
commonmarker (0.23.12)
87

98
GIT
109
remote: https://github.com/whomwah/rqrcode.git
@@ -242,10 +241,7 @@ GEM
242241
zeitwerk (~> 2.6)
243242
rainbow (3.1.1)
244243
rake (13.3.0)
245-
rake-compiler-dock (1.11.0)
246244
rb-readline (0.5.5)
247-
rb_sys (0.9.124)
248-
rake-compiler-dock (= 1.11.0)
249245
rdoc (6.14.2)
250246
erb
251247
psych (>= 4.0.0)
@@ -420,7 +416,7 @@ CHECKSUMS
420416
capybara (3.40.0) sha256=42dba720578ea1ca65fd7a41d163dd368502c191804558f6e0f71b391054aeef
421417
cgi (0.5.0) sha256=fe99f65bb2c146e294372ebb27602adbc3b4c008e9ea7038c6bd48c1ec9759da
422418
chunky_png (1.4.0) sha256=89d5b31b55c0cf4da3cf89a2b4ebc3178d8abe8cbaf116a1dba95668502fdcfe
423-
commonmarker (2.6.1)
419+
commonmarker (0.23.12)
424420
concurrent-ruby (1.3.5) sha256=813b3e37aca6df2a21a3b9f1d497f8cbab24a2b94cab325bffe65ee0f6cbebc6
425421
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
426422
crack (1.0.0) sha256=c83aefdb428cdc7b66c7f287e488c796f055c0839e6e545fec2c7047743c4a49
@@ -485,9 +481,7 @@ CHECKSUMS
485481
railties (8.1.1) sha256=fb0c7038b147bea41bf6697fa443ff1c5c47d3bb1eedd9ecf1bceeb90efcb868
486482
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
487483
rake (13.3.0) sha256=96f5092d786ff412c62fde76f793cc0541bd84d2eb579caa529aa8a059934493
488-
rake-compiler-dock (1.11.0) sha256=eab51f2cd533eb35cea6b624a75281f047123e70a64c58b607471bb49428f8c2
489484
rb-readline (0.5.5) sha256=9e9bd7e198bdef0822c46902f6c592b882c1f9777894a4c3dcf5b320824a8793
490-
rb_sys (0.9.124) sha256=513476557b12eaf73764b3da9f8746024558fe8699bda785fb548c9aa3877ae7
491485
rdoc (6.14.2) sha256=9fdd44df130f856ae70cc9a264dfd659b9b40de369b16581f4ab746e42439226
492486
regexp_parser (2.10.0) sha256=cb6f0ddde88772cd64bff1dbbf68df66d376043fe2e66a9ef77fcb1b0c548c61
493487
reline (0.6.2) sha256=1dad26a6008872d59c8e05244b119347c9f2ddaf4a53dce97856cd5f30a02846

benchmarks/lobsters/extras/markdowner.rb

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
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-
104
def self.to_html(text, opts = {})
115
if text.blank?
126
return ""
137
end
148

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

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

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

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

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})"
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
4772
else
48-
match
73+
node = nil
4974
end
5075
end
5176
end

0 commit comments

Comments
 (0)