Skip to content

Commit

Permalink
fix: bookmarks not showing up when request fails
Browse files Browse the repository at this point in the history
We are simplifying the implementation to no longer guess the description
based on the contents of the page. Doing that does work when the page
has paragraphs but for customized layouts there are no grantees.
Defaulting to showing title and URL when no meta tags are present is
consistent with how Notion does it.

Closes: #555
\#555: Bookmarks not working when using multiple bookmarks
  • Loading branch information
aalemayhu committed Apr 12, 2022
1 parent f105d1d commit 8b3c000
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
58 changes: 0 additions & 58 deletions server/lib/notion/blocks/media/BlockBookmark.tsx

This file was deleted.

30 changes: 30 additions & 0 deletions server/lib/notion/blocks/media/BlockBookmark/hooks/useMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import axios from "axios";

const metascraper = require("metascraper")([
require("metascraper-description")(),
require("metascraper-image")(),
require("metascraper-logo-favicon")(),
require("metascraper-title")(),
require("metascraper-url")(),
]);

export interface Metadata {
description: string;
title: string;
logo: string;
image: string;
}

export default async function useMetadata(url: string): Promise<Metadata> {
try {
let response = await axios.get(url);
return metascraper({ html: response.data, url: url });
} catch (error) {
return {
description: "",
title: new URL(url).hostname,
logo: "",
image: "",
};
}
}
30 changes: 30 additions & 0 deletions server/lib/notion/blocks/media/BlockBookmark/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ReactDOMServer from "react-dom/server";

import { GetBlockResponse } from "@notionhq/client/build/src/api-endpoints";
import useMetadata from "./hooks/useMetadata";

const BlockBookmark = async (
block: GetBlockResponse
): Promise<string | null> => {
/* @ts-ignore */
const bookmark = block.bookmark;
const metadata = await useMetadata(bookmark.url);

return ReactDOMServer.renderToStaticMarkup(
<a style={{margin: "4px"}} href={bookmark.url} className="bookmark source">
<div className="bookmark-info">
<div className="bookmark-text">
{metadata.title && <div className="bookmark-title">{metadata.title}</div>}
{metadata.description && <div className="bookmark-description">{metadata.description}</div>}
</div>
<div className="bookmark-href">
{metadata.logo && <img src={metadata.logo} className="icon bookmark-icon" />}
{bookmark.url}
</div>
</div>
{metadata.image && <img src={metadata.image} className="bookmark-image" />}
</a>
);
};

export default BlockBookmark;

0 comments on commit 8b3c000

Please sign in to comment.