add excerpt shortcode
This commit is contained in:
parent
70f4cc13ac
commit
83c3063720
2 changed files with 32 additions and 0 deletions
30
_config/shortcodes.js
Normal file
30
_config/shortcodes.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
const seperator = {
|
||||
start: '<!-- excerpt start -->',
|
||||
end: '<!-- excerpt end -->',
|
||||
total: '<!-- excerpt -->'
|
||||
};
|
||||
|
||||
module.exports = function (eleventyConfig) {
|
||||
// excerpt shortcode for feed layouts
|
||||
// taken from https://github.com/brob/eleventy-plugin-blog-tools
|
||||
eleventyConfig.addShortcode("excerpt", 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);
|
||||
let totalPosition = articleContent.toLowerCase().indexOf(seperator.total)
|
||||
|
||||
if (totalPosition !== -1) {
|
||||
excerpt = articleContent.substring(0, totalPosition);
|
||||
} else 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
|
||||
});
|
||||
};
|
||||
|
|
@ -5,6 +5,7 @@ const toml = require("@iarna/toml");
|
|||
const addDateParsing = require("./_config/date");
|
||||
const addFilters = require("./_config/filters");
|
||||
const addPlugins = require("./_config/plugins");
|
||||
const addShortcodes = require("./_config/shortcodes");
|
||||
|
||||
module.exports = function (eleventyConfig) {
|
||||
let siteData;
|
||||
|
|
@ -28,6 +29,7 @@ module.exports = function (eleventyConfig) {
|
|||
addDateParsing(eleventyConfig, { TIME_ZONE: siteData.timezone });
|
||||
addFilters(eleventyConfig, { TIME_ZONE: siteData.timezone, defaultLanguage: siteData.default_language });
|
||||
addPlugins(eleventyConfig, { defaultLanguage: siteData.default_language });
|
||||
addShortcodes(eleventyConfig);
|
||||
|
||||
return {
|
||||
markdownTemplateEngine: "njk",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue