name: Build & Deploy VitePress
on:
  push:
    branches: [main]

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - run: echo '{"type":"module","devDependencies":{"vitepress":"^1.0.0"}}' > package.json
      - run: npm i

      - run: touch .nojekyll

      - run: node .github/scripts/prepare.mjs   # 生成 sidebar.mjs

      # 关键替换：直接复制模板，避开 heredoc
      - run: |
          mkdir -p .vitepress
          cp .github/config.template.ts .vitepress/config.ts
      - run: npx vitepress build .

      - uses: actions/upload-pages-artifact@v3
        with:
          path: .vitepress/dist

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.d.outputs.page_url }}
    steps:
      - uses: actions/deploy-pages@v4
        id: d
