const EM_DASH_PATTERN = /---/g;

function visitTextNodes(node) {
  if (!node || typeof node !== "object") return;

  if (node.type === "text" && typeof node.value === "string") {
    node.value = node.value.replace(EM_DASH_PATTERN, "—");
    return;
  }

  if (!Array.isArray(node.children)) return;

  for (const child of node.children) {
    visitTextNodes(child);
  }
}

export default function remarkTypography() {
  return (tree) => {
    visitTextNodes(tree);
  };
}
