1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const {
|
13 | map, pipe, filter, list: flist, mapSort, uniq,
|
14 | } = require('ferrum');
|
15 | const {
|
16 | root, paragraph, text, heading, list, listItem, link, inlineCode,
|
17 | } = require('mdast-builder');
|
18 | const i18n = require('es2015-i18n-tag').default;
|
19 | const s = require('./symbols');
|
20 | const { gentitle, gendescription } = require('./formattingTools');
|
21 | const { keyword } = require('./keywords');
|
22 |
|
23 | function 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 |
|
42 |
|
43 |
|
44 | function 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]),
|
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),
|
62 | filter((schema) => !!schema[s.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 |
|
88 |
|
89 | return readmenode;
|
90 | }
|
91 | return null;
|
92 | };
|
93 | }
|
94 |
|
95 | module.exports = build;
|