Skip to content

Add table of contents to readme #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,69 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-D warnings"

jobs:
check-readme:
name: Check README
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Build readme
run: ./build-readme.sh

- name: Check for changes
id: git-check
run: |
if git diff --exit-code README.md; then
echo "TOC is up to date!"
else
echo "::error:: README.md out of date! Please run './build-readme.sh' locally and commit the changes."
git diff README.md
exit 1
fi

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt

- name: Rust cache
uses: Swatinem/rust-cache@v2

- name: Check formatting
run: cargo fmt --check

- name: Run clippy
run: cargo clippy -- -D warnings

test:
name: Test (${{ matrix.os }})
needs: [lint]
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
timeout-minutes: 15
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- uses: actions/checkout@v4

- name: Rust cache
uses: Swatinem/rust-cache@v2
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Build
run: cargo build --verbose
- name: Rust cache
uses: Swatinem/rust-cache@v2

- name: Lint
run: cargo clippy
- name: Build
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose
- name: Run tests
run: cargo test --verbose
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ If the instance you're attempting to replace has changed since the search was pe

![Scooter preview](media/preview.gif)

## Contents

<!-- TOC START -->
- [Features](#features)
- [Usage](#usage)
- [Search fields](#search-fields)
- [Installation](#installation)
- [Homebrew](#homebrew)
- [NixOS](#nixos)
- [AUR](#aur)
- [Prebuilt binaries](#prebuilt-binaries)
- [Cargo](#cargo)
- [Building from source](#building-from-source)
- [Editor configuration](#editor-configuration)
- [Helix](#helix)
- [Neovim](#neovim)
- [Contributing](#contributing)
<!-- TOC END -->

## Features

Scooter respects both `.gitignore` and `.ignore` files.
Expand Down
83 changes: 83 additions & 0 deletions build-readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

README_FILE="README.md"
TEMP_FILE="README.tmp"
TOC_START_MARKER="<!-- TOC START -->"
TOC_END_MARKER="<!-- TOC END -->"
TOC_HEADING="## Contents"

if [ ! -f "$README_FILE" ]; then
echo "Error: $README_FILE not found!"
exit 1
fi

cp "$README_FILE" "$TEMP_FILE"

# Extract headings (## and ###) from the README
# Skip the TOC section itself and any code blocks
toc=""
in_code_block=false
in_toc_section=false

while IFS= read -r line; do
if [[ "$line" =~ ^(\`\`\`|\~\~\~) ]]; then
if $in_code_block; then
in_code_block=false
else
in_code_block=true
fi
continue
fi

if $in_code_block; then
continue
fi

if [[ "$line" == "$TOC_START_MARKER" ]]; then
in_toc_section=true
continue
fi
if [[ "$line" == "$TOC_END_MARKER" ]]; then
in_toc_section=false
continue
fi

if $in_toc_section; then
continue
fi

if [[ "$line" == "$TOC_HEADING" ]]; then
continue
fi

if [[ "$line" =~ ^##\ (.+)$ ]] && [[ "$line" != "$TOC_HEADING" ]]; then
title="${BASH_REMATCH[1]}"
# Create anchor link (lowercase, replace spaces with hyphens)
anchor=$(echo "$title" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g' | sed 's/[^a-z0-9-]//g')
toc="${toc}- [${title}](#${anchor})\n"
elif [[ "$line" =~ ^###\ (.+)$ ]]; then
title="${BASH_REMATCH[1]}"
# Create anchor link (lowercase, replace spaces with hyphens)
anchor=$(echo "$title" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g' | sed 's/[^a-z0-9-]//g')
toc="${toc} - [${title}](#${anchor})\n"
fi
done < "$README_FILE"

# Insert the TOC between markers
awk -v toc="$toc" -v start="$TOC_START_MARKER" -v end="$TOC_END_MARKER" '
{
if ($0 == start) {
print $0
printf "%s", toc
in_toc = 1
} else if ($0 == end) {
in_toc = 0
print $0
} else if (!in_toc) {
print $0
}
}' "$README_FILE" > "$TEMP_FILE"

mv "$TEMP_FILE" "$README_FILE"

echo "Table of contents generated successfully!"
3 changes: 2 additions & 1 deletion tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ macro_rules! create_test_files {
)+

#[cfg(windows)]
sleep(Duration::from_millis(100));
let _ = sleep(Duration::from_millis(100));

temp_dir
}
};
Expand Down
Loading