Merge pull request #178 from life-itself/170-next-links

[site/components]: add support for next links in markdown
This commit is contained in:
Khalil Ali 2022-06-13 23:27:26 +03:00 committed by GitHub
commit 9411ba2270
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import { useEffect, useState } from "react"
import Link from 'next/link';
import { Tooltip } from './Tooltip';
/**
* Component for adding previews on hovering over anchor tags with relative paths
*/
@ -8,17 +9,44 @@ export const Anchor = (props) => {
const pathIsRelative = (path) => {
return path &&
path.indexOf("http:") !== 0 &&
path.indexOf("https:") !== 0 &&
path.indexOf("#") !== 0
path.indexOf("https:") !== 0
}
if (pathIsRelative(props.href)) {
const href = props.href.endsWith(".md")
? props.href.replace(".md", "")
: props.href;
const [ path, setPath ] = useState(href)
useEffect(() => {
let isMount = true
if (isMount) {
const link = document.createElement("a")
link.href = href
setPath(link.pathname)
}
return () => {
setPath(href)
isMount = false
}
},[])
if (pathIsRelative(href)) {
if (href.indexOf("#") !== 0) {
return (
<Tooltip {...props} href={href} render={ tooltipTriggerProps => (
<Link href={path}>
<a {...tooltipTriggerProps} href={path} />
</Link>
)}
/>
)
}
return (
<Tooltip {...props} render={ tooltipTriggerProps => (
<a {...tooltipTriggerProps} />
)}
/>
<Link href={href}>
<a {...props} />
</Link>
)
}
return <a {...props} />;
return <a target="_blank" rel="noopener" {...props} />;
};

View File

@ -117,7 +117,7 @@ export const Tooltip = ({ render, ...props }) => {
if (filePath.includes('notes')) {
return
}
const page = allOtherPages.find(p => p._raw.sourceFilePath === filePath)
const page = allOtherPages.find(p => p._raw.flattenedPath === filePath)
content = documentExtract(page.body.raw);
} catch {
return