Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/layouts/Base.astro
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ const {
<meta property="og:description" content={description} />
<meta property="og:image" content={thumbnail.src} />
<meta property="og:url" content={Astro.request.url} />
<meta property="og:site_name" content={SITE_TITLE} />

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta name="twitter:site" content="@VocaDB" />
<meta name="twitter:creator" content="@VocaDB" />
<ClientRouter />
<script is:inline>
const setTheme = () => {
Expand Down
17 changes: 16 additions & 1 deletion src/pages/docs/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { getCollection, type CollectionEntry } from "astro:content";
import DocPageLayout from "../../layouts/DocPageLayout.astro";
import { getAutoDescription } from "@/utils/getAutoDescription.ts";

export async function getStaticPaths() {
const blogEntries: CollectionEntry<"docs">[] = await getCollection("docs");
Expand All @@ -13,11 +14,25 @@ export async function getStaticPaths() {
}

const { entry } = Astro.props;

let description = await getAutoDescription(entry.data.description || "");

if (
!description ||
entry.data.title === description ||
entry.data.title === entry.data.description
) {
description = (await getAutoDescription(entry.body, 300)) || description;
}

const { Content, headings } = await entry.render();
---

<DocPageLayout
frontmatter={entry.data}
frontmatter={{
...entry.data,
description,
}}
headings={headings}
filePath={"src/content/docs/" + entry.id}
pagePath={"docs/" + entry.slug}
Expand Down
19 changes: 3 additions & 16 deletions src/pages/rules/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import RuleEmbed from "@/components/markdown/RuleEmbed.astro";
import { FormattedField } from "@/components/markdown/RuleFormattedField";
import { getCollection, type CollectionEntry } from "astro:content";
import DocPageLayout from "../../layouts/DocPageLayout.astro";
import { unified } from "unified";
import remarkParse from "remark-parse";
import stripMarkdown from "strip-markdown";
import remarkStringify from "remark-stringify";
import {
ruleDataKeyDetailsDictionary,
ruleDataKeyDictionary,
type RuleFields,
} from "@/content/config";
import { getAutoDescription } from "@/utils/getAutoDescription.ts";

export async function getStaticPaths() {
const ruleEntries: CollectionEntry<"rules">[] = await getCollection("rules");
Expand All @@ -34,28 +31,18 @@ const { headings } = await entry.render();

headings.push({ depth: 2, slug: "metadata", text: "Metadata" });

async function stripText(markdownInput: string) {
const file = await unified()
.use(remarkParse)
.use(stripMarkdown)
.use(remarkStringify)
.process(markdownInput);

return String(file);
}

let content;
if (entry.data.excerpt) {
content = await entry.render();
}
const description = await stripText(entry.data.excerpt || entry.body);
const description = await getAutoDescription(entry.data.excerpt || entry.body);
---

<DocPageLayout
frontmatter={{
...entry.data,
title: `Rule: ${entry.data.name}`,
description: description,
description,
}}
headings={headings}
filePath={"src/content/rules/" + entry.id}
Expand Down
65 changes: 65 additions & 0 deletions src/utils/getAutoDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkMdx from "remark-mdx";
import stripMarkdown from "strip-markdown";
import remarkStringify from "remark-stringify";
import { visit } from "unist-util-visit";

function removeMdx() {
return (tree: any) => {
tree.children = tree.children.filter(
(node: any) => node.type !== "mdxjsEsm",
);

visit(
tree,
["mdxJsxFlowElement", "mdxJsxTextElement"],
(node, index, parent) => {
const children = node.children || [];

if (parent && index !== null) {
parent.children.splice(index, 1, ...children);
}

return index;
},
);
};
}

function filterLeadSection() {
return (tree: any) => {
const firstHeadingIndex = tree.children.findIndex(
(node: any) => node.type === "heading",
);

if (firstHeadingIndex !== -1) {
tree.children = tree.children.slice(0, firstHeadingIndex);
}
};
}

export async function getAutoDescription(
markdownInput: string,
limit: number = 0,
) {
const file = await unified()
.use(remarkParse)
.use(remarkMdx)
.use(removeMdx)
.use(filterLeadSection)
.use(stripMarkdown)
.use(remarkStringify)
.process(markdownInput);

let text = String(file).trim();

if (limit && text.length > limit) {
const truncated = text.slice(0, limit);
const lastSpace = truncated.lastIndexOf(" ");

text = (lastSpace > 0 ? truncated.slice(0, lastSpace) : truncated) + "...";
}

return text;
}