Skip to content

Commit

Permalink
feat(markdown): show extensibility and abstraction in header
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 3, 2019
1 parent 6225b9f commit 90a9a8e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 16 deletions.
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ readdirp.promise(schemaPath, { root: schemaPath, fileFilter: `*.${schemaExtensio

// generate Markdown ASTs
build({
header: argv.h
header: argv.h,
links: docs,
}),

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

Expand Down
22 changes: 22 additions & 0 deletions lib/formatInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,33 @@ function formatInfo({ extension }) {
}
}

function isabstract(schema) {
return schema.definitions !== undefined &&
(!schema.properties || Object.keys(schema.properties).length === 0);
}

function isextensible(schema) {
return schema.definitions !== undefined || schema['meta:extensible'] === true;
}

function formatmeta(schema) {
return {
abstract: isabstract(schema.schema),
extensible: isextensible(schema.schema),
status: undefined,
identifiable: undefined,
custom: undefined,
additional: undefined,
definedin: undefined
};
}

return schemas => map(schemas, (schema) => {
const newobj = {
...schema,
...parsedescription(plaindescription(schema)),
title: formatname(schema),
meta: formatmeta(schema)
};
return newobj;
});
Expand Down
97 changes: 89 additions & 8 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,100 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { each, values } = require('ferrum');
const {
root, paragraph, text, heading, code
each, values, map, list: flist,
} = require('ferrum');
const {
root, paragraph, text, heading, code, table, tableRow, tableCell, link,
} = require('mdast-builder');

function build({ header }) {
function build({ header, links = {} }) {
const headerprops = [
{
name: 'abstract',
title: 'Abstract',
truelabel: 'Cannot be instantiated',
falselabel: 'Can be instantiated',
undefinedlabel: 'Unknown abstraction'
},
{
name: 'extensible',
title: 'Extensible',
undefinedlable: 'Unknown extensibility',
truelabel: 'Yes',
falselabel: 'No',
},
{
name: 'status',
title: 'Status',
undefinedlabel: 'Unknown status',
truelabel: 'Yes',
falselabel: 'No',
},
{
name: 'identifiable',
title: 'Identifiable',
truelabel: 'Yes',
falselabel: 'No',
undefinedlabel: 'Unknown identifiability'
},
{
name: 'custom',
title: 'Custom Properties',
truelabel: 'Allowed',
falselabel: 'Forbidden',
undefinedlabel: 'Unknown custom properties'
},
{
name: 'additional',
title: 'Additional Properties',
truelabel: 'Allowed',
falselabel: 'Forbidden',
undefinedlabel: 'Unknown additional properties'
},
{
name: 'defined',
title: 'Defined In',
undefinedlabel: 'Unknown definition'
},
];


function makeheader(schema) {
if (header) {
return [
heading(1, text(schema.title)),
paragraph(code('txt', schema.id + (schema.pointer ? '#' + schema.pointer : ''))),
schema.longdescription
]
heading(1, text(`${schema.title} Schema`)),
paragraph(code('txt', schema.id + (schema.pointer ? `#${schema.pointer}` : ''))),
schema.longdescription,
table('left', [
// iterate over header
tableRow(
flist(
map(headerprops,
({ name, title }) => {
if (links[name]) {
return tableCell(link(links[name], `What does ${title} mean?`, text(title)));
}
return tableCell(text(title));
}), Array,
),
),
tableRow(
flist(
map(headerprops,
(prop) => {
// this is a linked property

if (schema.meta && schema.meta[prop.name] && schema.meta[prop.name].link) {
return tableCell(link(schema.meta[prop.name].link, '', text(schema.meta[prop.name].text)));
}
const value = schema.meta ? schema.meta[prop.name] : undefined;
return tableCell(text(prop[String(value) + 'label'] || 'Unknown'));
}), Array,
),
),
]),
];
}
return [];
}
Expand All @@ -30,7 +111,7 @@ function build({ header }) {
// eslint-disable-next-line no-return-assign, no-param-reassign
each(values(schemas), schema => schema.markdown = root([
// todo add more elements
...makeheader(schema)
...makeheader(schema),
]));
return schemas;
};
Expand Down
12 changes: 6 additions & 6 deletions lib/writeMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fs = require('fs-extra');
const yaml = require('js-yaml');

function writeMarkdown({
out, debug, error, info, meta
out, debug, error, info, meta,
}) {
const dbg = (message) => {
if (debug && typeof message === 'object') {
Expand All @@ -38,13 +38,13 @@ function writeMarkdown({
dbg(schema.markdown);
const fileName = path.resolve(out, `${name}.schema.md`);

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

processor.stringify(schema.markdown);
+ processor.stringify(schema.markdown);


fs.writeFile(fileName, output, (err) => {
Expand Down

0 comments on commit 90a9a8e

Please sign in to comment.