@graphql-markdown/core 1.6.4 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/renderer.js DELETED
@@ -1,193 +0,0 @@
1
- const path = require("path");
2
-
3
- const {
4
- object: { hasProperty },
5
- string: { toSlug, startCase },
6
- url: { pathUrl },
7
- prettier: { prettifyJavascript, prettifyMarkdown },
8
- fs: { saveFile, ensureDir, copyFile, readFile, fileExists },
9
- graphql: { isDeprecated },
10
- } = require("@graphql-markdown/utils");
11
-
12
- const logger = require("@graphql-markdown/utils").logger.getInstance();
13
-
14
- const { ASSETS_LOCATION } = require("./config");
15
- const { schemaSidebar } = require(`${ASSETS_LOCATION}/sidebar.json`);
16
-
17
- const SIDEBAR = "sidebar-schema.js";
18
- const HOMEPAGE_ID = "schema";
19
- const CATEGORY_YAML = "_category_.yml";
20
- const SIDEBAR_POSITION = {
21
- FIRST: 1,
22
- LAST: 999,
23
- };
24
-
25
- module.exports = class Renderer {
26
- constructor(printer, outputDir, baseURL, group, prettify, docOptions) {
27
- this.group = group;
28
- this.outputDir = outputDir;
29
- this.baseURL = baseURL;
30
- this.printer = printer;
31
- this.prettify = prettify;
32
- this.options = docOptions;
33
- }
34
-
35
- async generateCategoryMetafile(
36
- category,
37
- dirPath,
38
- sidebarPosition = SIDEBAR_POSITION.FIRST,
39
- styleClass = undefined,
40
- ) {
41
- const filePath = path.join(dirPath, CATEGORY_YAML);
42
-
43
- if (await fileExists(filePath)) {
44
- return;
45
- }
46
-
47
- await ensureDir(dirPath);
48
-
49
- const label = startCase(category);
50
- const link =
51
- typeof this.options === "undefined" || !this.options.index
52
- ? "null"
53
- : `\n type: generated-index\n title: '${label} overview'`;
54
- const className =
55
- typeof styleClass === "string" ? `className: ${styleClass}\n` : "";
56
- await saveFile(
57
- filePath,
58
- `label: ${label}\nposition: ${sidebarPosition}\n${className}link: ${link}\n`,
59
- );
60
- }
61
-
62
- async generateCategoryMetafileType(type, name, rootTypeName) {
63
- let dirPath = this.outputDir;
64
-
65
- if (
66
- hasProperty(this.options, "deprecated") &&
67
- this.options.deprecated === "group" &&
68
- isDeprecated(type)
69
- ) {
70
- dirPath = path.join(dirPath, toSlug("deprecated"));
71
- await this.generateCategoryMetafile(
72
- "deprecated",
73
- dirPath,
74
- SIDEBAR_POSITION.LAST,
75
- "deprecated",
76
- );
77
- }
78
-
79
- if (
80
- hasProperty(this.group, rootTypeName) &&
81
- hasProperty(this.group[rootTypeName], name)
82
- ) {
83
- dirPath = path.join(dirPath, toSlug(this.group[rootTypeName][name]));
84
- await this.generateCategoryMetafile(
85
- this.group[rootTypeName][name],
86
- dirPath,
87
- );
88
- }
89
-
90
- dirPath = path.join(dirPath, toSlug(rootTypeName));
91
- await this.generateCategoryMetafile(rootTypeName, dirPath);
92
-
93
- return dirPath;
94
- }
95
-
96
- async renderRootTypes(rootTypeName, type) {
97
- if (typeof type !== "object" || type === null) {
98
- return undefined;
99
- }
100
-
101
- return Promise.all(
102
- Object.keys(type)
103
- .map(async (name) => {
104
- const dirPath = await this.generateCategoryMetafileType(
105
- type[name],
106
- name,
107
- rootTypeName,
108
- );
109
-
110
- return this.renderTypeEntities(dirPath, name, type[name]);
111
- })
112
- .filter((res) => typeof res !== "undefined"),
113
- );
114
- }
115
-
116
- async renderTypeEntities(dirPath, name, type) {
117
- const fileName = toSlug(name);
118
- const filePath = path.join(path.normalize(dirPath), `${fileName}.mdx`);
119
-
120
- let content;
121
- try {
122
- content = this.printer.printType(fileName, type, this.options);
123
- if (typeof content === "undefined") {
124
- return undefined;
125
- }
126
- } catch (error) {
127
- logger.warn(`An error occurred while processing "${type}"`);
128
- return undefined;
129
- }
130
-
131
- await saveFile(
132
- filePath,
133
- content,
134
- this.prettify ? prettifyMarkdown : undefined,
135
- );
136
-
137
- const pagePath = path.relative(this.outputDir, filePath);
138
- const page = pagePath.match(
139
- /(?<category>[A-Za-z0-9-]+)[\\/]+(?<pageId>[A-Za-z0-9-]+).mdx?$/,
140
- );
141
- const slug = pathUrl.join(page.groups.category, page.groups.pageId);
142
-
143
- return { category: startCase(page.groups.category), slug: slug };
144
- }
145
-
146
- async renderSidebar() {
147
- const sidebar = {
148
- schemaSidebar: schemaSidebar.map((entry) => {
149
- switch (entry.type) {
150
- case "doc":
151
- entry.id = pathUrl.join(this.baseURL, HOMEPAGE_ID);
152
- break;
153
- case "autogenerated":
154
- entry.dirName = this.baseURL;
155
- break;
156
- default: //do nothing
157
- }
158
- return entry;
159
- }),
160
- };
161
-
162
- const jsonSidebar = `
163
- /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
164
-
165
- module.exports = ${JSON.stringify(sidebar, null, 2)};
166
- `;
167
-
168
- const filePath = path.join(this.outputDir, SIDEBAR);
169
- await saveFile(
170
- filePath,
171
- jsonSidebar,
172
- this.prettify ? prettifyJavascript : undefined,
173
- );
174
-
175
- return path.relative("./", filePath);
176
- }
177
-
178
- async renderHomepage(homepageLocation) {
179
- const homePage = path.basename(homepageLocation);
180
- const destLocation = path.join(this.outputDir, homePage);
181
- const slug = pathUrl.resolve("/", this.baseURL);
182
-
183
- await copyFile(homepageLocation, destLocation);
184
-
185
- const template = await readFile(destLocation);
186
-
187
- const data = template
188
- .toString()
189
- .replace(/##baseURL##/gm, slug)
190
- .replace(/##generated-date-time##/gm, new Date().toLocaleString());
191
- await saveFile(destLocation, data);
192
- }
193
- };