From f29a33ce2b8ee5a86fb75b716fb87372f5e26e1a Mon Sep 17 00:00:00 2001 From: Robinson Zimmermann Date: Mon, 12 Feb 2024 22:19:31 +0100 Subject: [PATCH 1/2] Add authors to routes.txt file --- angular.json | 2 +- projects/utils/src/lib/permalink.ts | 4 ++++ routes.txt | 29 ----------------------------- tools/build-posts/index.js | 24 +++++++++++++++++++++--- 4 files changed, 26 insertions(+), 33 deletions(-) delete mode 100644 routes.txt diff --git a/angular.json b/angular.json index c2a40a50..c7315ab0 100644 --- a/angular.json +++ b/angular.json @@ -47,7 +47,7 @@ }, "styles": ["styles/styles.scss"], "prerender": { - "routesFile": "routes.txt" + "routesFile": "dist/routes.txt" }, "scripts": [ "node_modules/prismjs/prism.js", diff --git a/projects/utils/src/lib/permalink.ts b/projects/utils/src/lib/permalink.ts index c819fde6..65856bac 100644 --- a/projects/utils/src/lib/permalink.ts +++ b/projects/utils/src/lib/permalink.ts @@ -25,3 +25,7 @@ export function getPermalink( return `${base}/${titleDirectoryName}`; } + +export function getAuthorPermalink(authorName: string) { + return authorName.replace(/\W/g, '-').toLowerCase(); +} diff --git a/routes.txt b/routes.txt deleted file mode 100644 index 98c11ed8..00000000 --- a/routes.txt +++ /dev/null @@ -1,29 +0,0 @@ -/2020/03/04/kubernetes-application-developer-certification-tips -/2020/07/24/building-an-ios-14-widget-to-show-account-balance -/2020/08/14/configuration-driven-ui-from-epoxy-to-compose -/2021/06/16/flexible-configuration-with-deferred-resources -/2021/09/10/hello-blockchain -/2021/12/01/the-ultimate-guide-to-slack-etiquette -/2022/06/08/deploying-an-angular-app-on-azure -/2022/07/01/ease-your-life-at-work-with-fastlane -/2022/11/16/android-mobile-testing-fragments -/2023/05/01/shift-left-approach-to-testing -/2023/05/12/ngoptimizedimage-directive-in-angular-15 -/2023/05/16/hacking-netflix-eureka -/2023/07/03/angular-standalone-components -/2023/09/29/the-better-way-with-ruby-gems -/2023/09/30/installing-hms-core-in-android-studio-emulator -/2023/10/06/code-coverage-for-unit-tests -/2023/10/25/angular-unit-testing-with-spectator -/2023/11/03/visual-testing-using-playwright -/2023/11/21/a-new-chapter-exploring-azure-resources -/2023/12/13/angular-micro-frontends -/principles/innersource -/principles/software-engineering -/category/tech-life -/category/devops -/category/backend -/category/career -/category/frontend -/category/sdlc -/404 \ No newline at end of file diff --git a/tools/build-posts/index.js b/tools/build-posts/index.js index 5164b481..eb16116b 100644 --- a/tools/build-posts/index.js +++ b/tools/build-posts/index.js @@ -45,7 +45,20 @@ function scanDirectory(directoryPath, filesArray, routesArray) { ); } -function main() { +async function getAuthorRoutes() { + if (fs.existsSync('content/authors/authors.json')) { + const authors = JSON.parse(fs.readFileSync('content/authors/authors.json', 'utf8')); + + const utils = await loadEsmModule( + '../../dist/utils/esm2022/lib/permalink.mjs' + ); + return Object.keys(authors).map(name => + `people/${utils.getAuthorPermalink(name)}`); + } + return []; +} + +async function main() { const startDirectory = 'content/posts'; // Change this to the starting directory path const outputFilePath = 'content/posts/posts.json'; // Change this to the desired output file path @@ -68,11 +81,16 @@ function main() { '/category/career', '/category/frontend', '/category/sdlc', - '/404' + '/404', + ...(await getAuthorRoutes()) ); - fs.writeFileSync('routes.txt', routesArray.join('\r\n'), 'utf8'); + fs.writeFileSync('dist/routes.txt', routesArray.join('\r\n'), 'utf8'); console.log(`Scanning completed. Output written to ${outputFilePath}`); } main(); + +function loadEsmModule(modulePath) { + return new Function('modulePath', `return import(modulePath);`)(modulePath); +} From 1cc287789c5676d397835673da4c290944ad147c Mon Sep 17 00:00:00 2001 From: Robinson Zimmermann Date: Mon, 12 Feb 2024 22:26:10 +0100 Subject: [PATCH 2/2] Reuse author permalink --- src/app/core/services/authors.service.ts | 3 ++- tools/build-posts/index.js | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/app/core/services/authors.service.ts b/src/app/core/services/authors.service.ts index 482b2db1..39bfa9be 100644 --- a/src/app/core/services/authors.service.ts +++ b/src/app/core/services/authors.service.ts @@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Author, AuthorsList } from '../model/author.model'; import { Observable, map, shareReplay } from 'rxjs'; +import { getAuthorPermalink } from '@blog/utils'; @Injectable({ providedIn: 'root', @@ -22,7 +23,7 @@ export class AuthorsService { [curr]: { ...authors[curr], fullname: curr, - url: `people/${curr.toLowerCase().replace(/\W/g, '-')}`, + url: `/people/${getAuthorPermalink(curr)}`, }, }), {} diff --git a/tools/build-posts/index.js b/tools/build-posts/index.js index eb16116b..259c9809 100644 --- a/tools/build-posts/index.js +++ b/tools/build-posts/index.js @@ -45,15 +45,15 @@ function scanDirectory(directoryPath, filesArray, routesArray) { ); } -async function getAuthorRoutes() { - if (fs.existsSync('content/authors/authors.json')) { - const authors = JSON.parse(fs.readFileSync('content/authors/authors.json', 'utf8')); +async function getAuthorRoutes(source) { + if (fs.existsSync(source)) { + const authors = JSON.parse(fs.readFileSync(source, 'utf8')); const utils = await loadEsmModule( '../../dist/utils/esm2022/lib/permalink.mjs' ); return Object.keys(authors).map(name => - `people/${utils.getAuthorPermalink(name)}`); + `/people/${utils.getAuthorPermalink(name)}`); } return []; } @@ -61,6 +61,7 @@ async function getAuthorRoutes() { async function main() { const startDirectory = 'content/posts'; // Change this to the starting directory path const outputFilePath = 'content/posts/posts.json'; // Change this to the desired output file path + const authorsFilePath = 'content/authors/authors.json'; if (fs.existsSync(outputFilePath)) { fs.unlinkSync(outputFilePath); @@ -82,7 +83,7 @@ async function main() { '/category/frontend', '/category/sdlc', '/404', - ...(await getAuthorRoutes()) + ...(await getAuthorRoutes(authorsFilePath)) ); fs.writeFileSync('dist/routes.txt', routesArray.join('\r\n'), 'utf8');