Add GitHub link inline macro

This commit is contained in:
Gabriel Arazas 2023-03-06 12:04:04 +08:00
parent d03eb30b5f
commit 2debdde5bb
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 58 additions and 0 deletions

View File

@ -2,6 +2,7 @@ require 'asciidoctor'
require 'asciidoctor/extensions'
require_relative 'man-inline-macro/extension'
require_relative 'swhid-inline-macro/extension'
require_relative 'github-link-inline-macro/extension'
require_relative 'github-raw-content-include-processor/extension'
require_relative 'gitlab-raw-content-include-processor/extension'
require_relative 'gitlab-link-inline-macro/extension'
@ -9,6 +10,7 @@ require_relative 'gitlab-link-inline-macro/extension'
Asciidoctor::Extensions.register do
inline_macro ManInlineMacro
inline_macro SWHInlineMacro
inline_macro GitHubLinkInlineMacro
include_processor GitHubRawIncludeProcessor
include_processor GitLabRawIncludeProcessor
inline_macro GitLabLinkInlineMacro

View File

@ -0,0 +1,33 @@
= GitHub link inline macro
:toc:
An inline macro that easily links repositories from GitHub.
== Synopsis
[source, asciidoc]
----
github:$OWNER/$REPO[$CAPTION]
----
If caption is missing, the link text will be the namespace (i.e., `$OWNER/$REPO`) of the repo.
== Attributes
There are optional attributes for this macro.
- `rev` is the commit/branch/tag of the repo to be linked.
- `path` is the filepath to be linked within the repo.
== Example usage
- `github:foo-dogsquared/website` will link to link:https://github.com/foo-dogsquared/website[my website repository].
- `github:NixOS/nixpkgs[nixpkgs nixos-unstable branch, rev=nixos-unstable]` should link to the link:https://github.com/NixOS/nixpkgs/tree/nixos-unstable[NixOS unstable branch of nixpkgs] with a custom link text.
- `github:errata-ai/vale[Vale v2.3.0 README, path=README.md, rev=v2.3.0]` should link to the link:https://github.com/errata-ai/vale/blob/v2.3.0/README.md[README of Vale v2.3.0].

View File

@ -0,0 +1,23 @@
require 'uri'
class GitHubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :github
name_positional_attributes 'caption'
def process parent, target, attrs
doc = parent.document
text = attrs['caption'] || target
uri = URI.parse %(https://github.com/#{target})
uri.path += %(/tree/#{attrs['rev']}) if attrs['rev']
uri.path += %(/#{attrs['path']}) if attrs['path']
target = uri.to_s
doc.register :links, target
create_anchor parent, text, type: :link, target: target
end
end