如何在Asciidoctor中创建外部链接引用以避免重复输入URL?
Absolutely! Asciidoctor does support reusable external links (similar to Markdown’s reference links) where you can define a URL once and reference it multiple times with different link texts. Here’s how to do it—this is probably the :myid:-style functionality you remembered:
Using Document Attributes (The Go-To Method)
This is the most flexible way to handle reusable external links.
First, define your URL as a document attribute at the top of your file:
:example-site: https://example.com
Then, whenever you want to link to that URL, use the link: macro with the attribute and your custom text:
Check out link:{example-site}[our first example] or link:{example-site}[another take on the same site]—both point to the same URL.
When rendered, this creates two separate links to https://example.com, each with their own unique text. Best of all, if the URL ever changes, you only need to update the attribute definition once instead of hunting down every instance of the link.
Bonus: Link Macros for Default Text
If you have a link that’s used often with a standard text but occasionally needs a custom label, you can use a link macro:
- Define the macro with a default text:
:link-example: https://example.com[Default Example Text]
- Use it with the default text or override it:
<<link-example>> <!-- Shows "Default Example Text" --> <<link-example, Custom Link Text>> <!-- Shows your custom text -->
This is great for links you repeat frequently but don’t want to type the full link:{attr}[text] every time.
Both methods eliminate duplicate URL entries and make your document much easier to maintain.
内容的提问来源于stack exchange,提问作者Ciro Santilli OurBigBook.com

