-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add github action to check for redirects on rename
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const redirectsFilePath = path.join(__dirname, '_redirects'); | ||
const postsDirectory = path.join(__dirname, '_posts'); | ||
|
||
function updateRedirects(originalName, newName) { | ||
const redirects = fs.readFileSync(redirectsFilePath, 'utf-8'); | ||
const lines = redirects.split('\n'); | ||
|
||
const newRedirect = `/blog/${originalName} /blog/${newName}`; | ||
const updatedRedirects = lines.map(line => { | ||
if (line.trim() === newRedirect.trim()) { | ||
return newRedirect; | ||
} | ||
return line; | ||
}).join('\n'); | ||
|
||
fs.writeFileSync(redirectsFilePath, updatedRedirects, 'utf-8'); | ||
} | ||
|
||
const changedFiles = JSON.parse(process.env.GITHUB_EVENT_PATH).commits[0].added.concat( | ||
JSON.parse(process.env.GITHUB_EVENT_PATH).commits[0].modified | ||
); | ||
|
||
changedFiles.forEach(file => { | ||
if (file.startsWith('_posts/')) { | ||
const originalName = path.basename(file, path.extname(file)); | ||
const newName = path.basename(file, path.extname(file)).replace(/^\d{4}-\d{2}-\d{2}-/, ''); | ||
updateRedirects(originalName, newName); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Detect Renamed Posts | ||
|
||
on: | ||
|
||
pull_request: | ||
branches: [master] | ||
paths: | ||
- '_posts/**' | ||
|
||
jobs: | ||
detect_renamed_post: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set Up Node.js | ||
uses: actions/setup-node@v2 | ||
|
||
- name: Run Renaming Script | ||
run: node .github/workflows/detect-renamed-posts.js |
File renamed without changes.