From 2affed814fe32a96abc7d28e81be95d5183331c5 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Thu, 11 Aug 2022 11:00:58 -0700 Subject: [PATCH] Clean up embed rewrite code --- web/components/editor/embed-modal.tsx | 92 +++++++++++---------------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/web/components/editor/embed-modal.tsx b/web/components/editor/embed-modal.tsx index 5baac757..bb826e9e 100644 --- a/web/components/editor/embed-modal.tsx +++ b/web/components/editor/embed-modal.tsx @@ -7,65 +7,49 @@ import { Modal } from '../layout/modal' import { Row } from '../layout/row' import { Spacer } from '../layout/spacer' -function isValidIframe(text: string) { - return /^$/.test(text) +type EmbedPattern = { + // Regex should have a single capture group. + regex: RegExp + rewrite: (text: string) => string } -// A valid tweet URL looks like 'https://twitter.com/username/status/123456789' -// Return the tweetId if the URL is valid, otherwise return null. -function getTweetId(text: string) { - const match = text.match(/^https?:\/\/twitter\.com\/.*\/status\/(\d+)/) - return match ? match[1] : null -} - -// Manifold URL: https://manifold.markets/Austin/will-manifold-ever-be-worth-1b -function getManifoldSlug(text: string) { - const match = text.match(/^https?:\/\/manifold\.markets\/([^\/]+\/[^\/]+)/) - return match ? match[1] : null -} - -// Youtube URL: 'https://www.youtube.com/watch?v=ziq7FUKpCS8' -function getYoutubeId(text: string) { - const match = text.match(/^https?:\/\/www\.youtube\.com\/watch\?v=([^&]+)/) - return match ? match[1] : null -} - -// Metaculus URL: 'https://www.metaculus.com/questions/5320/chinese-annexation-of-half-of-taiwan-by-2050/' -function getMetaculusId(text: string) { - const match = text.match(/^https?:\/\/www\.metaculus\.com\/questions\/(\d+)/) - return match ? match[1] : null -} - -function isValidUrl(text: string) { - // Conjured by Codex - return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test( - text - ) -} +const embedPatterns: EmbedPattern[] = [ + { + regex: /^()$/, + rewrite: (text: string) => text, + }, + { + regex: /^https?:\/\/manifold\.markets\/([^\/]+\/[^\/]+)/, + rewrite: (slug) => + ``, + }, + { + regex: /^https?:\/\/twitter\.com\/.*\/status\/(\d+)/, + rewrite: (id) => ``, + }, + { + regex: /^https?:\/\/www\.youtube\.com\/watch\?v=([^&]+)/, + rewrite: (id) => + ``, + }, + { + regex: /^https?:\/\/www\.metaculus\.com\/questions\/(\d+)/, + rewrite: (id) => + ``, + }, + { + regex: /^(https?:\/\/.*)/, + rewrite: (url) => ``, + }, +] function embedCode(text: string) { - if (isValidIframe(text)) { - return text - } else if (getTweetId(text)) { - // Append a leading 't', to prevent tweetId from being interpreted as a number. - // If it's a number, there may be numeric precision issues. - return `` - } else if (getManifoldSlug(text)) { - return `` - } else if (getYoutubeId(text)) { - return `` - } else if (getMetaculusId(text)) { - return `` - } else if (isValidUrl(text)) { - return `` + for (const pattern of embedPatterns) { + const match = text.match(pattern.regex) + if (match) { + return pattern.rewrite(match[1]) + } } - // Return null if the text is not embeddable. return null }