/**
 * Wikkel elke <table> in <div class="table-wrap"> zodat brede markdown-tabellen
 * horizontaal kunnen scrollen zonder de table-layout te breken.
 */
export default function rehypeWrapTables() {
  return (tree) => {
    walk(tree);
  };
}

function walk(node) {
  if (!node?.children?.length) return;

  for (let i = 0; i < node.children.length; i++) {
    const child = node.children[i];
    if (child.type === "element" && child.tagName === "table") {
      node.children[i] = {
        type: "element",
        tagName: "div",
        properties: { className: ["table-wrap"] },
        children: [child],
      };
      continue;
    }
    walk(child);
  }
}
