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 meta tags #548

Closed
wants to merge 8 commits into from
Closed
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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
},
"dependencies": {
"@astrojs/check": "^0.2.0",
"@astrojs/sitemap": "^3.0.2",
"@astrojs/tailwind": "^5.0.0",
"@astrojs/sitemap": "^2.0.2",
"@astrojs/tailwind": "^4.0.0",
"@fontsource/lato": "^5.0.15",
"astro": "^3.2.2",
"astro": "^3.4.0",
"astro-icon": "^0.8.1",
"astro-navbar": "^2.2.1",
"astro-seo": "^0.8.0",
"dayjs": "^1.11.10",
"marked": "^9.0.3",
"remark": "^15.0.1",
"sass": "^1.68.0",
"strip-markdown": "^6.0.0",
"tailwindcss": "^3.0.24",
"tw-elements": "^1.0.0",
"typescript": "^5.2.2",
Expand Down
44 changes: 15 additions & 29 deletions src/components/articles/Article.astro
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
---
import { Image } from "astro:assets";
import dayjs from "dayjs";
import { marked } from "marked";
import { getEntry, type CollectionEntry } from "astro:content";
import { remark } from "remark";
import strip from "strip-markdown";

const { data, body, collection, slug } = Astro.props;

const author = (await getEntry(data.author)) as CollectionEntry<"authors">;

const dateObj = dayjs(data.date);
const dateString = dateObj.format("dddd, MMMM D, YYYY");
marked.use({
renderer: {
heading(text) {
return `<span>
${text}
</span>`;
},
paragraph(text) {
return `
<span>
${text}
</span>`;
},
link(href, title, text) {
return `
<span>
${text}
</span>`;
},
strong(text) {
return `
<span>
${text}
</span>`;
},
},
});
const content = marked.parse(body.split(" ").slice(0, 30).join(" ") + "...");

const content =
String(
await remark()
.use(strip, {
remove: ["heading"],
})
.process(body)
)
.split(" ")
.slice(0, 30)
.join(" ")
.replace("\\", "") + "...";
---

<a
Expand Down
43 changes: 14 additions & 29 deletions src/components/articles/FirstItem.astro
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
---
import { Image } from "astro:assets";
import dayjs from "dayjs";
import { marked } from "marked";
import { getEntry, type CollectionEntry } from "astro:content";
import { remark } from "remark";
import strip from "strip-markdown";

const { data, body, collection, slug } = Astro.props;

const author = (await getEntry(data.author)) as CollectionEntry<"authors">;

const dateObj = dayjs(data.date);
const dateString = dateObj.format("dddd, MMMM D, YYYY");
marked.use({
renderer: {
heading(text) {
return `<span>
${text}
</span>`;
},
paragraph(text) {
return `
<span>
${text}
</span>`;
},
link(href, title, text) {
return `
<span>
${text}
</span>`;
},
strong(text) {
return `
<span>
${text}
</span>`;
},
},
});
const content = marked.parse(body.split(" ").slice(0, 30).join(" ") + "...");
const content =
String(
await remark()
.use(strip, {
remove: ["heading"],
})
.process(body)
)
.split(" ")
.slice(0, 30)
.join(" ")
.replace("\\", "") + "...";
---

<div
Expand Down
16 changes: 4 additions & 12 deletions src/components/articles/RecentPosts.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import dayjs from "dayjs";
import { Image } from "astro:assets";
import { marked } from "marked";
import { remark } from "remark";
import strip from "strip-markdown";
import { parseDescription } from "@/lib/parseDescription";

const { posts, title } = Astro.props;

Expand Down Expand Up @@ -44,18 +47,7 @@ marked.use({
<h2 class="my-12 font-bold text-3xl">{title}</h2>
{
posts.map(async (post: any) => {
const { headings } = await post.render();

const h1 = headings[0]?.text;
const firstBlock = post.body.split("\n").slice(0, 6).join(" ");

const parsedDescription = marked.parse(
post.data.description ||
(h1?.length > 15 && h1) ||
(firstBlock.split(" ").length > 50
? firstBlock.split(" ").slice(0, 50).join(" ") + "..."
: firstBlock)
);
const parsedDescription = await parseDescription(post);
return (
<a
href={`/${post.collection}/${post.slug}`}
Expand Down
26 changes: 19 additions & 7 deletions src/components/head/BaseHead.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const {
description = "Trusted Firmware provides a reference implementation of secure software for Armv8-A, Armv9-A and Armv8-M. It provides SoC developers and OEMs with a reference trusted code base complying with the relevant Arm specifications.",
type = "website",
} = Astro.props;

const social_image =
"https://www.trustedfirmware.org/images/social-media-image.png";
---

<head>
Expand All @@ -28,28 +31,37 @@ const {
basic: {
title: title,
type: type,
image: "https://www.trustedfirmware.org/images/social-media-image.png",
image: social_image,
},
}}
,
twitter={{
title: title,
description: description.slice(0, 196) + "...",
card: "summary_large_image",
image: social_image,
creator: "@LinaroOrg",
}}
extend={{
link: [
{ rel: "icon", href: "/favicon.ico" },
{
rel: "me",
href: "https://mastodon.org.uk/@LinaroLtd@mastodon.social",
},
],
meta: [
{
name: "twitter:image",
content:
"https://www.trustedfirmware.org/images/social-media-image.png",
property: "twitter:image",
content: social_image,
},
{
property: "og:image",
content: social_image,
},
{
property: "og:description",
content: description,
},
{ name: "twitter:title", content: title },
{ name: "twitter:description", content: description },
],
}}
/>
Expand Down
3 changes: 3 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const news = defineCollection({
schema: ({ image }) =>
z.object({
author: reference("authors"),
description: z.string().optional(),
title: z.string(),
date: z.date(),
image: image(),
Expand All @@ -134,6 +135,7 @@ const blog = defineCollection({
schema: ({ image }) =>
z.object({
author: reference("authors"),
description: z.string().optional(),
title: z.string(),
date: z.date(),
image: image(),
Expand All @@ -145,6 +147,7 @@ const meetings = defineCollection({
schema: ({ image }) =>
z.object({
author: reference("authors"),
description: z.string().optional(),
title: z.string(),
date: z.date(),
image: image(),
Expand Down
22 changes: 17 additions & 5 deletions src/layouts/BlogLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import PostDetails from "@/components/articles/PostDetails.astro";
import RecentPosts from "@/components/articles/RecentPosts.astro";
import { getCollection } from "astro:content";
import dayjs from "dayjs";
import { remark } from "remark";
import strip from "strip-markdown";
import { parseDescription } from "@/lib/parseDescription";

const file = await remark()
.use(strip)
.process("Some *emphasis*, **importance**, and `code`.");

const { post } = Astro.props;
const { data, body } = post;
const { title, description, author, date } = data;
const { Content } = await post.render();
const { Content, headings } = await post.render();

const readTime = `${Math.ceil(body.split(" ").length / 183)} min read`;

Expand All @@ -20,9 +27,15 @@ const blogs = await getCollection("blog");
const latestPosts = [...news, ...blogs]
.sort((a, b) => dayjs(b.data.date).diff(a.data.date))
.slice(0, 5);

const parsedDescription = await parseDescription(post);
---

<BaseLayout title={title} description={description} type="article">
<BaseLayout
title={title}
description={description || parsedDescription}
type="article"
>
<main>
<Hero title="" background_image={BackgroundImage} />

Expand All @@ -32,10 +45,9 @@ const latestPosts = [...news, ...blogs]
class="px-5 md:px-16 prose:lg prose-p:mb-6 max-w-5xl mx-auto my-12 break-words
prose-headings:text-3xl prose-headings:font-bold prose-headings:my-8
prose-a:text-accent prose-a:no-underline hover:prose-a:underline
prose-li:list-item prose-ol:list-decimal prose-ul:list-disc
prose-li:list-item prose-ol:list-decimal prose-ul:list-disc
prose-li:prose-li:list-[circle] prose-ul:ml-8 prose-ol:ml-8 prose-ul:mb-4
prose-p:prose-li:mb-0
"
prose-p:prose-li:mb-0"
>
<Content />
</article>
Expand Down
29 changes: 29 additions & 0 deletions src/lib/parseDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { CollectionEntry } from "astro:content";
import { remark } from "remark";
import strip from "strip-markdown";

export const parseDescription = async (
post:
| CollectionEntry<"blog">
| CollectionEntry<"news">
| CollectionEntry<"meetings">
) => {
const { headings } = await post.render();

const h1 = headings[0]?.text;
const firstBlock = post.body.split("\n").slice(0, 6).join(" ");

return String(
await remark()
.use(strip, {
remove: h1?.length > 15 ? ["heading"] : [],
})
.process(
post.data.description ||
(h1?.length > 15 && h1) ||
(firstBlock.split(" ").length > 50
? firstBlock.split(" ").slice(0, 50).join(" ") + "..."
: firstBlock)
)
);
};
Loading
Loading