Skip to content

Commit c530954

Browse files
Merge pull request #1 from gaurav-nelson/fix_id_errors
Fixed broken links, headings with titles, instances of \"
2 parents 42ae868 + b15fcef commit c530954

File tree

2 files changed

+164
-19
lines changed

2 files changed

+164
-19
lines changed

scripts/splitspec.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,51 @@ function splitOpenApiSpec(inputFilePath) {
3737
// Function to collect definitions from a schema reference
3838
function collectDefinitions(tag, schema, definitions) {
3939
if (schema.$ref) {
40-
const refName = schema.$ref.split('/').pop(); // Get the last part after the last '/'
41-
// Perform a case-insensitive search for the definition
42-
const matchingDefinition = Object.keys(definitions).find(defName => defName.toLowerCase() === refName.toLowerCase());
43-
if (matchingDefinition && !tagMap[tag].definitions[uppercaseFirstChar(matchingDefinition)]) {
44-
// Add the definition with the first character uppercased
45-
tagMap[tag].definitions[uppercaseFirstChar(matchingDefinition)] = definitions[matchingDefinition];
46-
// Recursively collect definitions from the referenced definition
47-
collectDefinitions(tag, definitions[matchingDefinition], definitions);
48-
}
49-
} else if (schema.type === 'object' && schema.properties) {
50-
for (const prop of Object.values(schema.properties)) {
51-
collectDefinitions(tag, prop, definitions);
52-
}
53-
} else if (schema.type === 'array' && schema.items) {
54-
collectDefinitions(tag, schema.items, definitions);
55-
}
40+
const refName = schema.$ref.split('/').pop(); // Get the last part after the last '/'
41+
// Perform a case-insensitive search for the definition
42+
const matchingDefinition = Object.keys(definitions).find(defName => defName.toLowerCase() === refName.toLowerCase());
43+
if (matchingDefinition && !tagMap[tag].definitions[uppercaseFirstChar(matchingDefinition)]) {
44+
// Add the definition with the first character uppercased
45+
tagMap[tag].definitions[uppercaseFirstChar(matchingDefinition)] = definitions[matchingDefinition];
46+
// Recursively collect definitions from the referenced definition
47+
collectDefinitions(tag, definitions[matchingDefinition], definitions);
48+
}
49+
} else if (schema.type === 'object' && schema.properties) {
50+
for (const [propName, prop] of Object.entries(schema.properties)) {
51+
collectDefinitions(tag, prop, definitions);
52+
// Handle error property
53+
if (propName === 'error' && prop.$ref) {
54+
const errorRefName = prop.$ref.split('/').pop();
55+
const matchingErrorDefinition = Object.keys(definitions).find(defName => defName.toLowerCase() === errorRefName.toLowerCase());
56+
if (matchingErrorDefinition && !tagMap[tag].definitions[uppercaseFirstChar(matchingErrorDefinition)]) {
57+
tagMap[tag].definitions[uppercaseFirstChar(matchingErrorDefinition)] = definitions[matchingErrorDefinition];
58+
collectDefinitions(tag, definitions[matchingErrorDefinition], definitions);
59+
}
60+
}
61+
}
62+
} else if (schema.type === 'array' && schema.items) {
63+
collectDefinitions(tag, schema.items, definitions);
64+
} else if (schema.allOf) {
65+
for (const subSchema of schema.allOf) {
66+
collectDefinitions(tag, subSchema, definitions);
67+
}
68+
} else if (schema.properties) {
69+
for (const prop of Object.values(schema.properties)) {
70+
collectDefinitions(tag, prop, definitions);
71+
}
72+
} else if (schema.items) {
73+
collectDefinitions(tag, schema.items, definitions);
74+
} else if (schema.oneOf) {
75+
for (const subSchema of schema.oneOf) {
76+
collectDefinitions(tag, subSchema, definitions);
77+
}
78+
} else if (schema.anyOf) {
79+
for (const subSchema of schema.anyOf) {
80+
collectDefinitions(tag, subSchema, definitions);
81+
}
82+
} else if (schema.additionalProperties) {
83+
collectDefinitions(tag, schema.additionalProperties, definitions);
84+
}
5685
}
5786

5887
// Function to collect definitions from various parts of the OpenAPI spec

scripts/updateasciidoc.js

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@ const fs = require("fs");
22
const path = require("path");
33
const entities = require("entities");
44

5+
const specificReplacements = {
6+
"<<string>>": "`string`",
7+
"<<String>>": "`String`",
8+
"<<Object>>": "`Object`",
9+
"<<object>>": "`object`",
10+
"<<integer>>": "`integer`",
11+
"<<Integer>>": "`Integer`",
12+
};
13+
514
// Function to capitalize the first letter of a string
615
const capitalizeFirstLetter = (string) => {
716
return string.charAt(0).toUpperCase() + string.slice(1);
817
};
918

19+
// Function to process special case
20+
function processSpecialCase(line) {
21+
const match = line.match(/(.*?)(_.*?_)(.*)/);
22+
if (match) {
23+
const [_, part1, part2, part3] = match;
24+
return `${part1}${part2}\n${part3}`;
25+
}
26+
return line;
27+
}
28+
1029
// Function to update the AsciiDoc file
1130
const updateAsciiDoc = (filePath) => {
31+
const context = path.basename(fileName, path.extname(fileName));
1232
// Read the file
1333
fs.readFile(filePath, "utf8", (err, data) => {
1434
if (err) {
@@ -43,11 +63,16 @@ const updateAsciiDoc = (filePath) => {
4363

4464
// Stop processing when we encounter '[#models]'
4565
if (line.startsWith("[#models]")) {
46-
updatedLines.push('[id="common-object-reference"]');
66+
updatedLines.push('[id="common-object-reference_{context}"]');
4767
updatedLines.push("== Common object reference");
4868
updatedLines.push("");
4969
for (let j = i + 2; j < lines.length; j++) {
50-
const decodedLine = entities.decodeHTML(lines[j]);
70+
let decodedLine = entities.decodeHTML(lines[j]);
71+
72+
//replace \" with "
73+
if (decodedLine.includes('"')) {
74+
decodedLine = decodedLine.replace(/\"/g, '"');
75+
}
5176

5277
// Check if the current line is "JSON" and skip it
5378
if (decodedLine.trim() === "JSON") {
@@ -60,6 +85,49 @@ const updateAsciiDoc = (filePath) => {
6085
continue; // Skip adding the original "====" line
6186
}
6287

88+
if (decodedLine.startsWith("[#")) {
89+
const updatedLine = decodedLine.replace(
90+
/\[(#)(.*)\]/g,
91+
(match, p1, p2) => {
92+
return `[id="${p2}_{context}"]`;
93+
}
94+
);
95+
updatedLines.push(updatedLine);
96+
continue;
97+
}
98+
99+
// New case for updated lines starting with '=' and containing '_<some-text>_'
100+
if (decodedLine.startsWith("=") && /_[^_]+_/.test(decodedLine)) {
101+
decodedLine = processSpecialCase(decodedLine);
102+
updatedLines.push(decodedLine);
103+
continue;
104+
}
105+
106+
if (decodedLine.includes("<<") && decodedLine.includes(">>")) {
107+
// Check for the specific edge case
108+
if (decodedLine.includes("<<Stream_result_of_")) {
109+
decodedLine = decodedLine.replace(
110+
/<<Stream_result_of_(.*?)>>/g,
111+
(match, p1) => {
112+
// Capitalize the first letter of p1
113+
const capitalizedP1 =
114+
p1.charAt(0).toUpperCase() + p1.slice(1);
115+
return `<<StreamResultOf${capitalizedP1}_{context}, Stream_result_of_${p1}>>`;
116+
}
117+
);
118+
} else {
119+
for (const [key, value] of Object.entries(specificReplacements)) {
120+
decodedLine = decodedLine.replace(new RegExp(key, "g"), value);
121+
}
122+
123+
decodedLine = decodedLine.replace(/<<([^>]*)>>/g, (match, p1) => {
124+
return `<<${p1}_{context}, ${p1}>>`;
125+
});
126+
}
127+
128+
updatedLines.push(decodedLine);
129+
continue;
130+
}
63131
updatedLines.push(decodedLine);
64132
}
65133
break;
@@ -69,14 +137,17 @@ const updateAsciiDoc = (filePath) => {
69137
if (updatedLines.length === 0) {
70138
updatedLines.push("// Auto-generated by scripts. Do not edit.");
71139
updatedLines.push(":_mod-docs-content-type: ASSEMBLY");
140+
updatedLines.push(`:context: ${context}`);
72141
updatedLines.push("");
73142
nextHeadingIsMainHeading = true;
74143
}
75144

76145
// Capitalize the first letter of lines starting with '[.'
77146
if (line.startsWith("[.")) {
78147
const updatedLine = line.slice(2, -1).trim(); // Remove '[.' and trim spaces, then remove last character
79-
updatedLines.push(`[id="${capitalizeFirstLetter(updatedLine)}"]`); // Format as [id="..."]
148+
updatedLines.push(
149+
`[id="${capitalizeFirstLetter(updatedLine)}_{context}"]`
150+
);
80151
continue;
81152
}
82153

@@ -104,6 +175,11 @@ const updateAsciiDoc = (filePath) => {
104175
updatedLine = newLine;
105176
}
106177

178+
// New case for updated lines starting with '=' and containing '_<some-text>_'
179+
if (updatedLine.startsWith("=") && /_<[^_]+>_/.test(updatedLine)) {
180+
updatedLine = processSpecialCase(updatedLine);
181+
}
182+
107183
updatedLines.push(updatedLine); // Add the updated line
108184

109185
if (nextHeadingIsMainHeading) {
@@ -119,11 +195,51 @@ const updateAsciiDoc = (filePath) => {
119195
} else if (equalCount === 2) {
120196
// If there are exactly 2 '=', just trim the line
121197
let updatedLine = line.slice(2).trim();
198+
// New case for updated lines starting with '=' and containing '_<some-text>_'
199+
if (updatedLine.startsWith("=") && /_<[^_]+>_/.test(line)) {
200+
updatedLine = processSpecialCase(updatedLine);
201+
}
122202
updatedLines.push(updatedLine); // Add the updated line
123203
continue;
124204
}
125205
}
126206

207+
// Check if line contains both <<some_text>> and add context to it <<some_text_{context}>>
208+
if (line.includes("<<") && line.includes(">>")) {
209+
let updatedLine = line;
210+
211+
for (const [key, value] of Object.entries(specificReplacements)) {
212+
updatedLine = updatedLine.replace(new RegExp(key, "g"), value);
213+
}
214+
215+
// Check for the specific edge case
216+
if (updatedLine.includes("<<Stream_result_of_")) {
217+
updatedLine = updatedLine.replace(
218+
/<<Stream_result_of_(.*?)>>/g,
219+
(match, p1) => {
220+
// Capitalize the first letter of p1
221+
const capitalizedP1 = p1.charAt(0).toUpperCase() + p1.slice(1);
222+
return `<<StreamResultOf${capitalizedP1}_{context}, Stream_result_of_${p1}>>`;
223+
}
224+
);
225+
} else {
226+
updatedLine = updatedLine.replace(/<<([^>]*)>>/g, (match, p1) => {
227+
return `<<${p1}_{context}, ${p1}>>`;
228+
});
229+
}
230+
updatedLines.push(updatedLine);
231+
continue;
232+
}
233+
234+
// Check if line matches [#<some-text>] and change it to [id="<some-text>_{context}"]
235+
if (/$$#([^]]+)$$/.test(line)) {
236+
const updatedLine = line.replace(/$$#([^]]+)$$/g, (match, p1) => {
237+
return `[id="${p1}_{context}"]`;
238+
});
239+
updatedLines.push(updatedLine);
240+
continue;
241+
}
242+
127243
// Push other lines unchanged
128244
updatedLines.push(line);
129245
}

0 commit comments

Comments
 (0)