Custom plugin for description and "minutes reading"

This commit is contained in:
DarkCat09 2023-06-18 20:00:53 +04:00
parent 82d3214187
commit 94f3de5337
5 changed files with 236 additions and 8 deletions

View file

@ -34,13 +34,11 @@ const { slug } = Astro.params;
const post: postType = Astro.props.post;
const postObj = await post.render();
const fm = postObj.remarkPluginFrontmatter;
const title = postObj.headings[0]?.text || '';
const description = post.body
.replace(/[^A-Za-z0-9А-ЯЁа-яё+-]/g, ' ')
.replace(/ {2,}/g, ' ')
.slice(0, 100)
.trim() + '\u2026'; // 2026 = ellipsis
const description = fm.description;
const readingTime = fm.readingTime;
let dateStr = slug.split("-", 1)[0];
let date: Date | undefined;
@ -64,7 +62,7 @@ const image = (post.body.match(/(?:\s|^)!\[.*\]\((.*?)\)/) || [])[1];
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={image} />
<meta property="article:published_time" content={date.toISOString()} />
<meta property="article:published_time" content={date?.toISOString()} />
<meta property="article:author" content="DarkCat09" />
<meta property="twitter:card" content="summary" />
<meta property="twitter:title" content={title} />
@ -74,8 +72,14 @@ const image = (post.body.match(/(?:\s|^)!\[.*\]\((.*?)\)/) || [])[1];
<header>
<h1>{title}</h1>
<address>
<time datetime={date.toISOString()}>{date.toLocaleDateString()}</time>,
on
<time datetime={date?.toISOString()}>{date?.toLocaleDateString()}</time>
by
<a href="https://t.me/dcat09" rel="author">DarkCat09</a>
//
<span id="reading-time">
{readingTime}
</span>
</address>
</header>
<article>