name: Deploy GH pages
on:
  push:
    branches:
      - master
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - uses: actions/checkout@v4
      - name: Setup Pages
        uses: actions/configure-pages@v3
      - name: Install mdbook
        env:
          MDBOOK_VERSION: 'v0.4.27'
        run: |
          mkdir mdbook
          curl -sSL https://github.com/rust-lang/mdBook/releases/download/${MDBOOK_VERSION}/mdbook-${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
          echo `pwd`/mdbook >> $GITHUB_PATH
      - name: Build mdbook
        run: |
          cd doc
          mdbook build
      # Override mdbooks default highlight.js with a custom version containing CMake support.
      - uses: actions/checkout@v4
        with:
          repository: 'highlightjs/highlight.js'
          # mdbook currently (as of v0.4.27) does not support v11 yet.
          ref: '10.7.3'
          path: highlightjs
      - name: Build custom highlight.js
        run: |
          npm install
          node tools/build.js :common cmake yaml
        working-directory: highlightjs
      - name: Override highlightjs
        run: |
          cp highlightjs/build/highlight.min.js doc/book/highlight.js
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1
        with:
          path: 'doc/book'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1
