Giter Site home page Giter Site logo

gulp-front-matter's Introduction

gulp-front-matter

npm version Build Status Coverage Status

Information

Packagegulp-front-matter
Description Extract `YAML Front-Matter` header from files, removes it from `contents` and add a new `frontMatter` property to file object.
Node Version ≥ 4

Installation

Use npm.

npm install gulp-front-matter

Usage

const gulp = require('gulp');
const frontMatter = require('gulp-front-matter');

gulp.task('blog-posts', () => {
  return gulp.src('./posts/*.md')
    .pipe(frontMatter({ // optional configuration
      property: 'frontMatter', // property added to file object
                               // also works with deep property selectors
                               // e.g., 'data.foo.bar'
      remove: true // should we remove front-matter header?
    }))
    .pipe() // you may want to take a look at gulp-marked at this point
});

LICENSE

2-Clause BSD License © 2013 - 2015 Nicolas Chambrier, 2016 - 2017 Shinnosuke Watanabe

gulp-front-matter's People

Contributors

adam-lynch avatar mrzealot avatar naholyr avatar shannonmoeller avatar shinnn avatar shuhei avatar t8g avatar uhunkler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gulp-front-matter's Issues

Example on using the object

I'm sorry, I must be a noob, but it isn't clear to me how to use the object generated with gulp-front-matter. The examples are clear to me, but they don't show how you would use the object in a template for example.

I have the following code in my gulpfile.js

// Gulp modules
var gulp        = require( 'gulp' );
var markdown    = require( 'gulp-markdown' );
var frontMatter = require( 'gulp-front-matter' );
var template    = require( 'gulp-template' );

gulp.task('default', function () {
  return gulp.src( ['./**/*.{md,markdown}'] )
    .pipe(frontMatter({
      property: 'page' // property added to file object
    }))
    .pipe(template(page))
    .pipe(markdown())
    .pipe(gulp.dest(grOutput));
});

And am using it with this template:

---
name: MyName

---
Text
<%= page.name %>
Text

This is the error I get when I run gulp:

[gulp] Using gulpfile D:\Dropbox\Github\graphene\gulpfile.js
[gulp] Starting 'default'...
[gulp] 'default' errored after 7.49 ms page is not defined

D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\event-stream\node_modules\map-stream\index.js:103
        throw err
              ^
expected '<document start>', but found <block mapping end>
  in "undefined", line 12, column 1
    at ParserError.YAMLError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\errors.js:72:46)
    at ParserError.MarkedYAMLError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\errors.js:88:45)
    at new ParserError (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:17:48)
    at Constructor.Parser.Parser.parse_document_start (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:158:17)
    at Constructor.Parser.Parser.check_event (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\parser.js:63:48)
    at Constructor.Composer.Composer.get_single_node (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\composer.js:55:17)
    at Constructor.BaseConstructor.BaseConstructor.get_single_data (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\constructor.js:78:19)
    at load (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\node_modules\yaml-js\lib\yaml.js:113:19)
    at parse (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\index.js:26:27)
    at extractor (D:\Dropbox\Github\graphene\node_modules\gulp-front-matter\node_modules\front-matter\index.js:19:34)

I must be doing something wrong, and it's probably stupid. Or it might be a legitimate bug. Can you tell what's causing the error? I'm sure others of my 'skill level' will have the same problem. Maybe adding a simple usecase to the docs would solve this.

Thanks!

Expose Front Matter

Thanks for this plugin!

Just wanted to offer a suggestion - as a new gulp user, it took me a significant amount of time to figure out how to use it the way I wanted - to parse a markdown file with frontmatter content and perform some transformations on the frontmatter data. Here's an example markdown file:


---
id:           introduction
class:        step slide
properties:   data-x="-1000" data-y="-1500"

---

# Introduction
## Getting Started

Lorem ipsum ...

The documentation gives a clear example of where that data is stored:

property: 'frontMatter', // property added to file object

However - it's only clear now that I've learned about the file objects and pipelines, which was intimidating and time consuming as new user. At first I had absolutely no idea what the file object was or how to access it during the pipeline. It took a lot of learning to understand that one seemingly straight-forward thing.

There are currently several plugins for accessing the data in the pipeline - it seems like gulp-data is intended to be the defacto user-friendly interface, however I couldn't get it to work with gulp-front-matter (probably because I just don't understand it well enough yet).

I eventually found gulp-tap, and used the following code to tap into the pipeline and do what I needed (I put it here, because I've seen this same question asked several times on your issue tracker, on stackoverflow, and in several other places - but was unable to find any published solutions) :

var fs     = require('fs');
var gulp   = require('gulp');
var gutil  = require('gulp-util');
var tap    = require('gulp-tap');
var header = require('gulp-header');
var footer = require('gulp-footer');
var concat = require('gulp-concat');
var fm     = require('gulp-front-matter');
var marked = require('gulp-markdown');

// Build a single index.html from a glob of markdown files, specifically for use with impressjs.
gulp.task('build:index', function (callback) {

  // Parse a glob of markdown files.
  gulp.src(src + '/markup/**/*.md')

    // Strip the frontmatter - it gets shoved into file.fm['...']
    .pipe(fm ({property: 'fm', remove: true}))

    // Expose file.fm['..'] variables generated by gulp-front-matter using gulp-tap.
    // Because we don't want to write empty tags if the values aren't defined, we
    // set default variables that initialize the entire tag attribute.
    .pipe(tap(function(file, t) {
      slide_id = file.fm['id']==undefined ? '' : 'id="' + file.fm['id'] + '"'
      slide_class = file.fm['class']==undefined ? '' : 'class="' + file.fm['class'] + '"'
      slide_properties  = file.fm['properties']
    }))

    // Generate html from the remaining markdown.
    .pipe(marked())

    // Wrap the markup in div tags, generated from the frontmatter.
    // If the variables are not defined, an error is not generated, they simply are not
    // written. Perfect behavior for my use case, but probably should be more explicit.
    .pipe(header('<div <%= slide_id %> <%= slide_class %> <%= slide_properties %>>\n'))
    .pipe(footer('</div>\n'))

    // Concatenate all the html into a single file.
    .pipe(concat('index.html'))

    // Wrap the generating html in our impressjs header and footer.
    .pipe(header(fs.readFileSync('src/templates/impress-header.tpl', 'utf8')))
    .pipe(footer(fs.readFileSync('src/templates/impress-footer.tpl', 'utf8')))

    // Write the index file.
    .pipe(gulp.dest(dist))

    // Fill up our logs.
    gutil.log('Recompiled index.html')
});

Hopefully if some other gulp newbie comes along, they'll find this helpful.

Anyway, my suggestion is that it would probably be very helpful to new users if gulp-front-matter was compatible with gulp-data and included an example of how to parse that data, expose it with gulp-data, and then do some example processing.

Example of adding additional static/global data

I had a use case where I wanted the navigation to be defined in a separate file JSON file. It took me a while to figure how to combine this with the YAML that was being process for each page, so I thought I'd share here

gulp.task('templates', function() {
    // Load static / global content from JSON file
    var nav = require('./src/data/global.json');
    gulp.src('./src/pages/**/*.html')
        .pipe(frontMatter())
        .pipe(plugins.layout(function(file) {
             file.frontMatter["global"] = global;
             return file.frontMatter;
        }))
        .pipe(gulp.dest('.tmp/'));
});

Allow using global data alongside per-file front matter

Would it be possible to introduce a config option for importing a generic global configuration file? Something like the following:

# global.yaml
hello: world
foo: bar

With a file called index.md


---
title: Asdf

---

Hello world

Then in gulpfile.js

var fm = require('gulp-front-matter');

gulp.task('compile', function ()
{
    gulp.src('*.md')
        .pipe(fm({ property: 'data', import: 'global.yaml' }))
        .pipe(...)
});

Then the fm property data would contain things as if the Markdown frontmatter had contained


---
title: "Asdf"
hello: "world"
foo: "bar"

---

Or should/could I combine some other Gulp plugin to achieve this?

Adding body

Would it be possible to add the body attribute that frontmatter provide in their returned object. Adding to your plugin on line 24:

file[options.property].body = content.body

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.