Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/tdiary/style/gfm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def to_html(string)
# 2. Apply markdown conversion
r = CommonMarker.render_html(r, [:DEFAULT, :UNSAFE], [:autolink, :table])

# 3. Stash <pre> and <code> tags
# 3. Stash <pre>, <code>, <iframe> and <script> tags
pre_tag_stashes = []
r.gsub!(/<pre(.*?)<\/pre>/m) do |matched|
pre_tag_stashes.push(matched)
Expand All @@ -78,6 +78,18 @@ def to_html(string)
"@@tdiary_style_gfm_code_tag#{code_tag_stashes.length - 1}@@"
end

iframe_tag_stashes = []
r.gsub!(/<iframe(.*?)<\/iframe>/m) do |matched|
iframe_tag_stashes.push(matched)
"@@tdiary_style_gfm_iframe_tag#{iframe_tag_stashes.length - 1}@@"
end

script_tag_stashes = []
r.gsub!(/<script(.*?)<\/script>/m) do |matched|
script_tag_stashes.push(matched)
"@@tdiary_style_gfm_script_tag#{script_tag_stashes.length - 1}@@"
end

# 4. Convert miscellaneous
if pre_tag_stashes.none? && code_tag_stashes.none?
# STAGE 1: Stash all existing <a> tags from CommonMarker output
Expand Down Expand Up @@ -137,7 +149,7 @@ def to_html(string)
end
}

# 5. Unstash <pre>, <code> and plugin call
# 5. Unstash <pre>, <code>, <iframe> and <script> and plugin call
pre_tag_stashes.each.with_index do |str, i|
plugin_stashes.each.with_index do |(p_str, p_erb), j|
if str["@@tdiary_style_gfm_plugin#{j}@@"]
Expand All @@ -154,6 +166,22 @@ def to_html(string)
end
r["@@tdiary_style_gfm_code_tag#{i}@@"] = str
end
iframe_tag_stashes.each.with_index do |str, i|
plugin_stashes.each.with_index do |(p_str, p_erb), j|
if str["@@tdiary_style_gfm_plugin#{j}@@"]
str["@@tdiary_style_gfm_plugin#{j}@@"] = CGI.escapeHTML(p_str)
end
end
r["@@tdiary_style_gfm_iframe_tag#{i}@@"] = str
end
script_tag_stashes.each.with_index do |str, i|
plugin_stashes.each.with_index do |(p_str, p_erb), j|
if str["@@tdiary_style_gfm_plugin#{j}@@"]
str["@@tdiary_style_gfm_plugin#{j}@@"] = CGI.escapeHTML(p_str)
end
end
r["@@tdiary_style_gfm_script_tag#{i}@@"] = str
end
plugin_stashes.each.with_index do |(str, erb), i|
if r["@@tdiary_style_gfm_plugin#{i}@@"]
r["@@tdiary_style_gfm_plugin#{i}@@"] = erb
Expand Down
26 changes: 26 additions & 0 deletions spec/tdiary/style/gfm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,32 @@ def class
end
it { expect(@diary.to_html).to eq @html }
end

describe 'URLs with @ in embedding codes must not be converted into Twitter links.
' do
before do
source = <<-'EOF'
# subTitle

This is an embedding of a post of a Mathtodon server.

<iframe src="https://mathtod.online/@Nyoho/115394182617923497/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://mathtod.online/embed.js" async="async"></script>
EOF
@diary.append(source)

@html = <<-'EOF'
<div class="section">
<%=section_enter_proc( Time.at( 1041346800 ) )%>
<h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
<p>This is an embedding of a post of a Mathtodon server.</p>
<iframe src="https://mathtod.online/@Nyoho/115394182617923497/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://mathtod.online/embed.js" async="async"></script>
<%=section_leave_proc( Time.at( 1041346800 ) )%>
</div>
EOF
end
it { expect(@diary.to_html).to eq @html }
end

end

# Local Variables:
Expand Down
Loading