@graphql-markdown/docusaurus 1.14.0 → 1.15.1

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.
@@ -1,150 +0,0 @@
1
- const path = require("path");
2
-
3
- const { convertArrayToObject } = require("../utils/scalars/array");
4
- const { hasProperty } = require("../utils/scalars/object");
5
- const { toSlug, startCase } = require("../utils/scalars/string");
6
- const { pathUrl } = require("../utils/scalars/url");
7
- const {
8
- hasPrettierModule,
9
- prettifyJavascript,
10
- prettifyMarkdown,
11
- } = require("../utils/helpers/prettier");
12
- const {
13
- saveFile,
14
- ensureDir,
15
- copyFile,
16
- readFile,
17
- fileExists,
18
- } = require("../utils/helpers/fs");
19
-
20
- const { ASSETS_LOCATION } = require("../config");
21
- const { schemaSidebar } = require(`${ASSETS_LOCATION}/sidebar.json`);
22
-
23
- const SIDEBAR = "sidebar-schema.js";
24
- const HOMEPAGE_ID = "schema";
25
- const CATEGORY_YAML = "_category_.yml";
26
-
27
- module.exports = class Renderer {
28
- constructor(printer, outputDir, baseURL, group, prettify, docOptions) {
29
- this.group = group;
30
- this.outputDir = outputDir;
31
- this.baseURL = baseURL;
32
- this.printer = printer;
33
- this.prettify = prettify && hasPrettierModule();
34
- this.options = docOptions;
35
- }
36
-
37
- async generateCategoryMetafile(category, dirPath) {
38
- const filePath = path.join(dirPath, CATEGORY_YAML);
39
-
40
- if (await fileExists(filePath)) {
41
- return;
42
- }
43
-
44
- await ensureDir(dirPath);
45
-
46
- const label = startCase(category);
47
- const link =
48
- typeof this.options === "undefined" || !this.options.index
49
- ? "null"
50
- : `\n type: generated-index\n title: '${label} overview'\n`;
51
-
52
- await saveFile(filePath, `label: ${label}\nlink: ${link}\n`);
53
- }
54
-
55
- async renderRootTypes(rootTypeName, type) {
56
- if (typeof type === "undefined" || type === null) {
57
- return undefined;
58
- }
59
-
60
- if (Array.isArray(type)) {
61
- type = convertArrayToObject(type);
62
- }
63
-
64
- return Promise.all(
65
- Object.keys(type).map(async (name) => {
66
- let dirPath = this.outputDir;
67
-
68
- if (hasProperty(this.group, name)) {
69
- dirPath = path.join(dirPath, toSlug(this.group[name]));
70
- await this.generateCategoryMetafile(this.group[name], dirPath);
71
- }
72
-
73
- dirPath = path.join(dirPath, toSlug(rootTypeName));
74
- await this.generateCategoryMetafile(rootTypeName, dirPath);
75
-
76
- return this.renderTypeEntities(dirPath, name, type[name]);
77
- }),
78
- );
79
- }
80
-
81
- async renderTypeEntities(dirPath, name, type) {
82
- if (typeof type === "undefined" || type === null) {
83
- return undefined;
84
- }
85
-
86
- const fileName = toSlug(name);
87
- const filePath = path.join(path.normalize(dirPath), `${fileName}.mdx`);
88
-
89
- const content = this.printer.printType(fileName, type, this.options);
90
- await saveFile(
91
- filePath,
92
- this.prettify ? prettifyMarkdown(content) : content,
93
- );
94
-
95
- const pagePath = path.relative(this.outputDir, filePath);
96
- const page = pagePath.match(
97
- /(?<category>[A-Za-z0-9-]+)[\\/]+(?<pageId>[A-Za-z0-9-]+).mdx?$/,
98
- );
99
- const slug = pathUrl.join(page.groups.category, page.groups.pageId);
100
-
101
- return { category: startCase(page.groups.category), slug: slug };
102
- }
103
-
104
- async renderSidebar() {
105
- const sidebar = {
106
- schemaSidebar: schemaSidebar.map((entry) => {
107
- switch (entry.type) {
108
- case "doc":
109
- entry.id = pathUrl.join(this.baseURL, HOMEPAGE_ID);
110
- break;
111
- case "autogenerated":
112
- entry.dirName = this.baseURL;
113
- break;
114
- default: //do nothing
115
- }
116
- return entry;
117
- }),
118
- };
119
-
120
- const jsonSidebar = `
121
- /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
122
-
123
- module.exports = ${JSON.stringify(sidebar, null, 2)};
124
- `;
125
-
126
- const filePath = path.join(this.outputDir, SIDEBAR);
127
- await saveFile(
128
- filePath,
129
- this.prettify ? prettifyJavascript(jsonSidebar) : jsonSidebar,
130
- );
131
-
132
- return path.relative("./", filePath);
133
- }
134
-
135
- async renderHomepage(homepageLocation) {
136
- const homePage = path.basename(homepageLocation);
137
- const destLocation = path.join(this.outputDir, homePage);
138
- const slug = pathUrl.resolve("/", this.baseURL);
139
-
140
- await copyFile(homepageLocation, destLocation);
141
-
142
- const template = await readFile(destLocation);
143
-
144
- const data = template
145
- .toString()
146
- .replace(/##baseURL##/gm, slug)
147
- .replace(/##generated-date-time##/gm, new Date().toLocaleString());
148
- await saveFile(destLocation, data);
149
- }
150
- };
@@ -1,33 +0,0 @@
1
- const { promises: fs } = require("fs");
2
- const { dirname } = require("path");
3
-
4
- const readFile = fs.readFile;
5
- const copyFile = fs.copyFile;
6
-
7
- async function ensureDir(dirPath) {
8
- if (!(await fileExists(dirPath))) {
9
- await fs.mkdir(dirPath, { recursive: true });
10
- }
11
- }
12
-
13
- async function fileExists(filePath) {
14
- try {
15
- await fs.stat(filePath);
16
- return true;
17
- } catch (error) {
18
- return false;
19
- }
20
- }
21
-
22
- async function saveFile(filePath, data) {
23
- await ensureDir(dirname(filePath));
24
- await fs.writeFile(filePath, data);
25
- }
26
-
27
- module.exports = {
28
- copyFile,
29
- ensureDir,
30
- fileExists,
31
- readFile,
32
- saveFile,
33
- };
@@ -1,29 +0,0 @@
1
- /* istanbul ignore file */
2
- let prettier;
3
-
4
- try {
5
- prettier = require("prettier");
6
- } catch (e) {
7
- console.debug("Prettier is not found");
8
- }
9
-
10
- function hasPrettierModule() {
11
- return typeof prettier !== "undefined";
12
- }
13
-
14
- function prettify(content, parser) {
15
- if (!hasPrettierModule()) {
16
- return content;
17
- }
18
- return prettier.format(content, { parser });
19
- }
20
-
21
- function prettifyMarkdown(content) {
22
- return prettify(content, "markdown");
23
- }
24
-
25
- function prettifyJavascript(content) {
26
- return prettify(content, "babel");
27
- }
28
-
29
- module.exports = { hasPrettierModule, prettifyMarkdown, prettifyJavascript };
@@ -1,18 +0,0 @@
1
- /**
2
- * Array functions
3
- */
4
-
5
- function toArray(param) {
6
- if (param && typeof param === "object")
7
- return Object.keys(param).map((key) => param[key]);
8
- return undefined;
9
- }
10
-
11
- function convertArrayToObject(typeArray) {
12
- return typeArray.reduce(function (r, o) {
13
- if (o && o.name) r[o.name] = o;
14
- return r;
15
- }, {});
16
- }
17
-
18
- module.exports = { toArray, convertArrayToObject };
@@ -1,16 +0,0 @@
1
- /**
2
- * Object functions
3
- */
4
-
5
- function hasProperty(obj, prop) {
6
- return (
7
- !!(obj && obj[prop]) ||
8
- (obj instanceof Object && Object.prototype.hasOwnProperty.call(obj, prop))
9
- );
10
- }
11
-
12
- function hasMethod(obj, prop) {
13
- return hasProperty(obj, prop) && typeof obj[prop] === "function";
14
- }
15
-
16
- module.exports = { hasMethod, hasProperty };
@@ -1,86 +0,0 @@
1
- /**
2
- * String functions
3
- */
4
-
5
- function stringCaseBuilder(str, transformation, separator) {
6
- const hasTransformation = typeof transformation === "function";
7
- const stringCase = replaceDiacritics(str)
8
- .replace(/([a-z]+|\d+)([A-Z])/g, "$1 $2")
9
- .replace(/([a-z]+)(\d)/g, "$1 $2")
10
- .replace(/(\d+)([a-z])/g, "$1 $2")
11
- .split(/[^0-9A-Za-z]+/g)
12
- .map((word) => (hasTransformation ? transformation(word) : word))
13
- .join(separator);
14
- return prune(stringCase, separator);
15
- }
16
-
17
- function prune(str, char) {
18
- let res = str;
19
-
20
- if (res[0] === char) {
21
- // Remove first character
22
- res = res.slice(1);
23
- }
24
-
25
- if (res[res.length - 1] === char) {
26
- // Remove last character
27
- res = res.slice(0, -1);
28
- }
29
-
30
- return res;
31
- }
32
-
33
- function toSlug(str) {
34
- return kebabCase(str);
35
- }
36
-
37
- function toHTMLUnicode(char) {
38
- const unicodeChar = char.charCodeAt(0).toString(16).padStart(4, "0");
39
- return `&#x${unicodeChar.toUpperCase()};`;
40
- }
41
-
42
- function escapeMDX(str) {
43
- if (typeof str === "string") {
44
- return str.replace(/[<>{}]/g, toHTMLUnicode);
45
- }
46
- return str;
47
- }
48
-
49
- function firstUppercase(word) {
50
- const sliceUppercase = word.slice(0, 1).toUpperCase();
51
- const sliceDefaultCase = word.slice(1);
52
- return `${sliceUppercase}${sliceDefaultCase}`;
53
- }
54
-
55
- function capitalize(word) {
56
- return firstUppercase(word.toLowerCase());
57
- }
58
-
59
- // from https://stackoverflow.com/a/37511463
60
- function replaceDiacritics(str) {
61
- return str
62
- .toString()
63
- .normalize("NFD")
64
- .replace(/[\u0300-\u036f]/g, "");
65
- }
66
-
67
- function startCase(str) {
68
- return stringCaseBuilder(str, firstUppercase, " ");
69
- }
70
-
71
- function kebabCase(str) {
72
- return stringCaseBuilder(str, (word) => word.toLowerCase(), "-");
73
- }
74
-
75
- module.exports = {
76
- capitalize,
77
- escapeMDX,
78
- firstUppercase,
79
- kebabCase,
80
- prune,
81
- replaceDiacritics,
82
- startCase,
83
- stringCaseBuilder,
84
- toHTMLUnicode,
85
- toSlug,
86
- };
@@ -1,8 +0,0 @@
1
- /* istanbul ignore file */
2
- /**
3
- * Path functions
4
- */
5
-
6
- const pathUrl = require("path").posix;
7
-
8
- module.exports = { pathUrl };