From 40a51ac22b288c59bb1b93c5a0f2c1a1bf9ee930 Mon Sep 17 00:00:00 2001 From: olayway Date: Sun, 22 May 2022 15:00:04 +0200 Subject: [PATCH] [site/components][s]: contentlayer data in tooltip --- site/components/Anchor.js | 35 +++++++++-------------------------- site/components/Tooltip.js | 32 ++++++++++++++++++++++---------- site/styles/global.css | 2 +- site/utils/documentExtract.js | 2 +- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/site/components/Anchor.js b/site/components/Anchor.js index b75dbf4..6ef0b78 100644 --- a/site/components/Anchor.js +++ b/site/components/Anchor.js @@ -1,38 +1,21 @@ -import { useRouter } from 'next/router' - import { Tooltip } from './Tooltip'; import siteConfig from '../config/siteConfig.js' /** - * Component for adding previews on hover for specific anchor tags. - * Note: currently tooltips will be displayed only for anchor tags pointing to concepts. + * Component for adding previews on hovering over anchor tags with relative paths */ export const Anchor = (props) => { - const { href } = props; - const router = useRouter(); - - /* Check if the url is relative */ - const urlIsRelative = (url) => { - return href && - href.indexOf("http:") !== 0 && - href.indexOf("https:") !== 0 + /* Check if the path is relative */ + const pathIsRelative = (path) => { + return path && + path.indexOf("http:") !== 0 && + path.indexOf("https:") !== 0 && + path.indexOf("#") !== 0 } - /* Return absolute path to raw markdown content - * Note: currently disabled for guide page due to non-standard relative paths in some anchors (TBD) */ - const getRawMdContentUrl = (url, routerPath) => { - if (routerPath === '/guide' || !urlIsRelative(url)) { - return null - } - const currentPageMdUrl = [siteConfig.repoRawContentRoot, routerPath].join(""); - return new URL(href, currentPageMdUrl).href; - } - - const rawMdUrl = getRawMdContentUrl(href, router.asPath); - - if (rawMdUrl && !rawMdUrl.includes("notes") && !rawMdUrl.includes("claims")) { + if (pathIsRelative(props.href)) { return ( - ( + ( )} /> diff --git a/site/components/Tooltip.js b/site/components/Tooltip.js index 94c3214..0046426 100644 --- a/site/components/Tooltip.js +++ b/site/components/Tooltip.js @@ -14,13 +14,14 @@ import { useRole, } from '@floating-ui/react-dom-interactions' import { motion, AnimatePresence } from 'framer-motion'; +import { allOtherPages } from 'contentlayer/generated'; import documentExtract from '../utils/documentExtract' const tooltipBoxStyle = (theme) => ({ height: 'auto', - maxWidth: '30rem', + maxWidth: '40rem', padding: '1rem', background: theme === 'light' ? '#fff' : '#000', color: theme === 'light' ? 'rgb(99, 98, 98)' : '#A8A8A8', @@ -29,7 +30,7 @@ const tooltipBoxStyle = (theme) => ({ }) const tooltipBodyStyle = (theme) => ({ - maxHeight: '3.6rem', + maxHeight: '4.8rem', position: 'relative', lineHeight: '1.2rem', overflow: 'hidden', @@ -48,7 +49,8 @@ const tooltipArrowStyle = ({ theme, x, y, side }) => ({ transform: "rotate(45deg)" }) -export const Tooltip = ({ absolutePath, render, ...props }) => { +// export const Tooltip = ({ absolutePath, render, ...props }) => { +export const Tooltip = ({ render, ...props }) => { const theme = 'light'; // temporarily hard-coded; light theme tbd in next PR const arrowRef = useRef(null); @@ -104,15 +106,25 @@ export const Tooltip = ({ absolutePath, render, ...props }) => { const fetchTooltipContent = async () => { setTooltipContentLoaded(false); - const response = await fetch(absolutePath); - if (response.status !== 200) { - console.log(`Looks like there was a problem. Status Code: ${response.status}`) - return + let content; + // get tooltip content + try { + // create a temporary anchor tag to convert relative href to absolute path + const tempLink = document.createElement("a"); + tempLink.href = props.href; + // not all notes documents are structured so that the first paragraph will be the content summary + // TBD check if the docs can be adjusted and if previews for notes are even required + if (tempLink.pathname.includes('notes')) { + return + } + const filePath = tempLink.pathname.slice(1) // remove slash from the beginning + const page = allOtherPages.find(p => p._raw.sourceFilePath === filePath) + content = documentExtract(page.body.raw); + } catch { + content = 'An error occured...' } - const md = await response.text(); - const extract = documentExtract(md); - setTooltipContent(extract); + setTooltipContent(content); setTooltipContentLoaded(true); } diff --git a/site/styles/global.css b/site/styles/global.css index d295411..b0cea0b 100644 --- a/site/styles/global.css +++ b/site/styles/global.css @@ -33,7 +33,7 @@ body { content: ""; position: absolute; right: 0; - top: 2.4rem; /* multiple of $line-height used on the tooltip body */ + top: 3.6rem; /* multiple of $line-height used on the tooltip body (defined in tooltipBodyStyle) */ height: 1.2rem; /* ($top + $height)/$line-height is the number of lines we want to clip tooltip text at*/ width: 10rem; background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 100%); diff --git a/site/utils/documentExtract.js b/site/utils/documentExtract.js index f6462dc..5dff749 100644 --- a/site/utils/documentExtract.js +++ b/site/utils/documentExtract.js @@ -4,7 +4,7 @@ import find from 'unist-util-find' import { toString } from 'mdast-util-to-string' -// get first paragraph +// get first paragraph found in the document const documentExtract = (md) => { const mdast = unified().use(remarkParse).parse(md); let paragraph = find(mdast, (node) => node.type === "paragraph");