-
-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shortcode: Post excerpt/summary – How to access post content? #179
Comments
Hey @kleinfreund, where is |
Ah, yeah, that should’ve been in the original post (added the front matter there now!). In the site I’m converting, I have a bunch of posts that are already tagged (e.g. When logging |
Ah pagination, yeah. I’m almost certain this is related to #171 but I’ll leave it open until after 0.5.1 is out to confirm. |
Well, I’m confident, maybe almost certain is a little too much 😅 |
The other thing this made me think of is the excerpt feature on https://www.npmjs.com/package/gray-matter#optionsexcerpt
|
I added some additional tests to confirm that this should work in 0.5.1 |
…tent properly in collections. Related to #179
I think I spoke too soon. I ended up making a huge refactoring change in the |
Work for this also created most of #108, so we got that going for us 😅 |
No need to apologize. I takes what it takes. It seems very worthwhile. |
v0.5.1 is out, please retest! (and reopen if necessary) |
Yes and no. I’m not quite sure. It works as in every post (i.e. every document in a collection) generates an excerpt, but for the first page in the pagination (with pagination size 5, that’s the first 5 documents), this is still triggered: if (!doc.hasOwnProperty('templateContent')) {
console.warn('>>> Failed to extract excerpt: Document has no property `templateContent`.');
console.warn(doc.data.title);
return;
} Note that these documents do have an excerpt in the output meaning that every document has the Leaving this closed for now as the issue at hand is resolved. For reference and if you need it, here is what I’m currently using to generate post excerpts: module.exports = function (eleventyConfig) {
// Defines shortcode for generating post excerpts
eleventyConfig.addShortcode('excerpt', post => extractExcerpt(post));
// ...
};
const excerptMinimumLength = 140;
const excerptSeparator = '<!--more-->'
/**
* Extracts the excerpt from a document.
*
* @param {*} doc A real big object full of all sorts of information about a document.
* @returns {String} the excerpt.
*/
function extractExcerpt(doc) {
if (!doc.hasOwnProperty('templateContent')) {
console.warn('Failed to extract excerpt: Document has no property `templateContent`.');
return;
}
const content = doc.templateContent;
if (content.includes(excerptSeparator)) {
return content.substring(0, content.indexOf(excerptSeparator)).trim();
}
else if (content.length <= excerptMinimumLength) {
return content.trim();
}
const excerptEnd = findExcerptEnd(content);
return content.substring(0, excerptEnd).trim();
}
/**
* Finds the end position of the excerpt of a given piece of content.
* This should only be used when there is no excerpt marker in the content (e.g. no `<!--more-->`).
*
* @param {String} content The full text of a piece of content (e.g. a blog post)
* @param {Number?} skipLength Amount of characters to skip before starting to look for a `</p>`
* tag. This is used when calling this method recursively.
* @returns {Number} the end position of the excerpt
*/
function findExcerptEnd(content, skipLength = 0) {
if (content === '') {
return 0;
}
const paragraphEnd = content.indexOf('</p>', skipLength) + 4;
if (paragraphEnd < excerptMinimumLength) {
return paragraphEnd + findExcerptEnd(content.substring(paragraphEnd), paragraphEnd);
}
return paragraphEnd;
} |
I’m currently writing a shortcode for post excerpts/summaries:
Something along the lines of this:
Now, it seems like
post.templateContent
doesn’t always contain what I want. Sometimes, this property doesn’t exist. How would I go about this? Do I usepost.inputContent
and strip any front matter myself? Is that safe? Seems odd.The text was updated successfully, but these errors were encountered: