feat: render articles list & tags

This commit is contained in:
Sergey Yarkov 2022-07-10 23:17:50 +03:00
parent d8de43582f
commit 3102bcfaed
14 changed files with 142 additions and 79 deletions

View File

@ -1,4 +1,4 @@
.article-item {
.ArticleItem {
border-radius: var(--border-radius-md);
text-decoration: none;
& a {

View File

@ -1,4 +1,4 @@
import './ArticleItem.scss';
import styles from './ArticleItem.module.scss';
import { Component } from 'solid-js';
import { IArticle } from '@root/interfaces';
import { DateFormatter } from '@utils/date';
@ -8,7 +8,7 @@ export type ArticleItemProps = {
};
export const ArticleItem: Component<ArticleItemProps> = ({ data }) => (
<article class='article-item'>
<article class={styles.ArticleItem}>
<a href='#'>
<h3>{data.title}</h3>
<p>

View File

@ -32,7 +32,8 @@
list-style: none;
margin: 0;
padding: 0;
& li a {
& li {
cursor: pointer;
padding: 3px 10px;
border-radius: var(--border-radius-md);
border: 1px solid var(--border-color);

View File

@ -1,16 +1,27 @@
import './ArticlesList.scss';
import { Component } from 'solid-js';
import { Component, createSignal } from 'solid-js';
import { PageHeading } from '@components/PageHeading';
import { IArticle } from '@root/interfaces';
import * as utils from '@root/utils/articles';
import { ArticleItem } from '../ArticleItem';
export type ArticlesListProps = {
data: IArticle[];
tags: string[];
};
export const ArticlesList: Component<ArticlesListProps> = ({ data, tags }) => {
const [articles, setArticles] = createSignal(
utils.filterArticlesByYear(data)
);
export const ArticlesList: Component = () => {
return (
<section class='articles container'>
<header>
<h1 class='page-heading'>Статьи</h1>
<p class='page-description'>
Технические статьи, туториалы и различные справочные материалы которые
связаны с IT тематикой находятся здесь.
</p>
</header>
<PageHeading
heading='Статьи'
description='Технические статьи, туториалы и различные справочные материалы которые
связаны с IT тематикой находятся здесь.'
/>
<div class='articles-search'>
<svg
width='25'
@ -20,59 +31,29 @@ export const ArticlesList: Component = () => {
<path d='M10.6026 3.60001C11.8798 3.59993 13.1316 3.94283 14.2172 4.5901C15.3028 5.23736 16.179 6.16331 16.7472 7.26368C17.3154 8.36405 17.5531 9.59517 17.4334 10.8184C17.3137 12.0417 16.8415 13.2085 16.0699 14.1876L21.2427 19.164C21.4085 19.3248 21.5053 19.5399 21.5135 19.7662C21.5218 19.9926 21.4411 20.2137 21.2874 20.3853C21.1337 20.5569 20.9184 20.6665 20.6845 20.6921C20.4505 20.7178 20.2151 20.6576 20.0253 20.5236L19.9205 20.436L14.7476 15.4596C13.8802 16.0922 12.8687 16.5172 11.7968 16.6994C10.7248 16.8815 9.62324 16.8157 8.58298 16.5074C7.54272 16.199 6.59366 15.6569 5.81422 14.9259C5.03478 14.1949 4.44733 13.296 4.10042 12.3034C3.7535 11.3108 3.65708 10.2531 3.81911 9.21757C3.98114 8.18204 4.39698 7.19847 5.03226 6.34812C5.66755 5.49778 6.50404 4.80506 7.47263 4.32721C8.44122 3.84936 9.51411 3.60009 10.6026 3.60001V3.60001ZM10.6026 5.40001C9.27934 5.40001 8.01025 5.90572 7.07454 6.80589C6.13884 7.70607 5.61316 8.92697 5.61316 10.2C5.61316 11.473 6.13884 12.6939 7.07454 13.5941C8.01025 14.4943 9.27934 15 10.6026 15C11.9259 15 13.195 14.4943 14.1307 13.5941C15.0664 12.6939 15.5921 11.473 15.5921 10.2C15.5921 8.92697 15.0664 7.70607 14.1307 6.80589C13.195 5.90572 11.9259 5.40001 10.6026 5.40001Z'></path>
</svg>
<input type='text' placeholder='поиск по названию или описанию...' />
<span>3</span>
<span>{data.length}</span>
</div>
<div class='articles-tags'>
<nav>
<ul>
<li>
<a href='#'>Docker</a>
</li>
<li>
<a href='#'>JavaScript</a>
</li>
<li>
<a href='#'>AVR</a>
</li>
{tags.map((tag) => (
<li data-tag={tag.toLocaleLowerCase()}>{tag}</li>
))}
</ul>
</nav>
</div>
<div class='articles-list'>
<div class='articles-list__wrapper'>
<h2>2022</h2>
<article class='article-item'>
<a href='#'>
<h3>Развертывание вашего Node.js приложение с помощью Docker</h3>
<p>
<time datetime='2022-01-01 22:00'>ЯНВ 15, 2022</time>
</p>
</a>
</article>
<article class='article-item'>
<a href='#'>
<h3>
Создание блог-сайта с помощью статического генератора GatsbyJS
</h3>
<p>
<time datetime='2022-01-01 22:00'>ЯНВ 05, 2022</time>
</p>
</a>
</article>
</div>
<div class='articles-list__wrapper'>
<h2>2021</h2>
<article class='article-item'>
<a href='#'>
<h3>
Как взаимодействовать с микроконтроллером испульзуюя
WebSerialAPI
</h3>
<p>
<time datetime='2021-01-01 22:00'>ФЕВ 15, 2021</time>
</p>
</a>
</article>
</div>
{Object.keys(articles())
.map(Number)
.sort((a, b) => b - a)
.map((year) => (
<div class='articles-list__wrapper'>
<h2>{year}</h2>
{utils.sortArtcilesByDate(articles()[year]).map((article) => (
<ArticleItem data={article} />
))}
</div>
))}
</div>
</section>
);

View File

View File

@ -0,0 +1,10 @@
import { Component, JSX } from 'solid-js';
export type PageContentComposition = {
Heading?: Component;
};
export const PageContent: Component<{ children: JSX.Element }> &
PageContentComposition = (props) => {
return <section>{props.children}</section>;
};

View File

@ -0,0 +1,13 @@
.PageHeading {
font-size: var(--font-size-4xl);
text-align: center;
margin-bottom: 15px;
}
.PageDescription {
font-size: var(--font-size-lg);
text-align: center;
max-width: 650px;
margin: 0 auto;
margin-bottom: 2.5rem;
}

View File

@ -0,0 +1,23 @@
import { Component } from 'solid-js';
import styles from './PageHeading.module.scss';
export type PageHeadingProps = {
heading: string;
description?: string | undefined;
};
export const PageHeading: Component<PageHeadingProps> = ({
heading,
description,
}) => {
return (
<header>
<h1
class={styles.PageHeading}
style={!description && 'margin-bottom: 2.5rem;'}>
{heading}
</h1>
{description && <p class={styles.PageDescription}>{description}</p>}
</header>
);
};

View File

@ -0,0 +1 @@
export * from './PageHeading';

View File

@ -1,8 +1,20 @@
---
import Page from '@layouts/Page.astro';
import { ArticlesList } from '@components/ArticlesList';
import { IArticle } from '@root/interfaces';
import * as utils from '@utils/index';
const articles = await Astro.glob<IArticle>('../../content/articles/**/*.md');
const tags = new Set(
articles.map((article) => article.frontmatter.tags).flat()
);
console.log(tags);
---
<Page title='Статьи | Sergey Yarkov'>
<ArticlesList />
<ArticlesList
client:load
data={utils.transformArticles(articles)}
tags={[...tags]}
/>
</Page>

View File

@ -2,12 +2,10 @@
import Page from '@layouts/Page.astro';
import { IArticle } from '@root/interfaces';
import { ArticleItem } from '@components/ArticleItem';
import * as utils from '@utils/index';
const data = await Astro.glob<IArticle>('../../content/articles/**/*.md');
const recent = data.sort(
(a, b) =>
b.frontmatter.published_at.valueOf() - a.frontmatter.published_at.valueOf()
);
const recent = utils.sortArtcilesByDate(utils.transformArticles(data));
---
<Page title='Sergey Yarkov'>
@ -60,13 +58,13 @@ const recent = data.sort(
</div>
</div>
<div class='publications-list container'>
{recent.map((article) => <ArticleItem data={article.frontmatter} />)}
{recent.slice(0, 5).map((article) => <ArticleItem data={article} />)}
</div>
</section>
<style lang='scss'>
.welcome {
margin-top: 80px;
margin-top: 40px;
margin-bottom: 150px;
&-heading {
&__title {

View File

@ -124,20 +124,6 @@ body {
padding: 0 var(--content-padding);
}
.page-heading {
font-size: var(--font-size-4xl);
text-align: center;
margin-bottom: 15px;
}
.page-description {
font-size: var(--font-size-lg);
text-align: center;
max-width: 650px;
margin: 0 auto;
margin-bottom: 2.5rem;
}
a {
color: var(--color-blue-500);
font-weight: 500;

36
src/utils/articles.ts Normal file
View File

@ -0,0 +1,36 @@
import { IArticle } from '@root/interfaces';
import { MarkdownInstance } from 'astro';
export const transformArticles = (
articles: MarkdownInstance<IArticle>[]
): IArticle[] => {
return articles.map((article) => ({
title: article.frontmatter.title,
description: article.frontmatter.description,
tags: article.frontmatter.tags,
published_at: article.frontmatter.published_at,
}));
};
export const sortArtcilesByDate = (articles: IArticle[]): IArticle[] => {
return articles.sort(
(a, b) =>
new Date(b.published_at).valueOf() - new Date(a.published_at).valueOf()
);
};
export const filterArticlesByYear = (
articles: IArticle[]
): { [year: number]: IArticle[] } => {
const filtered = {};
for (const article of articles) {
const year = new Date(article.published_at).getFullYear();
if (!filtered[year]) filtered[year] = [];
filtered[year].push(article);
}
return filtered;
};

2
src/utils/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './articles';
export * from './date';