-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (124 loc) · 4.65 KB
/
index.js
File metadata and controls
142 lines (124 loc) · 4.65 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
const fs = require("fs");
const Handlebars = require("handlebars");
const path = require("path");
const { getMethod, getParameters, getResponses } = require("./helpers");
// Read and compile the template
const templateSource = fs.readFileSync(path.join(__dirname, "_layouts", "dynamic_template.hbs"), "utf8");
Handlebars.registerHelper("renderProperties", function (properties, options) {
if (!properties || typeof properties !== "object") {
return ""; // Early exit if not an object
}
const output = [];
for (const key in properties) {
const property = properties[key];
const { required, type } = property;
const escapedKey = Handlebars.Utils.escapeExpression(key);
const escapedType = Handlebars.Utils.escapeExpression(property.type);
const maxLength = property?.maxLength;
const maxLengthStr = maxLength ? `(${maxLength})` : "";
const requiredFlag = required ? '<span class="required">*</span>' : "";
switch (type) {
case "object":
if (property?.properties) {
const nestedProperties = Handlebars.helpers.renderProperties(property.properties, options);
output.push(`
<details>
<summary>
<span class="title">${escapedKey}${requiredFlag}</span>
<span class="type">${escapedType}${maxLengthStr}</span>
</summary>
<div class="nested">${nestedProperties}</div>
</details>
`);
}
break;
case "array":
if (property?.items?.properties) {
const maxItems = property?.maxItems;
const maxItemsStr = maxItems ? `(${maxItems})` : "";
const nestedProperties = Handlebars.helpers.renderProperties(property.items.properties, options);
output.push(`
<details>
<summary>
<span class="title">${escapedKey}${requiredFlag}</span>
<span class="type">${escapedType}${maxItemsStr}</span>
</summary>
<div class="nested">${nestedProperties}</div>
</details>
`);
}
break;
default:
output.push(`
<div>
<p class="type">
<span class="title">${escapedKey}${requiredFlag}</span>${escapedType}${maxLengthStr}
</p>
</div>
`);
break;
}
}
return new Handlebars.SafeString(output.join(""));
});
const template = Handlebars.compile(templateSource);
module.exports = {
book: {
assets: "./assets",
js: ["js/formHandler.js"],
css: ["style.css"],
},
hooks: {
init: function () {},
"page:before": async function (page) {
const markerStart = "<!-- API_START -->";
const markerEnd = "<!-- API_END -->";
let content = page.content;
while (content.includes(markerStart)) {
const start = content.indexOf(markerStart);
const end = content.indexOf(markerEnd, start);
if (end === -1) {
return page;
}
const beforeMarker = content.substring(0, start);
const afterMarker = content.substring(end + markerEnd.length);
const markerContent = content.substring(start + markerStart.length, end);
let data;
try {
data = JSON.parse(markerContent.trim());
} catch (error) {
console.error("Error parsing JSON for dynamic template:", error);
return page;
}
// Get all keys of the 'paths' object
const keys = Object.keys(data.paths);
// Get the first key from the keys array
const firstKey = keys[0];
const parameters = getParameters(data, firstKey);
const responses = getResponses(data, firstKey);
const { name = "", required, type, maxLength, schema = "" } = parameters || {};
const { info, schemes, host, basePath, paths } = data;
const { description, title } = info;
const endpointPath = Object.keys(paths ?? {})[0];
const paramName = `${parameters?.name}`;
const dynamicContent = template({
apiTitle: title,
method: getMethod(data, firstKey),
baseUrl: schemes[0] + "://" + host + basePath,
path: endpointPath === "/" ? "" : `${endpointPath.replace("/", "")}`,
description: description,
paramName: paramName,
required: required ? "*" : "",
type: type,
paramDescription: maxLength ? `max. length: ${maxLength}` : "--",
requestBody: schema,
responses: data.responses ?? responses,
});
content = beforeMarker + dynamicContent + afterMarker;
}
page.content = content;
return page;
},
},
filters: {},
};