"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/workers/linter/index.ts
var linter_exports = {};
module.exports = __toCommonJS(linter_exports);
var import_log7 = __toESM(require("@doc-tools/transform/lib/log"));
var import_path20 = require("path");
var import_observable = require("threads/observable");
var import_threads = require("threads");

// src/services/tocs.ts
var import_path14 = require("path");
var import_fs3 = require("fs");
var import_js_yaml4 = require("js-yaml");
var import_shelljs3 = __toESM(require("shelljs"));
var import_walk_sync = __toESM(require("walk-sync"));
var import_liquid2 = __toESM(require("@doc-tools/transform/lib/liquid"));
var import_log2 = __toESM(require("@doc-tools/transform/lib/log"));
var import_chalk2 = require("chalk");

// src/services/metadata.ts
var import_js_yaml = require("js-yaml");

// src/utils/logger.ts
var import_log = __toESM(require("@doc-tools/transform/lib/log"));
var import_chalk = require("chalk");
function writeLog(msg, fatal = false) {
  const { quiet } = argv_default.getConfig();
  if (quiet && !fatal) {
    return;
  }
  console.log(msg);
}
var logger = {
  info: function(pathToFile, extraMessage) {
    writeLog(`${(0, import_chalk.grey)("INFO")} ${extraMessage} ${pathToFile}`);
  },
  proc: function(pathToFile) {
    writeLog(`${(0, import_chalk.blue)("PROC")} Processing file ${pathToFile}`);
  },
  copy: function(pathToFile) {
    writeLog(`${(0, import_chalk.green)("COPY")} Copying file ${pathToFile}`);
  },
  upload: function(pathToFile) {
    writeLog(`${(0, import_chalk.green)("UPLOAD")} Uploading file ${pathToFile}`);
  },
  warn: function(pathToFile, extraMessage) {
    const message = `${(0, import_chalk.yellow)("WARNING")} file: ${pathToFile} error: ${extraMessage}`;
    writeLog(message);
    import_log.default.warn(`file: ${pathToFile} ${extraMessage}`);
  },
  error: function(pathToFile, extraMessage) {
    const message = `${(0, import_chalk.red)("ERROR")} file: ${pathToFile} error: ${extraMessage}`;
    writeLog(message, true);
    import_log.default.error(`file: ${pathToFile} ${extraMessage}`);
  }
};

// src/utils/promise.ts
var import_shelljs = __toESM(require("shelljs"));

// src/utils/markup.ts
var import_process = require("process");

// src/constants.ts
var import_path = require("path");

// src/services/includers/batteries/openapi/plugin/index.ts
var import_html_escaper = require("html-escaper");
function parserOpenapiSandboxBlock(state, start, end, silent) {
  let firstLine, lastLine, next, lastPos, found = false, pos = state.bMarks[start] + state.tShift[start], max = state.eMarks[start];
  const startMark = "{% openapi sandbox %}";
  const endMark = "{% end openapi sandbox %}";
  if (pos + startMark.length > max) {
    return false;
  }
  if (state.src.slice(pos, pos + startMark.length) !== startMark) {
    return false;
  }
  pos += startMark.length;
  firstLine = state.src.slice(pos, max);
  if (silent) {
    return true;
  }
  if (firstLine.slice(-endMark.length) === endMark) {
    firstLine = firstLine.slice(0, -endMark.length);
    found = true;
  }
  for (next = start; !found; ) {
    next++;
    if (next >= end) {
      break;
    }
    pos = state.bMarks[next] + state.tShift[next];
    max = state.eMarks[next];
    if (pos < max && state.tShift[next] < state.blkIndent) {
      break;
    }
    if (state.src.slice(pos, max).slice(-endMark.length) === endMark) {
      lastPos = state.src.slice(0, max).lastIndexOf(endMark);
      lastLine = state.src.slice(pos, lastPos);
      found = true;
    }
  }
  state.line = next + 1;
  const token = state.push("openapi_sandbox_block", "openapi_sandbox", 0);
  token.block = true;
  token.content = (firstLine ? firstLine + "\n" : "") + state.getLines(start + 1, next, state.tShift[start], true) + (lastLine ? lastLine : "");
  token.map = [start, state.line];
  token.markup = startMark;
  return true;
}
var openapiSandboxPlugin = (md) => {
  const openapiSandboxBlock = (jsonString) => {
    try {
      const props = (0, import_html_escaper.escape)(jsonString);
      return `<div class="yfm-sandbox-js" data-props="${props}"></div>`;
    } catch (error) {
      console.log(error);
      return jsonString;
    }
  };
  const openapiSandboxRenderer = (tokens, idx) => {
    return openapiSandboxBlock(tokens[idx].content);
  };
  try {
    md.block.ruler.before("meta", "openapi_sandbox_block", parserOpenapiSandboxBlock);
  } catch (e) {
    md.block.ruler.push("openapi_sandbox_block", parserOpenapiSandboxBlock);
  }
  md.renderer.rules.openapi_sandbox_block = openapiSandboxRenderer;
};
var plugin_default = openapiSandboxPlugin;

// src/constants.ts
var os = require("os");
var notes = require("@doc-tools/transform/lib/plugins/notes");
var anchors = require("@doc-tools/transform/lib/plugins/anchors");
var code = require("@doc-tools/transform/lib/plugins/code");
var cut = require("@doc-tools/transform/lib/plugins/cut");
var deflist = require("@doc-tools/transform/lib/plugins/deflist");
var imsize = require("@doc-tools/transform/lib/plugins/imsize");
var meta = require("@doc-tools/transform/lib/plugins/meta");
var sup = require("@doc-tools/transform/lib/plugins/sup");
var tabs = require("@doc-tools/transform/lib/plugins/tabs");
var video = require("@doc-tools/transform/lib/plugins/video");
var includes = require("@doc-tools/transform/lib/plugins/includes");
var links = require("@doc-tools/transform/lib/plugins/links");
var images = require("@doc-tools/transform/lib/plugins/images");
var monospace = require("@doc-tools/transform/lib/plugins/monospace");
var table = require("@doc-tools/transform/lib/plugins/table");
var term = require("@doc-tools/transform/lib/plugins/term");
var mermaid = require("@diplodoc/mermaid-extension");
includes.collect = require("@doc-tools/transform/lib/plugins/includes/collect");
images.collect = require("@doc-tools/transform/lib/plugins/images/collect");
var BUILD_FOLDER_PATH = (0, import_path.dirname)(process.mainModule?.filename || "");
var YFM_PLUGINS = [
  meta,
  deflist,
  includes,
  cut,
  links,
  images,
  notes,
  anchors,
  tabs,
  code,
  imsize,
  sup,
  video,
  monospace,
  table,
  term,
  plugin_default,
  mermaid.transform()
];
var MIN_CHUNK_SIZE = Number(process.env.MIN_CHUNK_SIZE) || 1e3;
var WORKERS_COUNT = Number(process.env.WORKERS_COUNT) || os.cpus().length - 1;
var metadataBorder = "---";

// src/utils/singlePage.ts
var import_node_html_parser = require("node-html-parser");
var import_path2 = require("path");
var import_utilsFS = require("@doc-tools/transform/lib/utilsFS");
var import_url = __toESM(require("url"));
var import_lodash = __toESM(require("lodash"));

// src/utils/url.ts
function isExternalHref(href) {
  return href.startsWith("http") || href.startsWith("//");
}

// src/utils/markup.ts
var \u0441arriage = import_process.platform === "win32" /* WINDOWS */ ? "\r\n" : "\n";

// src/utils/path.ts
var import_path3 = require("path");

// src/utils/toc.ts
var import_path4 = require("path");

// src/services/utils.ts
var import_evaluation = __toESM(require("@doc-tools/transform/lib/liquid/evaluation"));
var import_liquid = __toESM(require("@doc-tools/transform/lib/liquid"));
function filterFiles(items, itemsKey, vars, options) {
  if (!Array.isArray(items)) {
    return [];
  }
  const reducer = (results, item) => {
    if (shouldProcessItem(item, vars, options)) {
      const prop = item[itemsKey];
      if (prop) {
        const filteredProperty = filterFiles(prop, itemsKey, vars, options);
        if (filteredProperty.length) {
          results.push({
            ...item,
            [itemsKey]: filteredProperty
          });
        }
      } else {
        results.push(item);
      }
    }
    return results;
  };
  return items.reduce(reducer, []);
}
function filterTextItems(items, vars, options) {
  if (!Array.isArray(items)) {
    return items;
  }
  return items.reduce((result, item) => {
    if (!isObject(item)) {
      result.push(item);
      return result;
    }
    const useItem = shouldProcessItem(item, vars, options);
    if (useItem) {
      if (Array.isArray(item.text)) {
        result.push(...item.text);
      } else {
        result.push(item.text);
      }
    }
    return result;
  }, []);
}
function firstFilterTextItems(items, vars, options) {
  const filteredItems = filterTextItems(items, vars, options);
  if (!Array.isArray(filteredItems)) {
    return filteredItems || "";
  }
  return filteredItems[0] || "";
}
function shouldProcessItem(item, vars, options) {
  const { resolveConditions, removeHiddenTocItems } = options || {};
  let useItem = true;
  if (resolveConditions) {
    const { when } = item;
    useItem = when === true || when === void 0 || typeof when === "string" && (0, import_evaluation.default)(when, vars);
    delete item.when;
  }
  if (useItem && removeHiddenTocItems) {
    useItem = !item.hidden;
    delete item.hidden;
  }
  return useItem;
}
function liquidField(input, vars, path) {
  const { applyPresets, resolveConditions } = argv_default.getConfig();
  if (!applyPresets && !resolveConditions) {
    return input;
  }
  return (0, import_liquid.default)(input, vars, path, {
    substitutions: applyPresets,
    conditions: resolveConditions,
    keepNotVar: true,
    withSourceMap: false
  });
}
function isObject(o) {
  return typeof o === "object" && o !== null;
}

// src/utils/presets.ts
var import_path5 = require("path");
function getVarsPerFile(filePath) {
  const { vars: argVars } = argv_default.getConfig();
  return {
    ...preset_default.get((0, import_path5.dirname)(filePath)),
    ...argVars
  };
}
function getVarsPerRelativeFile(filePath) {
  const { input } = argv_default.getConfig();
  const root = (0, import_path5.resolve)(input);
  const relativeFilePath = (0, import_path5.relative)(root, filePath);
  return getVarsPerFile(relativeFilePath);
}

// src/utils/glob.ts
var import_glob = __toESM(require("glob"));

// src/utils/file.ts
var import_path6 = require("path");
var import_shelljs2 = __toESM(require("shelljs"));
var import_fs = require("fs");

// src/services/contributors.ts
var import_promises = require("fs/promises");
var import_path7 = require("path");

// src/services/metadata.ts
var import_path8 = require("path");
function getContentWithUpdatedStaticMetadata({
  fileContent,
  sourcePath,
  addSystemMeta,
  addSourcePath,
  resources,
  systemVars
}) {
  const newMetadatas = [];
  if ((!addSystemMeta || !systemVars) && !addSourcePath && !resources) {
    return fileContent;
  }
  const matches = matchMetadata(fileContent);
  if (addSystemMeta && systemVars && isObject(systemVars)) {
    newMetadatas.push(getSystemVarsMetadataString(systemVars));
  }
  if (resources) {
    newMetadatas.push((0, import_js_yaml.dump)(resources));
  }
  if (addSourcePath && sourcePath) {
    const sourcePathMetadataString = `sourcePath: ${sourcePath}`;
    newMetadatas.push(sourcePathMetadataString);
  }
  if (matches && matches.length > 0) {
    const [, fileMetadata, , fileMainContent] = matches;
    return `${getUpdatedMetadataString(newMetadatas, fileMetadata)}${fileMainContent}`;
  }
  return `${getUpdatedMetadataString(newMetadatas)}${fileContent}`;
}
function matchMetadata(fileContent) {
  if (!fileContent.startsWith("---")) {
    return null;
  }
  const regexpMetadata = "(?<=-{3}\\r?\\n)((.*\\r?\\n)*?)(?=-{3}\\r?\\n)";
  const regexpFileContent = "-{3}((.*[\r?\n]*)*)";
  const regexpParseFileContent = new RegExp(`${regexpMetadata}${regexpFileContent}`, "gm");
  return regexpParseFileContent.exec(fileContent);
}
function getUpdatedMetadataString(newMetadatas, defaultMetadata = "") {
  const newMetadata = newMetadatas.join(\u0441arriage) + (newMetadatas.length ? \u0441arriage : "");
  const preparedDefaultMetadata = defaultMetadata.trimRight();
  const defaultMetadata\u0421arriage = preparedDefaultMetadata ? \u0441arriage : "";
  const updatedMetadata = `${preparedDefaultMetadata}${defaultMetadata\u0421arriage}${newMetadata}`;
  return `${metadataBorder}${\u0441arriage}${updatedMetadata}${metadataBorder}${defaultMetadata.length ? "" : \u0441arriage}`;
}
function getSystemVarsMetadataString(systemVars) {
  return `__system: ${JSON.stringify(systemVars)}`;
}

// src/services/includers/index.ts
var import_path13 = require("path");
var import_lodash3 = require("lodash");

// src/services/includers/batteries/generic.ts
var import_promises2 = require("fs/promises");
var import_path9 = require("path");
var import_lodash2 = require("lodash");
var import_js_yaml2 = require("js-yaml");

// src/services/includers/batteries/openapi/index.ts
var import_assert = __toESM(require("assert"));
var import_path11 = require("path");
var import_promises3 = require("fs/promises");

// src/services/includers/batteries/openapi/utils.ts
var import_evaluation2 = require("@doc-tools/transform/lib/liquid/evaluation");

// src/services/includers/batteries/openapi/index.ts
var import_js_yaml3 = require("js-yaml");

// src/services/includers/batteries/openapi/parsers.ts
var import_slugify = __toESM(require("slugify"));
var import_http_status_codes = require("http-status-codes");

// src/services/includers/batteries/openapi/constants.ts
var EOL = "\n";
var BLOCK = EOL.repeat(2);
var LEADING_PAGE_MODES = /* @__PURE__ */ new Set(["leaf" /* Leaf */, "section" /* Section */]);

// src/services/includers/batteries/openapi/generators/main.ts
var import_path10 = require("path");
var import_json_stringify_safe = __toESM(require("json-stringify-safe"));

// src/services/includers/batteries/openapi/generators/endpoint.ts
var import_json_stringify_safe3 = __toESM(require("json-stringify-safe"));

// src/services/includers/batteries/openapi/generators/traverse.ts
var import_slugify2 = __toESM(require("slugify"));
var import_json_stringify_safe2 = __toESM(require("json-stringify-safe"));

// src/services/includers/batteries/common.ts
var { promises: { readdir } } = require("fs");

// src/services/includers/batteries/openapi/generators/constants.ts
var import_bem_cn_lite = __toESM(require("bem-cn-lite"));
var openapiBlock = (0, import_bem_cn_lite.default)("openapi");

// src/services/includers/batteries/openapi/index.ts
var import_swagger_parser = __toESM(require("@apidevtools/swagger-parser"));

// src/services/includers/batteries/unarchive.ts
var import_fs2 = require("fs");
var import_path12 = require("path");
var import_tar_stream = require("tar-stream");

// src/services/includers/index.ts
var includersUsage = `include:
  path: <path-where-to-include>
  includers:
    - name: <includer-name-0>
      <includer-parameter>: <value-for-includer-parameter>
    - name: <includer-name-1>
      <includer-parameter>: <value-for-includer-parameter>
`;
var includersMap;
var IncludersError = class extends Error {
  constructor(message, path) {
    super(message);
    this.name = "IncludersError";
    this.path = path;
  }
};
async function applyIncluders(path, item, vars) {
  if (!item.include?.includers) {
    return;
  }
  if (!includeValid(item.include)) {
    throw new IncludersError("include doesn't comply with includers standard", path);
  }
  item.include.mode = "link" /* LINK */;
  const { status, message } = includersValid(item.include.includers);
  if (!status) {
    throw new IncludersError(message ?? "", path);
  }
  let index = 0;
  for (const { name, ...rest } of item.include.includers) {
    const includer = getIncluder(name);
    const passedParams = { ...rest };
    await applyIncluder({ path, item, includer, passedParams, index, vars });
  }
  item.include.path = (0, import_path13.join)(item.include.path, "toc.yaml");
  index++;
}
function includeValid(include) {
  return (include.mode === "link" /* LINK */ || !include.mode) && include.path?.length;
}
function includersValid(includers) {
  for (const includer of includers) {
    const { status, message } = includerValid(includer);
    if (!status) {
      return { status, message };
    }
  }
  return { status: true };
}
function includerValid(includer) {
  if ((0, import_lodash3.isObject)(includer)) {
    if (typeof includer.name !== "string") {
      return {
        status: false,
        message: "use string in the includer.name to specify includers name"
      };
    }
    if (includerExists(includer)) {
      return { status: true };
    }
    return { status: false, message: `includer ${includer.name} not implemented` };
  }
  return {
    status: false,
    message: `use appropriate includers format:
${includersUsage}`
  };
}
function getIncluder(includerName) {
  return includersMap[includerName];
}
function includerExists(includer) {
  return includersMap[includer.name];
}
async function applyIncluder(args) {
  const { rootInput: readBasePath, input: writeBasePath } = argv_default.getConfig();
  const { path, item, includer, passedParams, index, vars } = args;
  const params = {
    tocPath: path,
    passedParams,
    index,
    item,
    readBasePath,
    writeBasePath,
    vars
  };
  return await includer.includerFunction(params);
}

// src/services/tocs.ts
var storage = /* @__PURE__ */ new Map();
var navigationPaths = [];
var includedTocPaths = /* @__PURE__ */ new Set();
async function add(path) {
  const {
    input: inputFolderPath,
    output: outputFolderPath,
    outputFormat,
    ignoreStage,
    vars
  } = argv_default.getConfig();
  const pathToDir = (0, import_path14.dirname)(path);
  const content = (0, import_fs3.readFileSync)((0, import_path14.resolve)(inputFolderPath, path), "utf8");
  const parsedToc = (0, import_js_yaml4.load)(content);
  if (parsedToc.stage === ignoreStage) {
    return;
  }
  const combinedVars = {
    ...preset_default.get(pathToDir),
    ...vars
  };
  if (parsedToc.title) {
    parsedToc.title = firstFilterTextItems(
      parsedToc.title,
      combinedVars,
      { resolveConditions: true }
    );
  }
  if (typeof parsedToc.title === "string") {
    parsedToc.title = liquidField(parsedToc.title, combinedVars, path);
  }
  parsedToc.items = await processTocItems(
    path,
    parsedToc.items,
    (0, import_path14.join)(inputFolderPath, pathToDir),
    (0, import_path14.resolve)(inputFolderPath),
    combinedVars
  );
  storage.set(path, parsedToc);
  parsedToc.base = pathToDir;
  if (outputFormat === "md") {
    const outputPath = (0, import_path14.resolve)(outputFolderPath, path);
    const outputToc = (0, import_js_yaml4.dump)(parsedToc);
    import_shelljs3.default.mkdir("-p", (0, import_path14.dirname)(outputPath));
    (0, import_fs3.writeFileSync)(outputPath, outputToc);
  }
  prepareNavigationPaths(parsedToc, pathToDir);
}
async function processTocItems(path, items, tocDir, sourcesDir, vars) {
  const {
    resolveConditions,
    removeHiddenTocItems
  } = argv_default.getConfig();
  let preparedItems = items;
  if (resolveConditions || removeHiddenTocItems) {
    try {
      preparedItems = filterFiles(items, "items", vars, {
        resolveConditions,
        removeHiddenTocItems
      });
    } catch (error) {
      import_log2.default.error(`Error while filtering toc file: ${path}. Error message: ${error}`);
    }
  }
  return _replaceIncludes(path, preparedItems, tocDir, sourcesDir, vars);
}
function getForPath(path) {
  return storage.get(path);
}
function getNavigationPaths() {
  return [...navigationPaths];
}
function getIncludedTocPaths() {
  return [...includedTocPaths];
}
function prepareNavigationPaths(parsedToc, dirPath) {
  function processItems(items, pathToDir) {
    items.forEach((item) => {
      if (!parsedToc.singlePage && item.items) {
        const preparedSubItems = item.items.map((yfmToc, index) => {
          yfmToc.id = `${yfmToc.name}-${index}-${Math.random()}`;
          return yfmToc;
        });
        processItems(preparedSubItems, pathToDir);
      }
      if (item.href && !isExternalHref(item.href)) {
        const href = (0, import_path14.join)(pathToDir, item.href);
        storage.set(href, parsedToc);
        const navigationPath = _normalizeHref(href);
        navigationPaths.push(navigationPath);
      }
    });
  }
  processItems([parsedToc], dirPath);
}
function _normalizeHref(href) {
  const preparedHref = (0, import_path14.normalize)(href);
  if (preparedHref.endsWith(".md") || preparedHref.endsWith(".yaml")) {
    return preparedHref;
  }
  if (preparedHref.endsWith(import_path14.sep)) {
    return `${preparedHref}index.yaml`;
  }
  return `${preparedHref}.md`;
}
function _copyTocDir(tocPath, destDir) {
  const { input: inputFolderPath } = argv_default.getConfig();
  const { dir: tocDir } = (0, import_path14.parse)(tocPath);
  const files = (0, import_walk_sync.default)(tocDir, {
    globs: ["**/*.*"],
    ignore: ["**/toc.yaml"],
    directories: false
  });
  files.forEach((relPath) => {
    const from = (0, import_path14.resolve)(tocDir, relPath);
    const to = (0, import_path14.resolve)(destDir, relPath);
    const fileExtension = (0, import_path14.extname)(relPath);
    const isMdFile = fileExtension === ".md";
    import_shelljs3.default.mkdir("-p", (0, import_path14.parse)(to).dir);
    if (isMdFile) {
      const fileContent = (0, import_fs3.readFileSync)(from, "utf8");
      const sourcePath = (0, import_path14.relative)(inputFolderPath, from);
      const updatedFileContent = getContentWithUpdatedStaticMetadata({
        fileContent,
        sourcePath,
        addSourcePath: true
      });
      (0, import_fs3.writeFileSync)(to, updatedFileContent);
    } else {
      (0, import_fs3.copyFileSync)(from, to);
    }
  });
}
function _replaceIncludesHrefs(items, includeTocDir, tocDir) {
  return items.reduce((acc, tocItem) => {
    if (tocItem.href) {
      tocItem.href = (0, import_path14.relative)(tocDir, (0, import_path14.resolve)(includeTocDir, tocItem.href));
    }
    if (tocItem.items) {
      tocItem.items = _replaceIncludesHrefs(tocItem.items, includeTocDir, tocDir);
    }
    if (tocItem.include) {
      const { path } = tocItem.include;
      tocItem.include.path = (0, import_path14.relative)(tocDir, (0, import_path14.resolve)(includeTocDir, path));
    }
    return acc.concat(tocItem);
  }, []);
}
function _liquidSubstitutions(input, vars, path) {
  const { outputFormat, applyPresets } = argv_default.getConfig();
  if (outputFormat === "md" && !applyPresets) {
    return input;
  }
  return (0, import_liquid2.default)(input, vars, path, {
    conditions: false,
    substitutions: true
  });
}
function addIncludeTocPath(includeTocPath) {
  includedTocPaths.add(includeTocPath);
}
async function _replaceIncludes(path, items, tocDir, sourcesDir, vars) {
  const result = [];
  for (const item of items) {
    let includedInlineItems = null;
    if (item.name) {
      const tocPath = (0, import_path14.join)(tocDir, "toc.yaml");
      item.name = _liquidSubstitutions(item.name, vars, tocPath);
    }
    try {
      await applyIncluders(path, item, vars);
    } catch (err) {
      if (err instanceof Error || err instanceof IncludersError) {
        const message = err.toString();
        const file = err instanceof IncludersError ? err.path : path;
        logger.error(file, message);
      }
    }
    if (item.include) {
      const { mode = "root_merge" /* ROOT_MERGE */ } = item.include;
      const includeTocPath = mode === "root_merge" /* ROOT_MERGE */ ? (0, import_path14.resolve)(sourcesDir, item.include.path) : (0, import_path14.resolve)(tocDir, item.include.path);
      const includeTocDir = (0, import_path14.dirname)(includeTocPath);
      try {
        const includeToc = (0, import_js_yaml4.load)((0, import_fs3.readFileSync)(includeTocPath, "utf8"));
        if (includeToc.stage === "tech-preview" /* TECH_PREVIEW */) {
          continue;
        }
        if (mode === "merge" /* MERGE */ || mode === "root_merge" /* ROOT_MERGE */) {
          _copyTocDir(includeTocPath, tocDir);
        }
        addIncludeTocPath(includeTocPath);
        let includedTocItems = (item.items || []).concat(includeToc.items);
        const baseTocDir = mode === "link" /* LINK */ ? includeTocDir : tocDir;
        includedTocItems = await processTocItems(path, includedTocItems, baseTocDir, sourcesDir, vars);
        if (mode === "link" /* LINK */) {
          includedTocItems = _replaceIncludesHrefs(includedTocItems, includeTocDir, tocDir);
        }
        if (item.name) {
          item.items = includedTocItems;
        } else {
          includedInlineItems = includedTocItems;
        }
      } catch (err) {
        const message = `Error while including toc: ${(0, import_chalk2.bold)(includeTocPath)} to ${(0, import_chalk2.bold)((0, import_path14.join)(tocDir, "toc.yaml"))}`;
        import_log2.default.error(message);
        continue;
      } finally {
        delete item.include;
      }
    } else if (item.items) {
      item.items = await processTocItems(path, item.items, tocDir, sourcesDir, vars);
    }
    if (includedInlineItems) {
      result.push(...includedInlineItems);
    } else {
      result.push(item);
    }
  }
  return result;
}
function getTocDir(pagePath) {
  const { input: inputFolderPath } = argv_default.getConfig();
  const tocDir = (0, import_path14.dirname)(pagePath);
  const tocPath = (0, import_path14.resolve)(tocDir, "toc.yaml");
  if (!tocDir.includes(inputFolderPath)) {
    throw new Error("Error while finding toc dir");
  }
  if ((0, import_fs3.existsSync)(tocPath)) {
    return tocDir;
  }
  return getTocDir(tocDir);
}
function setNavigationPaths(paths) {
  navigationPaths = paths;
}
var tocs_default = {
  add,
  getForPath,
  getNavigationPaths,
  getTocDir,
  getIncludedTocPaths,
  setNavigationPaths
};

// src/services/preset.ts
var import_path15 = require("path");
var presetStorage = /* @__PURE__ */ new Map();
function add2(parsedPreset, path, varsPreset) {
  const combinedValues = {
    ...parsedPreset.default || {},
    ...parsedPreset[varsPreset] || {}
  };
  const key = (0, import_path15.dirname)((0, import_path15.normalize)(path));
  presetStorage.set(key, combinedValues);
}
function get(path) {
  let combinedValues = {};
  let localPath = (0, import_path15.normalize)(path);
  while (localPath !== ".") {
    const presetValues = presetStorage.get(localPath) || {};
    localPath = (0, import_path15.dirname)(localPath);
    combinedValues = {
      ...presetValues,
      ...combinedValues
    };
  }
  combinedValues = {
    ...presetStorage.get("."),
    ...combinedValues
  };
  return combinedValues;
}
function getPresetStorage() {
  return presetStorage;
}
function setPresetStorage(preset) {
  presetStorage = preset;
}
var preset_default = {
  add: add2,
  get,
  getPresetStorage,
  setPresetStorage
};

// src/services/argv.ts
var _argv;
function getConfig() {
  return _argv;
}
function init(argv) {
  _argv = {
    ...argv,
    ignore: Array.isArray(argv.ignore) ? argv.ignore : [],
    vars: JSON.parse(argv.vars)
  };
}
function set(argv) {
  _argv = argv;
}
var argv_default = {
  getConfig,
  init,
  set
};

// src/services/leading.ts
var import_path16 = require("path");
var import_fs4 = require("fs");
var import_js_yaml5 = require("js-yaml");
var import_log3 = __toESM(require("@doc-tools/transform/lib/log"));

// src/services/plugins.ts
var plugins_exports = {};
__export(plugins_exports, {
  getCollectOfPlugins: () => getCollectOfPlugins,
  getCustomLintRules: () => getCustomLintRules,
  getDefaultLintConfig: () => getDefaultLintConfig,
  getHeadContent: () => getHeadContent,
  getPlugins: () => getPlugins,
  setPlugins: () => setPlugins
});
var plugins;
var collectionOfPlugins;
function setPlugins() {
  plugins = getAllPlugins();
  collectionOfPlugins = makeCollectOfPlugins();
}
function getPlugins() {
  return plugins;
}
function getCollectOfPlugins() {
  return collectionOfPlugins;
}
function makeCollectOfPlugins() {
  const pluginsWithCollect = plugins.filter((plugin) => {
    return typeof plugin.collect === "function";
  });
  return (output, options) => {
    let collectsOutput = output;
    pluginsWithCollect.forEach((plugin) => {
      const collectOutput = plugin.collect(collectsOutput, options);
      collectsOutput = typeof collectOutput === "string" ? collectOutput : collectsOutput;
    });
    return collectsOutput;
  };
}
function getAllPlugins() {
  const customPlugins = getCustomPlugins();
  return [...YFM_PLUGINS, ...customPlugins];
}
function getCustomPlugins() {
  try {
    const customPlugins = require(require.resolve("./plugins"));
    return Array.isArray(customPlugins) ? customPlugins : [];
  } catch (e) {
    return [];
  }
}
function getHeadContent() {
  try {
    return require(require.resolve("./head-content.js"));
  } catch (e) {
    return "";
  }
}
function getCustomLintRules() {
  try {
    return require(require.resolve("./lint-rules"));
  } catch (e) {
    return [];
  }
}
function getDefaultLintConfig() {
  try {
    return require(require.resolve("./default-lint-config"));
  } catch (e) {
    return void 0;
  }
}

// src/resolvers/md2md.ts
var import_fs5 = require("fs");
var import_path17 = require("path");
var import_shelljs4 = __toESM(require("shelljs"));
var import_log4 = __toESM(require("@doc-tools/transform/lib/log"));
var import_liquid3 = __toESM(require("@doc-tools/transform/lib/liquid"));
function liquidMd2Md(input, vars, path) {
  const {
    applyPresets,
    resolveConditions
  } = argv_default.getConfig();
  return (0, import_liquid3.default)(input, vars, path, {
    conditions: resolveConditions,
    substitutions: applyPresets,
    withSourceMap: true,
    keepNotVar: true
  });
}

// src/resolvers/md2html.ts
var import_path18 = require("path");
var import_fs6 = require("fs");
var import_js_yaml6 = __toESM(require("js-yaml"));
var import_transform = __toESM(require("@doc-tools/transform"));
var import_log5 = __toESM(require("@doc-tools/transform/lib/log"));
var import_liquid4 = __toESM(require("@doc-tools/transform/lib/liquid"));
function liquidMd2Html(input, vars, path) {
  const { conditionsInCode } = argv_default.getConfig();
  return (0, import_liquid4.default)(input, vars, path, {
    conditionsInCode,
    withSourceMap: true
  });
}

// src/resolvers/lintPage.ts
var import_path19 = require("path");
var import_log6 = __toESM(require("@doc-tools/transform/lib/log"));
var import_yfmlint = __toESM(require("@doc-tools/transform/lib/yfmlint"));
var import_fs7 = require("fs");
var import_chalk3 = require("chalk");
var FileLinter = {
  ".md": MdFileLinter
};
function lintPage(options) {
  const { inputPath, fileExtension, onFinish } = options;
  const { input } = argv_default.getConfig();
  const resolvedPath = (0, import_path19.resolve)(input, inputPath);
  try {
    const content = (0, import_fs7.readFileSync)(resolvedPath, "utf8");
    const lintFn = FileLinter[fileExtension];
    if (!lintFn) {
      return;
    }
    lintFn(content, { path: inputPath });
  } catch (e) {
    const message = `No such file or has no access to ${(0, import_chalk3.bold)(resolvedPath)}`;
    console.error(message, e);
    import_log6.default.error(message);
  }
  if (onFinish) {
    onFinish();
  }
}
function MdFileLinter(content, lintOptions) {
  const {
    input,
    lintConfig,
    disableLiquid,
    outputFormat,
    ...options
  } = argv_default.getConfig();
  const { path: filePath } = lintOptions;
  const plugins2 = outputFormat === "md" ? [] : plugins_exports.getPlugins();
  const vars = getVarsPerFile(filePath);
  const root = (0, import_path19.resolve)(input);
  const path = (0, import_path19.resolve)(input, filePath);
  let preparedContent = content;
  const assetsPublicPath = (0, import_path19.relative)((0, import_path19.dirname)(path), root);
  const lintMarkdown = function lintMarkdown2(opts) {
    const { input: localInput, path: localPath, sourceMap: sourceMap2 } = opts;
    const pluginOptions = {
      ...options,
      vars,
      root,
      path: localPath,
      lintMarkdown: lintMarkdown2,
      // Should pass the function for linting included files
      assetsPublicPath,
      disableLiquid,
      log: import_log6.default,
      getVarsPerFile: getVarsPerRelativeFile
    };
    (0, import_yfmlint.default)({
      input: localInput,
      lintConfig,
      pluginOptions,
      plugins: plugins2,
      defaultLintConfig: plugins_exports.getDefaultLintConfig(),
      customLintRules: plugins_exports.getCustomLintRules(),
      sourceMap: sourceMap2
    });
  };
  let sourceMap;
  if (!disableLiquid) {
    let liquidResult;
    if (outputFormat === "md") {
      liquidResult = liquidMd2Md(content, vars, path);
    } else {
      liquidResult = liquidMd2Html(content, vars, path);
    }
    preparedContent = liquidResult.output;
    sourceMap = liquidResult.sourceMap;
  }
  lintMarkdown({
    input: preparedContent,
    path,
    sourceMap
  });
}

// src/workers/linter/index.ts
var processedPages = new import_observable.Subject();
async function run({
  argvConfig,
  presetStorage: presetStorage2,
  navigationPaths: navigationPaths2
}) {
  argv_default.set(argvConfig);
  preset_default.setPresetStorage(presetStorage2);
  tocs_default.setNavigationPaths(navigationPaths2);
  plugins_exports.setPlugins();
  tocs_default.getNavigationPaths().forEach((pathToFile) => {
    lintPage({
      inputPath: pathToFile,
      fileExtension: (0, import_path20.extname)(pathToFile),
      onFinish: () => {
        processedPages.next(pathToFile);
      }
    });
  });
}
async function finish() {
  processedPages.complete();
  processedPages = new import_observable.Subject();
  return import_log7.default.get();
}
function getProcessedPages() {
  return import_observable.Observable.from(processedPages);
}
(0, import_threads.expose)({
  run,
  finish,
  getProcessedPages
});
//# sourceMappingURL=linter.js.map
