UNPKG

3.23 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const {
13 map, pipe, filter, list: flist, mapSort, uniq,
14} = require('ferrum');
15const {
16 root, paragraph, text, heading, list, listItem, link, inlineCode,
17} = require('mdast-builder');
18const i18n = require('es2015-i18n-tag').default;
19const s = require('./symbols');
20const { gentitle, gendescription } = require('./formattingTools');
21const { keyword } = require('./keywords');
22
23function makeversionnote(schemas) {
24 const versions = flist(uniq(schemas
25 .map((schema) => schema[keyword`$schema`])
26 .filter((e) => !!e)));
27
28 if (versions.length === 1) {
29 return [
30 heading(2, text(i18n`Version Note`)),
31 paragraph([
32 text('The schemas linked above follow the JSON Schema Spec version: '),
33 inlineCode(versions[0]),
34 ]),
35 ];
36 }
37 return [];
38}
39
40/**
41 * Generate the README.md
42 * @param {object} opts
43 */
44function build({ readme = true }) {
45 return (schemas) => {
46 if (readme) {
47 console.log('building readme');
48 const toplevel = flist(pipe(
49 schemas,
50 filter((schema) => !schema[s.parent]), // remove schemas with a parent
51 mapSort((schema) => gentitle(schema[s.titles], schema[keyword`type`])),
52 map((schema) => listItem(paragraph([
53 link(`./${schema[s.slug]}.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema[keyword`type`]))]),
54 text(' – '),
55 inlineCode(schema[keyword`$id`] || '-'),
56 ]))),
57 ), Array);
58
59 const bytype = (type) => flist(pipe(
60 schemas,
61 filter((schema) => schema[keyword`type`] === type), // remove schemas without matching type
62 filter((schema) => !!schema[s.parent]), // keep only schemas with a parent
63 mapSort((schema) => gentitle(schema[s.titles], schema[keyword`type`])),
64 map((schema) => listItem(paragraph([
65 link(`./${schema[s.slug]}.md`, gendescription(schema), [text(gentitle(schema[s.titles], schema[keyword`type`]))]),
66 text(' – '),
67 inlineCode(`${schema[s.id]}#${schema[s.pointer]}`),
68 ]))),
69 ), Array);
70
71 const readmenode = root([
72 heading(1, text(i18n`README`)),
73 heading(2, text(i18n`Top-level Schemas`)),
74 list('unordered', toplevel),
75
76 heading(2, text(i18n`Other Schemas`)),
77
78 heading(3, text(i18n`Objects`)),
79 list('unordered', bytype('object')),
80
81 heading(3, text(i18n`Arrays`)),
82 list('unordered', bytype('array')),
83
84 ...makeversionnote(schemas),
85 ]);
86
87 // console.log(inspect(readmenode));
88
89 return readmenode;
90 }
91 return null;
92 };
93}
94
95module.exports = build;