const STRAY_LABELS = /* @__PURE__ */ new Set([
  "javascript",
  "typescript",
  "mjs",
  "cjs",
  "json",
  "html",
  "css",
  "bash",
  "sh",
  "shell",
  "python",
  "py",
  "go",
  "rust",
  "java",
  "kotlin",
  "swift",
  "cpp",
  "c++",
  "csharp",
  "ruby",
  "php"
]);
const GLUED_LANG_PREFIXES = {
  ts: "ts",
  js: "js",
  tsx: "tsx",
  jsx: "jsx",
  py: "python",
  rb: "ruby",
  go: "go",
  rs: "rust",
  sh: "bash",
  json: "json",
  html: "html",
  css: "css",
  yaml: "yaml",
  yml: "yaml",
  toml: "toml",
  md: "markdown"
};
const POST_PREFIX_TOKENS = [
  "function",
  "const",
  "let",
  "var",
  "class",
  "interface",
  "type",
  "enum",
  "import",
  "export",
  "async",
  "await",
  "return",
  "if",
  "for",
  "while",
  "def",
  "print",
  "echo",
  "package",
  "public",
  "private",
  "protected",
  "struct",
  "fn",
  "pub",
  "use",
  "mod"
];
function unglueLangPrefix(line) {
  for (const [prefix, lang] of Object.entries(GLUED_LANG_PREFIXES)) {
    if (!line.startsWith(prefix)) continue;
    const rest = line.slice(prefix.length);
    for (const tok of POST_PREFIX_TOKENS) {
      if (rest.startsWith(tok)) {
        const next = rest.charAt(tok.length);
        if (next === "" || /[\s({<\[]/.test(next)) {
          return { lang, line: rest };
        }
      }
    }
  }
  return null;
}
function isFenceLine(line) {
  const m = line.match(/^(```+|~~~+)([a-zA-Z0-9_+-]*)\s*$/);
  if (!m) return { open: false, close: false };
  const lang = m[2]?.trim() || void 0;
  return { open: !!lang, close: !lang, lang };
}
function sanitizeExtractedMarkdown(md) {
  if (!md.includes("```") && !md.includes("~~~")) return md;
  const lines = md.split("\n");
  const out = [];
  let inFence = false;
  let fenceMarker = null;
  let pendingFirstContentLine = false;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    if (!inFence) {
      const f = line.match(/^(```+|~~~+)([a-zA-Z0-9_+-]*)\s*$/);
      if (f) {
        inFence = true;
        fenceMarker = f[1];
        pendingFirstContentLine = true;
        const declaredLang = f[2];
        if (declaredLang === "markdown") {
          out.push(fenceMarker);
        } else {
          out.push(line);
        }
        continue;
      }
      out.push(line);
      continue;
    }
    if (fenceMarker && line.startsWith(fenceMarker) && line.replace(fenceMarker, "").trim() === "") {
      inFence = false;
      fenceMarker = null;
      pendingFirstContentLine = false;
      out.push(line);
      continue;
    }
    if (pendingFirstContentLine) {
      pendingFirstContentLine = false;
      const unglued = unglueLangPrefix(line);
      if (unglued) {
        const lastIdx = out.length - 1;
        const prev = out[lastIdx];
        if (prev.startsWith("```") || prev.startsWith("~~~")) {
          out[lastIdx] = `${prev.match(/^(```+|~~~+)/)[1]}${unglued.lang ?? ""}`;
        }
        out.push(unglued.line);
        continue;
      }
    }
    const trimmed = line.trim();
    if (trimmed && STRAY_LABELS.has(trimmed.toLowerCase()) && !line.includes(" ")) {
      continue;
    }
    out.push(line);
  }
  return out.join("\n");
}
export {
  sanitizeExtractedMarkdown
};
//# sourceMappingURL=markdown-sanitize.js.map