l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

excerpt.js (741B)


      1 'use strict';
      2 
      3 const defaults = require('./defaults');
      4 
      5 module.exports = function(file, options) {
      6   const opts = defaults(options);
      7 
      8   if (file.data == null) {
      9     file.data = {};
     10   }
     11 
     12   if (typeof opts.excerpt === 'function') {
     13     return opts.excerpt(file, opts);
     14   }
     15 
     16   const sep = file.data.excerpt_separator || opts.excerpt_separator;
     17   if (sep == null && (opts.excerpt === false || opts.excerpt == null)) {
     18     return file;
     19   }
     20 
     21   const delimiter = typeof opts.excerpt === 'string'
     22     ? opts.excerpt
     23     : (sep || opts.delimiters[0]);
     24 
     25   // if enabled, get the excerpt defined after front-matter
     26   const idx = file.content.indexOf(delimiter);
     27   if (idx !== -1) {
     28     file.excerpt = file.content.slice(0, idx);
     29   }
     30 
     31   return file;
     32 };