-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathbuild-schema-file.ts
More file actions
216 lines (188 loc) · 6.26 KB
/
build-schema-file.ts
File metadata and controls
216 lines (188 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* build-schema-file.ts
*
* Collects the existing schemas and builds a single JSON file with
* their description
*
* Copyright (C) 2021-2022 Posit Software, PBC
*/
import { resourcePath } from "../resources.ts";
import {
getSchemaDefinitionsObject,
setSchemaDefinition,
} from "../lib/yaml-validation/schema.ts";
import { ensureSchemaResources } from "./yaml-schema.ts";
import { revealPluginSchema } from "../../format/reveal/schemas.ts";
import { Element, initDenoDom, parseHtml } from "../deno-dom.ts";
import { pandocBinaryPath } from "../resources.ts";
import { execProcess } from "../process.ts";
import { walkSchema } from "../lib/yaml-validation/schema-utils.ts";
import { Schema, SchemaDocumentation } from "../lib/yaml-schema/types.ts";
import {
exportYamlIntelligenceResources,
setYamlIntelligenceResources,
} from "../lib/yaml-intelligence/resources.ts";
import { getFormatAliases } from "../lib/yaml-schema/format-aliases.ts";
import { getFrontMatterSchema } from "../lib/yaml-schema/front-matter.ts";
import { getProjectConfigSchema } from "../lib/yaml-schema/project-config.ts";
import { getEngineOptionsSchema } from "../lib/yaml-schema/chunk-metadata.ts";
import { languages, languageSchema } from "../handlers/base.ts";
import { idSchema } from "../lib/yaml-schema/common.ts";
import { kLangCommentChars } from "../lib/partition-cell-options.ts";
import { generateTypesFromSchemas } from "./types-from-schema.ts";
import { generateJsonSchemasFromSchemas } from "./json-schema-from-schema.ts";
import { InternalError } from "../lib/error.ts";
import { generateZodTypesFromSchemas } from "./zod-types-from-schema.ts";
////////////////////////////////////////////////////////////////////////////////
export async function buildIntelligenceResources() {
await ensureSchemaResources();
// call all of these to add them to the definitions list so we can
// get the markdown translations and record them.
getFormatAliases();
await getFrontMatterSchema();
await getProjectConfigSchema();
await getEngineOptionsSchema();
// in addition, we do have to record the (right now, only)
// plugin schema that exists
//
// FIXME there should be a plugin registration api or something
const externalSchemas = [revealPluginSchema];
for (const schema of externalSchemas) {
setSchemaDefinition(schema);
}
// finally, we need to record the html descriptions from pandoc
// so we can patch them if requested.
const htmlDescriptions = await createHtmlDescriptions();
setYamlIntelligenceResources({
"schema/html-descriptions.yml": htmlDescriptions,
"schema/external-schemas.yml": externalSchemas,
"handlers/languages.yml": languages(),
"handlers/lang-comment-chars.yml": kLangCommentChars,
});
for (const language of languages()) {
let schema = await languageSchema(language);
if (schema) {
schema = idSchema(schema, `handlers/${language}`);
setYamlIntelligenceResources({
[`handlers/${language}/schema.yml`]: schema,
});
}
}
const yamlResources = exportYamlIntelligenceResources(true);
const yamlResourcesPath = resourcePath(
"/editor/tools/yaml/yaml-intelligence-resources.json",
);
Deno.writeTextFileSync(yamlResourcesPath, yamlResources);
const path = resourcePath();
await Promise.all([
generateTypesFromSchemas(path),
generateJsonSchemasFromSchemas(path),
generateZodTypesFromSchemas(path),
]);
}
function getMarkdownDescriptions() {
const result: string[] = [];
const schemaList = Object.values(getSchemaDefinitionsObject());
for (const schema of schemaList) {
walkSchema(schema, (s: Schema) => {
if (s === false || s === true) {
return;
}
const description = s?.tags
?.description as (SchemaDocumentation | undefined);
if (description === undefined) {
return;
}
result.push(`## annotation\n`);
if (typeof description === "string") {
result.push(`### string\n`);
result.push(description);
result.push("");
} else {
result.push(`### short\n`);
result.push(description?.short || "");
result.push("");
result.push(`### long\n`);
result.push(description?.long || "");
result.push("");
}
return;
});
}
return result.join("\n");
}
async function createHtmlDescriptions(): Promise<
(string | { short?: string; long?: string })[]
> {
const markdownDescriptions = getMarkdownDescriptions();
const descriptionList: (string | { short?: string; long?: string })[] = [];
// build the pandoc command (we'll feed it the input on stdin)
const cmd = [pandocBinaryPath(), "--to", "html"];
const pandocResult = await execProcess(
{ stdout: "piped", cmd: cmd[0], args: cmd.slice(1) },
markdownDescriptions,
);
if (!pandocResult.success) {
throw new InternalError("Couldn't run pandoc");
}
await initDenoDom();
const doc = await parseHtml(pandocResult.stdout!);
for (const entryNode of doc.querySelectorAll("h2")) {
const entry = entryNode as Element;
const values: Record<string, string> = {};
for (
const value of filter(
stopWhen(
siblings(entry),
(e) => e.tagName === "H2",
),
(e) => e.tagName === "H3",
)
) {
const name = value.textContent;
const html: string[] = [];
for (
const p of stopWhen(siblings(value), (e) => e.tagName !== "P")
) {
html.push(p.innerHTML);
}
values[name] = html.join("\n");
}
if (values["string"]) {
descriptionList.push(values["string"]);
} else if (values["short"] || values["long"]) {
descriptionList.push({
short: values["short"],
long: values["long"],
});
}
}
return descriptionList;
}
function* siblings(entry: Element): Generator<Element> {
do {
if (entry.nextElementSibling === null) {
return;
}
entry = entry.nextElementSibling as Element;
yield entry;
} while (true);
}
function* stopWhen(
gen: Generator<Element>,
when: (v: Element) => boolean,
): Generator<Element> {
for (const v of gen) {
if (when(v)) {
break;
}
yield v;
}
}
function* filter<T>(gen: Generator<T>, f: (v: T) => boolean): Generator<T> {
for (const v of gen) {
if (f(v)) {
yield v;
}
}
}