Skip to content

Commit 7c4408b

Browse files
Mudaafiesezenmocca102
authored
[CI-4292] Automate Table of Contents Generation using Doctoc (#219)
* add doctoc automation * Sets pre_push script to be git executable * Update pre_push hook to exclude changes made by doctoc * test pre_push hook * test: edits to doctoc section * test2: edits to doctoc section * update pre_push hook * update readme * update doctoc configs * lint * update pre-push hook to append a new commit instead of ammend * testing pre_push hook flow * revert back to amend * Test the hook * Test * testing * Test Removing section * a * b * c * Add thing * Add new topic * Revert "revert back to amend" This reverts commit e56366d. * Reapply "revert back to amend" This reverts commit 46be0f7. * Update readme * Change pre-push to pre-commit * base case * move to post-commit * fix post-commit script * Test: README Change outside TOC * docs: auto-update README TOC (post-commit) * fix post-commit script * test: local TOC change * test: addition outside TOC * docs: auto-update README TOC (post-commit) * test: removal outside TOC, TOC updated * run doctoc --------- Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com> Co-authored-by: mocca102 <islaam.agamy@gmail.com>
1 parent f81a7b3 commit 7c4408b

File tree

4 files changed

+3002
-530
lines changed

4 files changed

+3002
-530
lines changed

.husky/post-commit

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
# --- Configuration ---
5+
README_FILE="README.md"
6+
AUTO_COMMIT_MESSAGE=""
7+
8+
# --- Script Logic ---
9+
echo "Husky (post-commit): Checking for ${README_FILE} changes in the last commit."
10+
11+
# Check if README_FILE was modified in the last commit (HEAD)
12+
if git diff-tree --no-commit-id --name-only -r HEAD | grep -q "^${README_FILE}$"; then
13+
echo "Husky (post-commit): ${README_FILE} was modified in the last commit."
14+
15+
# Get the diff content of README_FILE from the last commit (HEAD vs HEAD^)
16+
# Ensure we handle the case of the very first commit (no HEAD^)
17+
if git rev-parse -q --verify HEAD^ >/dev/null 2>&1; then
18+
# Get the diff content, removing the meta-headers
19+
DIFF_CONTENT=$(git diff HEAD HEAD^ -U1000 "${README_FILE}" | sed 1,4d)
20+
else
21+
# First commit, consider all content as added outside TOC if not within TOC markers
22+
DIFF_CONTENT=$(git show HEAD:"${README_FILE}" -U1000 | sed 1,4d)
23+
fi
24+
25+
# Look for added lines (+) outside the TOC section
26+
DIFF_CONTENT_EXCLUDING_TOC=$(echo "$DIFF_CONTENT" | sed '/<!-- START doctoc/,/<!-- END doctoc/d' | grep '^[+-]' || true)
27+
28+
if [ -n "$DIFF_CONTENT_EXCLUDING_TOC" ]; then
29+
echo "Husky (post-commit): Changes detected in ${README_FILE} outside of the TOC section in the last commit."
30+
echo "Husky (post-commit): Running 'npm run doctoc-readme' to update the TOC..."
31+
32+
# Run doctoc. It will modify the working directory file if needed.
33+
npm run doctoc-readme
34+
35+
# Check if doctoc actually modified the README_FILE in the working directory
36+
if ! git diff --quiet "${README_FILE}"; then # True if working dir has changes for README_FILE
37+
echo "Husky (post-commit): 'npm run doctoc-readme' has modified ${README_FILE}."
38+
echo "Husky (post-commit): Staging these changes and creating a new commit for the TOC update."
39+
git add "${README_FILE}"
40+
41+
# Create a new commit for the TOC changes. Use --no-verify to prevent hooks from running again.
42+
git commit --no-verify -m "docs: auto-update README TOC (post-commit)"
43+
if [ $? -ne 0 ]; then
44+
echo "Husky (post-commit): ERROR - Failed to commit TOC update."
45+
fi
46+
47+
else
48+
echo "Husky (post-commit): 'npm run doctoc-readme' ran. No further changes were needed for ${README_FILE} or it was already up-to-date."
49+
fi
50+
else
51+
echo "Husky (post-commit): No changes detected in ${README_FILE} outside of the TOC section in the last commit."
52+
fi
53+
else
54+
echo "Husky (post-commit): ${README_FILE} was not modified in the last commit. Skipping TOC check."
55+
fi
56+
57+
exit 0 # Post-commit hooks should always exit 0

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@
2020

2121
</div>
2222

23+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
24+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
25+
## 📌 Table of Contents
26+
27+
- [Features](#features)
28+
- [React](#react)
29+
- [⚡ Installation & Quick Start](#-installation--quick-start)
30+
- [Shopify](#shopify)
31+
- [⚡ Installation & Quick Start](#-installation--quick-start-1)
32+
- [Bundle (Vanilla JS)](#bundle-vanilla-js)
33+
- [⚡ Installation & Quick Start](#-installation--quick-start-2)
34+
- [💡 Code Examples](#-code-examples)
35+
- [🎨 Customization](#-customization)
36+
- [🚀 Advanced Features](#-advanced-features)
37+
- [🛠 Troubleshooting](#-troubleshooting)
38+
- [📖 API Reference](#-api-reference)
39+
- [🔗 Complementary Resources](#-complementary-resources)
40+
- [🤝 Contributing](#-contributing)
41+
- [📜 License](#-license)
42+
43+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
44+
2345
## Features
2446

2547
- 🔌 Easy Integration – Quickly integrate with your app as a plug-and-play React component

0 commit comments

Comments
 (0)