Skip to content

Commit 3c50a67

Browse files
Fix unpublished permalink (#108)
1 parent c8a910a commit 3c50a67

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

projects/utils/src/lib/permalink.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
export function getPermalink(
22
title: string,
3+
specialCategory: boolean,
4+
category: string,
35
date?: string,
4-
category?: string
56
): string {
6-
const base = generateBase(date, category);
7+
const base = specialCategory ? category : generateBase(date);
78
const titleDirectoryName = toKebabCase(title);
89
return `${base}/${titleDirectoryName}`;
910
}
1011

11-
function generateBase(date?: string, category?: string): string {
12+
function generateBase(date?: string): string {
1213
if (date && !isNaN(Date.parse(date))) {
1314
const parsedDate = new Date(date);
1415
return parsedDate.toISOString().split('T')[0].replace(/-/g, '/');
1516
}
16-
return category ?? 'unpublished';
17+
return 'unpublished';
1718
}
1819

1920
function toKebabCase(str: string): string {

src/app/core/model/categories.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export enum Category {
99
'qa' = 'Quality Assurance',
1010
'mobile' = 'Mobile',
1111
}
12+
13+
export const SpecialCategories = [
14+
'principles',
15+
];

src/app/core/model/post.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Post {
1111
category: Category;
1212
tags: string[];
1313
readingTime: string;
14+
specialCategory: boolean;
1415
}
1516

1617
export interface Posts {

src/app/core/services/posts.service.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Post, Posts } from '../model/post.model';
44
import { HttpClient } from '@angular/common/http';
55
import { getPermalink } from '@blog/utils';
66
import { AuthorsService } from './authors.service';
7-
import { Category } from '../model/categories.model';
7+
import { Category, SpecialCategories } from '../model/categories.model';
88

99
const POSTS_PER_PAGE = 8;
1010

@@ -14,7 +14,14 @@ const POSTS_PER_PAGE = 8;
1414
export class PostsService {
1515
private cached: Observable<Post[]> = this.httpClient
1616
.get<Post[]>('posts.json')
17-
.pipe(shareReplay());
17+
.pipe(
18+
map((posts) => posts.map((post) => ({
19+
...post,
20+
specialCategory: SpecialCategories.includes(post.category),
21+
date: post.date?.match(/^\d{4}-\d{2}-\d{2}/) ? post.date : undefined,
22+
}))),
23+
shareReplay()
24+
);
1825

1926
constructor(
2027
private httpClient: HttpClient,
@@ -30,7 +37,7 @@ export class PostsService {
3037
map(
3138
posts =>
3239
posts.find(({ featured }) => featured) ??
33-
posts.find(({ date }) => !!date)
40+
posts.find(({ category }) => !SpecialCategories.includes(category))
3441
)
3542
);
3643
}
@@ -77,7 +84,7 @@ export class PostsService {
7784

7885
getPost(permalink: string | null): Observable<Post | undefined> {
7986
const filterByPermalink = (post: Post) =>
80-
getPermalink(post.title, post.date, post.category) === permalink;
87+
getPermalink(post.title, post.specialCategory, post.category, post.date) === permalink;
8188
return this.getPosts(0, 1, false, (post: Post) =>
8289
filterByPermalink(post)
8390
).pipe(map(({ posts }) => posts[0]));

src/app/core/utils/post-url.pipe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getPermalink } from '@blog/utils';
88
})
99
export class PostUrlPipe implements PipeTransform {
1010
transform(post: Post, content?: string): string {
11-
const postUrl = getPermalink(post.title, post.date, post.category);
11+
const postUrl = getPermalink(post.title, post.specialCategory, post.category, post.date);
1212

1313
if (content) {
1414
return `${postUrl}/${content}`;

src/app/features/search/search.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class SearchComponent implements OnInit {
6161
goToPost(event: MatAutocompleteSelectedEvent) {
6262
const post: Post = event.option.value;
6363
this.router.navigateByUrl(
64-
getPermalink(post.title, post.date, post.category)
64+
getPermalink(post.title, post.specialCategory, post.category, post.date)
6565
);
6666
this.control.setValue('');
6767
this.complete.emit();

tools/post-scaffolder/src/post-scaffolder/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function postScaffolder(options: any): Rule {
2323
...options,
2424
stringify,
2525
}),
26-
move(normalize(`content/posts/${getPermalink(options.title, 'unpublished')}`)),
26+
move(normalize(`content/posts/${getPermalink(options.title, false, '', undefined)}`)),
2727
]);
2828

2929
const rule = mergeWith(templateSource, MergeStrategy.Default);

tools/publish/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ async function moveUnpublishedDirectory(sourcePath, destinationRoot) {
4646
destinationRoot,
4747
utils.getPermalink(
4848
metaJsonObject.title,
49+
false,
50+
metaJsonObject.category,
4951
metaJsonObject.date,
50-
metaJsonObject.category
5152
)
5253
);
5354

0 commit comments

Comments
 (0)