Skip to content

Commit

Permalink
feat(markdown): highlight keyword usage for documentation
Browse files Browse the repository at this point in the history
see #100
  • Loading branch information
trieloff committed Dec 11, 2019
1 parent c6fbe81 commit d35e4ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/formatInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const parse = require('remark-parse');
const stringify = require('mdast-util-to-string');
const { formatname } = require('./formatname');
const s = require('./symbols');
const { keyword } = require('./keywords');


function isabstract(schema) {
Expand All @@ -25,14 +26,14 @@ function isabstract(schema) {
}

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

function isidentifiable(schema) {
if (!schema.properties) {
return 'undefined';
}
if (schema.properties['@id'] && schema.properties['@id'].type === 'string' && schema.properties['@id'].format === 'uri') {
if (schema.properties[keyword`@id`] && schema.properties[keyword`@id`].type === 'string' && schema.properties[keyword`@id`].format === 'uri') {
return 'true';
} else {
return 'false';
Expand Down Expand Up @@ -128,7 +129,7 @@ function formatmeta(schema) {
type: gettype(schema),
abstract: isabstract(schema),
extensible: isextensible(schema),
status: schema['meta:status'] || undefined,
status: schema[keyword`meta:status`] || undefined,
identifiable: isidentifiable(schema),
custom: iscustom(schema),
additional: schema.additionalProperties !== false,
Expand Down
15 changes: 8 additions & 7 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const i18n = require('es2015-i18n-tag').default;
const ghslugger = require('github-slugger');
const s = require('./symbols');
const { gentitle } = require('./formattingTools');
const { keyword } = require('./keywords');

function build({ header, links = {}, includeproperties = [] } = {}) {
const formats = {
Expand Down Expand Up @@ -253,7 +254,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {

function nullable(property) {
const types = Array.isArray(property.type) ? property.type : [property.type];
const nulltypes = flist(filter(types, mytype => mytype === 'null'));
const nulltypes = flist(filter(types, mytype => mytype === keyword`null`));
if (size(nulltypes)) {
return text(i18n`can be null`);
}
Expand Down Expand Up @@ -299,16 +300,16 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
function maketypefact(definition, isarray = '') {
const alltypes = Array.isArray(definition.type) ? definition.type : [definition.type];
// filter out types that are null
const realtypes = alltypes.filter(mytype => mytype !== 'null');
const realtypes = alltypes.filter(mytype => mytype !== keyword`null`);
// can the type be `null`
const isnullable = alltypes.filter(mytype => mytype === 'null').length > 0;
const isnullable = alltypes.filter(mytype => mytype === keyword`null`).length > 0;
// is there only a single type or can there be multiple types
const singletype = realtypes.length <= 1;
const [firsttype] = realtypes;
// is `null` the only allowed value
const nulltype = isnullable && realtypes.length === 0;

const array = firsttype === 'array';
const array = firsttype === keyword`array`;
const merged = !!(definition.allOf || definition.anyOf || definition.oneOf || definition.not);

if (array && definition.items) {
Expand All @@ -333,7 +334,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
if (definition.title) {
// if the type has a title, always create a link to the schema
return [text(' ('), link(`${definition[s.slug]}.md`, '', text(definition.title)), text(')')];
} else if (!singletype || firsttype === 'object' || merged) {
} else if (!singletype || firsttype === keyword`object` || merged) {
return [text(' ('), link(`${definition[s.slug]}.md`, '', text(i18n`Details`)), text(')')];
}
return [];
Expand All @@ -345,7 +346,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {
function makenullablefact(definition) {
const alltypes = Array.isArray(definition.type) ? definition.type : [definition.type];
// can the type be `null`
const isnullable = alltypes.filter(mytype => mytype === 'null').length > 0;
const isnullable = alltypes.filter(mytype => mytype === keyword`null`).length > 0;

if (isnullable) {
return listItem(paragraph(text(i18n`can be null`)));
Expand Down Expand Up @@ -452,7 +453,7 @@ function build({ header, links = {}, includeproperties = [] } = {}) {

if (schema.enum) {
console.log('enum!', schema[s.filename], schema[s.pointer]);
const metas = schema['meta:enum'] || {};
const metas = schema[keyword`meta:enum`] || {};
constraints.push(paragraph([strong(text(i18n`constant`)), text(': '), text(i18n`the value of this property must be equal to one of the following values:`)]));
constraints.push(table('left', [
tableRow([
Expand Down

0 comments on commit d35e4ed

Please sign in to comment.