vuepress-jsdoc 4.3.1 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,58 @@
1
+ import { StatisticType } from './constants.js';
1
2
  export interface DirectoryFile {
3
+ /** Path of the file relative to the working directory, e.g. `src/lib/foo.ts` */
2
4
  path: string;
3
- isDir: boolean;
4
- ext?: string;
5
+ /** Name of the generated page without extension. `index` files are prefixed with their folder. */
5
6
  name: string;
6
- folder?: string;
7
+ /** File extension, e.g. `.ts` */
8
+ ext: string;
9
+ /** Folder of the file relative to the working directory with a trailing slash, e.g. `src/lib/` */
10
+ folder: string;
7
11
  }
8
12
  export interface CLIArguments {
9
- include: string;
10
- exclude: string;
11
- partials: string[];
12
- rmPattern: string[];
13
- folder: string;
14
- jsDocConfigPath: string;
15
- source: string;
16
- dist: string;
17
- title: string;
18
- readme: string;
19
- watch: boolean;
13
+ /** Pattern to include files/folders (comma separated or list) */
14
+ include?: string | string[];
15
+ /** Pattern to exclude files/folders (comma separated or list) */
16
+ exclude?: string | string[];
17
+ /** jsdoc2markdown partial templates */
18
+ partials?: string[] | string;
19
+ /** Additional glob patterns of files to remove before generating */
20
+ rmPattern?: string[] | string;
21
+ /** Folder inside the destination folder, default `code` */
22
+ folder?: string;
23
+ /** Path to a jsdoc config */
24
+ jsDocConfigPath?: string;
25
+ /** Source folder, default `./src` */
26
+ source?: string;
27
+ /** Destination folder, default `./documentation` */
28
+ dist?: string;
29
+ /** Title of the documentation, default `API` */
30
+ title?: string;
31
+ /** Path to a custom readme */
32
+ readme?: string;
33
+ /** Watch the source files */
34
+ watch?: boolean;
35
+ /** Make the top level sidebar groups collapsible, default `false` */
36
+ collapsible?: boolean;
37
+ /** Sidebar format of the generated `config.js`, default `vuepress` */
38
+ format?: 'vuepress' | 'vitepress';
39
+ /** Path to a config file */
40
+ config?: string;
20
41
  }
42
+ /**
43
+ * Options of a `vuepress-jsdoc.config.js` file
44
+ */
45
+ export type UserConfig = Omit<CLIArguments, 'config'>;
21
46
  export interface ParseReturn {
22
- success: boolean;
47
+ /** Result of parsing */
48
+ type: StatisticType;
49
+ /** Destination folder relative to the working directory */
23
50
  dest: string;
24
51
  file: DirectoryFile;
52
+ /** Rendered markdown (including the frontmatter) */
25
53
  content: string;
26
- empty: boolean;
27
- excluded?: boolean;
28
- relativePathSrc: string;
29
- relativePathDest: string;
54
+ /** Error message, if the file could not be parsed */
55
+ error?: string;
30
56
  }
31
57
  export interface FileTree {
32
58
  name: string;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,17 +1,23 @@
1
- import { DirectoryFile } from '../interfaces';
1
+ import { DirectoryFile } from '../interfaces.js';
2
+ interface ParsedComment {
3
+ /** raw frontmatter string */
4
+ frontmatter: string | null;
5
+ /** parsed frontmatter attributes */
6
+ attributes: Record<string, any> | null;
7
+ }
2
8
  /**
3
- * Search in file for @vuepress comment
9
+ * Search in file for a `@vuepress` comment block and parse its frontmatter
4
10
  * @param {string} fileContent content of given file
5
11
  * @returns {object} object of found frontmatter data
6
12
  */
7
- export declare const parseComment: (fileContent: string) => import("front-matter").FrontMatterResult<Record<string, string>> | {
8
- frontmatter: null;
9
- attributes: null;
10
- };
13
+ export declare const parseComment: (fileContent: string) => ParsedComment;
11
14
  /**
12
- * Helper function to get header as structured markdown
15
+ * Helper function to get the header of a page as structured markdown.
16
+ * It contains the frontmatter and a headline (`# headline`).
17
+ * The headline is omitted for vue files, because vue-docgen-cli renders its own.
13
18
  * @param {string} content file content
14
- * @param {object} file file object
19
+ * @param {DirectoryFile} file file object
15
20
  * @returns {string} markdown header
16
21
  */
17
- export declare const parseVuepressFileHeader: (content: string, file: DirectoryFile) => string;
22
+ export declare const parseVuepressFileHeader: (content: string, file: Pick<DirectoryFile, 'name' | 'ext'>) => string;
23
+ export {};
@@ -1,74 +1,68 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.parseVuepressFileHeader = exports.parseComment = void 0;
7
1
  /*
8
2
  * @vuepress
9
3
  * ---
10
4
  * headline: lib/comment-parser.ts
11
5
  * ---
12
6
  */
13
- var front_matter_1 = __importDefault(require("front-matter"));
7
+ import { createRequire } from 'node:module';
8
+ // front-matter is a commonjs module with wrong types for es modules
9
+ const fm = createRequire(import.meta.url)('front-matter');
14
10
  /**
15
- * Search in file for @vuepress comment
11
+ * Search in file for a `@vuepress` comment block and parse its frontmatter
16
12
  * @param {string} fileContent content of given file
17
13
  * @returns {object} object of found frontmatter data
18
14
  */
19
- var parseComment = function (fileContent) {
15
+ export const parseComment = (fileContent) => {
16
+ const empty = { frontmatter: null, attributes: null };
17
+ if (typeof fileContent !== 'string')
18
+ return empty;
20
19
  try {
21
- var allCommentBlocks = fileContent.match(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/g);
22
- var vuepressBlock = allCommentBlocks === null || allCommentBlocks === void 0 ? void 0 : allCommentBlocks.filter(function (block) {
23
- return block.split('\n').filter(function (line) { return line.indexOf('@vuepress') >= 0; }).length;
24
- })[0];
25
- if (!vuepressBlock) {
26
- return {
27
- frontmatter: null,
28
- attributes: null
29
- };
30
- }
31
- return (0, front_matter_1.default)(vuepressBlock
32
- .replace(/\n /g, '\n')
33
- .replace('/*', '')
34
- .replace('*/', '')
35
- .replace(/@vuepress/g, '')
36
- .replace(/\*\s?/g, '')
37
- .trim());
20
+ const vuepressBlock = fileContent.match(/\/\*[\s\S]*?\*\//g)?.find((block) => /^[\s/*]*@vuepress\b/m.test(block));
21
+ if (!vuepressBlock)
22
+ return empty;
23
+ const body = vuepressBlock
24
+ .replace(/^\/\*+/, '')
25
+ .replace(/\*+\/$/, '')
26
+ .split('\n')
27
+ // remove the leading "*" of every comment line
28
+ .map((line) => line.replace(/^\s*\*\s?/, ''))
29
+ .filter((line) => !/^\s*@vuepress\s*$/.test(line))
30
+ .join('\n')
31
+ .trim();
32
+ const { frontmatter, attributes } = fm(body);
33
+ return { frontmatter: frontmatter || null, attributes: frontmatter ? attributes : null };
38
34
  }
39
- catch (e) {
40
- return {
41
- frontmatter: null,
42
- attributes: null
43
- };
35
+ catch {
36
+ return empty;
44
37
  }
45
38
  };
46
- exports.parseComment = parseComment;
47
39
  /**
48
- * Helper function to get header as structured markdown
40
+ * Quote a string for the use inside yaml frontmatter
41
+ * @param {string} value
42
+ * @returns {string} quoted string
43
+ */
44
+ const yamlString = (value) => JSON.stringify(value);
45
+ /**
46
+ * Helper function to get the header of a page as structured markdown.
47
+ * It contains the frontmatter and a headline (`# headline`).
48
+ * The headline is omitted for vue files, because vue-docgen-cli renders its own.
49
49
  * @param {string} content file content
50
- * @param {object} file file object
50
+ * @param {DirectoryFile} file file object
51
51
  * @returns {string} markdown header
52
52
  */
53
- var parseVuepressFileHeader = function (content, file) {
54
- var _a = (0, exports.parseComment)(content), frontmatter = _a.frontmatter, attributes = _a.attributes;
55
- var fileContent = '---\n';
56
- fileContent += !(attributes === null || attributes === void 0 ? void 0 : attributes.title) ? "title: ".concat(file.name) : '';
53
+ export const parseVuepressFileHeader = (content, file) => {
54
+ const { frontmatter, attributes } = parseComment(content);
55
+ const lines = [];
56
+ if (!attributes?.title) {
57
+ lines.push(`title: ${yamlString(file.name)}`);
58
+ }
57
59
  if (frontmatter) {
58
- fileContent += !(attributes === null || attributes === void 0 ? void 0 : attributes.title) ? '\n' : '';
59
- fileContent += "".concat(frontmatter);
60
+ lines.push(frontmatter);
60
61
  }
61
- fileContent += '\n---\n';
62
- if ((attributes === null || attributes === void 0 ? void 0 : attributes.title) || file.ext !== '.vue') {
63
- var headline = file.name;
64
- if (attributes === null || attributes === void 0 ? void 0 : attributes.headline) {
65
- headline = attributes.headline;
66
- }
67
- else if (attributes === null || attributes === void 0 ? void 0 : attributes.title) {
68
- headline = attributes.title;
69
- }
70
- fileContent += "\n# ".concat(headline, "\n\n");
62
+ let header = `---\n${lines.join('\n')}\n---\n`;
63
+ if (attributes?.title || file.ext !== '.vue') {
64
+ const headline = attributes?.headline || attributes?.title || file.name;
65
+ header += `\n# ${headline}\n\n`;
71
66
  }
72
- return fileContent;
67
+ return header;
73
68
  };
74
- exports.parseVuepressFileHeader = parseVuepressFileHeader;
@@ -0,0 +1,21 @@
1
+ import { UserConfig } from '../interfaces.js';
2
+ /**
3
+ * Helper for typed config files
4
+ * @example
5
+ * // vuepress-jsdoc.config.js
6
+ * import { defineConfig } from 'vuepress-jsdoc';
7
+ *
8
+ * export default defineConfig({ source: './lib', exclude: ['**\/*.test.js'] });
9
+ * @param {UserConfig} config
10
+ * @returns {UserConfig} the same config
11
+ */
12
+ export declare const defineConfig: (config: UserConfig) => UserConfig;
13
+ /**
14
+ * Load a config file. Without a path, the working directory is searched for a `vuepress-jsdoc.config.*` file.
15
+ * @param {string} [configPath] path to a config file
16
+ * @returns {Promise<{ path: string, config: UserConfig } | null>} config or null if there is no config file
17
+ */
18
+ export declare const loadConfig: (configPath?: string) => Promise<{
19
+ path: string;
20
+ config: UserConfig;
21
+ } | null>;
@@ -0,0 +1,57 @@
1
+ /*
2
+ * @vuepress
3
+ * ---
4
+ * headline: lib/config.ts
5
+ * ---
6
+ */
7
+ import fs from 'node:fs';
8
+ import path from 'node:path';
9
+ import { pathToFileURL } from 'node:url';
10
+ /**
11
+ * Names of config files, which are searched in the working directory
12
+ */
13
+ const CONFIG_FILES = [
14
+ 'vuepress-jsdoc.config.js',
15
+ 'vuepress-jsdoc.config.mjs',
16
+ 'vuepress-jsdoc.config.cjs',
17
+ 'vuepress-jsdoc.config.json'
18
+ ];
19
+ /**
20
+ * Helper for typed config files
21
+ * @example
22
+ * // vuepress-jsdoc.config.js
23
+ * import { defineConfig } from 'vuepress-jsdoc';
24
+ *
25
+ * export default defineConfig({ source: './lib', exclude: ['**\/*.test.js'] });
26
+ * @param {UserConfig} config
27
+ * @returns {UserConfig} the same config
28
+ */
29
+ export const defineConfig = (config) => config;
30
+ /**
31
+ * Load a config file. Without a path, the working directory is searched for a `vuepress-jsdoc.config.*` file.
32
+ * @param {string} [configPath] path to a config file
33
+ * @returns {Promise<{ path: string, config: UserConfig } | null>} config or null if there is no config file
34
+ */
35
+ export const loadConfig = async (configPath) => {
36
+ const file = configPath
37
+ ? path.resolve(configPath)
38
+ : CONFIG_FILES.map((name) => path.resolve(name)).find((p) => fs.existsSync(p));
39
+ if (!file)
40
+ return null;
41
+ if (!fs.existsSync(file)) {
42
+ throw new Error(`Config file not found: ${configPath}`);
43
+ }
44
+ let config;
45
+ if (file.endsWith('.json')) {
46
+ config = JSON.parse(await fs.promises.readFile(file, 'utf-8'));
47
+ }
48
+ else {
49
+ // the timestamp prevents a cached module in watch mode or tests
50
+ const module = await import(`${pathToFileURL(file).href}?t=${Date.now()}`);
51
+ config = module.default ?? module;
52
+ }
53
+ if (!config || typeof config !== 'object') {
54
+ throw new Error(`Config file has to export an object: ${file}`);
55
+ }
56
+ return { path: file, config };
57
+ };
@@ -1,15 +1,33 @@
1
- import { DirectoryFile, FileTree } from '../interfaces';
2
- /**
3
- * Recursively traverse folders and return exluded files, a file list and a file tree.
4
- * @param {string} srcPath path to source dir
5
- * @param {array} exclude exluded file patter list
6
- * @param {array} include included file patter list
7
- * @param {string} mainPath path to hold source dir
8
- * @param {object} tree tree array
9
- * @returns {object} paths array, tree, excluded array
10
- */
11
- export declare const listFolder: (srcPath: string, exclude: string[], include: string[], mainPath?: string, tree?: FileTree[]) => Promise<{
1
+ import { DirectoryFile, FileTree } from '../interfaces.js';
2
+ export interface ListFolderResult {
3
+ /** All files which should be parsed */
12
4
  paths: DirectoryFile[];
5
+ /** Folder structure of all parsable files */
13
6
  tree: FileTree[];
7
+ /** Files which were excluded by the include/exclude patterns */
14
8
  excluded: DirectoryFile[];
15
- }>;
9
+ }
10
+ /**
11
+ * Normalize a path to a posix path without leading `./` and trailing slash
12
+ * @param {string} p path
13
+ * @returns {string} normalized path
14
+ */
15
+ export declare const normalizePath: (p: string) => string;
16
+ /**
17
+ * Get the page name of a file. Index files are prefixed with their folder,
18
+ * because an `index.md` would collide with the `README.md` of the folder.
19
+ * @param {string} dir directory of the file
20
+ * @param {string} name file name without extension
21
+ * @returns {string} page name
22
+ */
23
+ export declare const getPageName: (dir: string, name: string) => string;
24
+ /**
25
+ * Recursively traverse folders and return excluded files, a file list and a file tree.
26
+ * Only `.js`, `.jsx`, `.mjs`, `.cjs`, `.ts`, `.tsx`, `.mts`, `.cts` and `.vue` files are listed.
27
+ * Hidden folders and `node_modules` are skipped.
28
+ * @param {string} srcPath path to source dir
29
+ * @param {string[]} exclude excluded file pattern list (relative to srcPath)
30
+ * @param {string[]} include included file pattern list (relative to srcPath)
31
+ * @returns {Promise<ListFolderResult>} paths array, tree, excluded array
32
+ */
33
+ export declare const listFolder: (srcPath: string, exclude?: string[], include?: string[]) => Promise<ListFolderResult>;