UNPKG

vitepress-jsdoc

Version:

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

96 lines 3.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseVitepressFileHeader = exports.parseComment = void 0; /* * @vitepress * --- * headline: Parse Vitepress Comment * --- */ const front_matter_1 = __importDefault(require("front-matter")); /** * Parses the content of a file to search for a @vitepress comment block and extracts the frontmatter data. * * @function * @param {string} fileContent - The content of the file to be parsed. * @returns {object} - An object containing the extracted frontmatter data and attributes. * @throws {Error} - Returns an object with null values if parsing fails. */ const parseComment = (fileContent) => { try { const allCommentBlocks = fileContent.match(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/g); const vitepressBlock = allCommentBlocks?.filter((block) => { return block.split('\n').filter((line) => line.includes('@vitepress')) .length; })[0]; if (!vitepressBlock) { return { body: '', bodyBegin: 0, attributes: {}, }; } // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call const parsed = front_matter_1.default(vitepressBlock .replaceAll('\n ', '\n') .replace('/*', '') .replace('*/', '') .replaceAll('@vitepress', '') .replaceAll(/\*\s?/g, '') .trim()); return parsed; /* The code `return fm<VitepressAttributes>(...)` is calling the `fm` function from the `front-matter` library to parse the frontmatter data from the `vitepressBlock`. */ // return fm<VitepressAttributes>( // vitepressBlock // .replaceAll('\n ', '\n') // .replace('/*', '') // .replace('*/', '') // .replaceAll('@vitepress', '') // .replaceAll(/\*\s?/g, '') // .trim(), // ); } catch { return { body: '', bodyBegin: 0, attributes: {}, }; } }; exports.parseComment = parseComment; /** * Parses the content of a file and constructs a structured markdown header based on the @vitepress comment block. * * @function * @param {string} content - The content of the file. * @param {DirectoryFile} file - The file object containing details like name and extension. * @returns {string} - A structured markdown header. */ const parseVitepressFileHeader = (content, file) => { const { frontmatter, attributes } = (0, exports.parseComment)(content); let fileContent = '---\n'; fileContent += attributes?.title ? '' : `title: ${file.name}`; if (frontmatter) { fileContent += attributes?.title ? '' : '\n'; fileContent += `${frontmatter}`; } fileContent += '\n---\n'; if (attributes?.title ?? file.ext !== '.vue') { let headline = file.name; if (attributes?.headline) { headline = attributes.headline; } else if (attributes?.title) { headline = attributes.title; } fileContent += `\n# ${headline}\n\n`; } return fileContent; }; exports.parseVitepressFileHeader = parseVitepressFileHeader; //# sourceMappingURL=comment.js.map