Usar Gray-matter
Importar gray-matter:
const matter = require('gray-matter');
Pasar un string a gray-matter:
console.log(matter('---\ntitle: Front Matter\n---\nThis is content.'));
Regresar:
{
content: '\nThis is content.',
data: {
title: 'Front Matter'
}
}
Ejemplo:
Crear un archivo file.html
con el siguiente contenido:
---
title: Hello
slug: home
---
<h1>Hello world!</h1>
Crear un archivo example.js
con el siguiente contenido:
const fs = require('fs');
const matter = require('gray-matter');
const str = fs.readFileSync('file.html', 'utf8');
console.log(matter(str));
Ejecutar en la terminal:
node example
Como resultado un objeto como este:
{
content: '<h1>Hello world!</h1>',
data: { title: 'Hello', slug: 'homes' },
isEmpty: false,
excerpt: ''
}
Enlaces de referencia
Last updated