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

Fix: Add correct types to news #13

Merged
merged 4 commits into from
Jul 12, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@angular/ssr": "^17.3.0",
"@jsverse/transloco": "7.4.2",
"express": "^4.18.2",
"marked": "^13.0.2",
"ngx-transloco-markup": "^6.0.2",
"ngx-transloco-markup-router-link": "^4.0.0",
"rxjs": "~7.8.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ <h2 class="mb-6 text-3xl font-extrabold tracking-tight text-gray-900 dark:text-w
@for (newsItem of news(); track newsItem.title) {
<div class="mb-6">
<h3 class="mb-3 text-2xl text-gray-900 dark:text-white">
<span class="text-gray-500 dark:text-gray-200">{{newsItem.date_published}}</span> - <span [innerHTML]="newsItem.title"></span>
<span class="text-gray-500 dark:text-gray-200">{{newsItem.datePublished}}</span> - <span [innerHTML]="newsItem.title"></span>
</h3>
@if (newsItem.excerpt) {
<p class="mb-2 font-light lg:text-xl" [innerHTML]="newsItem.excerpt"></p>
<p class="mb-2 font-light lg:text-xl" [innerHTML]="newsItem.excerpt | markdown | stripWrapperTag"></p>
}
@if (newsItem.moreLink) {
<p class="mb-2 font-light lg:text-xl">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {toPromise} from "../../../../types/resolvable";
import {RouterLink} from "@angular/router";
import {TranslocoMarkupComponent} from "ngx-transloco-markup";
import {TranslocoPipe} from "@jsverse/transloco";
import {MarkdownPipe} from "../../../../pipes/markdown.pipe";
import {StripWrapperTagPipe} from "../../../../pipes/strip-wrapper-tag.pipe";


@Component({
Expand All @@ -14,6 +16,8 @@ import {TranslocoPipe} from "@jsverse/transloco";
RouterLink,
TranslocoMarkupComponent,
TranslocoPipe,
MarkdownPipe,
StripWrapperTagPipe,
],
templateUrl: './homepage-latest-news.component.html',
styleUrl: './homepage-latest-news.component.scss'
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/news/news.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ <h1 class="mb-4 text-4xl font-extrabold leading-none tracking-tight md:text-5xl
@for (newsItem of news(); track newsItem.title) {
<div class="mb-6">
<h3 class="mb-3 text-2xl text-gray-900 dark:text-white">
<span class="text-gray-500 dark:text-gray-200">{{newsItem.date_published}}</span> - <span [innerHTML]="newsItem.title"></span>
<span class="text-gray-500 dark:text-gray-200">{{newsItem.datePublished}}</span> - <span [innerHTML]="newsItem.title"></span>
</h3>
@if (newsItem.excerpt) {
<p class="mb-2 font-light lg:text-xl" [innerHTML]="newsItem.excerpt"></p>
<p class="mb-2 font-light lg:text-xl" [innerHTML]="newsItem.excerpt | markdown | stripWrapperTag"></p>
}
@if (newsItem.moreLink) {
<p class="mb-2 font-light lg:text-xl">
Expand Down
6 changes: 5 additions & 1 deletion src/app/pages/news/news.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {toPromise} from "../../types/resolvable";
import {RouterLink} from "@angular/router";
import {TranslocoMarkupComponent} from "ngx-transloco-markup";
import {TranslocoPipe} from "@jsverse/transloco";
import {MarkdownPipe} from "../../pipes/markdown.pipe";
import {StripWrapperTagPipe} from "../../pipes/strip-wrapper-tag.pipe";


@Component({
Expand All @@ -14,6 +16,8 @@ import {TranslocoPipe} from "@jsverse/transloco";
RouterLink,
TranslocoMarkupComponent,
TranslocoPipe,
MarkdownPipe,
StripWrapperTagPipe,
],
templateUrl: './news.component.html',
styleUrl: './news.component.scss'
Expand All @@ -29,4 +33,4 @@ export class NewsComponent implements OnInit {
public async ngOnInit(): Promise<void> {
this.news.set(await toPromise(this.aiHorde.getNews()));
}
}
}
14 changes: 14 additions & 0 deletions src/app/pipes/markdown.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';
import {marked} from "marked";

@Pipe({
name: 'markdown',
standalone: true
})
export class MarkdownPipe implements PipeTransform {

transform(value: string): string {
return <string>marked(value);
}

}
30 changes: 30 additions & 0 deletions src/app/pipes/strip-wrapper-tag.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'stripWrapperTag',
standalone: true
})
export class StripWrapperTagPipe implements PipeTransform {

transform(value: string): string {
if (!value.startsWith('<')) {
return value;
}

const regex = /^<.+?>/;
if (!regex.test(value)) {
return value;
}

const match = regex.exec(value);
if (match === null) {
return value;
}

const tag = match[0];
const closing = `</${tag.substring(1)}`;

return value.substring(tag.length, value.length - closing.length - 1);
}

}
13 changes: 6 additions & 7 deletions src/app/services/ai-horde.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {TextTotalStats} from "../types/text-total-stats";
import {NewsItem} from "../types/news.types";
import {SingleInterrogationStatPoint} from "../types/single-interrogation-stat-point";
import {HtmlHordeDocument} from "../types/horde-document";
import {HordeNewsItem} from "../types/horde-news-item";
import {HordeUser} from "../types/horde-user";

@Injectable({
Expand Down Expand Up @@ -63,18 +64,16 @@ export class AiHordeService {
}

public getNews(count?: number): Observable<NewsItem[]> {
return this.httpClient.get<any[]>('https://aihorde.net/api/v2/status/news').pipe(
return this.httpClient.get<HordeNewsItem[]>('https://aihorde.net/api/v2/status/news').pipe(
map(newsItems => count ? newsItems.slice(0, count) : newsItems),
map(newsItems => newsItems.map(newsItem => {
const markdownLinkRegex = /\[([^\[]+)\]\(([^\)]+)\)/g;
const excerpt = newsItem.newspiece.replace(markdownLinkRegex, '<a href="$2">$1</a>');
return {
title: newsItem.title,
date_published: newsItem.date_published,
excerpt: excerpt,
moreLink: newsItem.more_info_urls.length > 0 ? newsItem.more_info_urls[0] : null
datePublished: newsItem.date_published,
excerpt: newsItem.newspiece,
moreLink: newsItem.more_info_urls.length > 0 ? newsItem.more_info_urls[0] : null,
};
}))
})),
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/app/types/horde-news-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface HordeNewsItem {
date_published: string;
newspiece: string;
importance: string;
tags: string[];
title: string;
more_info_urls: string[];
}
2 changes: 1 addition & 1 deletion src/app/types/news.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface NewsItem {
title: string;
date_published: string;
datePublished: string;
excerpt: string | null;
moreLink: string | null;
}
2 changes: 1 addition & 1 deletion src/app/types/resolvable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {lastValueFrom, Observable} from "rxjs";
import {from, lastValueFrom, Observable, of} from "rxjs";

export type Resolvable<T> = Promise<T> | Observable<T> | T;

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4497,6 +4497,11 @@ make-fetch-happen@^13.0.0, make-fetch-happen@^13.0.1:
promise-retry "^2.0.1"
ssri "^10.0.0"

marked@^13.0.2:
version "13.0.2"
resolved "https://registry.yarnpkg.com/marked/-/marked-13.0.2.tgz#d5d05bd2683a85cb9cc6afbe5240e3a8bffcb92a"
integrity sha512-J6CPjP8pS5sgrRqxVRvkCIkZ6MFdRIjDkwUwgJ9nL2fbmM6qGQeB2C16hi8Cc9BOzj6xXzy0jyi0iPIfnMHYzA==

media-typer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
Expand Down
Loading