jdsteinbach / eleventy-plugin-toc

11ty plugin to generate a TOC from page content
61 stars 19 forks source link

Use the table of content in another page #23

Closed julientaq closed 3 years ago

julientaq commented 3 years ago

Hi there! thanks for this amazing plugin.

i’m trying to use the generated table of content on another page than the one used, is it possible to add a preprend to the configuration to preprend the link in the url?

{{ templateContent | toc | safe }}

could then accept something along those line

{{ item.templaceContent | toc: '{"prepend": "{{item.permalink}}/"} | safe' }}

where item would be a page inside a loop.

I’m gonna have a look at the code and see if that could work, but i don’t know if the question was asked before.

julientaq commented 3 years ago

I digged a bit and i have a couple of questions.

The npm package for this plugin has only a toc.js, and the code is a bit different from the source files, and, if i try to do the following:

<aside>
  {{ content | toc: '{"tags":["h2","h3"],"wrapper":"div","wrapperClass":"content-tableau"}' }}
</aside>

i get a

`TemplateWriterWriteError` was thrown
> (./src/layouts/bookindex.njk) [Line 17, Column 49]

Thanks!

julientaq commented 3 years ago

It seems that there is a fork of this repo here (https://github.com/JordanShurmer/eleventy-plugin-nesting-toc) that support nesting, and that may explain where i got lost. i’m gonna close this issue, unless you want to have a look at it.

Thanks!

julientaq commented 3 years ago

(i fixed it using another filter:

  eleventyConfig.addFilter("prependLinks", function (value, prepend) {
    return value.replace(/<a href="/g, `<a href="${prepend}`)   
  });

and

{{ item.templateContent | toc | prependLinks(item.url) | safe}}

in my template file