0

I may be searching the wrong terms but I can't seem to find an obvious way to make easy links to page section ids.

Lets say I have a _widgets.md file with a section id=section-1 so I want to generate a link to: /widgets/#section-1

I found this for links:

Jekyll link within page

So an same page link is as simple as [Section 1](#section-1) but I can't seem to find how to add the anchor to links from another page like this [Widgets - Section 1]({% link _docs/widgets.md%}).

Dan Tappin
  • 2,692
  • 3
  • 37
  • 77
  • 1
    I'm not sure that's possible with Jekyll ... or perhaps I just don't understand the question. Could you use an [include](https://jekyllrb.com/docs/includes/)? Something like `{% include widget.html content="https://example.com/" %}` then inside the include you could assign `{% assign url = include.content %}` and then use `[Widgets - Section 1]({{ url }}#section-1)`? – Brad West May 28 '21 at 18:52
  • On the [docs](https://jekyllrb.com/docs/liquid/tags/), there is a hint to your concern > Note you cannot add filters to link tags. For example, you cannot append a string using Liquid filters, such as {% link mypage.html | append: "#section1" %}. To link to sections on a page, you will need to use regular HTML or Markdown linking techniques. – KargWare Oct 13 '21 at 03:56

2 Answers2

0

You can use the capture tag, see jekyll or shopify

In HTML

{% capture link_with_anchor %}{% link _pages/links.md %}#foobar{% endcapture %}
<a href="{{ link_with_anchor }}" target="_self">Links</a>

In Markdown

{% capture link_with_anchor %}{% link _pages/links.md %}#foobar{% endcapture %}
[Links]({{ link_with_anchor }})

You can benefit from the link features (check if page exists) and can add the anchor, e.g. foobar.

KargWare
  • 1,746
  • 3
  • 22
  • 35
0

One cool way to manage links is to have a central include file that defines the links by name, which is then referenced by each document.

For example you have a file in the _includes directory called "links.md" which defines all the cross-document links in your site:

[Section 1]: {% link widgets/page.md %}#section1
[Section 2]: {% link widgets/page2.md %}#section2
etc

Then in your markdown you can just say:

This is a link to [Section 1] isn't that nice.

... other content

{% include links.md %}
Kristof
  • 104
  • 4