const { visit } = require('unist-util-visit');

/**
 * Remark plugin to escape curly braces in text nodes
 * Prevents MDX from interpreting {variable} as JSX expressions
 */
function remarkEscapeBraces() {
  return (tree) => {
    visit(tree, 'text', (node, index, parent) => {
      // Skip code blocks and inline code
      if (parent && (parent.type === 'code' || parent.type === 'inlineCode')) {
        return;
      }
      // Escape curly braces in text
      node.value = node.value.replace(/\\{/g, '\\{').replace(/\\}/g, '\\}');
    });
  };
}

module.exports = remarkEscapeBraces;
