Skip to content

Commit

Permalink
feat(markdown): add YAML frontmatter support
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 3, 2019
1 parent 608674b commit 4df92e6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
generate,

// generate Markdown ASTs
build,
build({}),

// build readme
readme({
Expand All @@ -177,6 +177,7 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio
info,
error,
debug,
meta: argv.m
}),
))

Expand Down
18 changes: 11 additions & 7 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
*/
const { each, values } = require('ferrum');
const {
root, paragraph, text, heading,
root, paragraph, text, heading, brk, separator
} = require('mdast-builder');

function build(schemas) {
function build({ }) {


return (schemas) => {
// eslint-disable-next-line no-return-assign, no-param-reassign
each(values(schemas), schema => schema.markdown = root([
each(values(schemas), schema => schema.markdown = root([
// todo add more elements
heading(1, text(schema.title)),
paragraph(text('This is a schema.')),
]));
return schemas;
heading(1, text(schema.title)),
paragraph(text('This is a schema.')),
]));
return schemas;
};
}

module.exports = build;
17 changes: 14 additions & 3 deletions lib/writeMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const unified = require('unified');
const path = require('path');
const inspect = require('unist-util-inspect');
const fs = require('fs-extra');
const yaml = require('js-yaml');

function writeMarkdown({
out, debug, error, info,
out, debug, error, info, meta
}) {
const dbg = (message) => {
if (debug && typeof message === 'object') {
Expand All @@ -35,8 +36,18 @@ function writeMarkdown({
return (schemas) => {
each(pairs(schemas), ([name, schema]) => {
dbg(schema.markdown);
const fileName = path.resolve(out, `${name}.md`);
fs.writeFile(fileName, processor.stringify(schema.markdown), (err) => {
const fileName = path.resolve(out, `${name}.schema.md`);

const output =
// add YAML frontmatter
(!meta ? '' : '---\n') +
(!meta ? '' : yaml.safeDump(meta)) +
(!meta ? '' : '---\n\n') +

processor.stringify(schema.markdown);


fs.writeFile(fileName, output, (err) => {
if (err) {
error(err);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"ferrum": "^1.4.1",
"github-slugger": "^1.2.1",
"i18next": "^19.0.1",
"js-yaml": "^3.13.1",
"json-pointer": "^0.6.0",
"mdast-builder": "^1.1.1",
"mdast-util-to-string": "^1.0.7",
Expand Down

0 comments on commit 4df92e6

Please sign in to comment.