@@ -107,13 +107,37 @@ jobs:
107107 if [ "${{ steps.check_changelog.outputs.exists }}" == "true" ]; then
108108 # Insert new entry after the header
109109 if [ -f "new-changelog-entry.md" ]; then
110- # Create temp file with header, new entry, and existing content
111- echo "# Changelog" > temp-changelog.md
112- echo "" >> temp-changelog.md
113- cat new-changelog-entry.md >> temp-changelog.md
114- echo "" >> temp-changelog.md
115- # Skip the header line and add existing content
116- tail -n +3 CHANGELOG.md >> temp-changelog.md
110+ # Create a temporary file
111+ touch temp-changelog.md
112+
113+ # Process the file line by line
114+ header_found=false
115+ first_version_found=false
116+
117+ while IFS= read -r line; do
118+ if [[ "$line" =~ ^#[[:space:]]Changelog ]]; then
119+ # Found the header
120+ echo "$line" >> temp-changelog.md
121+ echo "" >> temp-changelog.md
122+ header_found=true
123+ elif [[ "$header_found" == "true" && "$first_version_found" == "false" && "$line" =~ ^##[[:space:]]\[ ]]; then
124+ # Found the first version entry, insert new content before it
125+ cat new-changelog-entry.md >> temp-changelog.md
126+ echo "" >> temp-changelog.md
127+ echo "$line" >> temp-changelog.md
128+ first_version_found=true
129+ else
130+ # Copy all other lines as-is
131+ echo "$line" >> temp-changelog.md
132+ fi
133+ done < CHANGELOG.md
134+
135+ # If no version entries were found (empty changelog), append the new entry
136+ if [[ "$first_version_found" == "false" && "$header_found" == "true" ]]; then
137+ cat new-changelog-entry.md >> temp-changelog.md
138+ fi
139+
140+ # Replace the original file
117141 mv temp-changelog.md CHANGELOG.md
118142 fi
119143 else
0 commit comments