-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will convert a string into an URL HREF anchor. Specifically, we anticipate using this for headings in GitHub markdown files to construct links to those headings.
- Loading branch information
1 parent
739e63f
commit 04f3ebf
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class String | ||
# Converts the string to an acceptable URL anchor. | ||
# | ||
# Thw rules for GitHub markdown links are: | ||
# - force lowercase | ||
# - strip punctuation | ||
# - replace spaces with dashes | ||
# @return [String] the string converted to an acceptable URL anchor | ||
def to_anchor | ||
downcase.gsub(/[^a-z0-9_ -]/, "").tr(" ", "-") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "twirp/protoc_plugin/core_ext/string/to_anchor" | ||
|
||
RSpec.describe String do | ||
describe "#to_anchor" do | ||
it "converts a string with brackets, numbers, and dates" do | ||
expect("[1.1.1] - 2024-05-22".to_anchor).to eq("111---2024-05-22") | ||
end | ||
|
||
it "converts a string with mixed case and backticks" do | ||
expect("Install the `protoc-gen-twirp_ruby` plugin gem".to_anchor).to eq("install-the-protoc-gen-twirp_ruby-plugin-gem") | ||
end | ||
end | ||
end |