Skip to content

Commit

Permalink
Change the link_to method to create a plain link with TTY::Link::Plai…
Browse files Browse the repository at this point in the history
…nLink
  • Loading branch information
piotrmurach committed Oct 2, 2024
1 parent cd3576c commit 04a7013
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/tty/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ class Link
DEFAULT_TEMPLATE = ":name -> :url"
private_constant :DEFAULT_TEMPLATE

# The replacement tokens pattern
#
# @return [Regexp]
#
# @api private
REPLACEMENT_TOKENS_PATTERN = /:(name|url)/i.freeze
private_constant :REPLACEMENT_TOKENS_PATTERN

# Generate terminal hyperlink
#
# @example
Expand Down Expand Up @@ -164,8 +156,7 @@ def link_to(name, url = nil, attrs: {})
if link?
ansi_link(name, url, attrs).to_s
else
replacements = {":name" => name, ":url" => url}
@plain.gsub(REPLACEMENT_TOKENS_PATTERN, replacements)
plain_link(name, url).to_s
end
end

Expand Down Expand Up @@ -207,6 +198,25 @@ def ansi_link(name, url, attrs)
ANSILink.new(name, url, attrs)
end

# Create a {TTY::Link::PlainLink} instance
#
# @example
# plain_link("TTY Toolkit", "https://ttytoolkit.org")
#
# @param [String] name
# the URL name
# @param [String] url
# the URL target
#
# @return [TTY::Link::PlainLink]
#
# @see PlainLink#new
#
# @api private
def plain_link(name, url)
PlainLink.new(name, url, @plain)
end

# Terminals for detecting hyperlink support
#
# @example
Expand Down

0 comments on commit 04a7013

Please sign in to comment.