Skip to content

Commit

Permalink
feat: truncate description in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdibha committed Jan 25, 2024
1 parent a32ca1d commit 934fbb9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Metadata, Viewport } from "next";
import { Analytics } from "@vercel/analytics/react";
import { Toaster } from "@/components/ui/sonner";
import { cn } from "@/utils/classes";
import { truncateOnWord } from "@/utils/text";
import { inter } from "@/styles/fonts";
import "@/styles/globals.css";
import { siteConfig } from "@/config";
Expand All @@ -12,7 +13,7 @@ const config = siteConfig.global;

export const metadata: Metadata = {
title: { default: config.title, template: `${config.name} - %s` },
description: config.description,
description: truncateOnWord(config.description, 148, true),
keywords: config.keywords,
authors: config.authors,
creator: config.creator,
Expand All @@ -27,14 +28,14 @@ export const metadata: Metadata = {
locale: "en_US",
url: config.url,
title: config.title,
description: config.description,
description: truncateOnWord(config.description, 148, true),
siteName: config.name,
images: [config.thumbnail],
},
twitter: {
card: "summary_large_image",
title: config.title,
description: config.description,
description: truncateOnWord(config.description,148,true),
images: [config.thumbnail],
creator: config.twitter.creator,
},
Expand Down
4 changes: 2 additions & 2 deletions src/config/site-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export const siteConfig = {
name: "Vapi",
logo: "/images/logo.png",
title: "Vapi - Première marketplace de vente de cigarettes électroniques en Tunisie.",
description:
description:
"Vapi, site de cigarette électronique, vous donne accès à la communeauté Vape In Tunisia. Vous avez également la possibilité de faire l'achat de vos cigarettes électroniques en ligne.",
keywords: [],
keywords: [""],
authors: [
{
name: "mehdibha",
Expand Down
14 changes: 14 additions & 0 deletions src/utils/text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const LINE_BREAK_LENGTH = 50;

export const truncateOnWord = (text: string, maxLength: number, ellipsis = true) => {
if (text.length <= maxLength) return text;

// First split on maxLength chars
let truncatedText = text.substring(0, maxLength);

// Then split on the last space, this way we split on the last word,
// which looks just a bit nicer.
truncatedText = truncatedText.substring(0, Math.min(truncatedText.length, truncatedText.lastIndexOf(" ")));

if (ellipsis) truncatedText += "...";

return truncatedText;
};
// TODO Fix this shit
export const truncate = (text: string, maxLength: number) => {
const lines = text.split("\n");
Expand Down

0 comments on commit 934fbb9

Please sign in to comment.