-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,208 additions
and
631 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
title: 'What is MemeBattle?' | ||
publishedAt: '2023-05-28' | ||
summary: "Post about MemeBattle. How it's started and what is MemeBattle today" | ||
image: '/content-images/what-is-memebattle/memeBattle-logo.svg' | ||
tags: ['MemeBattle'] | ||
--- | ||
|
||
## Started | ||
|
||
|
||
* [GitHub](https://github.com/MemeBattle) | ||
* [Linkedin](https://www.linkedin.com/company/memebattle/) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { BlogPost } from 'contentlayer/generated' | ||
import { allBlogPosts } from 'contentlayer/generated' | ||
import type { Language } from '../../../../i18n/i18n.settings' | ||
|
||
export { allBlogPosts } | ||
export type BlogPostWithTranslates = BlogPost & { translates: { [key in Language]?: BlogPost } } | ||
|
||
export const uniqTags = [ | ||
...allBlogPosts.reduce<Set<string>>((acc, { tags = [] }) => { | ||
tags.forEach(tag => { | ||
acc.add(tag) | ||
}) | ||
return acc | ||
}, new Set<string>()), | ||
] | ||
|
||
export const allBlogPostsWithTranslates: BlogPostWithTranslates[] = allBlogPosts.map((blogPost, index, blogPosts) => ({ | ||
...blogPost, | ||
translates: blogPosts | ||
.filter(({ slug }) => blogPost.slug === slug) | ||
.reduce((translatesAcc, blogPost) => ({ ...translatesAcc, [blogPost.lang]: blogPost }), {}), | ||
})) |
113 changes: 113 additions & 0 deletions
113
apps/blog/src/app/[locale]/posts/_utils/filterBlogPosts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { filterBlogPosts } from './filterBlogPosts' | ||
import type { BlogPostWithTranslates } from '../_content' | ||
|
||
const posts: BlogPostWithTranslates[] = [ | ||
{ | ||
title: 'Заголовок поста 1', | ||
publishedAt: '2023-05-28T00:00:00.000Z', | ||
summary: 'Первый пост в блоге', | ||
tags: ['тэг1'], | ||
image: '/content-images/what-is-memebattle/memeBattle-logo.svg', | ||
body: { | ||
raw: 'раз два три', | ||
code: '', | ||
}, | ||
_id: 'first.ru.mdx', | ||
_raw: { | ||
sourceFilePath: 'first.ru.mdx', | ||
sourceFileName: 'first.ru.mdx', | ||
sourceFileDir: '.', | ||
contentType: 'mdx', | ||
flattenedPath: 'first.ru', | ||
}, | ||
type: 'BlogPost', | ||
slug: 'first', | ||
toc: [ | ||
{ | ||
level: 'h1', | ||
text: 'text', | ||
}, | ||
], | ||
lang: 'ru', | ||
translates: {}, | ||
}, | ||
{ | ||
title: 'Заголовок поста 2', | ||
publishedAt: '2023-05-28T00:00:00.000Z', | ||
summary: 'Тезисное содержание поста 2', | ||
tags: [], | ||
image: '/content-images/what-is-memebattle/memeBattle-logo.svg', | ||
body: { | ||
raw: 'какой-то текст', | ||
code: '', | ||
}, | ||
_id: 'some-id.mdx', | ||
_raw: { | ||
sourceFilePath: 'some-id.mdx', | ||
sourceFileName: 'some-id.mdx', | ||
sourceFileDir: '.', | ||
contentType: 'mdx', | ||
flattenedPath: 'some-id', | ||
}, | ||
type: 'BlogPost', | ||
slug: 'some-id', | ||
toc: [ | ||
{ | ||
level: 'h1', | ||
text: 'text', | ||
}, | ||
], | ||
lang: 'ru', | ||
translates: {}, | ||
}, | ||
{ | ||
title: 'First post title', | ||
publishedAt: '2023-05-28T00:00:00.000Z', | ||
summary: 'First blog post', | ||
tags: ['tag1'], | ||
image: '/content-images/what-is-memebattle/memeBattle-logo.svg', | ||
body: { | ||
raw: 'one two three', | ||
code: '', | ||
}, | ||
_id: 'first.en.mdx', | ||
_raw: { | ||
sourceFilePath: 'first.en.mdx', | ||
sourceFileName: 'first.en.mdx', | ||
sourceFileDir: '.', | ||
contentType: 'mdx', | ||
flattenedPath: 'first.en', | ||
}, | ||
type: 'BlogPost', | ||
slug: 'first', | ||
toc: [ | ||
{ | ||
level: 'h1', | ||
text: 'text', | ||
}, | ||
], | ||
lang: 'en', | ||
translates: {}, | ||
}, | ||
] | ||
|
||
posts[0].translates = { en: posts[2] } | ||
posts[2].translates = { ru: posts[1] } | ||
|
||
describe('filterBlogPosts', () => { | ||
it('Should return all (uniq by locale) posts if search and tags empty', () => { | ||
expect(filterBlogPosts(posts, 'ru')).toEqual([posts[0], posts[1]]) | ||
}) | ||
|
||
it("Should return empty posts if posts doesn't contain words from search", () => { | ||
expect(filterBlogPosts(posts, 'ru', 'some query')).toEqual([]) | ||
}) | ||
|
||
it('Should return filtered post that contains words from search', () => { | ||
expect(filterBlogPosts(posts, 'ru', 'раз')).toEqual([posts[0]]) | ||
}) | ||
|
||
it('Should return filtered post that contains words from search', () => { | ||
expect(filterBlogPosts(posts, 'ru', 'раз')).toEqual([posts[0]]) | ||
}) | ||
}) |
24 changes: 24 additions & 0 deletions
24
apps/blog/src/app/[locale]/posts/_utils/filterBlogPosts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Language } from '../../../../i18n/i18n.settings' | ||
import { isPostShouldBePickedByLocale } from './isPostShouldBePickedByLocale' | ||
import type { BlogPostWithTranslates } from '../_content' | ||
|
||
export function filterBlogPosts(blogPosts: BlogPostWithTranslates[], locale: Language, search = '', tags: string[] = []): BlogPostWithTranslates[] { | ||
const keywords = search | ||
.toLowerCase() | ||
.split(' ') | ||
.filter(part => part !== '') | ||
|
||
const filteredByLocale = blogPosts.filter(blogPost => isPostShouldBePickedByLocale(blogPost, locale)) | ||
|
||
const filteredByTag = tags.length > 0 ? filteredByLocale.filter(blogPost => blogPost.tags?.some(tag => tags.includes(tag))) : filteredByLocale | ||
|
||
if (keywords.length === 0) { | ||
return filteredByTag | ||
} | ||
|
||
return filteredByTag.filter(blogPost => { | ||
const words = (blogPost.title + ' ' + blogPost.summary + ' ' + blogPost.body.raw).toLowerCase().split(' ') | ||
|
||
return keywords.every(keyWord => words.some(word => word.startsWith(keyWord))) | ||
}) | ||
} |
15 changes: 15 additions & 0 deletions
15
apps/blog/src/app/[locale]/posts/_utils/isPostShouldBePickedByLocale.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isPostShouldBePickedByLocale } from './isPostShouldBePickedByLocale' | ||
|
||
describe('isPostShouldBePickedByLocale', () => { | ||
it('Should return true if post in selected locale', () => { | ||
expect(isPostShouldBePickedByLocale({ slug: 'first', lang: 'ru', translates: {} }, 'ru')).toEqual(true) | ||
}) | ||
|
||
it('Should return false if post not in selected locale', () => { | ||
expect(isPostShouldBePickedByLocale({ slug: 'first', lang: 'ru', translates: {} }, 'en')).toEqual(false) | ||
}) | ||
|
||
it('Should return true if post in fallback locale and not exist in current', () => { | ||
expect(isPostShouldBePickedByLocale({ slug: 'first', lang: 'en', translates: {} }, 'ru')).toEqual(true) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
apps/blog/src/app/[locale]/posts/_utils/isPostShouldBePickedByLocale.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { BlogPostWithTranslates } from '../_content' | ||
import type { Language } from '../../../../i18n/i18n.settings' | ||
import { fallbackLanguage } from '../../../../i18n/i18n.settings' | ||
|
||
export const isPostShouldBePickedByLocale = (post: Pick<BlogPostWithTranslates, 'slug' | 'lang' | 'translates'>, locale: Language): boolean => { | ||
/** | ||
* Post in current locale should be picked | ||
*/ | ||
if (post.lang === locale) { | ||
return true | ||
} | ||
|
||
/** | ||
* Post in fallback locale should be picked if it doesn't exist in current | ||
*/ | ||
return post.lang === fallbackLanguage && !post.translates[locale] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
600b5c5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for
apps/ligretto-gameplay-backend
Test suite run success
12 tests passing in 1 suite.
Report generated by 🧪jest coverage report action from 600b5c5