Add support for gray-matter sections #2502
Open
+5
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
gray-matter
SectionsPreamble
The gray-matter package includes multiple ways to read data from files, Eleventy has already implemented two methods:
Front Matter
Input:
Output:
Excerpts
Input:
Output:
gray-matter
however includes one more method:Sections
Input:
Output:
Not only do I believe this feature should be added just on the merit of Eleventy missing one-third of it's front matter capability, but sections actually come with quite a few interesting quirks.
This feature of gray-matter is not documented entirely well on it's page or in it's options, which is probably due to the fact that it is actually from a different package called sections-matter (same author) that gets built into it.
So after digging through section-matter's docs, here is what I put together.
Quirks
1) When files are processed, the entirety of the section gets removed from the content
(This can be seen in the code example above)
This would facilitate a great many creative uses, the first one that comes to my mind being a solution to this issue: #685
Use Case
index.md:
layout.njk:
index.html (result):
2) section-matter can run functions on the collected data
That heading probably doesn't explain it too well so here's a very simple example:
If we were to add the following to our
.eleventy.js
:Then the added function would write
hi
to our console for each section that section-matter reads.This lends itself to a lot of potential cool uses:
Use Case
Seeing how front matter isn't rendered into eleventy, we can render the section with the function ourselves:
index.md:
By default this section when added back in via template wouldn't be rendered in markdown. But if we added this very rough snippet to our config:
Then the output section content would indeed be rendered as an html list.
Quick Notes
sections
wasn't passed to{{ page }}
the way that excerpts don'tsections: true
is passed in the config, the only change I made was sending the data to the templateCaveats
key
,data
, andcontent
objects are all on the same level within the data output:sections
, then write the options to the keysection
safe
filter for the output sections (at least for nunjucks)section_parse
when it is acutally justparse
Documentation outdated jonschlinkert/section-matter#1