From d4e3e70e6f862861eb76aa8dc099a29fc6123aae Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 6 May 2023 15:22:53 +0800 Subject: [PATCH] Add `repo` option for GitHub link inline macro --- lib/asciidoctor/github-link-inline-macro/README.adoc | 5 +++++ lib/asciidoctor/github-link-inline-macro/extension.rb | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/asciidoctor/github-link-inline-macro/README.adoc b/lib/asciidoctor/github-link-inline-macro/README.adoc index 88a1883..5e93050 100644 --- a/lib/asciidoctor/github-link-inline-macro/README.adoc +++ b/lib/asciidoctor/github-link-inline-macro/README.adoc @@ -30,6 +30,11 @@ For example, if `issue` and `rev` are both present, the link for issue will be t + When given no caption, it will update the default caption with `$OWNER/$REPO#$ISSUE`. +You can also change certain behaviors with the link:https://docs.asciidoctor.org/asciidoc/latest/attributes/options/[options attribute]. + +- `repo` sets the default link text with only the repo. +Pretty useful to quickly refer to the name of the software. + == Example usage diff --git a/lib/asciidoctor/github-link-inline-macro/extension.rb b/lib/asciidoctor/github-link-inline-macro/extension.rb index 16c2014..32366a8 100644 --- a/lib/asciidoctor/github-link-inline-macro/extension.rb +++ b/lib/asciidoctor/github-link-inline-macro/extension.rb @@ -11,7 +11,12 @@ class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor def process(parent, target, attrs) doc = parent.document - text = attrs['caption'] || target + default_caption = if attrs.key?('repo-option') + target.split('/').at(1) + else + target + end + text = attrs['caption'] || default_caption uri = URI.parse %(https://github.com/#{target}) if attrs.key? 'issue' @@ -20,7 +25,7 @@ class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor else uri.path += %(/tree/#{attrs['rev']}) if attrs.key? 'rev' uri.path += %(/#{attrs['path']}) if attrs.key? 'path' - text << "@#{attrs['rev']}" if text == target + text << "@#{attrs['rev']}" if attrs.key?('rev') && text == target end target = uri.to_s