JordanShurmer / eleventy-plugin-nesting-toc

11ty plugin to generate a TOC from page content
17 stars 5 forks source link

Support usage in a content template #4

Open JordanShurmer opened 3 years ago

JordanShurmer commented 3 years ago

Background

This comes from an issue in the upstream fork:

https://github.com/jdsteinbach/eleventy-plugin-toc/issues/21

Problem

Apparently, in certain situations the content is not coming through as String. The error that gets thrown is:

`Error` was thrown:
    Error: cheerio.load() expects a string

The crux of the problem, in the words of @netmikey

I found out that this happens when you try to use content on a content template itself. I guess you have to use this outside the content, e.g. on the layout template.

Solution

Need to find out what kind of object content is when used in a content file. Then turn it into the expected string (if possible) before passing it to cheerio.

jdsteinbach commented 3 years ago

Hi @JordanShurmer - thanks for your help/comments in my repo!

Eleventy doesn't have a content variable available inside a content file. In a content file, it's undefined: content doesn't exist until a layout template has incoming content to process. If you've got ideas on how to handle this better, I'd love to hear how you're aiming to solve it. Thanks!

JordanShurmer commented 3 years ago

I have not put much thought into this bug.

It's not something I personally have a problem with, but recorded the issue so that it can be tracked and hopefully a solution can be reached.

If there is no content available in the content file, perhaps documentation for other approaches is the best solution. For example, adding something like this to the READMEs: https://github.com/jdsteinbach/eleventy-plugin-toc/issues/21#issuecomment-780122410