def update_or_create_mdx(connector_name, connector_docs_dir, mdx_content):
    """Create or update main.mdx file for a connector."""
    # Create connector docs directory if it doesn't exist
    os.makedirs(connector_docs_dir, exist_ok=True)

    # Create or update main.mdx
    main_mdx_path = connector_docs_dir / "main.mdx"

    # Format display name for frontmatter
    display_name = connector_name.replace("_", " ").title()

    # Add frontmatter
    frontmatter = f"""---
title: "{display_name}"
description: "{display_name} integration with Airweave"
---

"""

    if main_mdx_path.exists():
        # If file exists, preserve custom content and update only auto-generated part
        with open(main_mdx_path, "r") as f:
            existing_content = f.read()

        # Replace content between auto-generated markers
        start_index = existing_content.find(CONTENT_START_MARKER)
        end_index = existing_content.find(CONTENT_END_MARKER) + len(CONTENT_END_MARKER)

        if start_index != -1 and end_index != -1:
            # Replace only the auto-generated part
            updated_content = (
                existing_content[:start_index]
                + CONTENT_START_MARKER
                + "\n\n"
                + mdx_content.replace(CONTENT_START_MARKER, "").replace(CONTENT_END_MARKER, "")
                + "\n\n"
                + CONTENT_END_MARKER
                + existing_content[end_index:]
            )

            with open(main_mdx_path, "w") as f:
                f.write(updated_content)

            print(f"  Updated auto-generated content for {connector_name}")
        else:
            # No markers found, treat as a new file
            with open(main_mdx_path, "w") as f:
                f.write(frontmatter + mdx_content)

            print(
                f"  Created new main.mdx for {connector_name} (no markers found in existing file)"
            )
    else:
        # Create the file with the frontmatter and generated content
        with open(main_mdx_path, "w") as f:
            f.write(frontmatter + mdx_content)

        print(f"  Created new main.mdx for {connector_name}")

    return True
Create or update main.mdx file for a connector.
fern/scripts/update_connector_docs/utils/file_utils.py