-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Open Graph and Twitter metadata to website
- Loading branch information
1 parent
be95d36
commit 141d0be
Showing
6 changed files
with
115 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { | ||
DocumentHeadProps, | ||
DocumentHeadValue, | ||
} from '@builder.io/qwik-city'; | ||
|
||
/** | ||
* Adds Open Graph and Twitter metadata to the document head. | ||
* | ||
* @param props The current head properties. | ||
* | ||
* @returns Open Graph and Twitter metadata. | ||
*/ | ||
export function addArticleMetadata({ | ||
url, | ||
head, | ||
}: DocumentHeadProps): DocumentHeadValue { | ||
// Get description from metadata | ||
const description = | ||
head.meta.find((item) => item.name === 'description')?.content || ''; | ||
|
||
// Create Open Graph image URL | ||
const imageUrl = `${url.origin}/og-image?title=${encodeURIComponent( | ||
head.title | ||
)}&description=${encodeURIComponent(description)}&path=${ | ||
url.pathname.split('/')[1] | ||
}`; | ||
|
||
// Return Open Graph and Twitter metadata | ||
return { | ||
meta: [ | ||
// Open Graph | ||
{ | ||
name: 'og:type', | ||
content: 'article', | ||
}, | ||
{ | ||
name: 'og:url', | ||
content: url.href, | ||
}, | ||
{ | ||
name: 'og:title', | ||
content: head.title, | ||
}, | ||
{ | ||
name: 'og:description', | ||
content: description, | ||
}, | ||
{ | ||
name: 'og:image', | ||
content: imageUrl, | ||
}, | ||
|
||
{ | ||
name: 'twitter:card', | ||
content: 'summary_large_image', | ||
}, | ||
{ | ||
name: 'twitter:title', | ||
content: head.title, | ||
}, | ||
{ | ||
name: 'twitter:description', | ||
content: description, | ||
}, | ||
{ | ||
name: 'twitter:image', | ||
content: imageUrl, | ||
}, | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './disableTransitions'; | ||
export * from './addArticleMetadata'; | ||
export * from './trackEvent'; |