Use Gray-matter
Import gray-matter
const matter = require('gray-matter');
Pass a string to gray-matter.
console.log(matter('---\ntitle: Front Matter\n---\nThis is content.'));
Returns:
{
content: '\nThis is content.',
data: {
title: 'Front Matter'
}
}
Example.
Create file file.html
with the following content.
---
title: Hello
slug: home
---
<h1>Hello world!</h1>
Create file example.js
with the following content.
const fs = require('fs');
const matter = require('gray-matter');
const str = fs.readFileSync('file.html', 'utf8');
console.log(matter(str));
Run in the terminal
node example
As a result an object like this.
{
content: '<h1>Hello world!</h1>',
data: { title: 'Hello', slug: 'homes' },
isEmpty: false,
excerpt: ''
}
Reference Links
Last updated