Skip to content

Commit

Permalink
feat(markdown): generate some property details
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 9, 2019
1 parent 011427c commit fa34cf1
Showing 1 changed file with 70 additions and 14 deletions.
84 changes: 70 additions & 14 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const {
each, values, map, list: flist, iter, flat, filter, size, foldl,
} = require('ferrum');
const {
root, paragraph, text, heading, code, table, tableRow, tableCell, link, inlineCode,
root, paragraph, text, heading, code, table, tableRow, tableCell, link, inlineCode, list, listItem
} = require('mdast-builder');
const i18n = require('es2015-i18n-tag').default;
const s = require('./symbols');
const { gentitle } = require('./formattingTools');
const { basename } = require('path');
const ghslugger = require('github-slugger');

function build({ header, links = {} }) {
const headerprops = [
Expand Down Expand Up @@ -165,12 +167,13 @@ function build({ header, links = {} }) {
* Generates the overview table row for a single property definition
* @param {*} param0
*/
function makepropheader(required = []) {
function makepropheader(required = [], slugger = ghslugger()) {
return ([name, definition]) => tableRow([
tableCell(text(name)), // Property
tableCell(link(`#${name}`, '', text(name))), // Property
tableCell(type(definition)),
tableCell(text(required.indexOf(name) > -1 ? i18n`Required` : i18n`Optional`)),
tableCell(nullable(definition)),
tableCell(link(`${definition[s.slug]}.md`, definition[s.id] + '#' + definition[s.pointer], text(definition[s.titles][0] || i18n`Untitled schema`))),
]);
}

Expand All @@ -179,7 +182,11 @@ function build({ header, links = {} }) {
* object.
* @param {*} props
*/
function makeproptable(props, required) {
function makeproptable(props, required, slugger) {
const proprows = Object.entries(props).map(makepropheader(required, slugger));

//const proprows = flist(map(iter(props || {}), makepropheader(required)));

return table('left', [
tableRow([
tableCell(text(i18n`Property`)),
Expand All @@ -188,19 +195,66 @@ function build({ header, links = {} }) {
tableCell(text(i18n`Nullable`)),
tableCell(text(i18n`Defined by`)),
]),
...flist(map(iter(props || {}), makepropheader(required))),
...proprows,
]);
}

function maketypefact(definition) {
return listItem([text(i18n`Type:`)]);
}

function makefactlist(name, definition, required = []) {
const children = [];


if (required.indexOf(name)>-1) {
children.push(listItem(text(i18n`is required`)))
} else {
children.push(listItem(text(i18n`is optional`)))
}

children.push(maketypefact(definition));

return list('unordered', children);
}

function makeproplist(properties, required, level = 2) {
if (!properties) {
return [];
}
return flist(flat(Object.entries(properties).map(([name, definition]) => {
const description = definition[s.meta].longdescription || paragraph(text(i18n`no description`));

return [
heading(level + 1, text(name)),
description,
paragraph(inlineCode(name)),
makefactlist(name, definition, required)
];
})));
}

/**
* Generates the definitions section for a schema
* @param {*} schema
*/
function makedefinitions(schema) {
if (schema.schema.definitions) {
function makedefinitions(schema, slugger) {
if (schema.definitions) {

const defgroups = Object.entries(schema.definitions).map(([groupname, subschema]) => {
const grouptable = makeproptable(subschema.properties, subschema.required, slugger);
return [
heading(2, text(i18n`Definitions group ${groupname}`)),
paragraph(text(i18n`Reference this group by using`)),
code('json', JSON.stringify({'$ref': `${subschema[s.id]}#${subschema[s.pointer]}`})),
grouptable,
...makeproplist(subschema.properties, subschema.required, 2)
]
});

return [
heading(1, text(i18n`${schema.title} Definitions`)),
makeproptable(schema.schema.definitions, schema.schema.required),
heading(1, text(i18n`${gentitle(schema[s.titles], schema.type)} Definitions`)),
...flist(flat(defgroups))
];
}
return [];
Expand All @@ -210,11 +264,11 @@ function build({ header, links = {} }) {
* Generates the properties section for a schema
* @param {*} schema
*/
function makeproperties(schema) {
if (schema.schema.properties) {
function makeproperties(schema, slugger) {
if (schema.properties) {
return [
heading(1, text(i18n`${schema.title} Properties`)),
makeproptable(schema.schema.properties, schema.schema.required),
makeproptable(schema.properties, schema.required, slugger),
];
}
return [];
Expand All @@ -223,12 +277,14 @@ function build({ header, links = {} }) {
console.log('generating markdown');
return (schemas) => {
return foldl(schemas, {}, (pv, schema) => {

const slugger = ghslugger();
// eslint-disable-next-line no-param-reassign
pv[schema[s.slug]] = root([
// todo add more elements
...makeheader(schema),
// ...makedefinitions(schema),
// ...makeproperties(schema),
...makedefinitions(schema, slugger),
...makeproperties(schema, slugger),
]);
return pv;
});
Expand Down

0 comments on commit fa34cf1

Please sign in to comment.