diff --git a/CHANGELOG.md b/CHANGELOG.md index 821aa1490..3ab5746ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [1.125.0] - Not released +### Added +- Add OpenGraphPreview component and integrate into LinkPreview + - Add OpenGraphPreview component to fetch and parse Open Graph metadata from pages + - Integrated OpenGraphPreview into LinkPreview to show open graph preview if no other specific preview available for a URL + - Created thoughtful styling for the OpenGraphPreview for a clean, polished, and readable design + - OpenGraphPreview provides a fallback preview option for sites that implement Open Graph metadata + - Parsing logic simplified using DOMParser and meta tag selectors + +### Changed +- Update LinkPreview component to check for Open Graph data and show OpenGraphPreview ## [1.124.2] - 2023-10-25 ### Fixed diff --git a/src/components/link-preview/open-graph.jsx b/src/components/link-preview/open-graph.jsx new file mode 100644 index 000000000..1d9f22dd0 --- /dev/null +++ b/src/components/link-preview/open-graph.jsx @@ -0,0 +1,42 @@ +import { useEffect, useState } from 'react'; + +export default function OpenGraphPreview({ url }) { + const [data, setData] = useState(null); + + useEffect(() => { + if (url.startsWith(window.location.origin)) { + return; + } + async function fetchData() { + const response = await fetch(`https://corsproxy.io/?${encodeURIComponent(url)}`); + const html = await response.text(); + const doc = new DOMParser().parseFromString(html, 'text/html'); + + const metaContent = (property) => + doc.querySelector(`meta[property="${property}"]`)?.getAttribute('content'); + + const [title, description, image, source] = [ + 'og:title', + 'og:description', + 'og:image', + 'og:site_name', + ].map(metaContent); + setData({ title, description, image, source }); + } + fetchData(); + }, [url]); + if (!data || data.title === undefined) { + return null; + } + + return ( +