Eleventy: Any {{ excerpt }} example ?

Created on 24 Apr 2020  Β·  3Comments  Β·  Source: 11ty/eleventy

Hi,
I try to parse {{ excerpts }} from content based on the example available on 11ty.dev with no success.
Does anyone have a website repository who use the excerpt in order to help me understand my mistake ?

education

All 3 comments

Thank you for this approach.

SΓ©bastien

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Saturday 25 April 2020 08:29, Distinct Perspective notifications@github.com wrote:

create a custom filter in eleventy.js

const excerpt = require("./src/excerpt");
module.exports = function(eleventyConfig) {
eleventyConfig.addShortcode("excerpt", excerpt);
}

call it like this
{{ content | excerpt }}

filter code /src/excerpt

const seperator = {start: '', end: ''};

module.exports = function(article) {
let excerpt = article.data.excerpt ? <p>${article.data.excerpt}</p> : "";
const articleContent = article.templateContent;

let startPosition = articleContent.toLowerCase().indexOf(seperator.start);
let endPosition = articleContent.toLowerCase().indexOf(seperator.end);
if (startPosition !== -1 && endPosition !== -1) {
    excerpt = articleContent.substring(startPosition + seperator.start.length, endPosition);
} else if (!article.data.excerpt) {
    let startPosition = articleContent.toLowerCase().indexOf('<p>');
    let endPosition = articleContent.toLowerCase().indexOf('</p>');

    excerpt = articleContent.substring(startPosition + 3, endPosition);
}
return excerpt

}

β€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

(Extracted from collapsed mail)

create a custom filter in eleventy.js

 const excerpt = require("./src/excerpt");
 module.exports = function(eleventyConfig) {
     eleventyConfig.addShortcode("excerpt", excerpt);
 }

call it like this

 {{ content | excerpt }}

filter code /src/excerpt

 const seperator = {start: '<!-- excerpt start -->', end: '<!-- excerpt end -->'};

 module.exports = function(article) {
     let excerpt = article.data.excerpt ? `<p>${article.data.excerpt}</p>` : "";
     const articleContent = article.templateContent;

     let startPosition = articleContent.toLowerCase().indexOf(seperator.start);
     let endPosition = articleContent.toLowerCase().indexOf(seperator.end);
     if (startPosition !== -1 && endPosition !== -1) {
         excerpt = articleContent.substring(startPosition + seperator.start.length, endPosition);
     } else if (!article.data.excerpt) {
         let startPosition = articleContent.toLowerCase().indexOf('<p>');
         let endPosition = articleContent.toLowerCase().indexOf('</p>');

         excerpt = articleContent.substring(startPosition + 3, endPosition);
     }
     return excerpt
 }

If that works for you, could you close the issue, @smaimon?

Thank you @Ryuno-Ki

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nilsmielke picture nilsmielke  Β·  4Comments

matt-auckland picture matt-auckland  Β·  3Comments

jamrelian picture jamrelian  Β·  3Comments

kaloja picture kaloja  Β·  3Comments

zac-heisey picture zac-heisey  Β·  3Comments