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,202 +1,116 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __values = (this && this.__values) || function(o) {
50
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
51
- if (m) return m.call(o);
52
- if (o && typeof o.length === "number") return {
53
- next: function () {
54
- if (o && i >= o.length) o = void 0;
55
- return { value: o && o[i++], done: !o };
56
- }
57
- };
58
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
59
- };
60
- var __read = (this && this.__read) || function (o, n) {
61
- var m = typeof Symbol === "function" && o[Symbol.iterator];
62
- if (!m) return o;
63
- var i = m.call(o), r, ar = [], e;
64
- try {
65
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
66
- }
67
- catch (error) { e = { error: error }; }
68
- finally {
69
- try {
70
- if (r && !r.done && (m = i["return"])) m.call(i);
71
- }
72
- finally { if (e) throw e.error; }
73
- }
74
- return ar;
75
- };
76
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
77
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
78
- if (ar || !(i in from)) {
79
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
80
- ar[i] = from[i];
81
- }
82
- }
83
- return to.concat(ar || Array.prototype.slice.call(from));
84
- };
85
- var __importDefault = (this && this.__importDefault) || function (mod) {
86
- return (mod && mod.__esModule) ? mod : { "default": mod };
87
- };
88
- Object.defineProperty(exports, "__esModule", { value: true });
89
- exports.listFolder = void 0;
90
1
  /*
91
2
  * @vuepress
92
3
  * ---
93
4
  * headline: lib/list-folder.ts
94
5
  * ---
95
6
  */
96
- var promises_1 = __importDefault(require("fs/promises"));
97
- var micromatch_1 = __importDefault(require("micromatch"));
98
- var path_1 = __importDefault(require("path"));
7
+ import mm from 'micromatch';
8
+ import fs from 'node:fs/promises';
9
+ import path from 'node:path';
10
+ import { SCRIPT_EXTENSIONS, VUE_EXTENSIONS } from '../constants.js';
11
+ const SUPPORTED_EXTENSIONS = [...SCRIPT_EXTENSIONS, ...VUE_EXTENSIONS];
12
+ /**
13
+ * Normalize a path to a posix path without leading `./` and trailing slash
14
+ * @param {string} p path
15
+ * @returns {string} normalized path
16
+ */
17
+ export const normalizePath = (p) => path.posix.normalize(p.split(path.sep).join('/')).replace(/\/$/, '') || '.';
18
+ /**
19
+ * Normalize pattern lists, which may be passed as comma separated string or array
20
+ * @param {string|string[]} patterns patterns
21
+ * @returns {string[]} list of non empty patterns
22
+ */
23
+ const toPatternList = (patterns) => {
24
+ if (typeof patterns === 'string')
25
+ return patterns.split(',').filter(Boolean);
26
+ if (Array.isArray(patterns))
27
+ return patterns.filter((p) => typeof p === 'string' && p.length > 0);
28
+ return [];
29
+ };
30
+ /**
31
+ * Check if a file should be documented
32
+ * @param {string} fileName name of the file
33
+ * @returns {boolean}
34
+ */
35
+ const isSupportedFile = (fileName) => SUPPORTED_EXTENSIONS.includes(path.extname(fileName)) && !/\.d\.[cm]?ts$/.test(fileName);
99
36
  /**
100
- * Recursively traverse folders and return exluded files, a file list and a file tree.
37
+ * Get the page name of a file. Index files are prefixed with their folder,
38
+ * because an `index.md` would collide with the `README.md` of the folder.
39
+ * @param {string} dir directory of the file
40
+ * @param {string} name file name without extension
41
+ * @returns {string} page name
42
+ */
43
+ export const getPageName = (dir, name) => {
44
+ if (name !== 'index')
45
+ return name;
46
+ const prefix = dir
47
+ .split('/')
48
+ .filter((segment) => segment && segment !== '.' && segment !== '..')
49
+ .join('-');
50
+ return `${prefix || path.basename(path.resolve(dir))}-${name}`;
51
+ };
52
+ /**
53
+ * Recursively traverse folders and return excluded files, a file list and a file tree.
54
+ * Only `.js`, `.jsx`, `.mjs`, `.cjs`, `.ts`, `.tsx`, `.mts`, `.cts` and `.vue` files are listed.
55
+ * Hidden folders and `node_modules` are skipped.
101
56
  * @param {string} srcPath path to source dir
102
- * @param {array} exclude exluded file patter list
103
- * @param {array} include included file patter list
104
- * @param {string} mainPath path to hold source dir
105
- * @param {object} tree tree array
106
- * @returns {object} paths array, tree, excluded array
57
+ * @param {string[]} exclude excluded file pattern list (relative to srcPath)
58
+ * @param {string[]} include included file pattern list (relative to srcPath)
59
+ * @returns {Promise<ListFolderResult>} paths array, tree, excluded array
107
60
  */
108
- var listFolder = function (srcPath, exclude, include, mainPath, tree) {
109
- if (tree === void 0) { tree = []; }
110
- return __awaiter(void 0, void 0, void 0, function () {
111
- var paths, excluded, dirs, _a, dirs_1, dirs_1_1, dirent, filePath, isDir, ext, name_1, folder, file, baseSrc, treeEntry, _b, _c, _d, _e, e_1_1;
112
- var e_1, _f;
113
- return __generator(this, function (_g) {
114
- switch (_g.label) {
115
- case 0:
116
- if (!Boolean(exclude) || !Array.isArray(exclude)) {
117
- exclude = [];
118
- }
119
- if (!Boolean(include) || !Array.isArray(include)) {
120
- include = [];
121
- }
122
- exclude = exclude.filter(Boolean);
123
- include = include.filter(Boolean);
124
- paths = [];
125
- excluded = [];
126
- if (!srcPath) return [3 /*break*/, 2];
127
- return [4 /*yield*/, promises_1.default.readdir(srcPath, {
128
- withFileTypes: true
129
- })];
130
- case 1:
131
- _a = _g.sent();
132
- return [3 /*break*/, 3];
133
- case 2:
134
- _a = [];
135
- _g.label = 3;
136
- case 3:
137
- dirs = _a;
138
- _g.label = 4;
139
- case 4:
140
- _g.trys.push([4, 12, 13, 14]);
141
- dirs_1 = __values(dirs), dirs_1_1 = dirs_1.next();
142
- _g.label = 5;
143
- case 5:
144
- if (!!dirs_1_1.done) return [3 /*break*/, 11];
145
- dirent = dirs_1_1.value;
146
- filePath = path_1.default.join(srcPath, dirent.name);
147
- isDir = dirent.isDirectory();
148
- ext = path_1.default.extname(filePath);
149
- name_1 = path_1.default.basename(filePath).replace(ext, '');
150
- folder = filePath.replace(name_1, '').replace(ext, '');
151
- if (name_1 === 'index') {
152
- name_1 = '__index__';
153
- }
154
- file = __assign({ isDir: isDir, name: name_1, path: filePath }, (!isDir ? { ext: ext, folder: folder } : {}));
155
- // skip readmes as they are automatically resolved
156
- if (name_1.toLowerCase() === 'readme')
157
- return [3 /*break*/, 10];
158
- baseSrc = mainPath || srcPath;
159
- if (!(micromatch_1.default.every(path_1.default.join(srcPath.replace(baseSrc, ''), dirent.name), __spreadArray(__spreadArray([], __read(include), false), __read(exclude.map(function (e) { return '!' + e; })), false)) ||
160
- isDir)) return [3 /*break*/, 9];
161
- treeEntry = {
162
- name: name_1
163
- };
164
- if (!isDir) return [3 /*break*/, 7];
165
- treeEntry.children = [];
166
- _c = (_b = paths.push).apply;
167
- _d = [paths];
168
- _e = [[]];
169
- return [4 /*yield*/, (0, exports.listFolder)(filePath, exclude, include, baseSrc, treeEntry.children)];
170
- case 6:
171
- _c.apply(_b, _d.concat([__spreadArray.apply(void 0, _e.concat([__read.apply(void 0, [(_g.sent()).paths]), false]))]));
172
- return [3 /*break*/, 8];
173
- case 7:
174
- treeEntry = __assign(__assign({}, treeEntry), { path: "/".concat(name_1), fullPath: path_1.default.join(srcPath, name_1), ext: ext });
175
- _g.label = 8;
176
- case 8:
177
- tree.push(treeEntry);
178
- paths.push(file);
179
- return [3 /*break*/, 10];
180
- case 9:
181
- excluded.push(file);
182
- _g.label = 10;
183
- case 10:
184
- dirs_1_1 = dirs_1.next();
185
- return [3 /*break*/, 5];
186
- case 11: return [3 /*break*/, 14];
187
- case 12:
188
- e_1_1 = _g.sent();
189
- e_1 = { error: e_1_1 };
190
- return [3 /*break*/, 14];
191
- case 13:
192
- try {
193
- if (dirs_1_1 && !dirs_1_1.done && (_f = dirs_1.return)) _f.call(dirs_1);
194
- }
195
- finally { if (e_1) throw e_1.error; }
196
- return [7 /*endfinally*/];
197
- case 14: return [2 /*return*/, { paths: paths, tree: tree, excluded: excluded }];
61
+ export const listFolder = async (srcPath, exclude = [], include = []) => {
62
+ const excludePatterns = toPatternList(exclude);
63
+ const includePatterns = toPatternList(include);
64
+ const patterns = [...includePatterns, ...excludePatterns.map((e) => '!' + e)];
65
+ const paths = [];
66
+ const excluded = [];
67
+ const tree = [];
68
+ if (!srcPath || typeof srcPath !== 'string') {
69
+ return { paths, tree, excluded };
70
+ }
71
+ const root = normalizePath(srcPath);
72
+ const walk = async (dir, subTree) => {
73
+ const dirents = await fs.readdir(dir, { withFileTypes: true });
74
+ dirents.sort((a, b) => a.name.localeCompare(b.name));
75
+ const files = dirents.filter((d) => !d.isDirectory());
76
+ const dirs = dirents.filter((d) => d.isDirectory());
77
+ for (const dirent of files) {
78
+ if (!isSupportedFile(dirent.name))
79
+ continue;
80
+ const filePath = path.posix.join(dir, dirent.name);
81
+ const relativePath = path.posix.relative(root, filePath);
82
+ const ext = path.extname(dirent.name);
83
+ const name = getPageName(dir, path.basename(dirent.name, ext));
84
+ const folder = `${dir}/`;
85
+ const file = { name, ext, folder, path: filePath };
86
+ if (patterns.length && !mm.every(relativePath, patterns)) {
87
+ excluded.push(file);
88
+ continue;
198
89
  }
199
- });
200
- });
90
+ paths.push(file);
91
+ subTree.push({
92
+ name,
93
+ path: `/${name}`,
94
+ fullPath: path.posix.join(dir, name),
95
+ ext
96
+ });
97
+ }
98
+ for (const dirent of dirs) {
99
+ if (dirent.name.startsWith('.') || dirent.name === 'node_modules')
100
+ continue;
101
+ const dirPath = path.posix.join(dir, dirent.name);
102
+ const relativeDir = path.posix.relative(root, dirPath);
103
+ // a folder can be excluded as a whole, e.g. --exclude="tests" or --exclude="**/__tests__"
104
+ if (excludePatterns.length && mm.isMatch(relativeDir, excludePatterns))
105
+ continue;
106
+ const children = [];
107
+ await walk(dirPath, children);
108
+ // do not add empty folders to the tree
109
+ if (children.length > 0) {
110
+ subTree.push({ name: dirent.name, children });
111
+ }
112
+ }
113
+ };
114
+ await walk(root, tree);
115
+ return { paths, tree, excluded };
201
116
  };
202
- exports.listFolder = listFolder;
@@ -1,37 +1,53 @@
1
- import { StatisticType } from '../constants';
2
- import { DirectoryFile, ParseReturn } from '../interfaces';
1
+ import { DirectoryFile, ParseReturn } from '../interfaces.js';
2
+ export interface ParseOptions {
3
+ /** source folder relative to the working directory */
4
+ srcFolder: string;
5
+ /** destination folder relative to the working directory */
6
+ destFolder: string;
7
+ /** path to a jsdoc config */
8
+ jsDocConfigPath?: string;
9
+ /** additional jsdoc2markdown partials */
10
+ partials?: string[];
11
+ }
3
12
  /**
4
- * Parse a typescript or javascript file
5
- * @param file {DirectoryFile}
6
- * @param srcFolder {string}
7
- * @param destFolder {string}
8
- * @param configPath {string}
9
- * @param partials {string | string[]}
10
- * @returns {object} file data
11
- */
12
- export declare const parseFile: (file: DirectoryFile, srcFolder: string, destFolder: string, configPath: string, partials: string | string[]) => Promise<ParseReturn | null>;
13
- /**
14
- * Parse a vue file
13
+ * Get the folder of the generated markdown file
15
14
  * @param {DirectoryFile} file
16
15
  * @param {string} srcFolder
17
16
  * @param {string} destFolder
18
- * @returns {object} file data
17
+ * @returns {string} destination folder
18
+ */
19
+ export declare const getDestFolder: (file: Pick<DirectoryFile, 'folder'>, srcFolder: string, destFolder: string) => string;
20
+ /**
21
+ * Remove TypeScript types from source code, so jsdoc is able to parse it.
22
+ * Types are replaced with whitespace, so all comments and line numbers stay intact.
23
+ * @param {string} code TypeScript code
24
+ * @returns {string} JavaScript code
25
+ */
26
+ export declare const stripTypes: (code: string) => string;
27
+ /**
28
+ * Convert TypeScript to JavaScript. Type stripping of Node.js is used first, because it keeps all line numbers.
29
+ * If the file contains syntax which needs a transformation, the TypeScript compiler is used.
30
+ * @param {string} code TypeScript code
31
+ * @param {string} fileName
32
+ * @returns {string} JavaScript code
33
+ */
34
+ export declare const typescriptToJavaScript: (code: string, fileName: string) => string;
35
+ /**
36
+ * Escape vue interpolations (`{{ }}`) outside of code, because VuePress compiles every page as a vue template.
37
+ * @param {string} markdown
38
+ * @returns {string} escaped markdown
39
+ */
40
+ export declare const escapeVueInterpolation: (markdown: string) => string;
41
+ /**
42
+ * Parse a javascript, typescript or vue file and create the markdown content
43
+ * @param {DirectoryFile} file
44
+ * @param {ParseOptions} options
45
+ * @returns {Promise<ParseReturn>} file data
19
46
  */
20
- export declare const parseVueFile: (file: DirectoryFile, srcFolder: string, destFolder: string) => Promise<ParseReturn | null>;
47
+ export declare const parseFile: (file: DirectoryFile, options: ParseOptions) => Promise<ParseReturn>;
21
48
  /**
22
- * Write content on disk
23
- * @param {ParseReturn | null} parseData
24
- * @param {string} dest
25
- * @returns {Promise | null} null or type with some data of the saved file
49
+ * Write the parsed content to disk. Files without content are skipped.
50
+ * @param {ParseReturn} parseData
51
+ * @returns {Promise<string | null>} path of the written file or null
26
52
  */
27
- export declare const writeContentToFile: (parseData: ParseReturn | null, dest: string) => Promise<{
28
- type: StatisticType;
29
- success?: boolean | undefined;
30
- dest?: string | undefined;
31
- file?: DirectoryFile | undefined;
32
- content?: string | undefined;
33
- empty?: boolean | undefined;
34
- excluded?: boolean | undefined;
35
- relativePathSrc?: string | undefined;
36
- relativePathDest?: string | undefined;
37
- } | null>;
53
+ export declare const writeContentToFile: (parseData: ParseReturn) => Promise<string | null>;