Skip to content

Commit

Permalink
add function to prep meta description text from description field
Browse files Browse the repository at this point in the history
  • Loading branch information
JKarlavige committed Dec 1, 2023
1 parent 4547e4b commit de9f3c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
37 changes: 35 additions & 2 deletions website/src/components/communitySpotlightCard/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import Link from '@docusaurus/Link';
import Head from "@docusaurus/Head";
import styles from './styles.module.css';
import imageCacheWrapper from '../../../functions/image-cache-wrapper';

Expand Down Expand Up @@ -51,11 +52,26 @@ function CommunitySpotlightCard({ frontMatter, isSpotlightMember = false }) {
communityAward
} = frontMatter

// Get meta description text
const metaDescription = stripHtml(description)

return (
<SpotlightWrapper
isSpotlightMember={isSpotlightMember}
frontMatter={frontMatter}
>
{isSpotlightMember && metaDescription ? (
<Head>
<meta
name="description"
content={metaDescription}
/>
<meta
property="og:description"
content={metaDescription}
/>
</Head>
) : null}
{communityAward ? (
<div className={styles.awardBadge}>
<span>Community Award Recipient</span>
Expand Down Expand Up @@ -162,12 +178,12 @@ function CommunitySpotlightCard({ frontMatter, isSpotlightMember = false }) {
);
}

// Truncate text
// Truncate description text for community member cards
function truncateText(str) {
// Max length of string
let maxLength = 300

// Check if anchor link starts within first 300 characters
// Check if anchor link starts within maxLength
let hasLinks = false
if(str.substring(0, maxLength - 3).match(/(?:<a)/g)) {
hasLinks = true
Expand All @@ -189,4 +205,21 @@ function truncateText(str) {
: str
}

// Strip HTML for meta description
function stripHtml(desc) {
const maxLength = 130

if(!desc) return null

// Remove HTML elements from string
const strippedHtml = desc?.replace(/(<([^>]+)>)/gi, "")

// Strip new lines and return 130 character substring for description
const updatedDesc = strippedHtml
?.substring(0, maxLength)
?.replace(/(\r\n|\r|\n)/g, "");

return desc?.length > maxLength ? `${updatedDesc}...` : updatedDesc
}

export default CommunitySpotlightCard
1 change: 1 addition & 0 deletions website/src/components/communitySpotlightList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function CommunitySpotlightList({ spotlightData }) {
<Head>
<title>{metaTitle}</title>
<meta property="og:title" content={metaTitle} />
<meta property="description" content={communityDescription} />
<meta property="og:description" content={communityDescription} />
</Head>
<Hero
Expand Down

0 comments on commit de9f3c1

Please sign in to comment.