-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d21f5cb
commit de3abec
Showing
4 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
import { optionToMandoc, envToMandoc } from './utils/converter.mjs'; | ||
import { writeFile, readFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
/** | ||
* This generator generates a mandoc version of the API docs | ||
* | ||
* @typedef {Array<ApiDocMetadataEntry>} Input | ||
* | ||
* @type {import('../types.d.ts').GeneratorMetadata<Input, string>} | ||
*/ | ||
export default { | ||
name: 'mandoc', | ||
|
||
version: '1.0.0', | ||
|
||
description: 'Generates the `node.1` file.', | ||
|
||
dependsOn: 'ast', | ||
|
||
async generate(input, options) { | ||
// Find the appropriate headers | ||
const optionsStart = input.findIndex(({ slug }) => slug === 'options'); | ||
const environmentStart = input.findIndex( | ||
({ slug }) => slug === 'environment-variables-1' | ||
); | ||
|
||
// If the headers are not found | ||
if (optionsStart + environmentStart <= 0) { | ||
throw new Error('Could not find headers'); | ||
} | ||
|
||
// Generate the option mandoc | ||
let optionsOutput = ''; | ||
for (let i = optionsStart + 1; i < environmentStart; i++) { | ||
const el = input[i]; | ||
if (el.heading?.depth === 3) { | ||
optionsOutput += optionToMandoc(el); | ||
} | ||
} | ||
|
||
// Generate the environment mandoc | ||
let envOutput = ''; | ||
for (let i = environmentStart + 1; i < input.length; i++) { | ||
const el = input[i]; | ||
if (el.heading?.depth === 3) { | ||
envOutput += envToMandoc(el); | ||
} | ||
if (el.heading?.depth < 3) break; | ||
} | ||
|
||
const apiTemplate = await readFile( | ||
join(import.meta.dirname, 'template.1'), | ||
'utf-8' | ||
); | ||
const template = apiTemplate | ||
.replace('__OPTIONS__', optionsOutput) | ||
.replace('__ENVIRONMENT__', envOutput); | ||
|
||
return await writeFile(options.output, template); | ||
}, | ||
}; |