UNPKG

@knennigtri/merge-markdown

Version:

Prepare input files to merge together and optionally output html/pdf/word presentation

725 lines (692 loc) • 35.4 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _regeneratorRuntime() { "use strict"; var r = _regenerator(), e = r.m(_regeneratorRuntime), t = (Object.getPrototypeOf ? Object.getPrototypeOf(e) : e.__proto__).constructor; function n(r) { var e = "function" == typeof r && r.constructor; return !!e && (e === t || "GeneratorFunction" === (e.displayName || e.name)); } var o = { "throw": 1, "return": 2, "break": 3, "continue": 3 }; function a(r) { var e, t; return function (n) { e || (e = { stop: function stop() { return t(n.a, 2); }, "catch": function _catch() { return n.v; }, abrupt: function abrupt(r, e) { return t(n.a, o[r], e); }, delegateYield: function delegateYield(r, o, a) { return e.resultName = o, t(n.d, _regeneratorValues(r), a); }, finish: function finish(r) { return t(n.f, r); } }, t = function t(r, _t, o) { n.p = e.prev, n.n = e.next; try { return r(_t, o); } finally { e.next = n.n; } }), e.resultName && (e[e.resultName] = n.v, e.resultName = void 0), e.sent = n.v, e.next = n.n; try { return r.call(this, e); } finally { n.p = e.prev, n.n = e.next; } }; } return (_regeneratorRuntime = function _regeneratorRuntime() { return { wrap: function wrap(e, t, n, o) { return r.w(a(e), t, n, o && o.reverse()); }, isGeneratorFunction: n, mark: r.m, awrap: function awrap(r, e) { return new _OverloadYield(r, e); }, AsyncIterator: _regeneratorAsyncIterator, async: function async(r, e, t, o, u) { return (n(e) ? _regeneratorAsyncGen : _regeneratorAsync)(a(r), e, t, o, u); }, keys: _regeneratorKeys, values: _regeneratorValues }; })(); } function _regeneratorValues(e) { if (null != e) { var t = e["function" == typeof Symbol && Symbol.iterator || "@@iterator"], r = 0; if (t) return t.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) return { next: function next() { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e }; } }; } throw new TypeError(_typeof(e) + " is not iterable"); } function _regeneratorKeys(e) { var n = Object(e), r = []; for (var t in n) r.unshift(t); return function e() { for (; r.length;) if ((t = r.pop()) in n) return e.value = t, e.done = !1, e; return e.done = !0, e; }; } function _regeneratorAsync(n, e, r, t, o) { var a = _regeneratorAsyncGen(n, e, r, t, o); return a.next().then(function (n) { return n.done ? n.value : a.next(); }); } function _regeneratorAsyncGen(r, e, t, o, n) { return new _regeneratorAsyncIterator(_regenerator().w(r, e, t, o), n || Promise); } function _regeneratorAsyncIterator(t, e) { function n(r, o, i, f) { try { var c = t[r](o), u = c.value; return u instanceof _OverloadYield ? e.resolve(u.v).then(function (t) { n("next", t, i, f); }, function (t) { n("throw", t, i, f); }) : e.resolve(u).then(function (t) { c.value = t, i(c); }, function (t) { return n("throw", t, i, f); }); } catch (t) { f(t); } } var r; this.next || (_regeneratorDefine2(_regeneratorAsyncIterator.prototype), _regeneratorDefine2(_regeneratorAsyncIterator.prototype, "function" == typeof Symbol && Symbol.asyncIterator || "@asyncIterator", function () { return this; })), _regeneratorDefine2(this, "_invoke", function (t, o, i) { function f() { return new e(function (e, r) { n(t, i, e, r); }); } return r = r ? r.then(f, f) : f(); }, !0); } function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); } function _OverloadYield(e, d) { this.v = e, this.k = d; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } var manifestUtil = require("./manifest.js"); var path = require("path"); var fs = require("fs"); var mkdirp = require("mkdirp"); var concat = require("concat"); var markdownLinkCheck = require("markdown-link-check"); var doctoc = require("doctoc/lib/transform"); var validUrl = require("valid-url"); var _require = require("url"), pathToFileURL = _require.pathToFileURL; var assetCleaner = require("./asset-cleaner.js"); var debug = require("debug")("merge"); var debugRelLinks = require("debug")("rellinks"); var debugYaml = require("debug")("o:yaml"); var debugDoctoc = require("debug")("o:doctoc"); var debugReplace = require("debug")("o:replace"); var debugLinkcheck = require("debug")("linkcheck"); var debugLinkcheckDeep = require("debug")("linkcheck:deep"); var debugAssetPreview = require("debug")("asset-preview"); var EXTS = { "linkcheck": ".linkcheck.md", "qa": ".qa.md" }; exports.EXTS = EXTS; exports.debbugOptions = { "merge": "messages for merge process", "rellinks": "relative links", "o:yaml": "yaml removal", "o:doctoc": "doctoc messages", "o:replace": "regex replace messages", "linkcheck": "linkcheck validation", "linkcheck:deep": "deep linkcheck validation", "asset-preview": "asset preview scanning during merge" }; function start(_x, _x2, _x3, _x4, _x5, _x6) { return _start.apply(this, arguments); } function _start() { _start = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(manifestFileStr, qaMode, skip_linkcheck, maintainAssetPaths, lang, disableAssetPreview) { var onlyQA, skipLinkcheck, keepAssetPaths, skipAssetPreview, manifest, manifestOutputName, manifestInput, manifestQARegEx, fileArr, refFileArr, linkcheckPromises, results, linkcheckFile; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: onlyQA = qaMode || false; skipLinkcheck = skip_linkcheck || false; keepAssetPaths = maintainAssetPaths || false; skipAssetPreview = disableAssetPreview || false; //Set manifest variables manifest = manifestUtil.getJSON_withABSPaths(manifestFileStr, onlyQA, lang); manifestOutputName = manifest.output.name; manifestInput = manifest.input; if (manifest.qa) manifestQARegEx = new RegExp(manifest.qa.exclude); if (onlyQA) console.log("QA exclude regex: " + manifestQARegEx); if (skipLinkcheck) console.log("Skipping linkcheck on all files");else debugLinkcheck("Phase 1: Checking assets and external URLs per file..."); //removes old .md files deleteGeneratedFiles([manifestOutputName, replaceExtension(manifestOutputName, EXTS.linkcheck), replaceExtension(manifestOutputName, EXTS.qa)]); //Iterate through all of the input files in manifest apply options fileArr = []; refFileArr = []; linkcheckPromises = []; console.log("\nšŸ“ Processing input files..."); Object.keys(manifestInput).forEach(function (inputKey) { var inputFileName = path.basename(inputKey); if (onlyQA && manifestQARegEx.test(inputFileName)) { console.log(" ⊘ " + inputFileName + " (excluded for QA)"); return; } if (!fs.existsSync(inputKey)) { var isAbsolutePath = path.isAbsolute(inputKey); if (isAbsolutePath) { console.log(inputKey + " (file not found)"); console.log("WARN: Absolute file paths in the manifest are not supported in Docker mode. Consider using relative paths instead."); } else { console.log("⚠ " + inputKey + " (file not found)"); } return; } console.log(" āœ“ " + inputFileName); var origContent = fs.readFileSync(inputKey, "utf-8"); //Force a new line at the end of the file to help with file merging origContent += "\n"; //updates all relative asset paths to the relative output location var generatedContent = updateAssetRelPaths(origContent, path.dirname(inputKey), path.dirname(manifestOutputName), keepAssetPaths); debug("--Apply file OPTIONS--"); //Content, local options, global options generatedContent = applyContentOptions(generatedContent, manifestInput[inputKey], manifest); var tempFile = inputKey + ".temp"; fs.writeFileSync(tempFile, generatedContent); if (!skipLinkcheck) { //Phase 1: checks for broken assets and external URLs debug("--Phase 1 linkcheck: assets and URLs--"); linkcheckPromises.push(checkAssetsAndUrls(tempFile, inputFileName)); } //add the temp file to the list to merge together fileArr.push(tempFile); debug(path.basename(tempFile)); }); // Wait for all Phase 1 linkchecks to complete, then write results in order if (!(!skipLinkcheck && linkcheckPromises.length > 0)) { _context.next = 27; break; } _context.next = 19; return Promise.all(linkcheckPromises); case 19: results = _context.sent; // Write Phase 1 results in order linkcheckFile = replaceExtension(manifestOutputName, EXTS.linkcheck); results.forEach(function (result) { if (fs.existsSync(linkcheckFile)) { safelyWriteFile(linkcheckFile, result, true); } else { safelyWriteFile(linkcheckFile, result, false); } }); // Continue with merge _context.next = 24; return continueMerge(fileArr, refFileArr, manifestOutputName, onlyQA, manifest, skipAssetPreview, skipLinkcheck); case 24: return _context.abrupt("return", _context.sent); case 27: _context.next = 29; return continueMerge(fileArr, refFileArr, manifestOutputName, onlyQA, manifest, skipAssetPreview, skipLinkcheck); case 29: return _context.abrupt("return", _context.sent); case 30: case "end": return _context.stop(); } }, _callee); })); return _start.apply(this, arguments); } function continueMerge(_x7, _x8, _x9, _x10, _x11, _x12, _x13) { return _continueMerge.apply(this, arguments); } function _continueMerge() { _continueMerge = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(fileArr, refFileArr, manifestOutputName, onlyQA, manifest, skipAssetPreview, skipLinkcheck) { var mergedFileArr, fileToWrite; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: //Merge lists and output single markdown file mergedFileArr = fileArr.concat(refFileArr); if (mergedFileArr.length > 0) { console.log("\nšŸ”— Merging " + mergedFileArr.length + " file(s)..."); } fileToWrite = manifestOutputName; if (onlyQA) { fileToWrite = replaceExtension(fileToWrite, EXTS.qa); } _context2.next = 6; return createSingleFile(mergedFileArr, fileToWrite, manifest, skipAssetPreview, skipLinkcheck); case 6: return _context2.abrupt("return", _context2.sent); case 7: case "end": return _context2.stop(); } }, _callee2); })); return _continueMerge.apply(this, arguments); } function createSingleFile(_x14, _x15, _x16, _x17, _x18) { return _createSingleFile.apply(this, arguments); } //Content, local options, global options function _createSingleFile() { _createSingleFile = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(list, fileToWrite, manifestJSON, skipAssetPreview, skipLinkcheck) { var resultContent, contentToWrite, result; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: debug("Creating single file"); if (!(!list || list.length === 0)) { _context3.next = 4; break; } console.log("List to merge is not valid. Aborting.."); throw new Error("Invalid list"); case 4: _context3.prev = 4; _context3.next = 7; return concat(list); case 7: resultContent = _context3.sent; contentToWrite = resultContent; if (Object.prototype.hasOwnProperty.call(manifestJSON.output, "doctoc") && manifestJSON.output.doctoc) { contentToWrite = optionBuildTOC(resultContent, manifestJSON.output.doctoc, manifestJSON.doctoc); } result = safelyWriteFile(fileToWrite, contentToWrite); deleteGeneratedFiles(list); //cleanup // Phase 2: Check internal anchors in merged file if (!skipLinkcheck) { debugLinkcheck("Phase 2: Checking internal anchors in merged file..."); console.log("šŸ” Validating internal anchors..."); checkAnchors(fileToWrite, replaceExtension(fileToWrite, EXTS.linkcheck)); } // Run asset preview scan if not disabled if (!skipAssetPreview && manifestJSON.input) { runAssetPreviewScan(Object.keys(manifestJSON.input)); } return _context3.abrupt("return", result); case 17: _context3.prev = 17; _context3.t0 = _context3["catch"](4); console.error("Error creating single file:", _context3.t0); throw _context3.t0; case 21: case "end": return _context3.stop(); } }, _callee3, null, [[4, 17]]); })); return _createSingleFile.apply(this, arguments); } function applyContentOptions(origContent, fileOptions, globalOptions) { var scrubbedContent = origContent; // if(fileOptions == undefined || fileOptions == "") return scrubbedContent; /* Apply noYAML */ //Apply local noYAML option if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "noYAML")) { if (fileOptions.noYAML) { debug("Using [Local] noYAML..."); scrubbedContent = optionRemoveYAML(origContent); } } else //Apply global noYAML option if (Object.prototype.hasOwnProperty.call(globalOptions, "noYAML") && globalOptions.noYAML) { debug("Using [Global] noYAML..."); scrubbedContent = optionRemoveYAML(origContent); } /* Apply find and replace */ //Apply local replace if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "replace")) { // merge global replace with local replace taking precedence if (Object.prototype.hasOwnProperty.call(globalOptions, "replace")) { for (var key in globalOptions.replace) { if (!Object.prototype.hasOwnProperty.call(fileOptions.replace, key)) { fileOptions.replace[key] = globalOptions.replace[key]; } } debug("Using [Local/Global] Find/Replace..."); scrubbedContent = optionReplaceStrings(scrubbedContent, fileOptions.replace); } else { debug("Using [Local] Find/Replace..."); scrubbedContent = optionReplaceStrings(scrubbedContent, fileOptions.replace); } } else //Apply global replace if (Object.prototype.hasOwnProperty.call(globalOptions, "replace")) { debug("Using [Global] Find/Replace..."); scrubbedContent = optionReplaceStrings(scrubbedContent, globalOptions.replace); } //Add TOC if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "doctoc")) { if (fileOptions.doctoc) { if (Object.prototype.hasOwnProperty.call(globalOptions, "doctoc")) { debugDoctoc("Using [Local/Global] DocToc..."); scrubbedContent = optionBuildTOC(scrubbedContent, fileOptions.doctoc, globalOptions.doctoc); } else { debugDoctoc("Using [Local] DocToc..."); scrubbedContent = optionBuildTOC(scrubbedContent, fileOptions.doctoc); } } } else if (Object.prototype.hasOwnProperty.call(globalOptions, "doctoc") && globalOptions.doctoc) { debug("Using [Global] DocToc..."); scrubbedContent = optionBuildTOC(scrubbedContent, null, globalOptions.doctoc); } return scrubbedContent; } /** Searches for ![*](relPath) or src="relPath" in a string and replaces the asset * relPath with an absolute one */ function updateAssetRelPaths(fileContents, inputFilePath, mergedFilePath, keepAssetPaths) { var resultContent = []; var regex = /(!\[(.*?)\][(](.*?)[)])|(src=[""](.*?)[""])/g; debug("Rewriting Relative Asset Paths"); debugRelLinks("inputFilePath: " + inputFilePath); debugRelLinks("mergedFilePath: " + mergedFilePath); //Go through each line that and var lines = fileContents.split("\n"); var count = 0; lines.forEach(function (line, lineNumber) { var match = line.match(regex); var origAssetPath = ""; //if found capture relPath if (match != null) { var origStr = match[0]; //Example: ![my-asset](links/my-asset.jpg) if (origStr.startsWith("![")) { origAssetPath = origStr.substring(origStr.indexOf("(") + 1, origStr.indexOf(")")); } else { //Example: src="links/my-asset.jpg" if (origStr.startsWith("src=")) { origAssetPath = origStr.substring(origStr.indexOf("\"") + 1, origStr.lastIndexOf("\"")); } } //Check if path is a URL if (!validUrl.isUri(origAssetPath)) { var newAssetPath = ""; //patch for issue #51 if (path.isAbsolute(origAssetPath)) { debugRelLinks("OrigPath"); newAssetPath = origAssetPath; } else { //resolve the asset path newAssetPath = path.resolve(inputFilePath, origAssetPath); if (keepAssetPaths) { debugRelLinks("relPath"); newAssetPath = path.relative(mergedFilePath, newAssetPath); } } count++; debugRelLinks("[Line " + lineNumber + "]: " + origAssetPath); debugRelLinks("Updated to: " + newAssetPath); var newLine = line.replace(origAssetPath, newAssetPath); resultContent.push(newLine); } else { resultContent.push(line); } } else { resultContent.push(line); } }); debugRelLinks(count + " relative paths updated"); return resultContent.join("\n"); } /** Removes the YAML at the top of a markdown file * @param {*} fileContents String that might contain YAML * @returns String that has YAML removed */ function optionRemoveYAML(fileContents) { debug("[OPTION] Remove YAML..."); var resultContent = fileContents; var lines = fileContents.split("\n"); var i = 0; var startYAML = -1; var endYAML = -1; for (i = 0; i < lines.length; i++) { var line = lines[i]; //Stop searching if there is no YAML if (i == 0 && line != "---") { break; } //Set start/end YAML indexes if they are available if (i == 0 && startYAML == -1 && line == "---") { startYAML = i; } else if (startYAML != -1 && endYAML == -1 && line == "---") { endYAML = i; break; } //TODO YAML is on line 2 or (\n\r before YAML..) //Stop searching if a header # is reached if (startYAML != -1 && line.startsWith("#")) { break; } } debugYaml("S(" + startYAML + ")E(" + endYAML + ")āœ“ d(" + i + ")T(" + lines.length + ")"); if (startYAML != -1 && endYAML != -1) { //shows YAML being removed debugYaml("Removing S(" + startYAML + ")->E(" + endYAML + ") YAML:"); var yaml = lines.splice(startYAML, 1 + endYAML - startYAML).join("\n"); debugYaml(yaml); resultContent = lines.join("\n"); debugYaml("YAML removed"); } else { debugYaml("No YAML found for removal"); } return resultContent; } /** Replaces any value with another value. * Regex values are allowed and will be wrapped with /str/g * Debug=merge:replace */ function optionReplaceStrings(fileContents, replacements) { debug("[OPTION] Find and Replace Strings..."); var replacedContent = fileContents; Object.keys(replacements).forEach(function (replaceKey) { var findRegex = ""; findRegex = new RegExp(replaceKey, "g"); var replaceStr = replacements[replaceKey]; debugReplace("Replacing: " + findRegex + " with: " + replaceStr); replacedContent = replacedContent.replace(findRegex, replaceStr); }); return replacedContent; } /* */ function optionBuildTOC(fileContents, doctocLocal, doctocGlobal) { debug("[OPTION] Running doctoc..."); var includeTOC = false; //TODO make this JSON available in DEBUG=defaults var defaultDocToc = { "mode": "github", "maxlevel": 3, "title": "", "notitle": true, "entryprefix": "", "all": false, "stdout": true, "update-only": false }; var finalDoctoc = defaultDocToc; var obj = { doctocGlobal: doctocGlobal, doctocLocal: doctocLocal }; for (var options in obj) { if (options == "doctocGlobal") debugDoctoc("Apply Global Options: " + JSON.stringify(obj[options]));else debugDoctoc("Apply Local Options: " + JSON.stringify(obj[options])); if (obj[options] != undefined) { if (typeof obj[options] === "boolean") { includeTOC = obj[options]; } else if (typeof obj[options] === "string") { finalDoctoc.title = obj[options]; finalDoctoc.notitle = false; includeTOC = true; } else { for (var key in finalDoctoc) { if (Object.prototype.hasOwnProperty.call(obj[options], key)) { finalDoctoc[key] = obj[options][key]; if (key == "title") finalDoctoc.notitle = false; includeTOC = true; } } } } } if (includeTOC) { debugDoctoc(JSON.stringify(finalDoctoc, null, 2)); var out = doctoc(fileContents, "github.com", finalDoctoc.maxlevel, finalDoctoc.title, finalDoctoc.notitle, finalDoctoc.entryprefix, finalDoctoc.all, finalDoctoc.stdout, finalDoctoc["update-only"]); if (!out.transformed) { debugDoctoc("No generated TOC based on document and maxlevel"); return fileContents; } if (out.data == null) return; debugDoctoc("doctoc TOC generated"); return out.data; } return fileContents; } /** * Phase 1: Check assets (markdown and HTML img tags) and external URLs in individual files * Ignores internal anchors to avoid false positives from cross-file references * Returns a promise that resolves with the linkcheck results string */ function checkAssetsAndUrls(inputFileStr, inputFileName) { debugLinkcheck("Phase 1: Asset and URL check for " + inputFileName); var inputFolder = path.dirname(inputFileStr); var fileContents = fs.readFileSync(inputFileStr, "utf-8"); return new Promise(function (resolve) { var linkcheckResults = "FILE: " + inputFileStr + "\n"; // Custom asset checking for both markdown and HTML images var assetIssues = checkAssetPaths(fileContents, inputFolder); assetIssues.forEach(function (issue) { linkcheckResults += " [x] ".concat(issue.type, ": ").concat(issue.path, " is dead\n"); }); // Use markdown-link-check for external URLs only (ignore anchors) markdownLinkCheck(fileContents, { baseUrl: pathToFileURL(path.resolve(inputFolder)), ignorePatterns: [{ pattern: "^http://localhost" }, { pattern: "^#" } // Ignore internal anchors in Phase 1 ], timeout: "2s" }, function (err, results) { if (err) { console.error("markdown-link-check failed on: " + inputFileStr); linkcheckResults += " Error during linkcheck\n\n \n"; resolve(linkcheckResults); return; } // Only report external URL issues results.forEach(function (result) { if (result.status === "dead" && !result.link.startsWith("#")) { linkcheckResults += " [x] URL: ".concat(result.link, " is dead\n"); } }); var urlCount = results.filter(function (r) { return !r.link.startsWith("#"); }).length; linkcheckResults += "\n ".concat(assetIssues.length, " asset(s) checked, ").concat(urlCount, " external URL(s) checked.\n\n \n"); resolve(linkcheckResults); }); }); } /** * Phase 2: Check internal anchors in the merged file * Validates custom anchor IDs (like {#my-id}) rather than auto-generated header anchors */ function checkAnchors(mergedFile, fileToWrite) { debugLinkcheck("Phase 2: Anchor check for merged file"); var fileContents = fs.readFileSync(mergedFile, "utf-8"); // Extract all anchor links from the content var anchorRegex = /\[([^\]]+)\]\((#[^)]+)\)/g; var anchors = []; var match; while ((match = anchorRegex.exec(fileContents)) !== null) { anchors.push(match[2]); // The #anchor part } // Extract all anchor IDs from the content (explicit {#id} syntax) var anchorIdRegex = /\{#([^}]+)\}/g; var definedAnchors = new Set(); while ((match = anchorIdRegex.exec(fileContents)) !== null) { definedAnchors.add("#".concat(match[1])); } var linkcheckResults = "\n=== Internal Anchor Validation of merged file ===\n"; linkcheckResults += "FILE: " + mergedFile + "\n"; if (anchors.length === 0) { linkcheckResults += " [āœ“] No internal anchor references found\n"; } else { var customAnchorsNotFound = 0; var customAnchorsFound = 0; var checkedAnchors = new Set(); anchors.forEach(function (anchor) { if (!checkedAnchors.has(anchor)) { checkedAnchors.add(anchor); if (!definedAnchors.has(anchor)) { customAnchorsNotFound++; } else { customAnchorsFound++; linkcheckResults += " [\u2713] ".concat(anchor, " - custom anchor ID found\n"); } } }); if (customAnchorsFound === 0 && customAnchorsNotFound > 0) { linkcheckResults += " [\u2713] ".concat(anchors.length, " anchor reference(s) found (appear to be auto-generated header anchors)\n"); linkcheckResults += "\nNote: Only explicit {#anchor} IDs are checked.\n"; linkcheckResults += "To validate header anchors, review the rendered output.\n"; } else if (customAnchorsFound > 0) { linkcheckResults += "\n ".concat(customAnchorsFound, " custom anchor(s) validated successfully.\n"); if (customAnchorsNotFound > 0) { linkcheckResults += "".concat(customAnchorsNotFound, " anchor(s) appear to be header-based (not validated).\n"); } } } linkcheckResults += "\n \n"; return safelyWriteFile(fileToWrite, linkcheckResults, true); } /** * Check asset paths from both markdown images and HTML img tags * Returns array of issues with missing assets */ function checkAssetPaths(content, baseDir) { var issues = []; // Check markdown images: ![alt](path) var mdImageRegex = /!\[.*?\]\(([^)]+)\)/g; var match; while ((match = mdImageRegex.exec(content)) !== null) { var imagePath = match[1].split(/[?#]/)[0]; // Remove query params and anchors if (!imagePath.startsWith("http://") && !imagePath.startsWith("https://")) { var fullPath = path.isAbsolute(imagePath) ? imagePath : path.resolve(baseDir, imagePath); if (!fs.existsSync(fullPath)) { issues.push({ type: "![]()", path: imagePath }); } } } // Check HTML images: <img src="path"> var htmlImageRegex = /<img[^>]+src=["']([^"']+)["']/g; while ((match = htmlImageRegex.exec(content)) !== null) { var _imagePath = match[1].split(/[?#]/)[0]; // Remove query params and anchors if (!_imagePath.startsWith("http://") && !_imagePath.startsWith("https://")) { var _fullPath = path.isAbsolute(_imagePath) ? _imagePath : path.resolve(baseDir, _imagePath); if (!fs.existsSync(_fullPath)) { issues.push({ type: "<img>", path: _imagePath }); } } } return issues; } /** Function that uses markdown-link-check to validate all URLS and relative links to images * Writes a file called outputFile.linkcheck.md with the results * https://github.com/tcort/markdown-link-check * DEBUG=merge:linkcheck * @deprecated This function is kept for reference but replaced by checkAssetsAndUrls and checkAnchors */ // eslint-disable-next-line no-unused-vars function linkcheck(inputFileStr, fileToWrite) { debugLinkcheck("LC start"); var inputFolder = path.dirname(inputFileStr); var base = pathToFileURL(path.resolve(inputFolder)); var fileContents = fs.readFileSync(inputFileStr, "utf-8"); debugLinkcheck("Async linkcheck for " + inputFileStr + " added."); markdownLinkCheck(fileContents, { baseUrl: base, ignorePatterns: [{ pattern: "^http://localhost" }], timeout: "2s" }, function (err, results) { if (err) { console.error("markdownlinkcheck failed on: " + inputFileStr); return; } var linkcheckResults = "FILE: " + inputFileStr; debugLinkcheck(linkcheckResults); linkcheckResults += " \n"; results.forEach(function (result) { var icon = ""; switch (result.status) { case "alive": icon = "āœ“"; break; case "dead": icon = "x"; break; case "ignored": icon = "-"; break; } var statusStr = "[" + icon + "] " + result.link + " is " + result.status; debugLinkcheckDeep(statusStr); if (result.status != "alive") { linkcheckResults += " " + statusStr + " \n"; } }); debugLinkcheck("deep complete"); linkcheckResults += "\n " + results.length + " links checked. \n\n \n"; if (fs.existsSync(fileToWrite)) { return safelyWriteFile(fileToWrite, linkcheckResults, true); } else { return safelyWriteFile(fileToWrite, linkcheckResults); } }); } function replaceExtension(filePath, newExt) { var dir = path.dirname(filePath); var name = path.basename(filePath, path.extname(filePath)); var ext = newExt.startsWith(".") ? newExt : "." + newExt; return path.join(dir, name + ext); } /** * Runs a quick asset preview scan and displays summary to user * @param {Array} inputFiles - Array of input file paths from manifest */ function runAssetPreviewScan(inputFiles) { debugAssetPreview("Starting asset preview scan"); try { // Run quick scan for unused assets var scanResult = assetCleaner.scanForUnusedAssets(inputFiles, "assets"); if (scanResult.hasUnusedAssets) { console.log("\n\uD83D\uDCA1 Found ".concat(scanResult.totalUnusedAssets, " unused asset").concat(scanResult.totalUnusedAssets === 1 ? "" : "s", " in your project directories.")); console.log(" Run 'merge-markdown --clean-assets' to see details and clean up."); } else if (scanResult.scannedDirectories > 0) { debugAssetPreview("No unused assets found in ".concat(scanResult.scannedDirectories, " directories")); } else { debugAssetPreview("No asset directories found to scan"); } } catch (error) { debugAssetPreview("Error during asset preview scan: ".concat(error.message)); } } function deleteGeneratedFiles(fileArr) { fileArr.forEach(function (element) { if (fs.existsSync(element)) { fs.unlinkSync(element); } }); } function safelyWriteFile(filePath, contents, append) { try { //Create the output directory structure if it doesn't exist mkdirp.sync(path.dirname(filePath)); if (append) { fs.appendFileSync(filePath, contents, "utf-8"); } else { fs.writeFileSync(filePath, contents, "utf-8"); } return filePath; } catch (err) { console.error("Error writing file: " + filePath); } } exports.start = start;