"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 html_exports = {};
__export(html_exports, {
  htmlParser: () => htmlParser
});
module.exports = __toCommonJS(html_exports);
const HEADING_RE = /<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/gi;
function stripHtmlTags(html) {
  return html.replace(/<[^>]+>/g, "").trim();
}
function extractHeadings(content) {
  const headings = [];
  let match;
  HEADING_RE.lastIndex = 0;
  while ((match = HEADING_RE.exec(content)) !== null) {
    const title = stripHtmlTags(match[2]);
    if (title.length === 0) continue;
    headings.push({
      title,
      level: parseInt(match[1], 10),
      byteOffset: Buffer.byteLength(content.slice(0, match.index), "utf8")
    });
  }
  return headings;
}
function buildHierarchy(headings, content) {
  const contentBytes = Buffer.byteLength(content, "utf8");
  if (headings.length === 0) {
    const titleMatch = content.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
    const title = titleMatch ? stripHtmlTags(titleMatch[1]) : "(document)";
    return [{
      title,
      level: 0,
      byteStart: 0,
      byteEnd: contentBytes,
      content: stripHtmlTags(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 rawContent = Buffer.from(content, "utf8").slice(start, end).toString("utf8");
    sections.push({ ...headings[i], byteEnd: end, content: rawContent });
  }
  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 htmlParser = {
  name: "html",
  extensions: [".html", ".htm"],
  parse(content, _filePath) {
    const headings = extractHeadings(content);
    const sections = buildHierarchy(headings, content);
    return { sections, format: "html" };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  htmlParser
});
//# sourceMappingURL=html.js.map
