erest
Version:
Easy to build api server depend on @leizm/web and express.
88 lines (87 loc) • 3.99 kB
JavaScript
/**
* @file API plugin generate-markdown
* @author Yourtion Guo <yourtion@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = generateMarkdown;
const path = require("node:path");
const debug_1 = require("../../debug");
const utils = require("../../utils");
const apis_1 = require("./apis");
const errors_1 = require("./errors");
const schema_1 = require("./schema");
const types_1 = require("./types");
const utils_1 = require("./utils");
function filePath(dir, name) {
const filename = name === "Home" ? name : name.toLowerCase();
const p = path.resolve(dir, `${filename}.md`);
(0, debug_1.plugin)("filePath: %s", p);
return p;
}
function generateMarkdown(data, dir, options, writter) {
(0, debug_1.plugin)("generateMarkdown: %s - %o", dir, options);
function getGroupName(name) {
return `${data.group[name]} ( ${name} )`;
}
const typeDoc = (0, utils_1.trimSpaces)((0, types_1.default)(data));
const errorDoc = (0, utils_1.trimSpaces)((0, errors_1.default)(data));
const schemaDoc = (0, utils_1.trimSpaces)((0, schema_1.default)(data));
if (options.wiki) {
writter(filePath(dir, "types"), typeDoc);
writter(filePath(dir, "errors"), errorDoc);
writter(filePath(dir, "schema"), schemaDoc);
}
const { list, groupTitles } = (0, apis_1.default)(data);
const indexDoc = [];
indexDoc.push(`# ${data.info.title}\n`);
indexDoc.push(`${data.info.description}\n`);
indexDoc.push(`测试服务器: ${data.info.host}${data.info.basePath}\n`);
indexDoc.push(`生成时间: ${data.genTime}\n`);
// FIXME: 需要根据配置输出文件名
if (options.axios)
indexDoc.push("JS-SDK( 基于 axios ):[jssdk.js](./jssdk.js)\n");
if (options.postman)
indexDoc.push("Postman 文件:[postman.json](./postman.json)\n");
if (options.swagger)
indexDoc.push("Swagger 文件:[swagger.json](./swagger.json)\n");
if (options.json)
indexDoc.push(`JSON 描述文件:[doc.json](./doc.json)\n`);
indexDoc.push("文档列表:\n");
const allInOneDoc = indexDoc.slice(0, indexDoc.length);
const wikiDoc = indexDoc.slice(0, indexDoc.length);
const wikiPath = utils.getPath("wiki", options.wiki);
for (const [name, title] of Object.entries(data.group)) {
const group = utils.camelCase2underscore(name);
indexDoc.push(`- [${title} ( ${name} ) 相关文档](./${name.toLowerCase()}.md)`);
allInOneDoc.push(`- [${title} ( ${name} ) 相关](#${name.toLowerCase()})`);
wikiDoc.push(`- [/${group} - ${title}相关文档](${wikiPath}${name.toLowerCase()})`);
if (options.wiki && groupTitles[name]) {
wikiDoc.push(groupTitles[name].join("\n"));
}
}
if (options.index) {
writter(filePath(dir, "index"), (0, utils_1.trimSpaces)(indexDoc.join("\n")));
}
if (options.wiki) {
for (const item of list) {
const titie = `# ${getGroupName(item.name)} 相关文档\n\n`;
writter(filePath(dir, item.name), titie + (0, utils_1.trimSpaces)(item.content));
}
writter(filePath(dir, "Home"), (0, utils_1.trimSpaces)(wikiDoc.join("\n")));
}
if (options.all) {
allInOneDoc.push(`- [类型相关文档](#types)`);
allInOneDoc.push(`- [错误信息文档](#errors)`);
allInOneDoc.push("\n");
for (const item of list) {
allInOneDoc.push(`# <a id="${item.name.toLowerCase()}">${getGroupName(item.name)} 相关文档</a>\n\n`);
allInOneDoc.push(item.content);
}
allInOneDoc.push(`# <a id="types">类型相关文档</a>\n\n`);
allInOneDoc.push(typeDoc);
allInOneDoc.push(`# <a id="errors">错误信息文档</a>\n\n`);
allInOneDoc.push(errorDoc);
writter(filePath(dir, `API文档-${data.info.title}`), (0, utils_1.trimSpaces)(allInOneDoc.join("\n")));
}
}
;