eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

URL as anchor links in collections

Open pl-mnm opened this issue 5 years ago • 3 comments

Hi. I have a bunch of files in a directory which I'd like to output on a page (via collections) but not to separate files, and I'd like these sections to have a URL with an anchor which I can reference elsewhere. So for example:

/some-directory
  /another-dir/
    index.njk
    /sections
      sections.json
      section-1.md
      section-2.md
      ...

where the URLs would be /some-directory/another-dir#section-1, /some-directory/another-dir#section-2, ...

// index.njk

{% for item in collections.sections %}
    {{ item.url }}
{% endfor %}

I'm really scratching my head on this one, but all I got is some convoluted way to try to get the parent path in the front-matter, but it's really messy, not dynamic and I don't have a single value to call later:

// sections.json

{
  "permalink": false,
  "anchorPath": "some-directory/another-dir",
  "tags": "sections"
}

Would the upcoming computed data feature be the most practical way to do this or am I missing something?

Any help would be much appreciated.

pl-mnm avatar Mar 24 '20 09:03 pl-mnm

Would markdown-it-anchor help you in the meantime?

Ryuno-Ki avatar Mar 24 '20 10:03 Ryuno-Ki

This looks like a job for pagination navigation.

reubenlillie avatar Mar 24 '20 12:03 reubenlillie

@Ryuno-Ki Although I could consider having each entry in a single md file, it doesn't seem as if I'd be able to iterate over a list of anchors elsewhere in the site. @reubenlillie This could do the trick, I need to dig into this a bit more.

pl-mnm avatar Mar 24 '20 14:03 pl-mnm

I believe this is a duplicate of #889, which will probably head into the enhancement queue soon.

zachleat avatar Nov 21 '25 15:11 zachleat