Skip to content

Commit 34e6d29

Browse files
committed
WIP: draft TODO. Simplify code.
1 parent ecc56ab commit 34e6d29

File tree

5 files changed

+68
-73
lines changed

5 files changed

+68
-73
lines changed

api-docs/scripts/generate-openapi-articles.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,42 @@ const execCommand = (command) => {
1818
};
1919

2020
// Function to generate data from OpenAPI
21-
function generateDataFromOpenAPI(specFile, dataOutPath, articleOutPath) {
22-
if (!fs.existsSync(dataOutPath)) {
23-
fs.mkdirSync(dataOutPath, { recursive: true });
21+
function generateApiData(specFile, dataPath) {
22+
if (!fs.existsSync(dataPath)) {
23+
fs.mkdirSync(dataPath, { recursive: true });
2424
}
2525

2626
openapiDocs.openapiToData({
27-
dataOutPath,
28-
articleOutPath,
29-
specFile
27+
specFile,
28+
dataPath
3029
});
3130
};
3231

3332
// Example usage of generateDataFromOpenAPI function
3433
// generateDataFromOpenAPI('path/to/openapi-file.yml', 'path/to/paths-output-folder', 'path/to/metadata-output-folder');
3534

36-
const generatePagesFromArticleData = async (articlesPath, contentPath) => {
35+
const generateApiPages = async (dataPath, contentPath) => {
3736
let config = {
3837
root: '.', //Root hugo folder, can be empty
39-
dataFolder: articlesPath, //Data folder path (will fetch ALL files from here)
38+
dataFolder: dataPath, //Data folder path (will fetch ALL files from here)
4039
type: "api", //Type name [basically layout] (save it under "layouts/NAME/single.html" or themes/THEME/layouts/NAME/single.html). Can be overridden on individual pages by defining "type" under "fields"
41-
pages: "articles", //Pages element in your data, in case it's "posts" or "articles" etc.
40+
pages: "paths", //Pages element in your data, in case it's "posts" or "articles" etc.
4241
contentPath: contentPath, //Path to content directory (in case it's not "content")
4342
hugoPath: `${DOCS_ROOT}/node_modules/.bin/hugo-extended` //Path to hugo binary (if global, e.g. /snap/bin/hugo)
4443
}
4544
configJson = `'${JSON.stringify(config)}'`;
46-
console.log('Clean...')
47-
execCommand(
48-
`node ./node_modules/hugo-data-to-pages/hugo.js clean --force --config=${configJson}`
49-
)
45+
// console.log('Clean...')
46+
// execCommand(
47+
// `node ./node_modules/hugo-data-to-pages/hugo.js clean --force --config=${configJson}`
48+
// )
5049
console.log('Generate...')
5150
execCommand(
5251
`node ./node_modules/hugo-data-to-pages/hugo.js generate --config=${configJson}`
5352
)
5453
}
5554

56-
// To output to a public folder where JavaScript can access it, use /static/<namespace>--for example: /static/openapi/
5755
const config = {
58-
dataOutPath: path.join(DOCS_ROOT, '/data/api/influxdb'),
59-
metadataOutPath: path.join(DOCS_ROOT, `/data/api-metadata/influxdb`),
56+
dataDir: path.join(DOCS_ROOT, '/data/api/influxdb'),
6057
apis: [
6158
{
6259
name: 'cloud-v2',
@@ -74,12 +71,19 @@ const config = {
7471
config.apis.forEach(api => {
7572
// Execute the getswagger.sh script
7673
execCommand(`${path.join(DOCS_ROOT, '/api-docs/getswagger.sh')} ${api.name} -B`);
77-
const dataOut = path.join(config.dataOutPath, api.name);
78-
const metadataOut = path.join(config.metadataOutPath, api.name);
79-
if (!fs.existsSync(dataOut)) {
80-
fs.mkdirSync(dataOut, { recursive: true });
74+
// Copy the generated spec to /data
75+
const specDataDir = path.join(config.dataDir, api.name);
76+
if (!fs.existsSync(specDataDir)) {
77+
fs.mkdirSync(specDataDir, { recursive: true });
8178
}
82-
fs.copyFileSync(api.spec_file, path.join(dataOut, api.name + '.yml'));
83-
generateDataFromOpenAPI(api.spec_file, dataOut, metadataOut);
84-
generatePagesFromArticleData(metadataOut, api.pages_dir);
79+
const specDataFile = path.join(specDataDir, api.name + '.yml');
80+
fs.copyFileSync(api.spec_file, specDataFile);
81+
const apiPathsDir = path.join(specDataDir, '/paths');
82+
generateApiData(api.spec_file, apiPathsDir);
83+
84+
// Remove old pages
85+
fs.rmSync(api.pages_dir, {recursive: true, force: true});
86+
// generateDataFromOpenAPI(api.spec_file, dataOut, metadataOut);
87+
fs.mkdirSync(api.pages_dir, { recursive: true });
88+
generateApiPages(apiPathsDir, api.pages_dir);
8589
});

api-docs/scripts/openapi-docs/index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const openapiUtils = {
1818
}
1919
}
2020

21-
function openapiPaths(openapi, prefix, outPath) {
21+
function openapiPaths(openapi, outPath) {
2222
const pathGroups = {};
2323
Object.keys(openapi.paths).sort()
2424
.forEach((p) => {
@@ -49,7 +49,7 @@ function openapiPaths(openapi, prefix, outPath) {
4949
if (!fs.existsSync(outPath)) {
5050
fs.mkdirSync(outPath, {recursive: true});
5151
}
52-
const realOutPath = path.resolve(outPath, `${prefix}${pg.replaceAll('/', '-').replace(/^-/, '')}.yaml`);
52+
const realOutPath = path.resolve(outPath, `${pg.replaceAll('/', '-').replace(/^-/, '')}.yaml`);
5353
writeDataFile(doc, realOutPath);
5454
} catch (err) {
5555
console.error(err);
@@ -117,14 +117,10 @@ function openapiMetadata(sourcePath, targetPath, opts) {
117117
}
118118

119119
function openapiToData(options) {
120-
const filenamePrefix = `${path.parse(options.specFile).name}-`;
121-
122120
const sourceFile = readFile(options.specFile, 'utf8');
123-
console.log(`Generating OpenAPI path files in ${options.dataOutPath}....`);
124-
openapiPaths(sourceFile, filenamePrefix, options.dataOutPath);
125-
126-
console.log(`Generating OpenAPI article data in ${options.articleOutPath}...`);
127-
openapiMetadata(options.dataOutPath, options.articleOutPath, {filePattern: filenamePrefix});
121+
console.log(`Generating OpenAPI path files in ${options.dataPath}....`);
122+
openapiPaths(sourceFile, options.dataPath);
123+
//openapiMetadata(options.dataPath);
128124
}
129125

130126
module.exports = {

content/api-ref-pipeline-notes.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# API reference pipeline with Hugo
2+
3+
- Hugo can render the following content formats as page content:
4+
Markdown, HTML, Emacs Org Mode, AsciiDoc, Pandoc, or reStructuredText.
5+
6+
- Front matter at the top of each content file is metadata that:
7+
8+
- Describes the content
9+
- Augments the content
10+
- Establishes relationships with other content
11+
- Controls the published structure of your site
12+
- Determines template selection
13+
14+
1. Create a content type (folder) or assign a `type` property for API reference (OpenAPI) paths. For example, if the page structure is `content/influxdb/v2/api/v2/[OpenAPI path]`, then add `type: api_path` to the frontmatter and, if necessary, specify a layout: `layout: api_path`.
15+
1. Store each product's OpenAPI spec files in YAML format inside a "namespaced" (to avoid collisions) directory structure in `/data`.
16+
1. For each product.path, generate a page with all path metadata in the frontmatter. See [`.Site.Data`](https://gohugo.io/methods/site/data/)
17+
2. Create a template to render the path
18+
3. Create additional templates that process page data for nav, filtering, links, code samples, etc.
19+
20+
21+
22+
## Useful functions
23+
24+
- [`data.GetJSON`](https://gohugo.io/functions/data/getjson/): Returns a JSON object from a local or remote JSON file, or an error if the file does not exist.
25+
- [`resources.GetRemote URL`](https://gohugo.io/functions/resources/getremote/): fetches and caches remote resources (images, js, etc.)

layouts/_default/api.html

Lines changed: 0 additions & 40 deletions
This file was deleted.

layouts/api/single.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
{{ partial "header.html" . }}
44
{{ partial "topnav.html" . }}
55

6+
{{ range $category, $api := .Site.Data.api.influxdb.v2_cloud.cloud_v2 }}
7+
<p>{{ $category | title }}</p>
8+
<ul>
9+
{{ range $api }}
10+
<li>{{ .info.title }}<br /> ({{ .info.description }})</li>
11+
{{ end }}
12+
</ul>
13+
{{ end }}
14+
15+
<!--Rapidoc demo
616
<div class="page-wrapper">
717
{{ partial "sidebar.html" . }}
818
<div class="content-wrapper">
@@ -35,5 +45,5 @@ <h2>{{ .Params.title }}</h2>
3545
<div class="copyright">© {{ now.Year }} InfluxData, Inc.</div>
3646
</div>
3747
</div>
38-
48+
-->
3949
{{ partial "footer.html" . }}

0 commit comments

Comments
 (0)