1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const fs = require('fs');
|
13 | const path = require('path');
|
14 | const unified = require('unified');
|
15 | const parse = require('remark-parse');
|
16 | const gfm = require('remark-gfm');
|
17 | const stringify = require('mdast-util-to-string');
|
18 | const s = require('./symbols');
|
19 | const { keyword } = require('./keywords');
|
20 |
|
21 | function isabstract(schema) {
|
22 | return schema.definitions !== undefined
|
23 | && (!schema[keyword`properties`] || Object.keys(schema[keyword`properties`]).length === 0);
|
24 | }
|
25 |
|
26 | function isextensible(schema) {
|
27 | return schema.definitions !== undefined || schema[keyword`meta:extensible`] === true;
|
28 | }
|
29 |
|
30 | function isidentifiable(schema) {
|
31 | if (!schema[keyword`properties`]) {
|
32 | return 'undefined';
|
33 | }
|
34 | if (schema[keyword`properties`][keyword`@id`] && schema[keyword`properties`][keyword`@id`][keyword`type`] === 'string' && schema[keyword`properties`][keyword`@id`].format === 'uri') {
|
35 | return 'true';
|
36 | } else {
|
37 | return 'false';
|
38 | }
|
39 | }
|
40 |
|
41 | function iscustom(schema) {
|
42 | return [...(schema[keyword`allOf`] || [])]
|
43 | .filter((e) => typeof e === 'object')
|
44 | .filter((e) => typeof e.$ref === 'string')
|
45 | .filter(({ $ref }) => $ref === 'https://ns.adobe.com/xdm/common/extensible.schema.json#/definitions/@context')
|
46 | .length > 0;
|
47 | }
|
48 |
|
49 | function getdefined(schema) {
|
50 | if (schema[s.parent]) {
|
51 | return {
|
52 | text: `${path.basename(schema[s.filename])}*`,
|
53 | link: schema[s.filename],
|
54 | };
|
55 | }
|
56 | return {
|
57 | text: path.basename(schema[s.filename]),
|
58 | link: schema[s.filename],
|
59 | };
|
60 | }
|
61 |
|
62 | function plaindescription(schema) {
|
63 | try {
|
64 | if (schema[s.filename] && !schema[s.parent]) {
|
65 | const filename = path.resolve(
|
66 | path.dirname(schema[s.filename]),
|
67 | schema[s.filename].replace(/\..*$/, '.description.md'),
|
68 | );
|
69 | const longdesc = fs.readFileSync(filename);
|
70 | return longdesc.toString();
|
71 | }
|
72 | } catch {
|
73 | return schema[keyword`description`] || '';
|
74 | }
|
75 | return schema[keyword`description`] || '';
|
76 | }
|
77 |
|
78 | function shorten(str) {
|
79 | return str.split('\n')[0].split('.')[0];
|
80 | }
|
81 |
|
82 | const parser = unified()
|
83 | .use(gfm)
|
84 | .use(parse);
|
85 |
|
86 | function parsedescription(str) {
|
87 | const markdown = parser.parse(str);
|
88 | return {
|
89 | longdescription: markdown,
|
90 | shortdescription: shorten(stringify(markdown)),
|
91 | description: str,
|
92 | };
|
93 | }
|
94 |
|
95 | function parsecomment(str = '') {
|
96 | const markdown = parser.parse(str);
|
97 | return {
|
98 | longcomment: markdown,
|
99 | shortcomment: shorten(stringify(markdown)),
|
100 | comment: str,
|
101 | };
|
102 | }
|
103 |
|
104 | function getstatus(schema) {
|
105 | if (schema[keyword`deprecated`] === true) {
|
106 | return 'deprecated';
|
107 | }
|
108 | return schema[keyword`meta:status`] || undefined;
|
109 | }
|
110 |
|
111 | function getrestrictions(schema) {
|
112 | if (schema[keyword`readOnly`] === true && schema[keyword`writeOnly`] === true) {
|
113 | return 'secret';
|
114 | } else if (schema[keyword`readOnly`] === true) {
|
115 | return 'readOnly';
|
116 | } else if (schema[keyword`writeOnly`] === true) {
|
117 | return 'writeOnly';
|
118 | }
|
119 | return undefined;
|
120 | }
|
121 |
|
122 | function formatmeta(schema) {
|
123 | return {
|
124 | abstract: isabstract(schema),
|
125 | extensible: isextensible(schema),
|
126 | status: getstatus(schema),
|
127 | identifiable: isidentifiable(schema),
|
128 | custom: iscustom(schema),
|
129 | additional: schema[keyword`additionalProperties`] !== false,
|
130 | definedin: getdefined(schema),
|
131 | restrictions: getrestrictions(schema),
|
132 | ...parsedescription(plaindescription(schema)),
|
133 | ...parsecomment(schema[keyword`$comment`]),
|
134 | };
|
135 | }
|
136 |
|
137 | module.exports.formatmeta = formatmeta;
|