Formats a Microformat JSON representation into eg. a Jekyll post
Requires at least Node.js 6.x
npm install format-microformat --save
Alpha
Currently formats data into a hardcoded Jekyll style. This is intended to become more dynamic and configurable in the future to adapt to more styles.
Simple:
const MicropubFormatter = require('format-microformat');
const formatter = new MicropubFormatter();
formatter.formatAll(micropubDocument)
.then(formatted => {
// Lots of formatted data that one can do lots of fun stuff with. Publish somewhere or such perhaps?
// Available keys are "filename", "url", "content" and lastly "files" if any files were uploaded
});
Advanced:
const MicropubFormatter = require('format-microformat');
const formatter = new MicropubFormatter('http://example.com/');
formatter.preFormat(micropubDocument)
.then(preFormatted => Promise.all([
formatter.formatFilename(preFormatted),
formatter.format(preFormatted),
formatter.formatURL(preFormatted),
]))
.then(([formattedFilename, formattedContent, formattedUrl]) => {
// Lots of formatted data that you can do lots of fun stuff with. Publish it somewhere maybe?
});
new MicropubFormatter([options])
true
then no conversion to Markdown will happen for the content.true
, then the slug creation will use the properties.content
data as a fallback to properties.name
prior to basing the slug on the timestamp.micropubDocument
with defaults that will be added as part of the preFormat()
. Useful to eg. ensure that all documents have a language explicitly set.false
to disable default category derivingtrue
to try and autodetect everything_posts/:year-:month-:day-:slug
:filesslug
. Defaults to: media/:year-:month-:slug/:filesslug
micropubpost
one, or false
to remove the layout completely from the front matterdate
false
then HTML-encoding will not happen for the content value. Defaults to true
layoutName
, filenameStyle
, filesStyle
and permalinkStyle
can all be set directly or through a callback that's given some data and that callback might either return a value directly or return a Promise
that eventually resolves to the value.
preFormat
:s and formats everything. Returns a Promise
that resolves to an object with the keys filename
, url
, content
, files
and raw
.micropubDocument
and ensures that all necessary parts are there. Currently required to run a micropubDocument
through this method before handing it to the rest of the methods (except formatAll()
).micropubDocument
. Includes the relative path to the file – which currently is always _posts/
properties.content
of the micropubDocument
– the rest of the data is put into the front matter.For exact implementation of how things are formatted, look at the code and the tests, this is just to get an overview of what one can expect the output to be.
The target is to get a filename similar to _posts/2015-06-30-awesomeness-is-awesome.html
. The date first and then either the defined slug or a slug derived from title or content.
The target is to get a relatuve URL similar to 2015/06/awesomeness-is-awesome/
where the slug is calculated in the same way as it is for the filename
. In some cases the URL will be prefixed with a category name – that's the case for eg. replies, likes and bookmarks – the first two are prefixed with interaction/
and the last one with bookmark/
.
The actual content of the file is the HTML of the properties.content
of the micropubDocument
. All other properties are added to the Jekyll Front Matter together with a couple of other defaults.
Some micropubDocument
properties receive special handling, the rest are included raw as arrays with an mf-
prefix to avoid collisions with pre-existing Jekull properties.
The ones with special handling are:
title
and if not specified, then a value of ''
will be used to avoid automatic generation of titlesslug
tags
date
and formatted as ISO-formatThere's also some defaults and derived values:
micropubpost
micropubDocument
properties and only used in some cases, like eg. for replies, likes and bookmarks – it's set to interaction
for the first two and to bookmark
for the last oneAn example of a generated Jekyll Front Matter:
layout: micropubpost
date: '2015-06-30T14:34:01.000Z'
title: awesomeness is awesome
slug: awesomeness-is-awesome
category: interaction
mf-like-of:
- 'http://example.com/liked/page'
An array of objects with a filename
containing the full path where the file should be uploaded as well as a buffer
key containing the same buffer object that was part of the original micropubDocument
files data. Also calculates URL:s for the location of the files post-upload and adds them to the proper photo
, video
or audio
property in the micropubDocument
so that they are made available in the content
.
Unlike the other formatting, file formatting happens fully in preFormat()
as it needs to be done prior to the rest of the step to make the URL:s of the uploaded files available to the rest of the formatting.
Currently supported file keys from the original micropubDocument
: photo
, video
and audio
micropubDocument
The format closely matches the JSON-representation of Micropub.
See the micropub-express module for documentation of this basic object.
In addition to the properties defined by the micropub-express
module, the preFormat()
method adds a top level derived
key that's used internally for values derived from.
The preFormat()
also flattens the files
object into an array of files and formats the filenames to the full path where a file should be uploaded.
micropubDocument
that can be used with this module