Skip to content
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

Automate removal of unused images #108

Merged
merged 3 commits into from
Nov 14, 2023
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
32 changes: 32 additions & 0 deletions .github/workflows/remove-unused-images.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Remove Unused Images

on:
workflow_dispatch:
branches:
- gh-pages

jobs:
remove-unused-images:
runs-on: "ubuntu-latest"
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16.13.x
cache: npm
- run: npm install
- name: Remove Unused Images
run: npm run remove-unused-images
- name: Create Pull Request if Needed
uses: peter-evans/create-pull-request@v4
with:
title: Auto Removed Images
branch: auto-removed-images
branch-suffix: short-commit-hash
commit-message: Auto Removed Images
base: gh-pages
body: |
This PR was automatically generated by a GitHub Action.
It removes all images that are not being used in any blog post.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test": "node lintPosts.js",
"compute-embeddings": "node scripts/generate-related/compute-embeddings.js",
"generate-related": "node scripts/generate-related/blog-metadata.js",
"remove-unused-images": "node scripts/images/remove-images.js",
"spellcheck": "mdspell \"**/ceberhardt/_posts/*.md\" --en-gb -a -n -x -t",
"style": "node-sass --omit-source-map-url --include --include-path=node_modules/cookieconsent/build/ --include-path=node_modules/foundation-sites/scss/ --output-style=compressed scss/style.scss style.css",
"scripts": "uglifyjs scripts/initialise-menu.js scripts/jquery-1.9.1.js scripts/jquery.jscroll-2.2.4.js scripts/load-clap-count.js scripts/elapsed.js scripts/graft-studio/header-scroll.js scripts/graft-studio/jquery.mmenu.all.js scripts/graft-studio/jquery.matchHeight.js scripts/load-google-tag-manager.js node_modules/applause-button/dist/applause-button.js node_modules/cookieconsent/build/cookieconsent.min.js -o script.js"
Expand Down
29 changes: 29 additions & 0 deletions scripts/images/remove-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const glob = require("glob-promise");
const fs = require("fs");

(async () => {
const postPaths = await glob('./**/*.{md,markdown,html,yml,js}').then((paths) => {
return paths;
});

const imagePaths = await glob('./**/*.{gif,png,jpg,jpeg}', {ignore:'./_site/**'}).then((paths) => {
return paths;
});

for (const imagePath of imagePaths) {
const image = imagePath.split("/").pop();
var found = false;

for (const postpath of postPaths) {
const file = fs.readFileSync(postpath).toString();
if(file.includes(image) || file.includes(encodeURI(image))){
found = true;
break;
}
}

if(!found){
fs.unlinkSync(imagePath);
}
}
})();