UNPKG

vitepress-jsdoc

Version:

A bridge between Vitepress and JSDoc-style commented codebases for hassle-free documentation.

100 lines 3.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsDocParser = void 0; const node_path_1 = require("node:path"); const jsdoc_to_markdown_1 = __importDefault(require("jsdoc-to-markdown")); const file_reader_js_1 = require("../utilities/file-reader.js"); const file_path_js_1 = require("../utilities/file-path.js"); /** * The JsDocParser class provides functionality to parse files * and generate markdown content based on JSDoc comments. * * @implements {Parser} */ class JsDocParser { /** * Parses the provided file and returns the generated markdown content. * * @param {DirectoryFile} file - The file to be parsed. * @param {ParserConfig} config - The configuration for parsing. * @returns {Promise<ParseReturn | undefined>} - The parsed content or undefined. */ async parse(file, config) { const fileContent = await this.getFileContent(file); const markdownContent = await this.getMarkdownContent(file, config); const paths = this.getPaths(file, config); return { success: Boolean(markdownContent), file, empty: !markdownContent, content: fileContent + markdownContent, ...paths, }; } /** * Retrieves the content of the provided file. * * @private * @param {DirectoryFile} file - The file whose content is to be retrieved. * @returns {Promise<string>} - The content of the file. */ async getFileContent(file) { return (0, file_reader_js_1.readFileContent)(file); } /** * Generates markdown content based on JSDoc comments in the provided file. * * @private * @param {DirectoryFile} file - The file to be parsed for JSDoc comments. * @param {ParserConfig} config - The configuration for parsing. * @returns {Promise<string>} - The generated markdown content. */ async getMarkdownContent(file, config) { const relativePathSrc = (0, file_path_js_1.getFileFolder)(file); const { partialsPath, helpersPath } = this.getHandlebarsPaths(config); return jsdoc_to_markdown_1.default.render({ 'no-cache': Boolean(config.jsDocConfigPath), files: [ (0, node_path_1.join)(process.cwd(), relativePathSrc, (0, file_path_js_1.getFileName)(file) + file.ext), ], configure: config.jsDocConfigPath, partial: partialsPath, helper: helpersPath, }); } /** * Resolves the paths to handlebars partials and helpers. * * @private * @param {ParserConfig} config - The configuration containing paths. * @returns {Object} - An object containing paths to partials and helpers. */ getHandlebarsPaths(config) { return { partialsPath: config.partials && config.partials.length > 0 ? config.partials : [], helpersPath: config.helpers && config.helpers.length > 0 ? config.helpers : [], }; } /** * Computes the paths for the provided file based on the configuration. * * @private * @param {DirectoryFile} file - The file for which paths are to be computed. * @param {ParserConfig} config - The configuration for path computation. * @returns {Object} - An object containing the computed paths. */ getPaths(file, config) { const { relativePathDest, folderInDest } = (0, file_path_js_1.computePaths)(file, config); const relativePathSrc = (0, file_path_js_1.getFileFolder)(file); return { relativePathDest, relativePathSrc, dest: folderInDest, }; } } exports.JsDocParser = JsDocParser; //# sourceMappingURL=jsdoc.js.map