"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var markdown_exports = {};
__export(markdown_exports, {
  markdownParser: () => markdownParser
});
module.exports = __toCommonJS(markdown_exports);
const ATX_HEADING = /^(#{1,6})\s+(.+?)(?:\s+#+)?$/;
const SETEXT_H1 = /^=+\s*$/;
const SETEXT_H2 = /^-+\s*$/;
const FENCE_OPEN = /^(`{3,}|~{3,})/;
function extractHeadings(content) {
  const lines = content.split("\n");
  const headings = [];
  let inFence = false;
  let fenceChar = "";
  let fenceLen = 0;
  let byteOffset = 0;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const lineBytes = Buffer.byteLength(line, "utf8") + 1;
    if (!inFence) {
      const fenceMatch = line.match(FENCE_OPEN);
      if (fenceMatch) {
        inFence = true;
        fenceChar = fenceMatch[1][0];
        fenceLen = fenceMatch[1].length;
        byteOffset += lineBytes;
        continue;
      }
    } else {
      const closingPattern = new RegExp(`^${fenceChar}{${fenceLen},}\\s*$`);
      if (closingPattern.test(line)) {
        inFence = false;
      }
      byteOffset += lineBytes;
      continue;
    }
    const atxMatch = line.match(ATX_HEADING);
    if (atxMatch) {
      headings.push({
        title: atxMatch[2].trim(),
        level: atxMatch[1].length,
        lineIndex: i,
        byteOffset
      });
      byteOffset += lineBytes;
      continue;
    }
    if (i + 1 < lines.length && line.trim().length > 0) {
      const nextLine = lines[i + 1];
      if (SETEXT_H1.test(nextLine)) {
        headings.push({
          title: line.trim(),
          level: 1,
          lineIndex: i,
          byteOffset
        });
      } else if (SETEXT_H2.test(nextLine)) {
        headings.push({
          title: line.trim(),
          level: 2,
          lineIndex: i,
          byteOffset
        });
      }
    }
    byteOffset += lineBytes;
  }
  return headings;
}
function buildHierarchy(headings, content) {
  const contentBytes = Buffer.byteLength(content, "utf8");
  if (headings.length === 0) {
    return [{
      title: "(document)",
      level: 0,
      byteStart: 0,
      byteEnd: contentBytes,
      content,
      children: []
    }];
  }
  const sections = [];
  for (let i = 0; i < headings.length; i++) {
    const start = headings[i].byteOffset;
    const end = i + 1 < headings.length ? headings[i + 1].byteOffset : contentBytes;
    const sectionContent = Buffer.from(content, "utf8").slice(start, end).toString("utf8");
    sections.push({ ...headings[i], byteEnd: end, content: sectionContent });
  }
  const root = [];
  const stack = [];
  for (const s of sections) {
    const parsed = {
      title: s.title,
      level: s.level,
      byteStart: s.byteOffset,
      byteEnd: s.byteEnd,
      content: s.content,
      children: []
    };
    while (stack.length > 0 && stack[stack.length - 1].level >= s.level) {
      stack.pop();
    }
    if (stack.length === 0) {
      root.push(parsed);
    } else {
      stack[stack.length - 1].section.children.push(parsed);
    }
    stack.push({ section: parsed, level: s.level });
  }
  return root;
}
const markdownParser = {
  name: "markdown",
  extensions: [".md", ".mdx", ".cheatmd"],
  parse(content, _filePath) {
    const headings = extractHeadings(content);
    const sections = buildHierarchy(headings, content);
    return {
      sections,
      format: "markdown"
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  markdownParser
});
//# sourceMappingURL=markdown.js.map
