excerpt.js 741 B

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