Skip to content

Instantly share code, notes, and snippets.

@jensgro
Forked from danfascia/11ty_excerpts.js
Created February 9, 2023 22:18
Show Gist options
  • Save jensgro/5b370d0ecb2352bd0c8d0921f67c5028 to your computer and use it in GitHub Desktop.
Save jensgro/5b370d0ecb2352bd0c8d0921f67c5028 to your computer and use it in GitHub Desktop.
11ty filter to split a string {{content}} into an excerpt (i.e. all stuff before <!--more-->) and the remainder (i.e. stuff after that tag...). Source: https://hawksworx.com
/**
* Split the content into excerpt and remainder
*
* @param {String} str
* @param {String [excerpt | remainder]} section
*
* If excerpt or nothing is passed as an argument, we return what was before the split marker.
* If remainder is passed as an argument, we return the rest of the post
*
*/
module.exports = function(str, section) {
var content = new String(str);
var delimit = "<!--more-->";
var parts = content.split(delimit);
var which = section == 'remainder' ? 1 : 0;
if(parts.length) {
return parts[which];
} else {
return str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment