Skip to content

Don't escape special chars when they are in inline_code (carried from #743) #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2020
Merged
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ AllCops:
- 'vendor/**/*'
- 'gemfiles/**/*'

Metrics/LineLength:
Layout/LineLength:
Enabled: false

Performance/RegexpMatch:
Expand Down
5 changes: 4 additions & 1 deletion lib/github_changelog_generator/generator/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def encapsulate_string(string)
string = string.gsub('\\', '\\\\')

ENCAPSULATED_CHARACTERS.each do |char|
string = string.gsub(char, "\\#{char}")
# Only replace char with escaped version if it isn't inside backticks (markdown inline code).
# This relies on each opening '`' being closed (ie an even number in total).
# A char is *outside* backticks if there is an even number of backticks following it.
string = string.gsub(%r{#{Regexp.escape(char)}(?=([^`]*`[^`]*`)*[^`]*$)}, "\\#{char}")
end

string
Expand Down
34 changes: 34 additions & 0 deletions spec/unit/generator/section_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module GitHubChangelogGenerator
RSpec.describe Section do
let(:options) { {} }
subject(:section) { described_class.new(options) }

describe "#encapsulate_string" do
let(:string) { "" }

context "with the empty string" do
it "returns the string" do
expect(section.send(:encapsulate_string, string)).to eq string
end
end

context "with a string with an escape-needing character in it" do
let(:string) { "<Inside> and outside" }

it "returns the string escaped" do
expect(section.send(:encapsulate_string, string)).to eq '\\<Inside\\> and outside'
end
end

context "with a backticked string with an escape-needing character in it" do
let(:string) { 'back `\` slash' }

it "returns the string" do
expect(section.send(:encapsulate_string, string)).to eq 'back `\` slash'
end
end
end
end
end