Skip to content

Commit

Permalink
Check author image URLs (#118)
Browse files Browse the repository at this point in the history
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
  • Loading branch information
harupy authored Oct 24, 2024
1 parent 01f815f commit b48522e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
run: yarn
- working-directory: website
run: yarn check-thumbnails
- working-directory: website
run: yarn check-authors
- working-directory: website
run: |
yarn fmt
Expand Down
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"fmt-notes": "ts-node scripts/release-note-format.js",
"compile": "ts-node scripts/compile.ts && prettier --write src/posts.ts",
"check-thumbnails": "ts-node scripts/check-thumbnails.ts",
"check-authors": "ts-node scripts/check-authors.ts",
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids",
"typecheck": "tsc",
Expand Down
55 changes: 55 additions & 0 deletions website/scripts/check-authors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from "fs";
import path from "path";
import yaml from "js-yaml";

function fileExistsWithCaseSync(filepath: string): boolean {
const dir = path.dirname(filepath);
if (dir === "/" || dir === ".") {
return true;
}
const filenames = fs.readdirSync(dir);
if (filenames.indexOf(path.basename(filepath)) === -1) {
return false;
}
return fileExistsWithCaseSync(dir);
}

type Author = {
name: string;
title: string;
url: string;
image_url?: string;
};

function readAuthors(): Record<string, Author> {
const authorsFile = fs.readFileSync(
path.join(process.cwd(), "blog", "authors.yml"),
"utf-8",
);
return yaml.load(authorsFile) as Record<string, Author>;
}

function main(): void {
const authors = readAuthors();
const authorsWithInvalidImageUrl: string[] = [];
Object.entries(authors).forEach(([author, authorData]) => {
if (
authorData.image_url &&
!/^https?:\/\//.test(authorData.image_url) &&
!fileExistsWithCaseSync(
path.join(process.cwd(), "static", authorData.image_url),
)
) {
authorsWithInvalidImageUrl.push(author);
}
});

if (authorsWithInvalidImageUrl.length > 0) {
console.log("Found authors with invalid image URLs:");
console.log(authorsWithInvalidImageUrl);
console.log("Please make sure the image exists in the static folder.");
process.exit(1);
}
}

main();

0 comments on commit b48522e

Please sign in to comment.