name: Remark

on:
  merge_group:
  pull_request:

jobs:
  remark:
    runs-on: ubuntu-latest

    steps:
    # Setup
    - name: Checkout
      uses: actions/checkout@v5
      with:
        # Unsetting this would make so that any malicious package could get our Github Token
        persist-credentials: false

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18.x'

    - name: Install remark
      run: npm install remark-cli remark-lint remark-lint-maximum-line-length@^3.1.3 remark-preset-lint-recommended remark-gfm

    - name: Install mdbook
      run: |
        mkdir mdbook
        curl -Lf https://github.com/rust-lang/mdBook/releases/download/v0.4.43/mdbook-v0.4.43-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
        echo `pwd`/mdbook >> $GITHUB_PATH

    # Run
    - name: Check *.md files
      run: ./node_modules/.bin/remark -u lint -f .

    - name: Linkcheck book
      run: |
        rustup toolchain install nightly --component rust-docs
        rustup override set nightly
        curl https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh -o linkcheck.sh
        sh linkcheck.sh clippy --path ./book

    - name: Build mdbook
      run: mdbook build book

  conclusion_remark:
    needs: [ remark ]
    # We need to ensure this job does *not* get skipped if its dependencies fail,
    # because a skipped job is considered a success by GitHub. So we have to
    # overwrite `if:`. We use `!cancelled()` to ensure the job does still not get run
    # when the workflow is canceled manually.
    #
    # ALL THE PREVIOUS JOBS NEED TO BE ADDED TO THE `needs` SECTION OF THIS JOB!
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    steps:
      # Manually check the status of all dependencies. `if: failure()` does not work.
      - name: Conclusion
        run: |
          # Print the dependent jobs to see them in the CI log
          jq -C <<< '${{ toJson(needs) }}'
          # Check if all jobs that we depend on (in the needs array) were successful.
          jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'
