name: Pull-Request-CI

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  Build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    defaults:
      run:
        shell: bash -l {0}
    steps:
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Install Python Packages
        run: |
          pip install -r requirements.txt
      - name: Build Website
        run: |
          mkdocs build
      - uses: actions/upload-artifact@v4
        with:
          name: site
          path: site/

  Spell-and-Grammar-Check:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    defaults:
      run:
        shell: bash -l {0}
    steps:
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
      - uses: actions/setup-node@v4
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Install Python Packages
        run: |
          pip install -r requirements.txt
          npm i gramma -g
      - name: Build Website
        run: |
          mkdocs build
      - name: codespell Check  
        run: | 
          codespell docs/
      - name: Grammar Check
        run: |
          git remote add origin https://github.com/DOI-USGS/asc-public-docs.git 
          git fetch origin main
          # List the files that changed 
          git diff --name-only origin/main
          # Grammar check only those files
          git diff --name-only origin/main | grep -e .md -e .MD | sed 's/^/"/;s/$/"/' | xargs -t -L1 gramma check -p || true

  Link-Check:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    defaults:
      run:
        shell: bash -l {0}
    steps:
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Install Python Packages
        run: |
          pip install -r requirements.txt
      - name: Build Website
        run: |
          mkdocs build
      - name: spell check 
        run: | 
          linkchecker site/index.html --ignore-url '(asc-public-docs/site/docs/software/|manuals/)'
