@diplodoc/cli 4.0.0 → 4.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.
package/build/lib.js DELETED
@@ -1,3374 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __objRest = (source, exclude) => {
26
- var target = {};
27
- for (var prop in source)
28
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
- target[prop] = source[prop];
30
- if (source != null && __getOwnPropSymbols)
31
- for (var prop of __getOwnPropSymbols(source)) {
32
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
- target[prop] = source[prop];
34
- }
35
- return target;
36
- };
37
- var __commonJS = (cb, mod) => function __require() {
38
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
39
- };
40
- var __export = (target, all) => {
41
- for (var name4 in all)
42
- __defProp(target, name4, { get: all[name4], enumerable: true });
43
- };
44
- var __copyProps = (to, from, except, desc) => {
45
- if (from && typeof from === "object" || typeof from === "function") {
46
- for (let key of __getOwnPropNames(from))
47
- if (!__hasOwnProp.call(to, key) && key !== except)
48
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
49
- }
50
- return to;
51
- };
52
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
53
- // If the importer is in node compatibility mode or this is not an ESM
54
- // file that has been converted to a CommonJS file using a Babel-
55
- // compatible transform (i.e. "__esModule" has not been set), then set
56
- // "default" to the CommonJS "module.exports" for node compatibility.
57
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
58
- mod
59
- ));
60
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
61
- var __async = (__this, __arguments, generator) => {
62
- return new Promise((resolve16, reject) => {
63
- var fulfilled = (value) => {
64
- try {
65
- step(generator.next(value));
66
- } catch (e) {
67
- reject(e);
68
- }
69
- };
70
- var rejected = (value) => {
71
- try {
72
- step(generator.throw(value));
73
- } catch (e) {
74
- reject(e);
75
- }
76
- };
77
- var step = (x) => x.done ? resolve16(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
78
- step((generator = generator.apply(__this, __arguments)).next());
79
- });
80
- };
81
-
82
- // scripts/client.js
83
- var require_client = __commonJS({
84
- "scripts/client.js"(exports, module2) {
85
- "use strict";
86
- var path = require("path");
87
- var CLIENT_PATH = path.dirname(require.resolve("@diplodoc/client"));
88
- var BUILD_PATH = path.resolve(__dirname, "..", "build");
89
- var BUNDLE_JS_FILENAME = "app.client.js";
90
- var BUNDLE_CSS_FILENAME = "app.client.css";
91
- var src = (target) => path.resolve(CLIENT_PATH, target);
92
- var dst = (target) => path.resolve(BUILD_PATH, target);
93
- module2.exports = {
94
- dst: {
95
- js: dst(BUNDLE_JS_FILENAME),
96
- css: dst(BUNDLE_CSS_FILENAME)
97
- },
98
- src: {
99
- js: src(BUNDLE_JS_FILENAME),
100
- css: src(BUNDLE_CSS_FILENAME)
101
- },
102
- bundle: {
103
- js(bundlePath) {
104
- return path.join(bundlePath, BUNDLE_JS_FILENAME);
105
- },
106
- css(bundlePath) {
107
- return path.join(bundlePath, BUNDLE_CSS_FILENAME);
108
- }
109
- }
110
- };
111
- }
112
- });
113
-
114
- // src/cmd/build/index.ts
115
- var build_exports = {};
116
- __export(build_exports, {
117
- build: () => build,
118
- handler: () => handler
119
- });
120
- module.exports = __toCommonJS(build_exports);
121
-
122
- // src/constants.ts
123
- var import_path = require("path");
124
- var os = require("os");
125
- var notes = require("@doc-tools/transform/lib/plugins/notes");
126
- var anchors = require("@doc-tools/transform/lib/plugins/anchors");
127
- var code = require("@doc-tools/transform/lib/plugins/code");
128
- var cut = require("@doc-tools/transform/lib/plugins/cut");
129
- var deflist = require("@doc-tools/transform/lib/plugins/deflist");
130
- var imsize = require("@doc-tools/transform/lib/plugins/imsize");
131
- var meta = require("@doc-tools/transform/lib/plugins/meta");
132
- var sup = require("@doc-tools/transform/lib/plugins/sup");
133
- var tabs = require("@doc-tools/transform/lib/plugins/tabs");
134
- var video = require("@doc-tools/transform/lib/plugins/video");
135
- var includes = require("@doc-tools/transform/lib/plugins/includes");
136
- var links = require("@doc-tools/transform/lib/plugins/links");
137
- var images = require("@doc-tools/transform/lib/plugins/images");
138
- var monospace = require("@doc-tools/transform/lib/plugins/monospace");
139
- var table = require("@doc-tools/transform/lib/plugins/table");
140
- var term = require("@doc-tools/transform/lib/plugins/term");
141
- var changelog = require("@doc-tools/transform/lib/plugins/changelog");
142
- var mermaid = require("@diplodoc/mermaid-extension");
143
- var openapi = require("@diplodoc/openapi-extension");
144
- includes.collect = require("@doc-tools/transform/lib/plugins/includes/collect");
145
- images.collect = require("@doc-tools/transform/lib/plugins/images/collect");
146
- changelog.collect = require("@doc-tools/transform/lib/plugins/changelog/collect");
147
- var BUNDLE_FOLDER = "_bundle";
148
- var TMP_INPUT_FOLDER = ".tmp_input";
149
- var TMP_OUTPUT_FOLDER = ".tmp_output";
150
- var YFM_CONFIG_FILENAME = ".yfm";
151
- var REDIRECTS_FILENAME = "redirects.yaml";
152
- var LINT_CONFIG_FILENAME = ".yfmlint";
153
- var SINGLE_PAGE_FILENAME = "single-page.html";
154
- var SINGLE_PAGE_DATA_FILENAME = "single-page.json";
155
- var CUSTOM_STYLE = "custom-style";
156
- var ResourceType = /* @__PURE__ */ ((ResourceType2) => {
157
- ResourceType2["style"] = "style";
158
- ResourceType2["script"] = "script";
159
- return ResourceType2;
160
- })(ResourceType || {});
161
- var BUILD_FOLDER_PATH = (0, import_path.dirname)(require.resolve("@diplodoc/client"));
162
- var YFM_PLUGINS = [
163
- meta,
164
- deflist,
165
- includes,
166
- cut,
167
- links,
168
- images,
169
- notes,
170
- anchors,
171
- tabs,
172
- code,
173
- imsize,
174
- sup,
175
- video,
176
- monospace,
177
- table,
178
- term,
179
- openapi.transform(),
180
- mermaid.transform(),
181
- changelog
182
- ];
183
- var PROCESSING_FINISHED = "Processing finished:";
184
- var LINTING_FINISHED = "Linting finished:";
185
- var GETTING_ALL_CONTRIBUTORS = "Getting all contributors.";
186
- var ALL_CONTRIBUTORS_RECEIVED = "All contributors received.";
187
- var getMsg\u0421onfigurationMustBeProvided = (repo) => `\u0421onfiguration must be provided for ${repo} like env variables or in .yfm file`;
188
- var FIRST_COMMIT_FROM_ROBOT_IN_GITHUB = "2dce14271359cd20d7e874956d604de087560cf4";
189
- var REGEXP_INCLUDE_CONTENTS = new RegExp("(?<=[{%]\\sinclude\\s).+(?=\\s[%}])", "gm");
190
- var REGEXP_INCLUDE_FILE_PATH = new RegExp("(?<=[(]).+(?=[)])", "g");
191
- var REGEXP_AUTHOR = new RegExp("(?<=author:\\s).+(?=\\r?\\n)", "g");
192
- var MIN_CHUNK_SIZE = Number(process.env.MIN_CHUNK_SIZE) || 1e3;
193
- var WORKERS_COUNT = Number(process.env.WORKERS_COUNT) || os.cpus().length - 1;
194
- var metadataBorder = "---";
195
-
196
- // src/validator.ts
197
- var import_path2 = require("path");
198
- var import_fs = require("fs");
199
- var import_js_yaml = require("js-yaml");
200
- var import_merge = __toESM(require("lodash/merge"));
201
- var import_log = __toESM(require("@doc-tools/transform/lib/log"));
202
- function notEmptyStringValidator(value) {
203
- if (typeof value === "string") {
204
- return Boolean(value);
205
- }
206
- return false;
207
- }
208
- function requiredValueValidator(value) {
209
- return Boolean(value);
210
- }
211
- var validators = {
212
- "storageEndpoint": {
213
- errorMessage: "Endpoint of S3 storage must be provided when publishes.",
214
- validateFn: notEmptyStringValidator
215
- },
216
- "storageBucket": {
217
- errorMessage: "Bucket name of S3 storage must be provided when publishes.",
218
- validateFn: notEmptyStringValidator
219
- },
220
- "storageKeyId": {
221
- errorMessage: "Key Id of S3 storage must be provided when publishes.",
222
- validateFn: notEmptyStringValidator,
223
- defaultValue: process.env.YFM_STORAGE_KEY_ID
224
- },
225
- "storageSecretKey": {
226
- errorMessage: "Secret key of S3 storage must be provided when publishes.",
227
- validateFn: notEmptyStringValidator,
228
- defaultValue: process.env.YFM_STORAGE_SECRET_KEY
229
- },
230
- "storageRegion": {
231
- errorMessage: "Region of S3 storage must be provided when publishes.",
232
- validateFn: notEmptyStringValidator,
233
- defaultValue: "eu-central-1"
234
- }
235
- };
236
- function validateRedirects(redirectsConfig, pathToRedirects) {
237
- const redirects = Object.keys(redirectsConfig).reduce((res, redirectSectionName) => {
238
- const sectionRedirects = redirectsConfig[redirectSectionName];
239
- res.push(...sectionRedirects);
240
- return res;
241
- }, []);
242
- const getContext = (from, to) => ` [Context:
243
- - from: ${from}
244
- - to: ${to} ]`;
245
- const formatMessage = (message, pathname, from, to) => `${pathname}: ${message} ${getContext(from, to)}`;
246
- redirects.forEach((redirect) => {
247
- const { from, to } = redirect;
248
- if (!from || !to) {
249
- throw new Error(formatMessage("One of the two parameters is missing", pathToRedirects, from, to));
250
- }
251
- if (from === to) {
252
- throw new Error(formatMessage("Parameters must be different", pathToRedirects, from, to));
253
- }
254
- });
255
- }
256
- function argvValidator(argv) {
257
- var _a, _b;
258
- try {
259
- const pathToConfig = argv.config ? String(argv.config) : (0, import_path2.join)(String(argv.input), YFM_CONFIG_FILENAME);
260
- const content = (0, import_fs.readFileSync)((0, import_path2.resolve)(pathToConfig), "utf8");
261
- Object.assign(argv, (0, import_js_yaml.load)(content) || {});
262
- } catch (error) {
263
- if (error.name === "YAMLException") {
264
- import_log.default.error(`Error to parse ${YFM_CONFIG_FILENAME}: ${error.message}`);
265
- }
266
- }
267
- let lintConfig = {};
268
- try {
269
- const pathToConfig = (0, import_path2.join)(String(argv.input), LINT_CONFIG_FILENAME);
270
- const content = (0, import_fs.readFileSync)((0, import_path2.resolve)(pathToConfig), "utf8");
271
- lintConfig = (0, import_js_yaml.load)(content) || {};
272
- } catch (error) {
273
- if (error.name === "YAMLException") {
274
- import_log.default.error(`Error to parse ${LINT_CONFIG_FILENAME}: ${error.message}`);
275
- }
276
- } finally {
277
- const preparedLintConfig = (0, import_merge.default)(lintConfig, {
278
- "log-levels": {
279
- MD033: argv.allowHTML ? "disabled" : "error"
280
- }
281
- });
282
- Object.assign(argv, { lintConfig: preparedLintConfig });
283
- }
284
- try {
285
- const pathToRedirects = (0, import_path2.join)(String(argv.input), REDIRECTS_FILENAME);
286
- const redirectsContent = (0, import_fs.readFileSync)((0, import_path2.resolve)(pathToRedirects), "utf8");
287
- const redirects = (0, import_js_yaml.load)(redirectsContent);
288
- validateRedirects(redirects, pathToRedirects);
289
- } catch (error) {
290
- if (error.name === "YAMLException") {
291
- import_log.default.error(`Error to parse ${REDIRECTS_FILENAME}: ${error.message}`);
292
- }
293
- if (error.code !== "ENOENT") {
294
- throw error;
295
- }
296
- }
297
- if (argv.publish) {
298
- for (const [field, validator] of Object.entries(validators)) {
299
- const value = (_a = argv[field]) != null ? _a : validator.defaultValue;
300
- if (!validator) {
301
- continue;
302
- }
303
- const validateFn = (_b = validator.validateFn) != null ? _b : requiredValueValidator;
304
- if (!validateFn(value)) {
305
- throw new Error(validator.errorMessage);
306
- }
307
- argv[field] = value;
308
- }
309
- }
310
- return true;
311
- }
312
-
313
- // src/cmd/build/index.ts
314
- var import_path27 = require("path");
315
-
316
- // src/services/tocs.ts
317
- var import_path13 = require("path");
318
- var import_fs4 = require("fs");
319
- var import_js_yaml4 = require("js-yaml");
320
- var import_shelljs2 = __toESM(require("shelljs"));
321
- var import_walk_sync = __toESM(require("walk-sync"));
322
- var import_liquid2 = __toESM(require("@doc-tools/transform/lib/liquid"));
323
- var import_log3 = __toESM(require("@doc-tools/transform/lib/log"));
324
- var import_chalk2 = require("chalk");
325
-
326
- // src/services/metadata.ts
327
- var import_js_yaml2 = require("js-yaml");
328
-
329
- // src/utils/logger.ts
330
- var import_log2 = __toESM(require("@doc-tools/transform/lib/log"));
331
- var import_chalk = require("chalk");
332
- function writeLog(msg, fatal = false) {
333
- const { quiet } = argv_default.getConfig();
334
- if (quiet && !fatal) {
335
- return;
336
- }
337
- console.log(msg);
338
- }
339
- var logger = {
340
- info: function(pathToFile, extraMessage) {
341
- writeLog(`${(0, import_chalk.grey)("INFO")} ${extraMessage} ${pathToFile}`);
342
- },
343
- proc: function(pathToFile) {
344
- writeLog(`${(0, import_chalk.blue)("PROC")} Processing file ${pathToFile}`);
345
- },
346
- copy: function(pathToFile) {
347
- writeLog(`${(0, import_chalk.green)("COPY")} Copying file ${pathToFile}`);
348
- },
349
- upload: function(pathToFile) {
350
- writeLog(`${(0, import_chalk.green)("UPLOAD")} Uploading file ${pathToFile}`);
351
- },
352
- warn: function(pathToFile, extraMessage) {
353
- const message = `${(0, import_chalk.yellow)("WARNING")} file: ${pathToFile} error: ${extraMessage}`;
354
- writeLog(message);
355
- import_log2.default.warn(`file: ${pathToFile} ${extraMessage}`);
356
- },
357
- error: function(pathToFile, extraMessage) {
358
- const message = `${(0, import_chalk.red)("ERROR")} file: ${pathToFile} error: ${extraMessage}`;
359
- writeLog(message, true);
360
- import_log2.default.error(`file: ${pathToFile} ${extraMessage}`);
361
- }
362
- };
363
-
364
- // src/utils/markup.ts
365
- var import_process = require("process");
366
-
367
- // src/utils/singlePage.ts
368
- var import_node_html_parser = require("node-html-parser");
369
- var import_path3 = require("path");
370
- var import_utilsFS = require("@doc-tools/transform/lib/utilsFS");
371
- var import_url = __toESM(require("url"));
372
- var import_lodash = __toESM(require("lodash"));
373
-
374
- // src/utils/url.ts
375
- function isExternalHref(href) {
376
- return href.startsWith("http") || href.startsWith("//");
377
- }
378
-
379
- // src/utils/singlePage.ts
380
- var HEADERS_SELECTOR = "h1, h2, h3, h4, h5, h6";
381
- function getNewNode(options) {
382
- const { rawTagName, innerHTML, attrEntries } = options;
383
- const nodeNew = (0, import_node_html_parser.parse)(`<html><${rawTagName}></${rawTagName}></html>`).querySelector(`${rawTagName}`);
384
- if (!nodeNew) {
385
- return null;
386
- }
387
- if (attrEntries) {
388
- for (const [name4, value] of attrEntries) {
389
- nodeNew.setAttribute(name4, value);
390
- }
391
- }
392
- nodeNew.innerHTML = innerHTML;
393
- return nodeNew;
394
- }
395
- function decreaseHeadingLevels(root) {
396
- const headersSelector = "h1, h2, h3, h4, h5";
397
- root.querySelectorAll(headersSelector).forEach((node) => {
398
- const { rawTagName } = node;
399
- const newHeadingLevel = Number(rawTagName.charAt(1)) + 1;
400
- node.rawTagName = `h${newHeadingLevel}`;
401
- });
402
- }
403
- function tryFixFirstPageHeader(root) {
404
- const firstPageHeader = root.querySelector(HEADERS_SELECTOR);
405
- if (!firstPageHeader || firstPageHeader.rawTagName === "h1") {
406
- return;
407
- }
408
- firstPageHeader.rawTagName = "h1";
409
- }
410
- function replaceLinks(rootEl, options) {
411
- const { root, path, tocDir } = options;
412
- rootEl.querySelectorAll('a:not(.yfm-anchor):not([target="_blank"])').forEach((node) => {
413
- const href = node.getAttribute("href") || "";
414
- const resolvedPath = (0, import_path3.resolve)(root, path);
415
- const linkFullPath = (0, import_utilsFS.resolveRelativePath)(resolvedPath, href);
416
- const isLinkOutOfToc = !linkFullPath.startsWith(tocDir);
417
- let preparedHref = href;
418
- if (isLinkOutOfToc) {
419
- preparedHref = (0, import_path3.relative)(tocDir, linkFullPath);
420
- } else {
421
- const { pathname, hash } = import_url.default.parse(href);
422
- if (pathname) {
423
- preparedHref = getSinglePageAnchorId({
424
- root,
425
- currentPath: resolvedPath,
426
- pathname,
427
- hash
428
- });
429
- } else if (hash) {
430
- preparedHref = getSinglePageAnchorId({ root, currentPath: resolvedPath, hash });
431
- }
432
- }
433
- node.setAttribute("href", preparedHref);
434
- });
435
- }
436
- function replaceImages(rootEl, options) {
437
- const { root, path, tocDir } = options;
438
- rootEl.querySelectorAll("img").forEach((node) => {
439
- const href = node.getAttribute("src") || "";
440
- if (isExternalHref(href)) {
441
- return;
442
- }
443
- const resolvedPath = (0, import_path3.resolve)(root, path);
444
- const linkFullPath = (0, import_utilsFS.resolveRelativePath)(resolvedPath, href);
445
- const preparedHref = (0, import_path3.relative)(tocDir, linkFullPath);
446
- node.setAttribute("src", preparedHref);
447
- });
448
- }
449
- function prepareAnchorAttr(name4, value, pageId) {
450
- switch (name4) {
451
- case "href":
452
- return `#${pageId}_${value.slice(1)}`;
453
- case "id":
454
- return `${pageId}_${value}`;
455
- default:
456
- return value;
457
- }
458
- }
459
- function prepareAnchorAttrs(node, pageId) {
460
- for (const [name4, value] of Object.entries(node.attributes)) {
461
- const preparedValue = prepareAnchorAttr(name4, value, pageId);
462
- node.setAttribute(name4, preparedValue);
463
- }
464
- }
465
- function addPagePrefixToAnchors(rootEl, options) {
466
- const { root, path } = options;
467
- const resolvedPath = (0, import_path3.resolve)(root, path);
468
- const pageIdAnchor = getSinglePageAnchorId({ root, currentPath: resolvedPath });
469
- const originalArticleHref = transformLinkToOriginalArticle({ root, currentPath: resolvedPath });
470
- const pageId = pageIdAnchor.slice(1);
471
- const anchorSelector = ".yfm-anchor";
472
- rootEl.querySelectorAll(anchorSelector).forEach((node) => {
473
- prepareAnchorAttrs(node, pageId);
474
- });
475
- const mainHeader = rootEl.querySelector("h1");
476
- if (mainHeader) {
477
- const anchor = (0, import_node_html_parser.parse)(`<a class="yfm-anchor" aria-hidden="true" href="${pageIdAnchor}" id="${pageId}"></a>`);
478
- if (!anchor) {
479
- return;
480
- }
481
- mainHeader.setAttribute("data-original-article", `${originalArticleHref}.html`);
482
- mainHeader.appendChild(anchor);
483
- }
484
- rootEl.querySelectorAll(HEADERS_SELECTOR).forEach((node) => {
485
- prepareAnchorAttrs(node, pageId);
486
- });
487
- }
488
- function addMainTitle(rootEl, options) {
489
- const { title } = options;
490
- if (!title) {
491
- return;
492
- }
493
- const mainTitle = getNewNode({ innerHTML: title, rawTagName: "h1" });
494
- if (!mainTitle) {
495
- return;
496
- }
497
- rootEl.insertAdjacentHTML("afterbegin", mainTitle.toString());
498
- }
499
- function getSinglePageAnchorId(args) {
500
- const { root, currentPath, pathname, hash } = args;
501
- let resultAnchor = currentPath;
502
- if (pathname) {
503
- resultAnchor = (0, import_utilsFS.resolveRelativePath)(currentPath, pathname);
504
- }
505
- resultAnchor = resultAnchor.replace(root, "").replace(/\.(md|ya?ml|html)$/i, "").replace(new RegExp(import_lodash.default.escapeRegExp(import_path3.sep), "gi"), "_");
506
- if (hash) {
507
- resultAnchor = resultAnchor + "_" + hash.slice(1);
508
- }
509
- return `#${resultAnchor}`;
510
- }
511
- function transformLinkToOriginalArticle(opts) {
512
- const { root, currentPath } = opts;
513
- return currentPath.replace(root, "").replace(/\.(md|ya?ml|html)$/i, "");
514
- }
515
- function preprocessPageHtmlForSinglePage(content, options) {
516
- const root = (0, import_node_html_parser.parse)(content);
517
- addMainTitle(root, options);
518
- tryFixFirstPageHeader(root);
519
- addPagePrefixToAnchors(root, options);
520
- decreaseHeadingLevels(root);
521
- replaceLinks(root, options);
522
- replaceImages(root, options);
523
- return root.toString();
524
- }
525
-
526
- // src/utils/markup.ts
527
- var import_client = require("@diplodoc/client");
528
- var import_client2 = __toESM(require_client());
529
- function generateStaticMarkup(props, pathToBundle) {
530
- const { title: metaTitle, style, script } = props.data.meta || {};
531
- const { title: tocTitle } = props.data.toc;
532
- const { title: pageTitle } = props.data;
533
- const title = getTitle({
534
- metaTitle,
535
- tocTitle,
536
- pageTitle
537
- });
538
- const resources = getResources({ style, script });
539
- const { staticContent } = argv_default.getConfig();
540
- const html = staticContent ? (0, import_client.render)(props) : "";
541
- return `
542
- <!DOCTYPE html>
543
- <html lang="${props.lang}">
544
- <head>
545
- <meta charset="utf-8">
546
- ${getMetadata(props.data.meta)}
547
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
548
- <title>${title}</title>
549
- <style type="text/css">
550
- body {
551
- height: 100vh;
552
- }
553
- </style>
554
- <link type="text/css" rel="stylesheet" href="${import_client2.default.bundle.css(pathToBundle)}" />
555
- ${plugins_exports.getHeadContent()}
556
- ${resources}
557
- </head>
558
- <body class="yc-root yc-root_theme_light">
559
- <div id="root">${html}</div>
560
- <script type="application/javascript">
561
- window.STATIC_CONTENT = ${staticContent}
562
- window.__DATA__ = ${JSON.stringify(props)};
563
- </script>
564
- <script type="application/javascript" src="${import_client2.default.bundle.js(pathToBundle)}"></script>
565
- </body>
566
- </html>
567
- `;
568
- }
569
- function getTitle({ tocTitle, metaTitle, pageTitle }) {
570
- const resultPageTitle = metaTitle || pageTitle;
571
- if (!resultPageTitle && tocTitle) {
572
- return tocTitle;
573
- }
574
- if (resultPageTitle && !tocTitle) {
575
- return resultPageTitle;
576
- }
577
- return resultPageTitle && tocTitle ? `${resultPageTitle} | ${tocTitle}` : "";
578
- }
579
- function getMetadata(metadata) {
580
- if (!metadata) {
581
- return "";
582
- }
583
- const metaEntries = Object.entries(metadata).filter(([key]) => !Object.keys(ResourceType).includes(key));
584
- return metaEntries.map(([name4, content]) => {
585
- return `<meta name="${name4}" content="${content}">`;
586
- }).join("\n");
587
- }
588
- function getResources({ style, script }) {
589
- const resourcesTags = [];
590
- if (style) {
591
- style.forEach((el, id) => resourcesTags.push(
592
- `<link rel="stylesheet" type="text/css" href="${el}" ${id === 0 && `id="${CUSTOM_STYLE}"`}>`
593
- ));
594
- }
595
- if (script) {
596
- script.forEach((el) => resourcesTags.push(
597
- `<script src="${el}"></script>`
598
- ));
599
- }
600
- return resourcesTags.join("\n");
601
- }
602
- var \u0441arriage = import_process.platform === "win32" /* WINDOWS */ ? "\r\n" : "\n";
603
- function joinSinglePageResults(singlePageResults2, root, tocDir) {
604
- const delimeter = `${\u0441arriage}${\u0441arriage}<hr class="yfm-page__delimeter">${\u0441arriage}${\u0441arriage}`;
605
- return singlePageResults2.filter(({ content }) => content).map(({ content, path, title }) => preprocessPageHtmlForSinglePage(content, { root, path, tocDir, title })).join(delimeter);
606
- }
607
- function replaceDoubleToSingleQuotes(str) {
608
- return str.replace(/"/g, "'");
609
- }
610
-
611
- // src/utils/path.ts
612
- var import_path4 = require("path");
613
- function addSlashPrefix(path) {
614
- const slashPrefix = path.startsWith(import_path4.sep) ? "" : import_path4.sep;
615
- return `${slashPrefix}${path}`;
616
- }
617
- function convertBackSlashToSlash(path) {
618
- if (process.platform === "win32" /* WINDOWS */) {
619
- return path.replace(/\\/g, "/");
620
- }
621
- return path;
622
- }
623
-
624
- // src/utils/toc.ts
625
- var import_path5 = require("path");
626
-
627
- // src/services/utils.ts
628
- var import_evaluation = __toESM(require("@doc-tools/transform/lib/liquid/evaluation"));
629
- var import_liquid = __toESM(require("@doc-tools/transform/lib/liquid"));
630
- function filterFiles(items, itemsKey, vars, options) {
631
- if (!Array.isArray(items)) {
632
- return [];
633
- }
634
- const reducer = (results, item) => {
635
- if (shouldProcessItem(item, vars, options)) {
636
- const prop = item[itemsKey];
637
- if (prop) {
638
- const filteredProperty = filterFiles(prop, itemsKey, vars, options);
639
- if (filteredProperty.length) {
640
- results.push(__spreadProps(__spreadValues({}, item), {
641
- [itemsKey]: filteredProperty
642
- }));
643
- }
644
- } else {
645
- results.push(item);
646
- }
647
- }
648
- return results;
649
- };
650
- return items.reduce(reducer, []);
651
- }
652
- function filterTextItems(items, vars, options) {
653
- if (!Array.isArray(items)) {
654
- return items;
655
- }
656
- return items.reduce((result, item) => {
657
- if (!isObject(item)) {
658
- result.push(item);
659
- return result;
660
- }
661
- const useItem = shouldProcessItem(item, vars, options);
662
- if (useItem) {
663
- if (Array.isArray(item.text)) {
664
- result.push(...item.text);
665
- } else {
666
- result.push(item.text);
667
- }
668
- }
669
- return result;
670
- }, []);
671
- }
672
- function firstFilterTextItems(items, vars, options) {
673
- const filteredItems = filterTextItems(items, vars, options);
674
- if (!Array.isArray(filteredItems)) {
675
- return filteredItems || "";
676
- }
677
- return filteredItems[0] || "";
678
- }
679
- function shouldProcessItem(item, vars, options) {
680
- const { resolveConditions, removeHiddenTocItems } = options || {};
681
- let useItem = true;
682
- if (resolveConditions) {
683
- const { when } = item;
684
- useItem = when === true || when === void 0 || typeof when === "string" && (0, import_evaluation.default)(when, vars);
685
- delete item.when;
686
- }
687
- if (useItem && removeHiddenTocItems) {
688
- useItem = !item.hidden;
689
- delete item.hidden;
690
- }
691
- return useItem;
692
- }
693
- function liquidFields(fields, vars, path) {
694
- if (typeof fields === "string") {
695
- return liquidField(fields, vars, path);
696
- }
697
- if (!Array.isArray(fields)) {
698
- return fields;
699
- }
700
- return fields.map((item) => {
701
- if (typeof item === "string") {
702
- return liquidField(item, vars, path);
703
- }
704
- return item;
705
- });
706
- }
707
- function liquidField(input, vars, path) {
708
- const { applyPresets, resolveConditions } = argv_default.getConfig();
709
- if (!applyPresets && !resolveConditions) {
710
- return input;
711
- }
712
- return (0, import_liquid.default)(input, vars, path, {
713
- substitutions: applyPresets,
714
- conditions: resolveConditions,
715
- keepNotVar: true,
716
- withSourceMap: false
717
- });
718
- }
719
- function isObject(o) {
720
- return typeof o === "object" && o !== null;
721
- }
722
-
723
- // src/utils/toc.ts
724
- function transformToc(toc, pathToFileDirectory) {
725
- if (!toc) {
726
- return null;
727
- }
728
- const localToc = JSON.parse(JSON.stringify(toc));
729
- if (localToc.items) {
730
- localToc.items = filterFiles(localToc.items, "items", {}, {
731
- removeHiddenTocItems: true
732
- });
733
- }
734
- const baseTocPath = localToc.base || "";
735
- const navigationItemQueue = [localToc];
736
- while (navigationItemQueue.length) {
737
- const navigationItem = navigationItemQueue.shift();
738
- if (!navigationItem) {
739
- continue;
740
- }
741
- const { items, href } = navigationItem;
742
- if (items) {
743
- navigationItemQueue.push(...navigationItem.items);
744
- }
745
- if (href && !isExternalHref(href)) {
746
- const pathToIndexDirectory = (0, import_path5.relative)(pathToFileDirectory, baseTocPath);
747
- const fileExtension = (0, import_path5.extname)(href);
748
- const filename = (0, import_path5.basename)(href, fileExtension);
749
- const transformedFilename = (0, import_path5.format)({
750
- name: filename,
751
- ext: ".html"
752
- });
753
- navigationItem.href = (0, import_path5.join)(pathToIndexDirectory, (0, import_path5.dirname)(href), transformedFilename);
754
- }
755
- }
756
- return localToc;
757
- }
758
- function transformTocForSinglePage(toc, options) {
759
- const { root, currentPath } = options;
760
- if (!toc) {
761
- return null;
762
- }
763
- const localToc = JSON.parse(JSON.stringify(toc));
764
- if (localToc.items) {
765
- localToc.items = filterFiles(localToc.items, "items", {}, {
766
- removeHiddenTocItems: true
767
- });
768
- }
769
- function processItems(items) {
770
- items.forEach((item) => {
771
- if (item.items) {
772
- processItems(item.items);
773
- }
774
- if (item.href && !isExternalHref(item.href)) {
775
- item.href = getSinglePageAnchorId({ root, currentPath, pathname: item.href });
776
- }
777
- });
778
- }
779
- processItems(localToc.items);
780
- localToc.singlePage = true;
781
- return localToc;
782
- }
783
-
784
- // src/utils/presets.ts
785
- var import_path6 = require("path");
786
- function getVarsPerFile(filePath) {
787
- const { vars: argVars } = argv_default.getConfig();
788
- return __spreadValues(__spreadValues({}, preset_default.get((0, import_path6.dirname)(filePath))), argVars);
789
- }
790
- function getVarsPerRelativeFile(filePath) {
791
- const { input } = argv_default.getConfig();
792
- const root = (0, import_path6.resolve)(input);
793
- const relativeFilePath = (0, import_path6.relative)(root, filePath);
794
- return getVarsPerFile(relativeFilePath);
795
- }
796
-
797
- // src/utils/glob.ts
798
- var import_glob = __toESM(require("glob"));
799
- var glob = (pattern, options) => __async(void 0, null, function* () {
800
- return new Promise((res, rej) => {
801
- const state = (0, import_glob.default)(
802
- pattern,
803
- options,
804
- (err) => err ? rej(err) : res({ state })
805
- );
806
- });
807
- });
808
-
809
- // src/utils/file.ts
810
- var import_path7 = require("path");
811
- var import_shelljs = __toESM(require("shelljs"));
812
- var import_fs2 = require("fs");
813
- function copyFiles(inputFolderPath, outputFolderPath, files) {
814
- for (const pathToAsset of files) {
815
- const outputDir = (0, import_path7.resolve)(outputFolderPath, (0, import_path7.dirname)(pathToAsset));
816
- const from = (0, import_path7.resolve)(inputFolderPath, pathToAsset);
817
- const to = (0, import_path7.resolve)(outputFolderPath, pathToAsset);
818
- import_shelljs.default.mkdir("-p", outputDir);
819
- (0, import_fs2.copyFileSync)(from, to);
820
- logger.copy(pathToAsset);
821
- }
822
- }
823
-
824
- // src/services/authors.ts
825
- function updateAuthorMetadataStringByAuthorLogin(authorLogin, vcsConnector) {
826
- return __async(this, null, function* () {
827
- if (!vcsConnector) {
828
- return "";
829
- }
830
- const user = yield getAuthorDetails(vcsConnector, authorLogin);
831
- if (user) {
832
- return user;
833
- }
834
- return "";
835
- });
836
- }
837
- function updateAuthorMetadataStringByFilePath(filePath, vcsConnector) {
838
- return __async(this, null, function* () {
839
- if (!vcsConnector) {
840
- return "";
841
- }
842
- const user = vcsConnector.getExternalAuthorByPath(filePath);
843
- if (user) {
844
- const author = replaceDoubleToSingleQuotes(JSON.stringify(user));
845
- return author;
846
- }
847
- return "";
848
- });
849
- }
850
- function getAuthorDetails(vcsConnector, author) {
851
- return __async(this, null, function* () {
852
- if (typeof author === "object") {
853
- return replaceDoubleToSingleQuotes(JSON.stringify(author));
854
- }
855
- try {
856
- JSON.parse(author);
857
- return replaceDoubleToSingleQuotes(author);
858
- } catch (e) {
859
- const user = yield vcsConnector.getUserByLogin(author);
860
- if (user) {
861
- return replaceDoubleToSingleQuotes(JSON.stringify(user));
862
- }
863
- return null;
864
- }
865
- });
866
- }
867
-
868
- // src/services/contributors.ts
869
- var import_promises = require("fs/promises");
870
- var import_path8 = require("path");
871
- function getFileContributorsMetadata(fileData, vcsConnector) {
872
- return __async(this, null, function* () {
873
- const contributors = yield getFileContributorsString(fileData, vcsConnector);
874
- return `contributors: ${contributors}`;
875
- });
876
- }
877
- function getFileContributorsString(fileData, vcsConnector) {
878
- return __async(this, null, function* () {
879
- const { tmpInputFilePath, inputFolderPathLength } = fileData;
880
- const relativeFilePath = tmpInputFilePath.substring(inputFolderPathLength);
881
- const fileContributors = yield vcsConnector.getContributorsByPath(relativeFilePath);
882
- let nestedContributors = {};
883
- if (!fileContributors.hasIncludes) {
884
- nestedContributors = yield getContributorsForNestedFiles(fileData, vcsConnector);
885
- vcsConnector.addNestedContributorsForPath(relativeFilePath, nestedContributors);
886
- }
887
- const fileContributorsWithContributorsIncludedFiles = __spreadValues(__spreadValues({}, fileContributors.contributors), nestedContributors);
888
- const contributorsArray = Object.entries(fileContributorsWithContributorsIncludedFiles).map(([, contributor]) => contributor);
889
- return replaceDoubleToSingleQuotes(JSON.stringify(contributorsArray));
890
- });
891
- }
892
- function getContributorsForNestedFiles(fileData, vcsConnector) {
893
- return __async(this, null, function* () {
894
- const { fileContent, inputFolderPathLength } = fileData;
895
- const includeContents = fileContent.match(REGEXP_INCLUDE_CONTENTS);
896
- if (!includeContents || includeContents.length === 0) {
897
- return {};
898
- }
899
- const includesContributors = [];
900
- const relativeIncludeFilePaths = getRelativeIncludeFilePaths(fileData, includeContents);
901
- for (const relativeIncludeFilePath of relativeIncludeFilePaths.values()) {
902
- const relativeFilePath = relativeIncludeFilePath.substring(inputFolderPathLength);
903
- const includeContributors = yield vcsConnector.getContributorsByPath(relativeFilePath);
904
- let nestedContributors = {};
905
- if (!includeContributors.hasIncludes) {
906
- let contentIncludeFile;
907
- try {
908
- contentIncludeFile = yield (0, import_promises.readFile)(relativeIncludeFilePath, "utf8");
909
- } catch (err) {
910
- if (err.code === "ENOENT") {
911
- continue;
912
- }
913
- throw err;
914
- }
915
- const newFileData = __spreadProps(__spreadValues({}, fileData), {
916
- fileContent: contentIncludeFile,
917
- tmpInputFilePath: relativeIncludeFilePath
918
- });
919
- nestedContributors = yield getContributorsForNestedFiles(newFileData, vcsConnector);
920
- vcsConnector.addNestedContributorsForPath(relativeFilePath, nestedContributors);
921
- }
922
- includesContributors.push(includeContributors.contributors);
923
- includesContributors.push(nestedContributors);
924
- }
925
- return Object.assign({}, ...includesContributors);
926
- });
927
- }
928
- function getRelativeIncludeFilePaths(fileData, includeContents) {
929
- const { tmpInputFilePath } = fileData;
930
- const relativeIncludeFilePaths = /* @__PURE__ */ new Set();
931
- includeContents.forEach((includeContent) => {
932
- const relativeIncludeFilePath = includeContent.match(REGEXP_INCLUDE_FILE_PATH);
933
- if (relativeIncludeFilePath && relativeIncludeFilePath.length !== 0) {
934
- const relativeIncludeFilePathWithoutHash = relativeIncludeFilePath[0].split("#");
935
- const includeFilePath = (0, import_path8.join)((0, import_path8.dirname)(tmpInputFilePath), relativeIncludeFilePathWithoutHash[0]);
936
- relativeIncludeFilePaths.add(includeFilePath);
937
- }
938
- });
939
- return relativeIncludeFilePaths;
940
- }
941
-
942
- // src/services/metadata.ts
943
- var import_path9 = require("path");
944
- function getContentWithUpdatedMetadata(fileContent, options, systemVars) {
945
- return __async(this, null, function* () {
946
- var _a;
947
- let result;
948
- result = getContentWithUpdatedStaticMetadata({
949
- fileContent,
950
- sourcePath: (_a = options == null ? void 0 : options.fileData) == null ? void 0 : _a.sourcePath,
951
- addSystemMeta: options == null ? void 0 : options.addSystemMeta,
952
- addSourcePath: options == null ? void 0 : options.addSourcePath,
953
- resources: options == null ? void 0 : options.resources,
954
- systemVars
955
- });
956
- result = yield getContentWithUpdatedDynamicMetadata(result, options);
957
- return result;
958
- });
959
- }
960
- function getContentWithUpdatedStaticMetadata({
961
- fileContent,
962
- sourcePath,
963
- addSystemMeta,
964
- addSourcePath,
965
- resources,
966
- systemVars
967
- }) {
968
- const newMetadatas = [];
969
- if ((!addSystemMeta || !systemVars) && !addSourcePath && !resources) {
970
- return fileContent;
971
- }
972
- const matches = matchMetadata(fileContent);
973
- if (addSystemMeta && systemVars && isObject(systemVars)) {
974
- newMetadatas.push(getSystemVarsMetadataString(systemVars));
975
- }
976
- if (resources) {
977
- newMetadatas.push((0, import_js_yaml2.dump)(resources));
978
- }
979
- if (addSourcePath && sourcePath) {
980
- const sourcePathMetadataString = `sourcePath: ${sourcePath}`;
981
- newMetadatas.push(sourcePathMetadataString);
982
- }
983
- if (matches && matches.length > 0) {
984
- const [, fileMetadata, , fileMainContent] = matches;
985
- return `${getUpdatedMetadataString(newMetadatas, fileMetadata)}${fileMainContent}`;
986
- }
987
- return `${getUpdatedMetadataString(newMetadatas)}${fileContent}`;
988
- }
989
- function getContentWithUpdatedDynamicMetadata(fileContent, options) {
990
- return __async(this, null, function* () {
991
- if (!options || !(options == null ? void 0 : options.isContributorsEnabled)) {
992
- return fileContent;
993
- }
994
- let fileMetadata, fileMainContent;
995
- const matches = matchMetadata(fileContent);
996
- if (matches && matches.length > 0) {
997
- const [, matchedFileMetadata, , matchedFileMainContent] = matches;
998
- fileMetadata = matchedFileMetadata;
999
- fileMainContent = matchedFileMainContent;
1000
- }
1001
- const newMetadatas = [];
1002
- const { isContributorsEnabled } = options;
1003
- if (isContributorsEnabled) {
1004
- const contributorsMetaData = yield getContributorsMetadataString(options, fileContent);
1005
- if (contributorsMetaData) {
1006
- newMetadatas.push(contributorsMetaData);
1007
- }
1008
- let authorMetadata = "";
1009
- if (fileMetadata) {
1010
- const matchAuthor = fileMetadata.match(REGEXP_AUTHOR);
1011
- if (matchAuthor) {
1012
- const matchedAuthor = matchAuthor[0];
1013
- authorMetadata = yield updateAuthorMetadataStringByAuthorLogin(matchedAuthor, options.vcsConnector);
1014
- }
1015
- }
1016
- if (!authorMetadata) {
1017
- const { fileData: { tmpInputFilePath, inputFolderPathLength } } = options;
1018
- const relativeFilePath = tmpInputFilePath.substring(inputFolderPathLength);
1019
- authorMetadata = yield updateAuthorMetadataStringByFilePath(relativeFilePath, options.vcsConnector);
1020
- }
1021
- if (authorMetadata) {
1022
- newMetadatas.push(`author: ${authorMetadata}`);
1023
- }
1024
- }
1025
- if (fileMetadata && fileMainContent) {
1026
- let updatedFileMetadata = fileMetadata;
1027
- const matchAuthor = fileMetadata.match(REGEXP_AUTHOR);
1028
- const isNewMetadataIncludesAuthor = newMetadatas.some((item) => /^author: /.test(item));
1029
- if (matchAuthor && isNewMetadataIncludesAuthor) {
1030
- updatedFileMetadata = updatedFileMetadata.replace(`author: ${matchAuthor[0]}`, "");
1031
- }
1032
- return `${getUpdatedMetadataString(newMetadatas, updatedFileMetadata)}${fileMainContent}`;
1033
- }
1034
- return `${getUpdatedMetadataString(newMetadatas)}${fileContent}`;
1035
- });
1036
- }
1037
- function matchMetadata(fileContent) {
1038
- if (!fileContent.startsWith("---")) {
1039
- return null;
1040
- }
1041
- const regexpMetadata = "(?<=-{3}\\r?\\n)((.*\\r?\\n)*?)(?=-{3}\\r?\\n)";
1042
- const regexpFileContent = "-{3}((.*[\r?\n]*)*)";
1043
- const regexpParseFileContent = new RegExp(`${regexpMetadata}${regexpFileContent}`, "gm");
1044
- return regexpParseFileContent.exec(fileContent);
1045
- }
1046
- function getContributorsMetadataString(options, fileContent) {
1047
- return __async(this, null, function* () {
1048
- const { isContributorsEnabled, vcsConnector, fileData } = options;
1049
- if (isContributorsEnabled && vcsConnector) {
1050
- const updatedFileData = __spreadProps(__spreadValues({}, fileData), {
1051
- fileContent
1052
- });
1053
- return getFileContributorsMetadata(updatedFileData, vcsConnector);
1054
- }
1055
- return void 0;
1056
- });
1057
- }
1058
- function getUpdatedMetadataString(newMetadatas, defaultMetadata = "") {
1059
- const newMetadata = newMetadatas.join(\u0441arriage) + (newMetadatas.length ? \u0441arriage : "");
1060
- const preparedDefaultMetadata = defaultMetadata.trimRight();
1061
- const defaultMetadata\u0421arriage = preparedDefaultMetadata ? \u0441arriage : "";
1062
- const updatedMetadata = `${preparedDefaultMetadata}${defaultMetadata\u0421arriage}${newMetadata}`;
1063
- return `${metadataBorder}${\u0441arriage}${updatedMetadata}${metadataBorder}${defaultMetadata.length ? "" : \u0441arriage}`;
1064
- }
1065
- function getUpdatedMetadata(options, fileContent, meta2) {
1066
- return __async(this, null, function* () {
1067
- const { vcsConnector } = options;
1068
- const newMetadata = {
1069
- contributors: yield getContributorsMetadata(options, fileContent)
1070
- };
1071
- if (!meta2) {
1072
- return newMetadata;
1073
- }
1074
- const updatedAuthor = yield getAuthorMetadata(meta2, vcsConnector);
1075
- return __spreadProps(__spreadValues(__spreadValues({}, meta2), newMetadata), {
1076
- author: updatedAuthor
1077
- });
1078
- });
1079
- }
1080
- function getContributorsMetadata(options, fileContent) {
1081
- return __async(this, null, function* () {
1082
- const { isContributorsEnabled, vcsConnector, fileData } = options;
1083
- if (isContributorsEnabled && vcsConnector) {
1084
- const updatedFileData = __spreadProps(__spreadValues({}, fileData), {
1085
- fileContent
1086
- });
1087
- return getFileContributorsString(updatedFileData, vcsConnector);
1088
- }
1089
- return JSON.stringify([]);
1090
- });
1091
- }
1092
- function getAuthorMetadata(meta2, vcsConnector) {
1093
- return __async(this, null, function* () {
1094
- if (meta2.author && vcsConnector) {
1095
- const updatedAuthor = yield getAuthorDetails(vcsConnector, meta2.author);
1096
- return updatedAuthor;
1097
- }
1098
- return null;
1099
- });
1100
- }
1101
- function getSystemVarsMetadataString(systemVars) {
1102
- return `__system: ${JSON.stringify(systemVars)}`;
1103
- }
1104
- function getAssetsPublicPath(filePath) {
1105
- const { input } = argv_default.getConfig();
1106
- const path = (0, import_path9.resolve)(input, filePath);
1107
- return (0, import_path9.relative)((0, import_path9.dirname)(path), (0, import_path9.resolve)(input));
1108
- }
1109
-
1110
- // src/services/includers/index.ts
1111
- var includers_exports = {};
1112
- __export(includers_exports, {
1113
- IncludersError: () => IncludersError,
1114
- applyIncluders: () => applyIncluders,
1115
- init: () => init
1116
- });
1117
- var import_path12 = require("path");
1118
- var import_lodash3 = require("lodash");
1119
-
1120
- // src/services/includers/batteries/generic.ts
1121
- var generic_exports = {};
1122
- __export(generic_exports, {
1123
- default: () => generic_default,
1124
- includerFunction: () => includerFunction,
1125
- name: () => name
1126
- });
1127
- var import_promises2 = require("fs/promises");
1128
- var import_path10 = require("path");
1129
- var import_lodash2 = require("lodash");
1130
- var import_js_yaml3 = require("js-yaml");
1131
- var GenericIncluderError = class extends Error {
1132
- constructor(message, path) {
1133
- super(message);
1134
- this.name = "GenericIncluderError";
1135
- this.path = path;
1136
- }
1137
- };
1138
- var name = "generic";
1139
- var MD_GLOB = "**/*.md";
1140
- function includerFunction(params) {
1141
- return __async(this, null, function* () {
1142
- var _a, _b;
1143
- const { readBasePath, writeBasePath, tocPath, item, passedParams: { input, leadingPage }, index } = params;
1144
- if (!(input == null ? void 0 : input.length) || !((_a = item.include) == null ? void 0 : _a.path)) {
1145
- throw new GenericIncluderError("provide includer with input parameter", tocPath);
1146
- }
1147
- try {
1148
- const leadingPageName = (_b = leadingPage == null ? void 0 : leadingPage.name) != null ? _b : "Overview";
1149
- const tocDirPath = (0, import_path10.dirname)(tocPath);
1150
- const contentPath = index === 0 ? (0, import_path10.join)(writeBasePath, tocDirPath, input) : (0, import_path10.join)(readBasePath, tocDirPath, input);
1151
- let cache = {};
1152
- let found = [];
1153
- ({ state: { found, cache } } = yield glob(MD_GLOB, {
1154
- cwd: contentPath,
1155
- nosort: true,
1156
- nocase: true,
1157
- cache
1158
- }));
1159
- const writePath = (0, import_path10.join)(writeBasePath, tocDirPath, item.include.path);
1160
- yield (0, import_promises2.mkdir)(writePath, { recursive: true });
1161
- for (const filePath of found) {
1162
- const file = yield (0, import_promises2.readFile)((0, import_path10.join)(contentPath, filePath));
1163
- yield (0, import_promises2.mkdir)((0, import_path10.dirname)((0, import_path10.join)(writePath, filePath)), { recursive: true });
1164
- yield (0, import_promises2.writeFile)((0, import_path10.join)(writePath, filePath), file);
1165
- }
1166
- const graph = createGraphFromPaths(found);
1167
- const toc = createToc(leadingPageName, item.include.path)(graph, []);
1168
- yield (0, import_promises2.writeFile)((0, import_path10.join)(writePath, "toc.yaml"), (0, import_js_yaml3.dump)(toc));
1169
- } catch (err) {
1170
- throw new GenericIncluderError(err.toString(), tocPath);
1171
- }
1172
- });
1173
- }
1174
- function createGraphFromPaths(paths) {
1175
- const graph = {};
1176
- for (const path of paths) {
1177
- const chunks = path.split("/").filter(Boolean);
1178
- if (chunks.length < 2) {
1179
- if (chunks.length === 1) {
1180
- graph.files = chunks;
1181
- }
1182
- continue;
1183
- }
1184
- const file = chunks.pop();
1185
- (0, import_lodash2.updateWith)(graph, chunks, (old) => {
1186
- return old ? { files: [...old.files, file] } : { files: [file] };
1187
- }, Object);
1188
- }
1189
- return graph;
1190
- }
1191
- function createToc(leadingPageName, tocName) {
1192
- return function createTocRec(graph, cursor) {
1193
- var _a, _b;
1194
- const handler2 = (file) => ({
1195
- name: (0, import_path10.parse)(file).name === "index" ? leadingPageName : file,
1196
- href: (0, import_path10.join)(...cursor, file)
1197
- });
1198
- const recurse = (key) => createTocRec(graph[key], [...cursor, key]);
1199
- const current = {
1200
- name: (_a = cursor[cursor.length - 1]) != null ? _a : tocName,
1201
- items: [
1202
- ...((_b = graph.files) != null ? _b : []).map(handler2),
1203
- ...Object.keys(graph).filter((key) => key !== "files").map(recurse)
1204
- ]
1205
- };
1206
- return current;
1207
- };
1208
- }
1209
- var generic_default = { name, includerFunction };
1210
-
1211
- // src/services/includers/batteries/sourcedocs.ts
1212
- var sourcedocs_exports = {};
1213
- __export(sourcedocs_exports, {
1214
- default: () => sourcedocs_default,
1215
- includerFunction: () => includerFunction2,
1216
- name: () => name2
1217
- });
1218
- var name2 = "sourcedocs";
1219
- var usage = `include:
1220
- path: <path-where-to-include>
1221
- includers:
1222
- - name: generic
1223
- input: <path-to-directory-with-markdown>
1224
- leadingPage:
1225
- name: <leading-page-name>
1226
- `;
1227
- function includerFunction2(params) {
1228
- return __async(this, null, function* () {
1229
- logger.warn(params.tocPath, `sourcedocs inlcuder is getting depricated in favor of generic includer
1230
- ${usage}`);
1231
- yield generic_default.includerFunction(params);
1232
- });
1233
- }
1234
- var sourcedocs_default = { name: name2, includerFunction: includerFunction2 };
1235
-
1236
- // src/services/includers/batteries/unarchive.ts
1237
- var unarchive_exports = {};
1238
- __export(unarchive_exports, {
1239
- default: () => unarchive_default,
1240
- includerFunction: () => includerFunction3,
1241
- name: () => name3
1242
- });
1243
- var import_fs3 = require("fs");
1244
- var import_path11 = require("path");
1245
- var import_tar_stream = require("tar-stream");
1246
- var name3 = "unarchive";
1247
- var UnarchiveIncluderError = class extends Error {
1248
- constructor(message, path) {
1249
- super(message);
1250
- this.name = "UnarchiveIncluderError";
1251
- this.path = path;
1252
- }
1253
- };
1254
- function pipeline(readPath, writeBasePath) {
1255
- return new Promise((res, rej) => {
1256
- const reader = (0, import_fs3.createReadStream)(readPath);
1257
- reader.on("error", (err) => {
1258
- rej(err);
1259
- });
1260
- const extractor = (0, import_tar_stream.extract)();
1261
- extractor.on("error", (err) => {
1262
- rej(err);
1263
- });
1264
- (0, import_fs3.mkdirSync)(writeBasePath, { recursive: true });
1265
- extractor.on("entry", (header, stream, next) => {
1266
- const { type, name: name4 } = header;
1267
- const writePath = (0, import_path11.join)(writeBasePath, name4);
1268
- const writeDirPath = type === "directory" ? writePath : (0, import_path11.dirname)(writePath);
1269
- (0, import_fs3.mkdirSync)(writeDirPath, { recursive: true });
1270
- if (type !== "directory") {
1271
- const writer = (0, import_fs3.createWriteStream)(writePath, { flags: "w" });
1272
- writer.on("error", (err) => {
1273
- rej(err);
1274
- });
1275
- stream.pipe(writer);
1276
- }
1277
- stream.on("end", () => {
1278
- next();
1279
- });
1280
- stream.resume();
1281
- });
1282
- reader.pipe(extractor).on("finish", () => {
1283
- res();
1284
- });
1285
- });
1286
- }
1287
- function includerFunction3(params) {
1288
- return __async(this, null, function* () {
1289
- const { readBasePath, writeBasePath, tocPath, passedParams: { input, output }, index } = params;
1290
- if (!(input == null ? void 0 : input.length) || !(output == null ? void 0 : output.length)) {
1291
- throw new UnarchiveIncluderError("provide includer with input parameter", tocPath);
1292
- }
1293
- const contentPath = index === 0 ? (0, import_path11.join)(writeBasePath, input) : (0, import_path11.join)(readBasePath, input);
1294
- const writePath = (0, import_path11.join)(writeBasePath, output);
1295
- try {
1296
- yield pipeline(contentPath, writePath);
1297
- } catch (err) {
1298
- throw new UnarchiveIncluderError(err.toString(), tocPath);
1299
- }
1300
- });
1301
- }
1302
- var unarchive_default = { name: name3, includerFunction: includerFunction3 };
1303
-
1304
- // src/services/includers/index.ts
1305
- var includersUsage = `include:
1306
- path: <path-where-to-include>
1307
- includers:
1308
- - name: <includer-name-0>
1309
- <includer-parameter>: <value-for-includer-parameter>
1310
- - name: <includer-name-1>
1311
- <includer-parameter>: <value-for-includer-parameter>
1312
- `;
1313
- var includersMap;
1314
- var IncludersError = class extends Error {
1315
- constructor(message, path) {
1316
- super(message);
1317
- this.name = "IncludersError";
1318
- this.path = path;
1319
- }
1320
- };
1321
- function init(custom = []) {
1322
- if (includersMap) {
1323
- return;
1324
- }
1325
- includersMap = { generic: generic_exports, sourcedocs: sourcedocs_exports, unarchive: unarchive_exports };
1326
- for (const includer of custom) {
1327
- includersMap[includer.name] = includer;
1328
- }
1329
- }
1330
- function applyIncluders(path, item, vars) {
1331
- return __async(this, null, function* () {
1332
- var _a;
1333
- if (!((_a = item.include) == null ? void 0 : _a.includers)) {
1334
- return;
1335
- }
1336
- if (!includeValid(item.include)) {
1337
- throw new IncludersError("include doesn't comply with includers standard", path);
1338
- }
1339
- item.include.mode = "link" /* LINK */;
1340
- const { status, message } = includersValid(item.include.includers);
1341
- if (!status) {
1342
- throw new IncludersError(message != null ? message : "", path);
1343
- }
1344
- let index = 0;
1345
- for (const _b of item.include.includers) {
1346
- const _c = _b, { name: name4 } = _c, rest = __objRest(_c, ["name"]);
1347
- const includer = getIncluder(name4);
1348
- const passedParams = __spreadValues({}, rest);
1349
- yield applyIncluder({ path, item, includer, passedParams, index, vars });
1350
- }
1351
- item.include.path = (0, import_path12.join)(item.include.path, "toc.yaml");
1352
- index++;
1353
- });
1354
- }
1355
- function includeValid(include) {
1356
- var _a;
1357
- return (include.mode === "link" /* LINK */ || !include.mode) && ((_a = include.path) == null ? void 0 : _a.length);
1358
- }
1359
- function includersValid(includers) {
1360
- for (const includer of includers) {
1361
- const { status, message } = includerValid(includer);
1362
- if (!status) {
1363
- return { status, message };
1364
- }
1365
- }
1366
- return { status: true };
1367
- }
1368
- function includerValid(includer) {
1369
- if ((0, import_lodash3.isObject)(includer)) {
1370
- if (typeof includer.name !== "string") {
1371
- return {
1372
- status: false,
1373
- message: "use string in the includer.name to specify includers name"
1374
- };
1375
- }
1376
- if (includerExists(includer)) {
1377
- return { status: true };
1378
- }
1379
- return { status: false, message: `includer ${includer.name} not implemented` };
1380
- }
1381
- return {
1382
- status: false,
1383
- message: `use appropriate includers format:
1384
- ${includersUsage}`
1385
- };
1386
- }
1387
- function getIncluder(includerName) {
1388
- return includersMap[includerName];
1389
- }
1390
- function includerExists(includer) {
1391
- return includersMap[includer.name];
1392
- }
1393
- function applyIncluder(args) {
1394
- return __async(this, null, function* () {
1395
- const { rootInput: readBasePath, input: writeBasePath } = argv_default.getConfig();
1396
- const { path, item, includer, passedParams, index, vars } = args;
1397
- const params = {
1398
- tocPath: path,
1399
- passedParams,
1400
- index,
1401
- item,
1402
- readBasePath,
1403
- writeBasePath,
1404
- vars
1405
- };
1406
- return yield includer.includerFunction(params);
1407
- });
1408
- }
1409
-
1410
- // src/services/tocs.ts
1411
- var storage = /* @__PURE__ */ new Map();
1412
- var navigationPaths = [];
1413
- var includedTocPaths = /* @__PURE__ */ new Set();
1414
- function add(path) {
1415
- return __async(this, null, function* () {
1416
- const {
1417
- input: inputFolderPath,
1418
- output: outputFolderPath,
1419
- outputFormat,
1420
- ignoreStage,
1421
- vars
1422
- } = argv_default.getConfig();
1423
- const pathToDir = (0, import_path13.dirname)(path);
1424
- const content = (0, import_fs4.readFileSync)((0, import_path13.resolve)(inputFolderPath, path), "utf8");
1425
- const parsedToc = (0, import_js_yaml4.load)(content);
1426
- if (parsedToc.stage === ignoreStage) {
1427
- return;
1428
- }
1429
- const combinedVars = __spreadValues(__spreadValues({}, preset_default.get(pathToDir)), vars);
1430
- if (parsedToc.title) {
1431
- parsedToc.title = firstFilterTextItems(
1432
- parsedToc.title,
1433
- combinedVars,
1434
- { resolveConditions: true }
1435
- );
1436
- }
1437
- if (typeof parsedToc.title === "string") {
1438
- parsedToc.title = liquidField(parsedToc.title, combinedVars, path);
1439
- }
1440
- parsedToc.items = yield processTocItems(
1441
- path,
1442
- parsedToc.items,
1443
- (0, import_path13.join)(inputFolderPath, pathToDir),
1444
- (0, import_path13.resolve)(inputFolderPath),
1445
- combinedVars
1446
- );
1447
- storage.set(path, parsedToc);
1448
- parsedToc.base = pathToDir;
1449
- if (outputFormat === "md") {
1450
- const outputPath = (0, import_path13.resolve)(outputFolderPath, path);
1451
- const outputToc = (0, import_js_yaml4.dump)(parsedToc);
1452
- import_shelljs2.default.mkdir("-p", (0, import_path13.dirname)(outputPath));
1453
- (0, import_fs4.writeFileSync)(outputPath, outputToc);
1454
- }
1455
- prepareNavigationPaths(parsedToc, pathToDir);
1456
- });
1457
- }
1458
- function processTocItems(path, items, tocDir, sourcesDir, vars) {
1459
- return __async(this, null, function* () {
1460
- const {
1461
- resolveConditions,
1462
- removeHiddenTocItems
1463
- } = argv_default.getConfig();
1464
- let preparedItems = items;
1465
- if (resolveConditions || removeHiddenTocItems) {
1466
- try {
1467
- preparedItems = filterFiles(items, "items", vars, {
1468
- resolveConditions,
1469
- removeHiddenTocItems
1470
- });
1471
- } catch (error) {
1472
- import_log3.default.error(`Error while filtering toc file: ${path}. Error message: ${error}`);
1473
- }
1474
- }
1475
- return _replaceIncludes(path, preparedItems, tocDir, sourcesDir, vars);
1476
- });
1477
- }
1478
- function getForPath(path) {
1479
- return storage.get(path);
1480
- }
1481
- function getNavigationPaths() {
1482
- return [...navigationPaths];
1483
- }
1484
- function getIncludedTocPaths() {
1485
- return [...includedTocPaths];
1486
- }
1487
- function prepareNavigationPaths(parsedToc, dirPath) {
1488
- function processItems(items, pathToDir) {
1489
- items.forEach((item) => {
1490
- if (!parsedToc.singlePage && item.items) {
1491
- const preparedSubItems = item.items.map((yfmToc, index) => {
1492
- yfmToc.id = `${yfmToc.name}-${index}-${Math.random()}`;
1493
- return yfmToc;
1494
- });
1495
- processItems(preparedSubItems, pathToDir);
1496
- }
1497
- if (item.href && !isExternalHref(item.href)) {
1498
- const href = (0, import_path13.join)(pathToDir, item.href);
1499
- storage.set(href, parsedToc);
1500
- const navigationPath = _normalizeHref(href);
1501
- navigationPaths.push(navigationPath);
1502
- }
1503
- });
1504
- }
1505
- processItems([parsedToc], dirPath);
1506
- }
1507
- function _normalizeHref(href) {
1508
- const preparedHref = (0, import_path13.normalize)(href);
1509
- if (preparedHref.endsWith(".md") || preparedHref.endsWith(".yaml")) {
1510
- return preparedHref;
1511
- }
1512
- if (preparedHref.endsWith(import_path13.sep)) {
1513
- return `${preparedHref}index.yaml`;
1514
- }
1515
- return `${preparedHref}.md`;
1516
- }
1517
- function _copyTocDir(tocPath, destDir) {
1518
- const { input: inputFolderPath } = argv_default.getConfig();
1519
- const { dir: tocDir } = (0, import_path13.parse)(tocPath);
1520
- const files = (0, import_walk_sync.default)(tocDir, {
1521
- globs: ["**/*.*"],
1522
- ignore: ["**/toc.yaml"],
1523
- directories: false
1524
- });
1525
- files.forEach((relPath) => {
1526
- const from = (0, import_path13.resolve)(tocDir, relPath);
1527
- const to = (0, import_path13.resolve)(destDir, relPath);
1528
- const fileExtension = (0, import_path13.extname)(relPath);
1529
- const isMdFile = fileExtension === ".md";
1530
- import_shelljs2.default.mkdir("-p", (0, import_path13.parse)(to).dir);
1531
- if (isMdFile) {
1532
- const fileContent = (0, import_fs4.readFileSync)(from, "utf8");
1533
- const sourcePath = (0, import_path13.relative)(inputFolderPath, from);
1534
- const updatedFileContent = getContentWithUpdatedStaticMetadata({
1535
- fileContent,
1536
- sourcePath,
1537
- addSourcePath: true
1538
- });
1539
- (0, import_fs4.writeFileSync)(to, updatedFileContent);
1540
- } else {
1541
- (0, import_fs4.copyFileSync)(from, to);
1542
- }
1543
- });
1544
- }
1545
- function _replaceIncludesHrefs(items, includeTocDir, tocDir) {
1546
- return items.reduce((acc, tocItem) => {
1547
- if (tocItem.href) {
1548
- tocItem.href = (0, import_path13.relative)(tocDir, (0, import_path13.resolve)(includeTocDir, tocItem.href));
1549
- }
1550
- if (tocItem.items) {
1551
- tocItem.items = _replaceIncludesHrefs(tocItem.items, includeTocDir, tocDir);
1552
- }
1553
- if (tocItem.include) {
1554
- const { path } = tocItem.include;
1555
- tocItem.include.path = (0, import_path13.relative)(tocDir, (0, import_path13.resolve)(includeTocDir, path));
1556
- }
1557
- return acc.concat(tocItem);
1558
- }, []);
1559
- }
1560
- function _liquidSubstitutions(input, vars, path) {
1561
- const { outputFormat, applyPresets } = argv_default.getConfig();
1562
- if (outputFormat === "md" && !applyPresets) {
1563
- return input;
1564
- }
1565
- return (0, import_liquid2.default)(input, vars, path, {
1566
- conditions: false,
1567
- substitutions: true
1568
- });
1569
- }
1570
- function addIncludeTocPath(includeTocPath) {
1571
- includedTocPaths.add(includeTocPath);
1572
- }
1573
- function _replaceIncludes(path, items, tocDir, sourcesDir, vars) {
1574
- return __async(this, null, function* () {
1575
- const result = [];
1576
- for (const item of items) {
1577
- let includedInlineItems = null;
1578
- if (item.name) {
1579
- const tocPath = (0, import_path13.join)(tocDir, "toc.yaml");
1580
- item.name = _liquidSubstitutions(item.name, vars, tocPath);
1581
- }
1582
- try {
1583
- yield applyIncluders(path, item, vars);
1584
- } catch (err) {
1585
- if (err instanceof Error || err instanceof IncludersError) {
1586
- const message = err.toString();
1587
- const file = err instanceof IncludersError ? err.path : path;
1588
- logger.error(file, message);
1589
- }
1590
- }
1591
- if (item.include) {
1592
- const { mode = "root_merge" /* ROOT_MERGE */ } = item.include;
1593
- const includeTocPath = mode === "root_merge" /* ROOT_MERGE */ ? (0, import_path13.resolve)(sourcesDir, item.include.path) : (0, import_path13.resolve)(tocDir, item.include.path);
1594
- const includeTocDir = (0, import_path13.dirname)(includeTocPath);
1595
- try {
1596
- const includeToc = (0, import_js_yaml4.load)((0, import_fs4.readFileSync)(includeTocPath, "utf8"));
1597
- if (includeToc.stage === "tech-preview" /* TECH_PREVIEW */) {
1598
- continue;
1599
- }
1600
- if (mode === "merge" /* MERGE */ || mode === "root_merge" /* ROOT_MERGE */) {
1601
- _copyTocDir(includeTocPath, tocDir);
1602
- }
1603
- addIncludeTocPath(includeTocPath);
1604
- let includedTocItems = (item.items || []).concat(includeToc.items);
1605
- const baseTocDir = mode === "link" /* LINK */ ? includeTocDir : tocDir;
1606
- includedTocItems = yield processTocItems(path, includedTocItems, baseTocDir, sourcesDir, vars);
1607
- if (mode === "link" /* LINK */) {
1608
- includedTocItems = _replaceIncludesHrefs(includedTocItems, includeTocDir, tocDir);
1609
- }
1610
- if (item.name) {
1611
- item.items = includedTocItems;
1612
- } else {
1613
- includedInlineItems = includedTocItems;
1614
- }
1615
- } catch (err) {
1616
- const message = `Error while including toc: ${(0, import_chalk2.bold)(includeTocPath)} to ${(0, import_chalk2.bold)((0, import_path13.join)(tocDir, "toc.yaml"))}`;
1617
- import_log3.default.error(message);
1618
- continue;
1619
- } finally {
1620
- delete item.include;
1621
- }
1622
- } else if (item.items) {
1623
- item.items = yield processTocItems(path, item.items, tocDir, sourcesDir, vars);
1624
- }
1625
- if (includedInlineItems) {
1626
- result.push(...includedInlineItems);
1627
- } else {
1628
- result.push(item);
1629
- }
1630
- }
1631
- return result;
1632
- });
1633
- }
1634
- function getTocDir(pagePath) {
1635
- const { input: inputFolderPath } = argv_default.getConfig();
1636
- const tocDir = (0, import_path13.dirname)(pagePath);
1637
- const tocPath = (0, import_path13.resolve)(tocDir, "toc.yaml");
1638
- if (!tocDir.includes(inputFolderPath)) {
1639
- throw new Error("Error while finding toc dir");
1640
- }
1641
- if ((0, import_fs4.existsSync)(tocPath)) {
1642
- return tocDir;
1643
- }
1644
- return getTocDir(tocDir);
1645
- }
1646
- function setNavigationPaths(paths) {
1647
- navigationPaths = paths;
1648
- }
1649
- var tocs_default = {
1650
- add,
1651
- getForPath,
1652
- getNavigationPaths,
1653
- getTocDir,
1654
- getIncludedTocPaths,
1655
- setNavigationPaths
1656
- };
1657
-
1658
- // src/services/preset.ts
1659
- var import_path14 = require("path");
1660
- var presetStorage = /* @__PURE__ */ new Map();
1661
- function add2(parsedPreset, path, varsPreset) {
1662
- const combinedValues = __spreadValues(__spreadValues({}, parsedPreset.default || {}), parsedPreset[varsPreset] || {});
1663
- const key = (0, import_path14.dirname)((0, import_path14.normalize)(path));
1664
- presetStorage.set(key, combinedValues);
1665
- }
1666
- function get(path) {
1667
- let combinedValues = {};
1668
- let localPath = (0, import_path14.normalize)(path);
1669
- while (localPath !== ".") {
1670
- const presetValues = presetStorage.get(localPath) || {};
1671
- localPath = (0, import_path14.dirname)(localPath);
1672
- combinedValues = __spreadValues(__spreadValues({}, presetValues), combinedValues);
1673
- }
1674
- combinedValues = __spreadValues(__spreadValues({}, presetStorage.get(".")), combinedValues);
1675
- return combinedValues;
1676
- }
1677
- function getPresetStorage() {
1678
- return presetStorage;
1679
- }
1680
- function setPresetStorage(preset) {
1681
- presetStorage = preset;
1682
- }
1683
- var preset_default = {
1684
- add: add2,
1685
- get,
1686
- getPresetStorage,
1687
- setPresetStorage
1688
- };
1689
-
1690
- // src/services/argv.ts
1691
- var import_path15 = require("path");
1692
- var import_fs5 = require("fs");
1693
- var _argv;
1694
- function getConfig() {
1695
- return _argv;
1696
- }
1697
- function init2(argv) {
1698
- _argv = __spreadProps(__spreadValues({}, argv), {
1699
- ignore: Array.isArray(argv.ignore) ? argv.ignore : []
1700
- });
1701
- if (argv.vars) {
1702
- _argv.vars = JSON.parse(argv.vars);
1703
- }
1704
- try {
1705
- const ignorefile = (0, import_fs5.readFileSync)((0, import_path15.join)(_argv.rootInput, ".yfmignore"), "utf8");
1706
- const ignore = ignorefile.split("\n");
1707
- _argv.ignore = _argv.ignore.concat(ignore);
1708
- } catch (e) {
1709
- }
1710
- }
1711
- function set(argv) {
1712
- _argv = argv;
1713
- }
1714
- var argv_default = {
1715
- getConfig,
1716
- init: init2,
1717
- set
1718
- };
1719
-
1720
- // src/services/leading.ts
1721
- var import_path16 = require("path");
1722
- var import_fs6 = require("fs");
1723
- var import_js_yaml5 = require("js-yaml");
1724
- var import_log4 = __toESM(require("@doc-tools/transform/lib/log"));
1725
- function filterFile(path) {
1726
- var _a, _b;
1727
- const {
1728
- input: inputFolderPath,
1729
- vars
1730
- } = argv_default.getConfig();
1731
- const pathToDir = (0, import_path16.dirname)(path);
1732
- const filePath = (0, import_path16.resolve)(inputFolderPath, path);
1733
- const content = (0, import_fs6.readFileSync)(filePath, "utf8");
1734
- const parsedIndex = (0, import_js_yaml5.load)(content);
1735
- const combinedVars = __spreadValues(__spreadValues({}, preset_default.get(pathToDir)), vars);
1736
- try {
1737
- const title = firstFilterTextItems(
1738
- parsedIndex.title,
1739
- combinedVars,
1740
- { resolveConditions: true }
1741
- );
1742
- parsedIndex.title = liquidField(title, combinedVars, path);
1743
- const description = filterTextItems(
1744
- parsedIndex.description,
1745
- combinedVars,
1746
- { resolveConditions: true }
1747
- );
1748
- parsedIndex.description = liquidFields(description, combinedVars, path);
1749
- if ((_a = parsedIndex.meta) == null ? void 0 : _a.title) {
1750
- const metaTitle = firstFilterTextItems(
1751
- parsedIndex.meta.title,
1752
- combinedVars,
1753
- { resolveConditions: true }
1754
- );
1755
- parsedIndex.meta.title = liquidField(metaTitle, combinedVars, path);
1756
- }
1757
- if ((_b = parsedIndex.meta) == null ? void 0 : _b.description) {
1758
- const metaDescription = firstFilterTextItems(
1759
- parsedIndex.meta.description,
1760
- combinedVars,
1761
- { resolveConditions: true }
1762
- );
1763
- parsedIndex.meta.description = liquidField(metaDescription, combinedVars, path);
1764
- }
1765
- if (parsedIndex.nav) {
1766
- const navTitle = firstFilterTextItems(
1767
- parsedIndex.nav.title,
1768
- combinedVars,
1769
- { resolveConditions: true }
1770
- );
1771
- parsedIndex.nav.title = liquidField(navTitle, combinedVars, path);
1772
- }
1773
- parsedIndex.links = filterFiles(parsedIndex.links, "links", combinedVars, { resolveConditions: true });
1774
- parsedIndex.links.forEach((link) => {
1775
- if (link.title) {
1776
- link.title = liquidField(link.title, combinedVars, path);
1777
- }
1778
- if (link.description) {
1779
- link.description = liquidField(link.description, combinedVars, path);
1780
- }
1781
- });
1782
- (0, import_fs6.writeFileSync)(filePath, (0, import_js_yaml5.dump)(parsedIndex));
1783
- } catch (error) {
1784
- import_log4.default.error(`Error while filtering index file: ${path}. Error message: ${error}`);
1785
- }
1786
- }
1787
- var leading_default = {
1788
- filterFile
1789
- };
1790
-
1791
- // src/services/plugins.ts
1792
- var plugins_exports = {};
1793
- __export(plugins_exports, {
1794
- getCollectOfPlugins: () => getCollectOfPlugins,
1795
- getCustomLintRules: () => getCustomLintRules,
1796
- getDefaultLintConfig: () => getDefaultLintConfig,
1797
- getHeadContent: () => getHeadContent,
1798
- getPlugins: () => getPlugins,
1799
- setPlugins: () => setPlugins
1800
- });
1801
- var plugins;
1802
- var collectionOfPlugins;
1803
- function setPlugins() {
1804
- plugins = getAllPlugins();
1805
- collectionOfPlugins = makeCollectOfPlugins();
1806
- }
1807
- function getPlugins() {
1808
- return plugins;
1809
- }
1810
- function getCollectOfPlugins() {
1811
- return collectionOfPlugins;
1812
- }
1813
- function makeCollectOfPlugins() {
1814
- const pluginsWithCollect = plugins.filter((plugin) => {
1815
- return typeof plugin.collect === "function";
1816
- });
1817
- return (output, options) => {
1818
- let collectsOutput = output;
1819
- pluginsWithCollect.forEach((plugin) => {
1820
- const collectOutput = plugin.collect(collectsOutput, options);
1821
- collectsOutput = typeof collectOutput === "string" ? collectOutput : collectsOutput;
1822
- });
1823
- return collectsOutput;
1824
- };
1825
- }
1826
- function getAllPlugins() {
1827
- const customPlugins = getCustomPlugins();
1828
- return [...YFM_PLUGINS, ...customPlugins];
1829
- }
1830
- function getCustomPlugins() {
1831
- try {
1832
- const customPlugins = require(require.resolve("./plugins"));
1833
- return Array.isArray(customPlugins) ? customPlugins : [];
1834
- } catch (e) {
1835
- return [];
1836
- }
1837
- }
1838
- function getHeadContent() {
1839
- try {
1840
- return require(require.resolve("./head-content.js"));
1841
- } catch (e) {
1842
- return "";
1843
- }
1844
- }
1845
- function getCustomLintRules() {
1846
- try {
1847
- return require(require.resolve("./lint-rules"));
1848
- } catch (e) {
1849
- return [];
1850
- }
1851
- }
1852
- function getDefaultLintConfig() {
1853
- try {
1854
- return require(require.resolve("./default-lint-config"));
1855
- } catch (e) {
1856
- return void 0;
1857
- }
1858
- }
1859
-
1860
- // src/cmd/build/index.ts
1861
- var import_includer = __toESM(require("@diplodoc/openapi-extension/includer"));
1862
-
1863
- // src/steps/processAssets.ts
1864
- var import_walk_sync2 = __toESM(require("walk-sync"));
1865
- var import_shelljs3 = __toESM(require("shelljs"));
1866
- var import_client3 = __toESM(require_client());
1867
- function processAssets(outputBundlePath) {
1868
- const {
1869
- input: inputFolderPath,
1870
- output: outputFolderPath
1871
- } = argv_default.getConfig();
1872
- const assetFilePath = (0, import_walk_sync2.default)(inputFolderPath, {
1873
- directories: false,
1874
- includeBasePath: false,
1875
- ignore: [
1876
- "**/*.yaml",
1877
- "**/*.md"
1878
- ]
1879
- });
1880
- copyFiles(inputFolderPath, outputFolderPath, assetFilePath);
1881
- import_shelljs3.default.mkdir("-p", outputBundlePath);
1882
- for (const path of Object.values(import_client3.default.dst)) {
1883
- import_shelljs3.default.cp(path, outputBundlePath);
1884
- }
1885
- }
1886
-
1887
- // src/steps/processExcludedFiles.ts
1888
- var import_path17 = require("path");
1889
- var import_walk_sync3 = __toESM(require("walk-sync"));
1890
- var import_shelljs4 = __toESM(require("shelljs"));
1891
- function processExcludedFiles() {
1892
- const {
1893
- input: inputFolderPath,
1894
- output: outputFolderPath,
1895
- ignore
1896
- } = argv_default.getConfig();
1897
- const allContentFiles = (0, import_walk_sync3.default)(inputFolderPath, {
1898
- directories: false,
1899
- includeBasePath: true,
1900
- globs: [
1901
- "**/*.md",
1902
- "**/index.yaml",
1903
- ...ignore
1904
- ],
1905
- // Ignores service directories like "_includes", "_templates" and etc.
1906
- ignore: ["**/_*/**/*"]
1907
- });
1908
- const navigationPaths2 = tocs_default.getNavigationPaths().map((filePath) => convertBackSlashToSlash((0, import_path17.resolve)(inputFolderPath, filePath)));
1909
- const tocSpecifiedFiles = new Set(navigationPaths2);
1910
- const excludedFiles = allContentFiles.filter((filePath) => !tocSpecifiedFiles.has(filePath));
1911
- import_shelljs4.default.rm("-f", excludedFiles);
1912
- const includedTocPaths2 = tocs_default.getIncludedTocPaths().map((filePath) => {
1913
- const relativeTocPath = (0, import_path17.relative)(inputFolderPath, filePath);
1914
- const destTocPath = (0, import_path17.resolve)(outputFolderPath, relativeTocPath);
1915
- return convertBackSlashToSlash(destTocPath);
1916
- });
1917
- import_shelljs4.default.rm("-rf", includedTocPaths2);
1918
- }
1919
-
1920
- // src/steps/processLogs.ts
1921
- var import_log5 = __toESM(require("@doc-tools/transform/lib/log"));
1922
- var import_uniq = __toESM(require("lodash/uniq"));
1923
- function processLogs(inputFolder) {
1924
- const replacementRegExp = new RegExp(inputFolder, "ig");
1925
- const { info, warn, error } = import_log5.default.get();
1926
- const outputLogs = (0, import_uniq.default)([
1927
- "",
1928
- ...info,
1929
- "",
1930
- ...warn,
1931
- "",
1932
- ...error,
1933
- ""
1934
- ]);
1935
- for (const outputLog of outputLogs) {
1936
- const preparedLog = outputLog.replace(replacementRegExp, "");
1937
- console.log(preparedLog);
1938
- }
1939
- }
1940
-
1941
- // src/steps/processPages.ts
1942
- var import_path22 = require("path");
1943
- var import_shelljs6 = __toESM(require("shelljs"));
1944
- var import_fs10 = require("fs");
1945
- var import_chalk4 = require("chalk");
1946
- var import_js_yaml7 = require("js-yaml");
1947
- var import_async = require("async");
1948
- var import_log11 = __toESM(require("@doc-tools/transform/lib/log"));
1949
-
1950
- // src/resolvers/md2md.ts
1951
- var import_fs7 = require("fs");
1952
- var import_path18 = require("path");
1953
- var import_shelljs5 = __toESM(require("shelljs"));
1954
- var import_log6 = __toESM(require("@doc-tools/transform/lib/log"));
1955
- var import_liquid3 = __toESM(require("@doc-tools/transform/lib/liquid"));
1956
- function resolveMd2Md(options) {
1957
- return __async(this, null, function* () {
1958
- const { inputPath, outputPath, metadata } = options;
1959
- const { input, output } = argv_default.getConfig();
1960
- const resolvedInputPath = (0, import_path18.resolve)(input, inputPath);
1961
- const vars = getVarsPerFile(inputPath);
1962
- const content = yield getContentWithUpdatedMetadata(
1963
- (0, import_fs7.readFileSync)(resolvedInputPath, "utf8"),
1964
- metadata,
1965
- vars.__system
1966
- );
1967
- const { result, changelogs } = transformMd2Md(content, {
1968
- path: resolvedInputPath,
1969
- destPath: outputPath,
1970
- root: (0, import_path18.resolve)(input),
1971
- destRoot: (0, import_path18.resolve)(output),
1972
- collectOfPlugins: plugins_exports.getCollectOfPlugins(),
1973
- vars,
1974
- log: import_log6.default,
1975
- copyFile
1976
- });
1977
- (0, import_fs7.writeFileSync)(outputPath, result);
1978
- if (changelogs == null ? void 0 : changelogs.length) {
1979
- const mdFilename = (0, import_path18.basename)(outputPath, (0, import_path18.extname)(outputPath));
1980
- const outputDir = (0, import_path18.dirname)(outputPath);
1981
- changelogs.forEach((changes, index) => {
1982
- let changesName;
1983
- const changesDate = changes.date;
1984
- const changesIdx = changes.index;
1985
- if (typeof changesIdx === "number") {
1986
- changesName = String(changesIdx);
1987
- }
1988
- if (!changesName && changesDate && /^\d{4}/.test(changesDate)) {
1989
- changesName = Math.trunc(new Date(changesDate).getTime() / 1e3);
1990
- }
1991
- if (!changesName) {
1992
- changesName = `name-${mdFilename}-${String(changelogs.length - index).padStart(3, "0")}`;
1993
- }
1994
- const changesPath = (0, import_path18.join)(outputDir, `changes-${changesName}.json`);
1995
- if ((0, import_fs7.existsSync)(changesPath)) {
1996
- throw new Error(`Changelog ${changesPath} already exists!`);
1997
- }
1998
- (0, import_fs7.writeFileSync)(changesPath, JSON.stringify(__spreadProps(__spreadValues({}, changes), {
1999
- source: mdFilename
2000
- })));
2001
- });
2002
- }
2003
- logger.info(inputPath, PROCESSING_FINISHED);
2004
- return void 0;
2005
- });
2006
- }
2007
- function copyFile(targetPath, targetDestPath, options) {
2008
- import_shelljs5.default.mkdir("-p", (0, import_path18.dirname)(targetDestPath));
2009
- if (options) {
2010
- const sourceIncludeContent = (0, import_fs7.readFileSync)(targetPath, "utf8");
2011
- const { result } = transformMd2Md(sourceIncludeContent, options);
2012
- (0, import_fs7.writeFileSync)(targetDestPath, result);
2013
- } else {
2014
- import_shelljs5.default.cp(targetPath, targetDestPath);
2015
- }
2016
- }
2017
- function liquidMd2Md(input, vars, path) {
2018
- const {
2019
- applyPresets,
2020
- resolveConditions,
2021
- conditionsInCode
2022
- } = argv_default.getConfig();
2023
- return (0, import_liquid3.default)(input, vars, path, {
2024
- conditions: resolveConditions,
2025
- substitutions: applyPresets,
2026
- conditionsInCode,
2027
- withSourceMap: true,
2028
- keepNotVar: true
2029
- });
2030
- }
2031
- function transformMd2Md(input, options) {
2032
- const {
2033
- disableLiquid
2034
- } = argv_default.getConfig();
2035
- const {
2036
- vars = {},
2037
- path,
2038
- root,
2039
- destPath,
2040
- destRoot,
2041
- collectOfPlugins,
2042
- log: pluginLog,
2043
- copyFile: pluginCopyFile
2044
- } = options;
2045
- let output = input;
2046
- const changelogs = [];
2047
- if (!disableLiquid) {
2048
- const liquidResult = liquidMd2Md(input, vars, path);
2049
- output = liquidResult.output;
2050
- }
2051
- if (collectOfPlugins) {
2052
- output = collectOfPlugins(output, {
2053
- vars,
2054
- path,
2055
- root,
2056
- destPath,
2057
- destRoot,
2058
- log: pluginLog,
2059
- copyFile: pluginCopyFile,
2060
- collectOfPlugins,
2061
- changelogs,
2062
- extractChangelogs: true
2063
- });
2064
- }
2065
- return {
2066
- result: output,
2067
- changelogs,
2068
- logs: pluginLog.get()
2069
- };
2070
- }
2071
-
2072
- // src/resolvers/md2html.ts
2073
- var import_path19 = require("path");
2074
- var import_fs8 = require("fs");
2075
- var import_js_yaml6 = __toESM(require("js-yaml"));
2076
- var import_transform = __toESM(require("@doc-tools/transform"));
2077
- var import_log7 = __toESM(require("@doc-tools/transform/lib/log"));
2078
- var import_liquid4 = __toESM(require("@doc-tools/transform/lib/liquid"));
2079
- var FileTransformer = {
2080
- ".yaml": YamlFileTransformer,
2081
- ".md": MdFileTransformer
2082
- };
2083
- var fixRelativePath = (relativeTo) => (path) => {
2084
- return (0, import_path19.join)(getAssetsPublicPath(relativeTo), path);
2085
- };
2086
- function resolveMd2HTML(options) {
2087
- return __async(this, null, function* () {
2088
- var _a;
2089
- const { inputPath, fileExtension, outputPath, outputBundlePath, metadata } = options;
2090
- const pathToDir = (0, import_path19.dirname)(inputPath);
2091
- const toc = tocs_default.getForPath(inputPath) || null;
2092
- const tocBase = toc && toc.base ? toc.base : "";
2093
- const pathToFileDir = pathToDir === tocBase ? "" : pathToDir.replace(`${tocBase}${import_path19.sep}`, "");
2094
- const relativePathToIndex = (0, import_path19.relative)(pathToDir, `${tocBase}${import_path19.sep}`);
2095
- const { input, lang, allowCustomResources } = argv_default.getConfig();
2096
- const resolvedPath = (0, import_path19.resolve)(input, inputPath);
2097
- const content = (0, import_fs8.readFileSync)(resolvedPath, "utf8");
2098
- const transformFn = FileTransformer[fileExtension];
2099
- const { result } = transformFn(content, { path: inputPath });
2100
- const updatedMetadata = metadata && metadata.isContributorsEnabled ? yield getUpdatedMetadata(metadata, content, result == null ? void 0 : result.meta) : result.meta;
2101
- const fileMeta = fileExtension === ".yaml" ? (_a = result.data.meta) != null ? _a : {} : updatedMetadata;
2102
- if (allowCustomResources) {
2103
- const { script, style } = (metadata == null ? void 0 : metadata.resources) || {};
2104
- fileMeta.style = (fileMeta.style || []).concat(style || []).map(fixRelativePath(inputPath));
2105
- fileMeta.script = (fileMeta.script || []).concat(script || []).map(fixRelativePath(inputPath));
2106
- } else {
2107
- fileMeta.style = [];
2108
- fileMeta.script = [];
2109
- }
2110
- const props = {
2111
- data: __spreadProps(__spreadValues({
2112
- leading: inputPath.endsWith(".yaml"),
2113
- toc: transformToc(toc, pathToDir) || {}
2114
- }, result), {
2115
- meta: fileMeta
2116
- }),
2117
- router: {
2118
- pathname: (0, import_path19.join)(relativePathToIndex, pathToFileDir, (0, import_path19.basename)(outputPath))
2119
- },
2120
- lang: lang || "ru" /* RU */
2121
- };
2122
- const outputDir = (0, import_path19.dirname)(outputPath);
2123
- const relativePathToBundle = (0, import_path19.relative)((0, import_path19.resolve)(outputDir), (0, import_path19.resolve)(outputBundlePath));
2124
- const outputFileContent = generateStaticMarkup(props, relativePathToBundle);
2125
- (0, import_fs8.writeFileSync)(outputPath, outputFileContent);
2126
- logger.info(inputPath, PROCESSING_FINISHED);
2127
- return props;
2128
- });
2129
- }
2130
- function YamlFileTransformer(content) {
2131
- var _a;
2132
- let data = null;
2133
- try {
2134
- data = import_js_yaml6.default.load(content);
2135
- } catch (error) {
2136
- import_log7.default.error(`Yaml transform has been failed. Error: ${error}`);
2137
- }
2138
- if (!data) {
2139
- return {
2140
- result: { data: {} }
2141
- };
2142
- }
2143
- const links2 = (_a = data == null ? void 0 : data.links) == null ? void 0 : _a.map(
2144
- (link) => link.href ? __spreadProps(__spreadValues({}, link), { href: link.href.replace(/.md$/gmu, ".html") }) : link
2145
- );
2146
- if (links2) {
2147
- data.links = links2;
2148
- }
2149
- return {
2150
- result: { data }
2151
- };
2152
- }
2153
- function liquidMd2Html(input, vars, path) {
2154
- const { conditionsInCode } = argv_default.getConfig();
2155
- return (0, import_liquid4.default)(input, vars, path, {
2156
- conditionsInCode,
2157
- withSourceMap: true
2158
- });
2159
- }
2160
- function MdFileTransformer(content, transformOptions) {
2161
- const _a = argv_default.getConfig(), { input } = _a, options = __objRest(_a, ["input"]);
2162
- const { path: filePath } = transformOptions;
2163
- const plugins2 = plugins_exports.getPlugins();
2164
- const vars = getVarsPerFile(filePath);
2165
- const root = (0, import_path19.resolve)(input);
2166
- const path = (0, import_path19.resolve)(input, filePath);
2167
- return (0, import_transform.default)(content, __spreadProps(__spreadValues({}, options), {
2168
- plugins: plugins2,
2169
- vars,
2170
- root,
2171
- path,
2172
- assetsPublicPath: getAssetsPublicPath(filePath),
2173
- getVarsPerFile: getVarsPerRelativeFile,
2174
- extractTitle: true
2175
- }));
2176
- }
2177
-
2178
- // src/resolvers/lintPage.ts
2179
- var import_path20 = require("path");
2180
- var import_log8 = __toESM(require("@doc-tools/transform/lib/log"));
2181
- var import_yfmlint = __toESM(require("@doc-tools/transform/lib/yfmlint"));
2182
- var import_fs9 = require("fs");
2183
- var import_chalk3 = require("chalk");
2184
- var FileLinter = {
2185
- ".md": MdFileLinter
2186
- };
2187
- function lintPage(options) {
2188
- const { inputPath, fileExtension, onFinish } = options;
2189
- const { input } = argv_default.getConfig();
2190
- const resolvedPath = (0, import_path20.resolve)(input, inputPath);
2191
- try {
2192
- const content = (0, import_fs9.readFileSync)(resolvedPath, "utf8");
2193
- const lintFn = FileLinter[fileExtension];
2194
- if (!lintFn) {
2195
- return;
2196
- }
2197
- lintFn(content, { path: inputPath });
2198
- } catch (e) {
2199
- const message = `No such file or has no access to ${(0, import_chalk3.bold)(resolvedPath)}`;
2200
- console.error(message, e);
2201
- import_log8.default.error(message);
2202
- }
2203
- if (onFinish) {
2204
- onFinish();
2205
- }
2206
- }
2207
- function MdFileLinter(content, lintOptions) {
2208
- const _a = argv_default.getConfig(), {
2209
- input,
2210
- lintConfig,
2211
- disableLiquid,
2212
- outputFormat
2213
- } = _a, options = __objRest(_a, [
2214
- "input",
2215
- "lintConfig",
2216
- "disableLiquid",
2217
- "outputFormat"
2218
- ]);
2219
- const { path: filePath } = lintOptions;
2220
- const plugins2 = outputFormat === "md" ? [] : plugins_exports.getPlugins();
2221
- const vars = getVarsPerFile(filePath);
2222
- const root = (0, import_path20.resolve)(input);
2223
- const path = (0, import_path20.resolve)(input, filePath);
2224
- let preparedContent = content;
2225
- const assetsPublicPath = (0, import_path20.relative)((0, import_path20.dirname)(path), root);
2226
- const lintMarkdown = function lintMarkdown2(opts) {
2227
- const { input: localInput, path: localPath, sourceMap: sourceMap2 } = opts;
2228
- const pluginOptions = __spreadProps(__spreadValues({}, options), {
2229
- vars,
2230
- root,
2231
- path: localPath,
2232
- lintMarkdown: lintMarkdown2,
2233
- // Should pass the function for linting included files
2234
- assetsPublicPath,
2235
- disableLiquid,
2236
- log: import_log8.default,
2237
- getVarsPerFile: getVarsPerRelativeFile
2238
- });
2239
- (0, import_yfmlint.default)({
2240
- input: localInput,
2241
- lintConfig,
2242
- pluginOptions,
2243
- plugins: plugins2,
2244
- defaultLintConfig: plugins_exports.getDefaultLintConfig(),
2245
- customLintRules: plugins_exports.getCustomLintRules(),
2246
- sourceMap: sourceMap2
2247
- });
2248
- };
2249
- let sourceMap;
2250
- if (!disableLiquid) {
2251
- let liquidResult;
2252
- if (outputFormat === "md") {
2253
- liquidResult = liquidMd2Md(content, vars, path);
2254
- } else {
2255
- liquidResult = liquidMd2Html(content, vars, path);
2256
- }
2257
- preparedContent = liquidResult.output;
2258
- sourceMap = liquidResult.sourceMap;
2259
- }
2260
- lintMarkdown({
2261
- input: preparedContent,
2262
- path,
2263
- sourceMap
2264
- });
2265
- }
2266
-
2267
- // src/vcs-connector/github.ts
2268
- var import_core = require("@octokit/core");
2269
- var import_path21 = require("path");
2270
- var import_simple_git = __toESM(require("simple-git"));
2271
- var import_minimatch = require("minimatch");
2272
-
2273
- // src/vcs-connector/client/github.ts
2274
- var import_log10 = __toESM(require("@doc-tools/transform/lib/log"));
2275
-
2276
- // src/vcs-connector/connector-validator.ts
2277
- var import_log9 = __toESM(require("@doc-tools/transform/lib/log"));
2278
- var githubConnectorValidator = {
2279
- ["endpoint" /* ENDPOINT */]: {
2280
- warnMessage: `'${"endpoint" /* ENDPOINT */}' must be provided for GitHub repo.`,
2281
- validateFn: notEmptyValue,
2282
- defaultValue: process.env.GITHUB_BASE_URL
2283
- },
2284
- ["token" /* TOKEN */]: {
2285
- warnMessage: `'${"token" /* TOKEN */}' must be provided for GitHub repo.`,
2286
- validateFn: notEmptyValue,
2287
- defaultValue: process.env.GITHUB_TOKEN
2288
- },
2289
- ["owner" /* OWNER */]: {
2290
- warnMessage: `'${"owner" /* OWNER */}' must be provided for GitHub repo.`,
2291
- validateFn: notEmptyValue,
2292
- defaultValue: process.env.GITHUB_OWNER
2293
- },
2294
- ["repo" /* REPO */]: {
2295
- warnMessage: `'${"repo" /* REPO */}' must be provided for GitHub repo.`,
2296
- validateFn: notEmptyValue,
2297
- defaultValue: process.env.GITHUB_REPO
2298
- }
2299
- };
2300
- var connectorValidator = {
2301
- "type": {
2302
- warnMessage: "'type' must be provided for repo.",
2303
- validateFn: notEmptyValue
2304
- },
2305
- ["github" /* GITHUB */]: {
2306
- warnMessage: `'${"github" /* GITHUB */}' object must be filled needed fields.`,
2307
- validateFn: notEmptyObject,
2308
- relatedValidator: githubConnectorValidator
2309
- }
2310
- };
2311
- function notEmptyObject(filed) {
2312
- if (typeof filed === "object") {
2313
- return Boolean(filed && Object.getOwnPropertyNames(filed).length);
2314
- }
2315
- return false;
2316
- }
2317
- function notEmptyValue(value) {
2318
- if (typeof value === "string") {
2319
- return Boolean(value);
2320
- }
2321
- return false;
2322
- }
2323
- function validateConnectorFields(sourceType, fieldNames, repoProperties) {
2324
- var _a;
2325
- const repoValidator = connectorValidator[sourceType];
2326
- if (!repoValidator) {
2327
- import_log9.default.error(`Invalid repo type: ${repoValidator}`);
2328
- return {};
2329
- }
2330
- const isValidRepo = repoValidator.validateFn(repoProperties && repoProperties[sourceType]);
2331
- const relatedRepoValidator = repoValidator.relatedValidator;
2332
- if (!repoProperties || !isValidRepo || !relatedRepoValidator) {
2333
- createLog(repoValidator);
2334
- return {};
2335
- }
2336
- let isValidProperties = true;
2337
- const validatedFields = {};
2338
- for (const property of fieldNames) {
2339
- const propertyValidator = relatedRepoValidator[property];
2340
- if (!propertyValidator) {
2341
- import_log9.default.warn(`The property '${property}' doesn't exist in ${sourceType} repo.`);
2342
- continue;
2343
- }
2344
- const propertyValue = propertyValidator.defaultValue || ((_a = repoProperties[sourceType]) == null ? void 0 : _a[property]);
2345
- if (!propertyValidator.validateFn(propertyValue)) {
2346
- createLog(propertyValidator);
2347
- isValidProperties = false;
2348
- }
2349
- validatedFields[property] = propertyValue;
2350
- }
2351
- if (isValidProperties) {
2352
- return validatedFields;
2353
- }
2354
- import_log9.default.warn(getMsg\u0421onfigurationMustBeProvided(sourceType));
2355
- return {};
2356
- }
2357
- function createLog(validator) {
2358
- if (validator.errorMessage) {
2359
- return import_log9.default.error(validator.errorMessage);
2360
- }
2361
- if (validator.warnMessage) {
2362
- return import_log9.default.warn(validator.warnMessage);
2363
- }
2364
- throw new Error(`Invalid validator: ${JSON.stringify(validator)}.`);
2365
- }
2366
-
2367
- // src/vcs-connector/client/github.ts
2368
- function getRepoUser(octokit, username) {
2369
- return __async(this, null, function* () {
2370
- try {
2371
- const user = yield octokit.request("GET /users/{username}", {
2372
- username
2373
- });
2374
- return user.data;
2375
- } catch (error) {
2376
- import_log10.default.warn(`Getting user for GitHub has been failed. Username: ${username}. Error: ${error}`);
2377
- return null;
2378
- }
2379
- });
2380
- }
2381
- function getRepoCommitByHash(httpClientByToken, hashCommit) {
2382
- return __async(this, null, function* () {
2383
- const { connector } = argv_default.getConfig();
2384
- const neededProperties = ["owner" /* OWNER */, "repo" /* REPO */];
2385
- const validatedFileds = validateConnectorFields("github" /* GITHUB */, neededProperties, connector);
2386
- if (Object.keys(validatedFileds).length === 0) {
2387
- return null;
2388
- }
2389
- try {
2390
- const commit = yield httpClientByToken.request("GET /repos/{owner}/{repo}/commits/{commit_sha}", {
2391
- owner: validatedFileds["owner" /* OWNER */],
2392
- repo: validatedFileds["repo" /* REPO */],
2393
- commit_sha: hashCommit
2394
- });
2395
- return commit.data;
2396
- } catch (error) {
2397
- import_log10.default.warn(`Getting commit by sha has been failed for GitHub. SHA commit: ${hashCommit}. Error: ${error}`);
2398
- return null;
2399
- }
2400
- });
2401
- }
2402
- var github_default = {
2403
- getRepoUser,
2404
- getRepoCommitByHash
2405
- };
2406
-
2407
- // src/vcs-connector/github.ts
2408
- var import_process2 = __toESM(require("process"));
2409
- var authorByGitEmail = /* @__PURE__ */ new Map();
2410
- var authorByPath = /* @__PURE__ */ new Map();
2411
- var contributorsByPath = /* @__PURE__ */ new Map();
2412
- var contributorsData = /* @__PURE__ */ new Map();
2413
- function getGitHubVCSConnector() {
2414
- return __async(this, null, function* () {
2415
- const { contributors } = argv_default.getConfig();
2416
- const httpClientByToken = getHttpClientByToken();
2417
- if (!httpClientByToken) {
2418
- return void 0;
2419
- }
2420
- let addNestedContributorsForPath = () => {
2421
- };
2422
- let getContributorsByPath = () => Promise.resolve({});
2423
- const getExternalAuthorByPath = (path) => {
2424
- var _a;
2425
- return (_a = authorByPath.get(path)) != null ? _a : null;
2426
- };
2427
- if (contributors) {
2428
- yield getAllContributorsTocFiles(httpClientByToken);
2429
- addNestedContributorsForPath = (path, nestedContributors) => addNestedContributorsForPathFunction(path, nestedContributors);
2430
- getContributorsByPath = (path) => __async(this, null, function* () {
2431
- return getFileContributorsByPath(path);
2432
- });
2433
- }
2434
- return {
2435
- getExternalAuthorByPath,
2436
- addNestedContributorsForPath,
2437
- getContributorsByPath,
2438
- getUserByLogin: (login) => getUserByLogin(httpClientByToken, login)
2439
- };
2440
- });
2441
- }
2442
- function getHttpClientByToken() {
2443
- const { connector, contributors } = argv_default.getConfig();
2444
- if (!contributors) {
2445
- return null;
2446
- }
2447
- const neededProperties = ["token" /* TOKEN */, "endpoint" /* ENDPOINT */];
2448
- const validatedFileds = validateConnectorFields("github" /* GITHUB */, neededProperties, connector);
2449
- if (Object.keys(validatedFileds).length === 0) {
2450
- return null;
2451
- }
2452
- const octokit = new import_core.Octokit({
2453
- auth: validatedFileds["token" /* TOKEN */],
2454
- baseUrl: validatedFileds["endpoint" /* ENDPOINT */]
2455
- });
2456
- return octokit;
2457
- }
2458
- function getAllContributorsTocFiles(httpClientByToken) {
2459
- return __async(this, null, function* () {
2460
- const { rootInput } = argv_default.getConfig();
2461
- const options = {
2462
- baseDir: rootInput
2463
- };
2464
- logger.info("", GETTING_ALL_CONTRIBUTORS);
2465
- const masterDir = "./_yfm-master";
2466
- const tmpMasterBranch = "yfm-tmp-master";
2467
- try {
2468
- yield (0, import_simple_git.default)(options).raw("worktree", "add", "-b", tmpMasterBranch, masterDir, "origin/master");
2469
- const fullRepoLogString = yield (0, import_simple_git.default)({
2470
- baseDir: (0, import_path21.join)(rootInput, masterDir)
2471
- }).raw(
2472
- "log",
2473
- `${FIRST_COMMIT_FROM_ROBOT_IN_GITHUB}..HEAD`,
2474
- "--pretty=format:%ae, %an, %H",
2475
- "--name-only"
2476
- );
2477
- const repoLogs = fullRepoLogString.split("\n\n");
2478
- if (import_process2.default.env.ENABLE_EXPERIMANTAL_AUTHORS) {
2479
- const fullAuthorRepoLogString = yield (0, import_simple_git.default)({
2480
- baseDir: (0, import_path21.join)(rootInput, masterDir)
2481
- }).raw(
2482
- "log",
2483
- `${FIRST_COMMIT_FROM_ROBOT_IN_GITHUB}..HEAD`,
2484
- "--diff-filter=A",
2485
- "--pretty=format:%ae;%an;%H",
2486
- "--name-only"
2487
- );
2488
- const authorRepoLog = fullAuthorRepoLogString.split("\n\n");
2489
- yield matchAuthorsForEachPath(authorRepoLog, httpClientByToken);
2490
- }
2491
- yield matchContributionsForEachPath(repoLogs, httpClientByToken);
2492
- } finally {
2493
- yield (0, import_simple_git.default)(options).raw("worktree", "remove", masterDir);
2494
- yield (0, import_simple_git.default)(options).raw("branch", "-d", tmpMasterBranch);
2495
- }
2496
- logger.info("", ALL_CONTRIBUTORS_RECEIVED);
2497
- });
2498
- }
2499
- function matchContributionsForEachPath(repoLogs, httpClientByToken) {
2500
- return __async(this, null, function* () {
2501
- for (const repoLog of repoLogs) {
2502
- if (!repoLog) {
2503
- continue;
2504
- }
2505
- const dataArray = repoLog.split("\n");
2506
- const userData = dataArray[0];
2507
- const [email, name4, hashCommit] = userData.split(", ");
2508
- if (shouldAuthorBeIgnored({ email, name: name4 })) {
2509
- continue;
2510
- }
2511
- const hasContributorData = contributorsData.get(email);
2512
- let contributorDataByHash;
2513
- if (hasContributorData === void 0) {
2514
- logger.info("Contributors: Getting data for", email);
2515
- contributorDataByHash = yield getContributorDataByHashCommit(httpClientByToken, hashCommit);
2516
- if (contributorDataByHash) {
2517
- const paths = dataArray.splice(1);
2518
- addContributorForPath(paths, {
2519
- [email]: contributorDataByHash
2520
- });
2521
- }
2522
- contributorsData.set(email, contributorDataByHash);
2523
- } else if (hasContributorData) {
2524
- const paths = dataArray.splice(1);
2525
- addContributorForPath(paths, {
2526
- [email]: hasContributorData
2527
- });
2528
- }
2529
- }
2530
- });
2531
- }
2532
- function matchAuthorsForEachPath(authorRepoLogs, httpClientByToken) {
2533
- return __async(this, null, function* () {
2534
- for (const repoLog of authorRepoLogs) {
2535
- if (!repoLog) {
2536
- continue;
2537
- }
2538
- const dataArray = repoLog.split("\n");
2539
- const [userData, ...paths] = dataArray;
2540
- const [email, name4, hashCommit] = userData.split(";");
2541
- if (shouldAuthorBeIgnored({ email, name: name4 })) {
2542
- continue;
2543
- }
2544
- yield getAuthorByPaths({ email, hashCommit }, paths, httpClientByToken);
2545
- }
2546
- });
2547
- }
2548
- function getContributorDataByHashCommit(httpClientByToken, hashCommit) {
2549
- return __async(this, null, function* () {
2550
- const repoCommit = yield github_default.getRepoCommitByHash(httpClientByToken, hashCommit);
2551
- if (!repoCommit) {
2552
- return null;
2553
- }
2554
- const { author, commit } = repoCommit;
2555
- if (!author) {
2556
- return null;
2557
- }
2558
- const { avatar_url: avatar, html_url: url2, login } = author;
2559
- return {
2560
- avatar,
2561
- email: commit.author.email,
2562
- login,
2563
- name: commit.author.name,
2564
- url: url2
2565
- };
2566
- });
2567
- }
2568
- function getAuthorByPaths(commitInfo, paths, httpClientByToken) {
2569
- return __async(this, null, function* () {
2570
- for (const path of paths) {
2571
- if (!path) {
2572
- continue;
2573
- }
2574
- const normalizePath = (0, import_path21.normalize)(addSlashPrefix(path));
2575
- const { email, hashCommit } = commitInfo;
2576
- let authorToReturn = authorByGitEmail.get(email) || null;
2577
- if (!authorToReturn) {
2578
- logger.info("Authors: Getting data for", email);
2579
- const repoCommit = yield github_default.getRepoCommitByHash(httpClientByToken, hashCommit);
2580
- if (!repoCommit) {
2581
- continue;
2582
- }
2583
- const { author, commit } = repoCommit;
2584
- if (!author) {
2585
- continue;
2586
- }
2587
- const { avatar_url: avatar, html_url: url2, login } = author;
2588
- authorToReturn = {
2589
- avatar,
2590
- email: commit.author.email,
2591
- login,
2592
- name: commit.author.name,
2593
- url: url2
2594
- };
2595
- authorByGitEmail.set(email, authorToReturn);
2596
- }
2597
- authorByPath.set(normalizePath, authorToReturn);
2598
- }
2599
- });
2600
- }
2601
- function getFileContributorsByPath(path) {
2602
- return __async(this, null, function* () {
2603
- if (contributorsData.size === 0 || !contributorsByPath.has(path)) {
2604
- return {};
2605
- }
2606
- return contributorsByPath.get(path);
2607
- });
2608
- }
2609
- function getUserByLogin(octokit, userLogin) {
2610
- return __async(this, null, function* () {
2611
- const user = yield github_default.getRepoUser(octokit, userLogin);
2612
- if (!user) {
2613
- return null;
2614
- }
2615
- const { avatar_url: avatar, html_url: url2, email, login, name: name4 } = user;
2616
- return {
2617
- avatar,
2618
- email,
2619
- login,
2620
- name: name4,
2621
- url: url2
2622
- };
2623
- });
2624
- }
2625
- function addNestedContributorsForPathFunction(path, nestedContributors) {
2626
- addContributorForPath([path], nestedContributors, true);
2627
- }
2628
- function addContributorForPath(paths, newContributor, hasIncludes = false) {
2629
- paths.forEach((path) => {
2630
- const normalizePath = (0, import_path21.normalize)(addSlashPrefix(path));
2631
- if (!contributorsByPath.has(normalizePath)) {
2632
- contributorsByPath.set(normalizePath, {
2633
- contributors: newContributor,
2634
- hasIncludes
2635
- });
2636
- return;
2637
- }
2638
- const oldContributors = contributorsByPath.get(normalizePath);
2639
- contributorsByPath.set(normalizePath, {
2640
- contributors: __spreadValues(__spreadValues({}, oldContributors == null ? void 0 : oldContributors.contributors), newContributor),
2641
- hasIncludes
2642
- });
2643
- });
2644
- }
2645
- function shouldAuthorBeIgnored({ email, name: name4 }) {
2646
- if (!(email || name4)) {
2647
- return false;
2648
- }
2649
- const { ignoreAuthorPatterns } = argv_default.getConfig();
2650
- if (!ignoreAuthorPatterns) {
2651
- return false;
2652
- }
2653
- for (const pattern of ignoreAuthorPatterns) {
2654
- if (email && (0, import_minimatch.minimatch)(email, pattern)) {
2655
- return true;
2656
- }
2657
- if (name4 && (0, import_minimatch.minimatch)(name4, pattern)) {
2658
- return true;
2659
- }
2660
- }
2661
- return false;
2662
- }
2663
- var github_default2 = getGitHubVCSConnector;
2664
-
2665
- // src/vcs-connector/index.ts
2666
- function getVCSConnector() {
2667
- return __async(this, null, function* () {
2668
- const { connector } = argv_default.getConfig();
2669
- const connectorType = process.env.VCS_CONNECTOR_TYPE || connector && connector.type;
2670
- switch (connectorType) {
2671
- case "github" /* GITHUB */:
2672
- return github_default2();
2673
- default:
2674
- return void 0;
2675
- }
2676
- });
2677
- }
2678
-
2679
- // src/steps/processPages.ts
2680
- var singlePageResults = {};
2681
- var singlePagePaths = {};
2682
- function processPages(outputBundlePath) {
2683
- return __async(this, null, function* () {
2684
- const {
2685
- input: inputFolderPath,
2686
- output: outputFolderPath,
2687
- outputFormat,
2688
- singlePage,
2689
- resolveConditions
2690
- } = argv_default.getConfig();
2691
- const vcsConnector = yield getVCSConnector();
2692
- plugins_exports.setPlugins();
2693
- const navigationPaths2 = tocs_default.getNavigationPaths();
2694
- const concurrency = 500;
2695
- yield (0, import_async.mapLimit)(navigationPaths2, concurrency, (0, import_async.asyncify)((pathToFile) => __async(this, null, function* () {
2696
- const pathData = getPathData(pathToFile, inputFolderPath, outputFolderPath, outputFormat, outputBundlePath);
2697
- logger.proc(pathToFile);
2698
- const metaDataOptions = getMetaDataOptions(pathData, inputFolderPath.length, vcsConnector);
2699
- yield preparingPagesByOutputFormat(pathData, metaDataOptions, resolveConditions, singlePage);
2700
- })));
2701
- if (singlePage) {
2702
- yield saveSinglePages(outputBundlePath);
2703
- }
2704
- });
2705
- }
2706
- function getPathData(pathToFile, inputFolderPath, outputFolderPath, outputFormat, outputBundlePath) {
2707
- const pathToDir = (0, import_path22.dirname)(pathToFile);
2708
- const filename = (0, import_path22.basename)(pathToFile);
2709
- const fileExtension = (0, import_path22.extname)(pathToFile);
2710
- const fileBaseName = (0, import_path22.basename)(filename, fileExtension);
2711
- const outputDir = (0, import_path22.resolve)(outputFolderPath, pathToDir);
2712
- const outputFileName = `${fileBaseName}.${outputFormat}`;
2713
- const outputPath = (0, import_path22.resolve)(outputDir, outputFileName);
2714
- const resolvedPathToFile = (0, import_path22.resolve)(inputFolderPath, pathToFile);
2715
- const outputTocDir = tocs_default.getTocDir(resolvedPathToFile);
2716
- const pathData = {
2717
- pathToFile,
2718
- resolvedPathToFile,
2719
- filename,
2720
- fileBaseName,
2721
- fileExtension,
2722
- outputDir,
2723
- outputPath,
2724
- outputFormat,
2725
- outputBundlePath,
2726
- outputTocDir,
2727
- inputFolderPath,
2728
- outputFolderPath
2729
- };
2730
- return pathData;
2731
- }
2732
- function saveSinglePages(outputBundlePath) {
2733
- return __async(this, null, function* () {
2734
- const {
2735
- input: inputFolderPath,
2736
- output: outputFolderPath,
2737
- lang,
2738
- resources
2739
- } = argv_default.getConfig();
2740
- try {
2741
- yield Promise.all(Object.keys(singlePageResults).map((tocDir) => __async(this, null, function* () {
2742
- if (!singlePageResults[tocDir].length) {
2743
- return;
2744
- }
2745
- const singlePageBody = joinSinglePageResults(singlePageResults[tocDir], inputFolderPath, tocDir);
2746
- const tocPath = (0, import_path22.join)((0, import_path22.relative)(inputFolderPath, tocDir), "toc.yaml");
2747
- const toc = tocs_default.getForPath(tocPath) || null;
2748
- const preparedToc = transformTocForSinglePage(toc, {
2749
- root: inputFolderPath,
2750
- currentPath: (0, import_path22.join)(tocDir, SINGLE_PAGE_FILENAME)
2751
- });
2752
- const pageData = {
2753
- data: {
2754
- leading: false,
2755
- html: singlePageBody,
2756
- headings: [],
2757
- meta: resources || {},
2758
- toc: preparedToc
2759
- },
2760
- router: {
2761
- pathname: SINGLE_PAGE_FILENAME
2762
- },
2763
- lang: lang || "ru" /* RU */
2764
- };
2765
- const outputTocDir = (0, import_path22.resolve)(outputFolderPath, (0, import_path22.relative)(inputFolderPath, tocDir));
2766
- const relativeOutputBundlePath = (0, import_path22.relative)(outputTocDir, outputBundlePath);
2767
- const singlePageFn = (0, import_path22.join)(tocDir, SINGLE_PAGE_FILENAME);
2768
- const singlePageDataFn = (0, import_path22.join)(tocDir, SINGLE_PAGE_DATA_FILENAME);
2769
- const singlePageContent = generateStaticMarkup(pageData, relativeOutputBundlePath);
2770
- (0, import_fs10.writeFileSync)(singlePageFn, singlePageContent);
2771
- (0, import_fs10.writeFileSync)(singlePageDataFn, JSON.stringify(pageData));
2772
- })));
2773
- } catch (error) {
2774
- console.log(error);
2775
- }
2776
- });
2777
- }
2778
- function savePageResultForSinglePage(pageProps, pathData) {
2779
- const { pathToFile, outputTocDir } = pathData;
2780
- if (pageProps.data.leading) {
2781
- return;
2782
- }
2783
- singlePagePaths[outputTocDir] = singlePagePaths[outputTocDir] || /* @__PURE__ */ new Set();
2784
- if (singlePagePaths[outputTocDir].has(pathToFile)) {
2785
- return;
2786
- }
2787
- singlePagePaths[outputTocDir].add(pathToFile);
2788
- singlePageResults[outputTocDir] = singlePageResults[outputTocDir] || [];
2789
- singlePageResults[outputTocDir].push({
2790
- path: pathToFile,
2791
- content: pageProps.data.html,
2792
- title: pageProps.data.title
2793
- });
2794
- }
2795
- function getMetaDataOptions(pathData, inputFolderPathLength, vcsConnector) {
2796
- const { contributors, addSystemMeta, resources, allowCustomResources } = argv_default.getConfig();
2797
- const metaDataOptions = {
2798
- vcsConnector,
2799
- fileData: {
2800
- tmpInputFilePath: pathData.resolvedPathToFile,
2801
- inputFolderPathLength,
2802
- fileContent: ""
2803
- },
2804
- isContributorsEnabled: Boolean(contributors && vcsConnector),
2805
- addSystemMeta
2806
- };
2807
- if (allowCustomResources && resources) {
2808
- const allowedResources = Object.entries(resources).reduce((acc, [key, val]) => {
2809
- if (Object.keys(ResourceType).includes(key)) {
2810
- acc[key] = val;
2811
- }
2812
- return acc;
2813
- }, {});
2814
- metaDataOptions.resources = allowedResources;
2815
- }
2816
- return metaDataOptions;
2817
- }
2818
- function preparingPagesByOutputFormat(path, metaDataOptions, resolveConditions, singlePage) {
2819
- return __async(this, null, function* () {
2820
- const {
2821
- filename,
2822
- fileExtension,
2823
- fileBaseName,
2824
- outputDir,
2825
- resolvedPathToFile,
2826
- outputFormat,
2827
- pathToFile
2828
- } = path;
2829
- const { allowCustomResources } = argv_default.getConfig();
2830
- try {
2831
- import_shelljs6.default.mkdir("-p", outputDir);
2832
- const isYamlFileExtension = fileExtension === ".yaml";
2833
- if (resolveConditions && fileBaseName === "index" && isYamlFileExtension) {
2834
- leading_default.filterFile(pathToFile);
2835
- }
2836
- if (outputFormat === "md" && isYamlFileExtension && allowCustomResources) {
2837
- processingYamlFile(path, metaDataOptions);
2838
- return;
2839
- }
2840
- if (outputFormat === "md" && isYamlFileExtension || outputFormat === "html" && !isYamlFileExtension && fileExtension !== ".md") {
2841
- copyFileWithoutChanges(resolvedPathToFile, outputDir, filename);
2842
- return;
2843
- }
2844
- switch (outputFormat) {
2845
- case "md":
2846
- yield processingFileToMd(path, metaDataOptions);
2847
- return;
2848
- case "html": {
2849
- const resolvedFileProps = yield processingFileToHtml(path, metaDataOptions);
2850
- if (singlePage) {
2851
- savePageResultForSinglePage(resolvedFileProps, path);
2852
- }
2853
- return;
2854
- }
2855
- }
2856
- } catch (e) {
2857
- const message = `No such file or has no access to ${(0, import_chalk4.bold)(resolvedPathToFile)}`;
2858
- console.log(message, e);
2859
- import_log11.default.error(message);
2860
- }
2861
- });
2862
- }
2863
- function processingYamlFile(path, metaDataOptions) {
2864
- const { pathToFile, outputFolderPath, inputFolderPath } = path;
2865
- const filePath = (0, import_path22.resolve)(inputFolderPath, pathToFile);
2866
- const content = (0, import_fs10.readFileSync)(filePath, "utf8");
2867
- const parsedContent = (0, import_js_yaml7.load)(content);
2868
- if (metaDataOptions.resources) {
2869
- parsedContent.meta = __spreadValues(__spreadValues({}, parsedContent.meta), metaDataOptions.resources);
2870
- }
2871
- (0, import_fs10.writeFileSync)((0, import_path22.resolve)(outputFolderPath, pathToFile), (0, import_js_yaml7.dump)(parsedContent));
2872
- }
2873
- function copyFileWithoutChanges(resolvedPathToFile, outputDir, filename) {
2874
- const from = resolvedPathToFile;
2875
- const to = (0, import_path22.resolve)(outputDir, filename);
2876
- (0, import_fs10.copyFileSync)(from, to);
2877
- }
2878
- function processingFileToMd(path, metaDataOptions) {
2879
- return __async(this, null, function* () {
2880
- const { outputPath, pathToFile } = path;
2881
- yield resolveMd2Md({
2882
- inputPath: pathToFile,
2883
- outputPath,
2884
- metadata: metaDataOptions
2885
- });
2886
- });
2887
- }
2888
- function processingFileToHtml(path, metaDataOptions) {
2889
- return __async(this, null, function* () {
2890
- const {
2891
- outputBundlePath,
2892
- filename,
2893
- fileExtension,
2894
- outputPath,
2895
- pathToFile
2896
- } = path;
2897
- return resolveMd2HTML({
2898
- inputPath: pathToFile,
2899
- outputBundlePath,
2900
- fileExtension,
2901
- outputPath,
2902
- filename,
2903
- metadata: metaDataOptions
2904
- });
2905
- });
2906
- }
2907
-
2908
- // src/steps/processLinter.ts
2909
- var import_log12 = __toESM(require("@doc-tools/transform/lib/log"));
2910
- var import_threads = require("threads");
2911
- var import_path23 = require("path");
2912
-
2913
- // src/utils/worker.ts
2914
- function splitOnChunks(array, chunkSize = 1e3) {
2915
- const chunks = [];
2916
- for (let i = 0, j = array.length; i < j; i += chunkSize) {
2917
- const chunk = array.slice(i, i + chunkSize);
2918
- chunks.push(chunk);
2919
- }
2920
- return chunks;
2921
- }
2922
-
2923
- // src/steps/processLinter.ts
2924
- var processLinterWorkers;
2925
- var navigationPathsChunks;
2926
- function processLinter() {
2927
- return __async(this, null, function* () {
2928
- const argvConfig = argv_default.getConfig();
2929
- const navigationPaths2 = tocs_default.getNavigationPaths();
2930
- if (!processLinterWorkers) {
2931
- lintPagesFallback(navigationPaths2);
2932
- return;
2933
- }
2934
- const presetStorage2 = preset_default.getPresetStorage();
2935
- processLinterWorkers.forEach((worker) => {
2936
- worker.getProcessedPages().subscribe((pathToFile) => {
2937
- logger.info(pathToFile, LINTING_FINISHED);
2938
- });
2939
- });
2940
- yield Promise.all(
2941
- processLinterWorkers.map((worker, i) => {
2942
- const navigationPathsChunk = navigationPathsChunks[i];
2943
- return worker.run({
2944
- argvConfig,
2945
- presetStorage: presetStorage2,
2946
- navigationPaths: navigationPathsChunk
2947
- });
2948
- })
2949
- );
2950
- yield Promise.all(
2951
- processLinterWorkers.map((worker) => {
2952
- return worker.finish().then((logs) => {
2953
- import_log12.default.add(logs);
2954
- });
2955
- })
2956
- );
2957
- yield Promise.all(
2958
- processLinterWorkers.map((worker) => {
2959
- return import_threads.Thread.terminate(worker);
2960
- })
2961
- );
2962
- });
2963
- }
2964
- function initLinterWorkers() {
2965
- return __async(this, null, function* () {
2966
- const navigationPaths2 = tocs_default.getNavigationPaths();
2967
- const chunkSize = getChunkSize(navigationPaths2);
2968
- if (process.env.DISABLE_PARALLEL_BUILD || chunkSize < MIN_CHUNK_SIZE || WORKERS_COUNT <= 0) {
2969
- return;
2970
- }
2971
- navigationPathsChunks = splitOnChunks(navigationPaths2, chunkSize).filter((arr) => arr.length);
2972
- const workersCount = navigationPathsChunks.length;
2973
- processLinterWorkers = yield Promise.all(new Array(workersCount).fill(null).map(() => {
2974
- return (0, import_threads.spawn)(new import_threads.Worker("./linter"), { timeout: 6e4 });
2975
- }));
2976
- });
2977
- }
2978
- function getChunkSize(arr) {
2979
- return Math.ceil(arr.length / WORKERS_COUNT);
2980
- }
2981
- function lintPagesFallback(navigationPaths2) {
2982
- plugins_exports.setPlugins();
2983
- navigationPaths2.forEach((pathToFile) => {
2984
- lintPage({
2985
- inputPath: pathToFile,
2986
- fileExtension: (0, import_path23.extname)(pathToFile),
2987
- onFinish: () => {
2988
- logger.info(pathToFile, LINTING_FINISHED);
2989
- }
2990
- });
2991
- });
2992
- }
2993
-
2994
- // src/steps/processServiceFiles.ts
2995
- var import_path24 = require("path");
2996
- var import_walk_sync4 = __toESM(require("walk-sync"));
2997
- var import_fs11 = require("fs");
2998
- var import_js_yaml8 = require("js-yaml");
2999
- var import_log13 = __toESM(require("@doc-tools/transform/lib/log"));
3000
- var import_shelljs7 = __toESM(require("shelljs"));
3001
- function processServiceFiles() {
3002
- return __async(this, null, function* () {
3003
- const { input: inputFolderPath, ignore = [] } = argv_default.getConfig();
3004
- const getFilePathsByGlobals = (globs) => {
3005
- return (0, import_walk_sync4.default)(inputFolderPath, {
3006
- directories: false,
3007
- includeBasePath: false,
3008
- globs,
3009
- ignore
3010
- });
3011
- };
3012
- preparingPresetFiles(getFilePathsByGlobals);
3013
- yield preparingTocFiles(getFilePathsByGlobals);
3014
- });
3015
- }
3016
- function preparingPresetFiles(getFilePathsByGlobals) {
3017
- const {
3018
- input: inputFolderPath,
3019
- varsPreset = "",
3020
- outputFormat,
3021
- applyPresets,
3022
- resolveConditions
3023
- } = argv_default.getConfig();
3024
- try {
3025
- const presetsFilePaths = getFilePathsByGlobals(["**/presets.yaml"]);
3026
- for (const path of presetsFilePaths) {
3027
- logger.proc(path);
3028
- const pathToPresetFile = (0, import_path24.resolve)(inputFolderPath, path);
3029
- const content = (0, import_fs11.readFileSync)(pathToPresetFile, "utf8");
3030
- const parsedPreset = (0, import_js_yaml8.load)(content);
3031
- preset_default.add(parsedPreset, path, varsPreset);
3032
- if (outputFormat === "md" && (!applyPresets || !resolveConditions)) {
3033
- saveFilteredPresets(path, parsedPreset);
3034
- }
3035
- }
3036
- } catch (error) {
3037
- import_log13.default.error(`Preparing presets.yaml files failed. Error: ${error}`);
3038
- throw error;
3039
- }
3040
- }
3041
- function saveFilteredPresets(path, parsedPreset) {
3042
- const { output: outputFolderPath, varsPreset = "" } = argv_default.getConfig();
3043
- const outputPath = (0, import_path24.resolve)(outputFolderPath, path);
3044
- const filteredPreset = {
3045
- default: parsedPreset.default
3046
- };
3047
- if (parsedPreset[varsPreset]) {
3048
- filteredPreset[varsPreset] = parsedPreset[varsPreset];
3049
- }
3050
- const outputPreset = (0, import_js_yaml8.dump)(filteredPreset, {
3051
- lineWidth: 120
3052
- });
3053
- import_shelljs7.default.mkdir("-p", (0, import_path24.dirname)(outputPath));
3054
- (0, import_fs11.writeFileSync)(outputPath, outputPreset);
3055
- }
3056
- function preparingTocFiles(getFilePathsByGlobals) {
3057
- return __async(this, null, function* () {
3058
- try {
3059
- const tocFilePaths = getFilePathsByGlobals(["**/toc.yaml"]);
3060
- for (const path of tocFilePaths) {
3061
- logger.proc(path);
3062
- yield tocs_default.add(path);
3063
- }
3064
- } catch (error) {
3065
- import_log13.default.error(`Preparing toc.yaml files failed. Error: ${error}`);
3066
- throw error;
3067
- }
3068
- });
3069
- }
3070
-
3071
- // src/steps/processMapFile.ts
3072
- var import_fs12 = require("fs");
3073
- var import_path25 = require("path");
3074
- function prepareMapFile() {
3075
- const {
3076
- output: outputFolderPath
3077
- } = argv_default.getConfig();
3078
- const navigationPathsWithoutExtensions = tocs_default.getNavigationPaths().map((path) => {
3079
- let preparedPath = convertBackSlashToSlash(path.replace((0, import_path25.extname)(path), ""));
3080
- if (preparedPath.endsWith("/index")) {
3081
- preparedPath = preparedPath.substring(0, preparedPath.length - 5);
3082
- }
3083
- return preparedPath;
3084
- });
3085
- const navigationPaths2 = { files: [...new Set(navigationPathsWithoutExtensions)] };
3086
- const filesMapBuffer = Buffer.from(JSON.stringify(navigationPaths2, null, " "), "utf8");
3087
- const mapFile = (0, import_path25.join)(outputFolderPath, "files.json");
3088
- (0, import_fs12.writeFileSync)(mapFile, filesMapBuffer);
3089
- }
3090
-
3091
- // src/cmd/build/index.ts
3092
- var import_shelljs8 = __toESM(require("shelljs"));
3093
-
3094
- // src/cmd/publish/upload.ts
3095
- var import_fs13 = require("fs");
3096
- var import_walk_sync5 = __toESM(require("walk-sync"));
3097
- var import_path26 = require("path");
3098
- var import_client_s3 = require("@aws-sdk/client-s3");
3099
- var import_mime_types = __toESM(require("mime-types"));
3100
- var import_async2 = require("async");
3101
- function upload(props) {
3102
- return __async(this, null, function* () {
3103
- const {
3104
- input,
3105
- ignore = [],
3106
- endpoint,
3107
- region,
3108
- bucket,
3109
- prefix,
3110
- accessKeyId,
3111
- secretAccessKey
3112
- } = props;
3113
- const s3Client = new import_client_s3.S3Client({
3114
- endpoint,
3115
- region,
3116
- credentials: { accessKeyId, secretAccessKey }
3117
- });
3118
- const filesToPublish = (0, import_walk_sync5.default)((0, import_path26.resolve)(input), {
3119
- directories: false,
3120
- includeBasePath: false,
3121
- ignore
3122
- });
3123
- yield (0, import_async2.mapLimit)(filesToPublish, 100, (0, import_async2.asyncify)((pathToFile) => __async(this, null, function* () {
3124
- const mimeType = import_mime_types.default.lookup(pathToFile);
3125
- logger.upload(pathToFile);
3126
- try {
3127
- yield s3Client.send(new import_client_s3.PutObjectCommand({
3128
- ContentType: mimeType ? mimeType : void 0,
3129
- Bucket: bucket,
3130
- Key: convertBackSlashToSlash((0, import_path26.join)(prefix, pathToFile)),
3131
- Body: (0, import_fs13.createReadStream)((0, import_path26.resolve)(input, pathToFile))
3132
- }));
3133
- } catch (error) {
3134
- logger.error(pathToFile, error.message);
3135
- }
3136
- })));
3137
- });
3138
- }
3139
-
3140
- // src/cmd/build/index.ts
3141
- var import_glob3 = __toESM(require("glob"));
3142
- var build = {
3143
- command: ["build", "$0"],
3144
- description: "Build documentation in target directory",
3145
- handler,
3146
- builder
3147
- };
3148
- function builder(argv) {
3149
- return argv.option("input", {
3150
- alias: "i",
3151
- describe: "Path to input folder with .md files",
3152
- type: "string",
3153
- group: "Build options:"
3154
- }).option("output", {
3155
- alias: "o",
3156
- describe: "Path to output folder",
3157
- type: "string",
3158
- group: "Build options:"
3159
- }).option("varsPreset", {
3160
- default: "default",
3161
- describe: "Target vars preset of documentation <external|internal>",
3162
- group: "Build options:"
3163
- }).option("output-format", {
3164
- default: "html",
3165
- describe: "Format of output file <html|md>",
3166
- group: "Build options:"
3167
- }).option("vars", {
3168
- alias: "v",
3169
- default: "{}",
3170
- describe: "List of markdown variables",
3171
- group: "Build options:"
3172
- }).option("apply-presets", {
3173
- default: true,
3174
- describe: "Should apply presets. Only for --output-format=md",
3175
- type: "boolean",
3176
- group: "Build options:"
3177
- }).option("resolve-conditions", {
3178
- default: true,
3179
- describe: "Should resolve conditions. Only for --output-format=md",
3180
- type: "boolean",
3181
- group: "Build options:"
3182
- }).option("conditions-in-code", {
3183
- default: false,
3184
- describe: "Meet conditions in code blocks",
3185
- type: "boolean",
3186
- group: "Build options:"
3187
- }).option("disable-liquid", {
3188
- default: false,
3189
- describe: "Disable template engine",
3190
- type: "boolean",
3191
- group: "Build options:"
3192
- }).option("allowHTML", {
3193
- default: false,
3194
- describe: "Allow to use HTML in Markdown files",
3195
- type: "boolean",
3196
- group: "Build options:"
3197
- }).option("ignore-stage", {
3198
- default: "skip" /* SKIP */,
3199
- describe: "Ignore tocs with stage",
3200
- group: "Build options:"
3201
- }).option("ignore-author-patterns", {
3202
- default: [],
3203
- describe: "Ignore authors if they contain passed string",
3204
- group: "Build options:",
3205
- type: "array"
3206
- }).option("contributors", {
3207
- default: false,
3208
- describe: "Should attach contributors into files",
3209
- type: "boolean",
3210
- group: "Build options:"
3211
- }).option("add-system-meta", {
3212
- default: false,
3213
- describe: "Should add system section variables form presets into files meta data",
3214
- type: "boolean",
3215
- group: "Build options:"
3216
- }).option("add-map-file", {
3217
- default: false,
3218
- describe: "Should add all paths of documentation into file.json",
3219
- type: "boolean",
3220
- group: "Build options:"
3221
- }).option("single-page", {
3222
- default: false,
3223
- describe: "Beta functionality: Build a single page in the output folder also",
3224
- type: "boolean",
3225
- group: "Build options:"
3226
- }).option("publish", {
3227
- default: false,
3228
- describe: "Should upload output files to S3 storage",
3229
- type: "boolean",
3230
- group: "Build options:"
3231
- }).option("remove-hidden-toc-items", {
3232
- default: false,
3233
- describe: "Remove hidden toc items",
3234
- type: "boolean",
3235
- group: "Build options:"
3236
- }).option("lint-disabled", {
3237
- default: false,
3238
- describe: "Disable linting",
3239
- type: "boolean",
3240
- group: "Build options:"
3241
- }).option("build-disabled", {
3242
- default: false,
3243
- describe: "Disable building",
3244
- type: "boolean",
3245
- group: "Build options:"
3246
- }).option("allow-custom-resources", {
3247
- default: false,
3248
- describe: "Allow loading custom resources",
3249
- type: "boolean",
3250
- group: "Build options:"
3251
- }).option("static-content", {
3252
- default: false,
3253
- describe: "Include static content in the page",
3254
- type: "boolean",
3255
- group: "Build options:"
3256
- }).check(argvValidator).example("yfm -i ./input -o ./output", "").demandOption(["input", "output"], "Please provide input and output arguments to work with this tool");
3257
- }
3258
- function handler(args) {
3259
- return __async(this, null, function* () {
3260
- var _a;
3261
- const includers = args.includers || [import_includer.default];
3262
- const userOutputFolder = (0, import_path27.resolve)(args.output);
3263
- const tmpInputFolder = (0, import_path27.resolve)(args.output, TMP_INPUT_FOLDER);
3264
- const tmpOutputFolder = (0, import_path27.resolve)(args.output, TMP_OUTPUT_FOLDER);
3265
- console.log(args);
3266
- try {
3267
- argv_default.init(__spreadProps(__spreadValues({}, args), {
3268
- rootInput: args.input,
3269
- input: tmpInputFolder,
3270
- output: tmpOutputFolder
3271
- }));
3272
- includers_exports.init(includers);
3273
- const {
3274
- output: outputFolderPath,
3275
- outputFormat,
3276
- publish,
3277
- lintDisabled,
3278
- buildDisabled,
3279
- addMapFile,
3280
- allowCustomResources,
3281
- resources
3282
- } = argv_default.getConfig();
3283
- preparingTemporaryFolders(userOutputFolder);
3284
- yield processServiceFiles();
3285
- processExcludedFiles();
3286
- if (addMapFile) {
3287
- prepareMapFile();
3288
- }
3289
- const outputBundlePath = (0, import_path27.join)(outputFolderPath, BUNDLE_FOLDER);
3290
- const pathToConfig = args.config || (0, import_path27.join)(args.input, YFM_CONFIG_FILENAME);
3291
- const pathToRedirects = (0, import_path27.join)(args.input, REDIRECTS_FILENAME);
3292
- const pathToLintConfig = (0, import_path27.join)(args.input, LINT_CONFIG_FILENAME);
3293
- if (!lintDisabled) {
3294
- yield initLinterWorkers();
3295
- }
3296
- const processes = [
3297
- !lintDisabled && processLinter(),
3298
- !buildDisabled && processPages(outputBundlePath)
3299
- ].filter(Boolean);
3300
- yield Promise.all(processes);
3301
- if (!buildDisabled) {
3302
- switch (outputFormat) {
3303
- case "html":
3304
- processAssets(outputBundlePath);
3305
- break;
3306
- case "md": {
3307
- import_shelljs8.default.cp((0, import_path27.resolve)(pathToConfig), tmpOutputFolder);
3308
- import_shelljs8.default.cp((0, import_path27.resolve)(pathToRedirects), tmpOutputFolder);
3309
- import_shelljs8.default.cp((0, import_path27.resolve)(pathToLintConfig), tmpOutputFolder);
3310
- if (resources && allowCustomResources) {
3311
- const resourcePaths = [];
3312
- Object.keys(resources).forEach((type) => {
3313
- var _a2;
3314
- return (_a2 = resources[type]) == null ? void 0 : _a2.forEach((path) => resourcePaths.push(path));
3315
- });
3316
- copyFiles(args.input, tmpOutputFolder, resourcePaths);
3317
- }
3318
- break;
3319
- }
3320
- }
3321
- import_shelljs8.default.cp("-r", [(0, import_path27.join)(tmpOutputFolder, "*"), (0, import_path27.join)(tmpOutputFolder, ".*")], userOutputFolder);
3322
- if (publish) {
3323
- const DEFAULT_PREFIX = (_a = process.env.YFM_STORAGE_PREFIX) != null ? _a : "";
3324
- const {
3325
- ignore = [],
3326
- storageRegion,
3327
- storageEndpoint: endpoint,
3328
- storageBucket: bucket,
3329
- storagePrefix: prefix = DEFAULT_PREFIX,
3330
- storageKeyId: accessKeyId,
3331
- storageSecretKey: secretAccessKey
3332
- } = argv_default.getConfig();
3333
- yield upload({
3334
- input: userOutputFolder,
3335
- region: storageRegion,
3336
- ignore,
3337
- endpoint,
3338
- bucket,
3339
- prefix,
3340
- accessKeyId,
3341
- secretAccessKey
3342
- });
3343
- }
3344
- }
3345
- } catch (err) {
3346
- logger.error("", err.message);
3347
- } finally {
3348
- processLogs(tmpInputFolder);
3349
- import_shelljs8.default.rm("-rf", tmpInputFolder, tmpOutputFolder);
3350
- }
3351
- });
3352
- }
3353
- function preparingTemporaryFolders(userOutputFolder) {
3354
- const args = argv_default.getConfig();
3355
- import_shelljs8.default.mkdir("-p", userOutputFolder);
3356
- import_shelljs8.default.rm("-rf", args.input, args.output);
3357
- import_shelljs8.default.mkdir(args.input, args.output);
3358
- import_shelljs8.default.chmod("-R", "u+w", args.input);
3359
- copyFiles(args.rootInput, args.input, import_glob3.default.sync("**", {
3360
- cwd: args.rootInput,
3361
- nodir: true,
3362
- follow: true,
3363
- ignore: [
3364
- "node_modules/**",
3365
- "*/node_modules/**"
3366
- ]
3367
- }));
3368
- }
3369
- // Annotate the CommonJS export names for ESM import in node:
3370
- 0 && (module.exports = {
3371
- build,
3372
- handler
3373
- });
3374
- //# sourceMappingURL=lib.js.map