Skip to content
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
9 changes: 5 additions & 4 deletions projects/utils/src/lib/permalink.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
export function getPermalink(
title: string,
specialCategory: boolean,
category: string,
date?: string,
category?: string
): string {
const base = generateBase(date, category);
const base = specialCategory ? category : generateBase(date);
const titleDirectoryName = toKebabCase(title);
return `${base}/${titleDirectoryName}`;
}

function generateBase(date?: string, category?: string): string {
function generateBase(date?: string): string {
if (date && !isNaN(Date.parse(date))) {
const parsedDate = new Date(date);
return parsedDate.toISOString().split('T')[0].replace(/-/g, '/');
}
return category ?? 'unpublished';
return 'unpublished';
}

function toKebabCase(str: string): string {
Expand Down
4 changes: 4 additions & 0 deletions src/app/core/model/categories.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export enum Category {
'qa' = 'Quality Assurance',
'mobile' = 'Mobile',
}

export const SpecialCategories = [
'principles',
];
1 change: 1 addition & 0 deletions src/app/core/model/post.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Post {
category: Category;
tags: string[];
readingTime: string;
specialCategory: boolean;
}

export interface Posts {
Expand Down
15 changes: 11 additions & 4 deletions src/app/core/services/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Post, Posts } from '../model/post.model';
import { HttpClient } from '@angular/common/http';
import { getPermalink } from '@blog/utils';
import { AuthorsService } from './authors.service';
import { Category } from '../model/categories.model';
import { Category, SpecialCategories } from '../model/categories.model';

const POSTS_PER_PAGE = 8;

Expand All @@ -14,7 +14,14 @@ const POSTS_PER_PAGE = 8;
export class PostsService {
private cached: Observable<Post[]> = this.httpClient
.get<Post[]>('posts.json')
.pipe(shareReplay());
.pipe(
map((posts) => posts.map((post) => ({
...post,
specialCategory: SpecialCategories.includes(post.category),
date: post.date?.match(/^\d{4}-\d{2}-\d{2}/) ? post.date : undefined,
}))),
shareReplay()
);

constructor(
private httpClient: HttpClient,
Expand All @@ -30,7 +37,7 @@ export class PostsService {
map(
posts =>
posts.find(({ featured }) => featured) ??
posts.find(({ date }) => !!date)
posts.find(({ category }) => !SpecialCategories.includes(category))
)
);
}
Expand Down Expand Up @@ -77,7 +84,7 @@ export class PostsService {

getPost(permalink: string | null): Observable<Post | undefined> {
const filterByPermalink = (post: Post) =>
getPermalink(post.title, post.date, post.category) === permalink;
getPermalink(post.title, post.specialCategory, post.category, post.date) === permalink;
return this.getPosts(0, 1, false, (post: Post) =>
filterByPermalink(post)
).pipe(map(({ posts }) => posts[0]));
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/utils/post-url.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getPermalink } from '@blog/utils';
})
export class PostUrlPipe implements PipeTransform {
transform(post: Post, content?: string): string {
const postUrl = getPermalink(post.title, post.date, post.category);
const postUrl = getPermalink(post.title, post.specialCategory, post.category, post.date);

if (content) {
return `${postUrl}/${content}`;
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class SearchComponent implements OnInit {
goToPost(event: MatAutocompleteSelectedEvent) {
const post: Post = event.option.value;
this.router.navigateByUrl(
getPermalink(post.title, post.date, post.category)
getPermalink(post.title, post.specialCategory, post.category, post.date)
);
this.control.setValue('');
this.complete.emit();
Expand Down
2 changes: 1 addition & 1 deletion tools/post-scaffolder/src/post-scaffolder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function postScaffolder(options: any): Rule {
...options,
stringify,
}),
move(normalize(`content/posts/${getPermalink(options.title, 'unpublished')}`)),
move(normalize(`content/posts/${getPermalink(options.title, false, '', undefined)}`)),
]);

const rule = mergeWith(templateSource, MergeStrategy.Default);
Expand Down
3 changes: 2 additions & 1 deletion tools/publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ async function moveUnpublishedDirectory(sourcePath, destinationRoot) {
destinationRoot,
utils.getPermalink(
metaJsonObject.title,
false,
metaJsonObject.category,
metaJsonObject.date,
metaJsonObject.category
)
);

Expand Down