UNPKG

4.04 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 fs = require('fs');
13const path = require('path');
14const unified = require('unified');
15const parse = require('remark-parse');
16const gfm = require('remark-gfm');
17const stringify = require('mdast-util-to-string');
18const s = require('./symbols');
19const { keyword } = require('./keywords');
20
21function isabstract(schema) {
22 return schema.definitions !== undefined
23 && (!schema[keyword`properties`] || Object.keys(schema[keyword`properties`]).length === 0);
24}
25
26function isextensible(schema) {
27 return schema.definitions !== undefined || schema[keyword`meta:extensible`] === true;
28}
29
30function 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
41function 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
49function 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
62function 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
78function shorten(str) {
79 return str.split('\n')[0].split('.')[0];
80}
81
82const parser = unified()
83 .use(gfm)
84 .use(parse);
85
86function parsedescription(str) {
87 const markdown = parser.parse(str);
88 return {
89 longdescription: markdown,
90 shortdescription: shorten(stringify(markdown)),
91 description: str,
92 };
93}
94
95function parsecomment(str = '') {
96 const markdown = parser.parse(str);
97 return {
98 longcomment: markdown,
99 shortcomment: shorten(stringify(markdown)),
100 comment: str,
101 };
102}
103
104function getstatus(schema) {
105 if (schema[keyword`deprecated`] === true) {
106 return 'deprecated';
107 }
108 return schema[keyword`meta:status`] || undefined;
109}
110
111function 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
122function 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
137module.exports.formatmeta = formatmeta;