11import { getFile , putFile , listFiles } from './github' ;
2- import { parseFrontmatter , stringifyFrontmatter , generateId , transliterate } from './utils' ;
2+ import { parseFrontmatter , stringifyFrontmatter , generateId , transliterate , normalizeText } from './utils' ;
33
44export interface Forum {
55 slug : string ;
@@ -28,6 +28,22 @@ export interface Post {
2828 body : string ;
2929}
3030
31+ function matchesSearch ( item : { author : string ; body : string } , options ?: { author ?: string ; text ?: string } ) : boolean {
32+ if ( ! options ) return true ;
33+ const { author : authorQuery , text : textQuery } = options ;
34+ if ( authorQuery ) {
35+ const normalizedAuthor = normalizeText ( item . author ) ;
36+ const normalizedQuery = normalizeText ( authorQuery ) ;
37+ if ( ! normalizedAuthor . includes ( normalizedQuery ) ) return false ;
38+ }
39+ if ( textQuery ) {
40+ const normalizedBody = normalizeText ( item . body ) ;
41+ const normalizedQuery = normalizeText ( textQuery ) ;
42+ if ( ! normalizedBody . includes ( normalizedQuery ) ) return false ;
43+ }
44+ return true ;
45+ }
46+
3147export async function listForums ( ) : Promise < Forum [ ] > {
3248 const files = await listFiles ( 'forums' ) ;
3349 const forums : Forum [ ] = [ ] ;
@@ -66,7 +82,7 @@ export async function createForum(data: Omit<Forum, 'slug'> & {slug: string}): P
6682 } ;
6783}
6884
69- export async function listTopics ( forumSlug : string ) : Promise < Topic [ ] > {
85+ export async function listTopics ( forumSlug : string , options ?: { author ?: string ; text ?: string } ) : Promise < Topic [ ] > {
7086 const files = await listFiles ( 'topics' ) ;
7187 const topics : Topic [ ] = [ ] ;
7288
@@ -76,11 +92,14 @@ export async function listTopics(forumSlug: string): Promise<Topic[]> {
7692 if ( fileData ) {
7793 const { data, content } = parseFrontmatter < Topic > ( fileData . content ) ;
7894 if ( data . forumSlug === forumSlug ) {
79- topics . push ( {
95+ const topic = {
8096 ...data ,
8197 id : file . name . replace ( '.md' , '' ) ,
8298 body : content
83- } ) ;
99+ } ;
100+ if ( matchesSearch ( topic , options ) ) {
101+ topics . push ( topic ) ;
102+ }
84103 }
85104 }
86105 }
@@ -123,7 +142,7 @@ export async function createTopic(data: Omit<Topic, 'id' | 'createdAt' | 'titleT
123142 } ;
124143}
125144
126- export async function listPosts ( topicId : string ) : Promise < Post [ ] > {
145+ export async function listPosts ( topicId : string , options ?: { author ?: string ; text ?: string } ) : Promise < Post [ ] > {
127146 const files = await listFiles ( 'posts' ) ;
128147 const posts : Post [ ] = [ ] ;
129148
@@ -133,11 +152,14 @@ export async function listPosts(topicId: string): Promise<Post[]> {
133152 if ( fileData ) {
134153 const { data, content } = parseFrontmatter < Post > ( fileData . content ) ;
135154 if ( data . topicId === topicId ) {
136- posts . push ( {
155+ const post = {
137156 ...data ,
138157 id : file . name . replace ( '.md' , '' ) ,
139158 body : content
140- } ) ;
159+ } ;
160+ if ( matchesSearch ( post , options ) ) {
161+ posts . push ( post ) ;
162+ }
141163 }
142164 }
143165 }
0 commit comments