From 4788ce809767064aead0025fb313c84aaadc1eac Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Fri, 27 May 2022 21:26:59 -0700 Subject: [PATCH] Preview hover-links on Manifold TODO: make it feel faster by hiding Tippy default CSS? --- web/components/page.tsx | 5 ----- web/pages/_app.tsx | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/web/components/page.tsx b/web/components/page.tsx index 4f69beb4..2f2977ed 100644 --- a/web/components/page.tsx +++ b/web/components/page.tsx @@ -15,11 +15,6 @@ export function Page(props: { }) { const { margin, assertUser, children, rightSidebar, suspend } = props - if (typeof document !== 'undefined') { - // Seems to be undefined; because of https://i.imgur.com/4bMQ8rA.png ? - console.log(initPreviews) - } - return ( <>
https://manifold.markets/embed/SG/will-elon-musk-buy-twitter-this-yea + */ +function rewriteToEmbed(link: string) { + const alreadyEmbed = link.includes(`localhost:3000/embed/`) + if (alreadyEmbed) return link + + const match = link.match(regex)! + + console.log( + 'Hello world', + `http://localhost:3000/embed/${match[1]}/${match[2]}` + ) + + return `http://localhost:3000/embed/${match[1]}/${match[2]}` +} +// Only rewrite market links in regex +const regex = /^https?:\/\/localhost\:3000\/(?!charity\/)([^\/]+)\/([^\/]+)/ + +if (typeof document !== 'undefined') { + // Seems to be undefined; because of https://i.imgur.com/4bMQ8rA.png ? + console.log('initPreviews', initPreviews) + initPreviews({ + renderers: [ + // Just for localhost markets + { + canRender: async (url: URL) => regex.test(url.href), + + render: async (url: URL) => + createIframe(rewriteToEmbed(url.href), 'manifold-preview'), + }, + // All localhost URLs + iframeRenderer(/localhost\:3000/), + ...defaultRenderers, + ], + }) +} + function MyApp({ Component, pageProps }: AppProps) { usePreserveScroll()