{ "version": 3, "sources": ["../../../src/lib/utils/embeds.ts"], "sourcesContent": ["import { EMBED_DEFINITIONS, EmbedDefinition } from '@tldraw/editor'\n\n// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js\nfunction escapeStringRegexp(string: string) {\n\tif (typeof string !== 'string') {\n\t\tthrow new TypeError('Expected a string')\n\t}\n\n\t// Escape characters with special meaning either inside or outside character sets.\n\t// Use a simple backslash escape when it\u2019s always valid, and a `\\xnn` escape when the simpler form would be disallowed by Unicode patterns\u2019 stricter grammar.\n\treturn string.replace(/[|\\\\{}()[\\]^$+*?.]/g, '\\\\$&').replace(/-/g, '\\\\x2d')\n}\n\n/** @public */\nexport function matchEmbedUrl(url: string) {\n\tconst host = new URL(url).host.replace('www.', '')\n\tfor (const localEmbedDef of EMBED_DEFINITIONS) {\n\t\tif (checkHostnames(localEmbedDef.hostnames, host)) {\n\t\t\tconst originalUrl = localEmbedDef.fromEmbedUrl(url)\n\t\t\tif (originalUrl) {\n\t\t\t\treturn {\n\t\t\t\t\tdefinition: localEmbedDef,\n\t\t\t\t\turl: originalUrl,\n\t\t\t\t\tembedUrl: url,\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nconst globlikeRegExp = (input: string) => {\n\treturn input\n\t\t.split('*')\n\t\t.map((str) => escapeStringRegexp(str))\n\t\t.join('.+')\n}\n\nconst checkHostnames = (hostnames: readonly string[], targetHostname: string) => {\n\treturn !!hostnames.find((hostname) => {\n\t\tconst re = new RegExp(globlikeRegExp(hostname))\n\t\treturn targetHostname.match(re)\n\t})\n}\n\n/** @public */\nexport function matchUrl(url: string) {\n\tconst host = new URL(url).host.replace('www.', '')\n\tfor (const localEmbedDef of EMBED_DEFINITIONS) {\n\t\tif (checkHostnames(localEmbedDef.hostnames, host)) {\n\t\t\tconst embedUrl = localEmbedDef.toEmbedUrl(url)\n\n\t\t\tif (embedUrl) {\n\t\t\t\treturn {\n\t\t\t\t\tdefinition: localEmbedDef,\n\t\t\t\t\tembedUrl,\n\t\t\t\t\turl,\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n/** @public */\nexport type TLEmbedResult =\n\t| {\n\t\t\tdefinition: EmbedDefinition\n\t\t\turl: string\n\t\t\tembedUrl: string\n\t }\n\t| undefined\n\n/**\n * Tests whether an URL supports embedding and returns the result.\n *\n * @param inputUrl - The URL to match\n * @public\n */\nexport function getEmbedInfoUnsafely(inputUrl: string): TLEmbedResult {\n\tconst result = matchUrl(inputUrl) ?? matchEmbedUrl(inputUrl)\n\treturn result\n}\n\n/**\n * Tests whether an URL supports embedding and returns the result. If we encounter an error, we\n * return undefined.\n *\n * @param inputUrl - The URL to match\n * @public\n */\nexport function getEmbedInfo(inputUrl: string): TLEmbedResult {\n\ttry {\n\t\treturn getEmbedInfoUnsafely(inputUrl)\n\t} catch (e) {\n\t\t// Don't throw here! We'll throw it from the embed shape's shape util\n\t\tconsole.error(e)\n\t}\n\n\treturn undefined\n}\n"], "mappings": "AAAA,SAAS,yBAA0C;AAGnD,SAAS,mBAAmB,QAAgB;AAC3C,MAAI,OAAO,WAAW,UAAU;AAC/B,UAAM,IAAI,UAAU,mBAAmB;AAAA,EACxC;AAIA,SAAO,OAAO,QAAQ,uBAAuB,MAAM,EAAE,QAAQ,MAAM,OAAO;AAC3E;AAGO,SAAS,cAAc,KAAa;AAC1C,QAAM,OAAO,IAAI,IAAI,GAAG,EAAE,KAAK,QAAQ,QAAQ,EAAE;AACjD,aAAW,iBAAiB,mBAAmB;AAC9C,QAAI,eAAe,cAAc,WAAW,IAAI,GAAG;AAClD,YAAM,cAAc,cAAc,aAAa,GAAG;AAClD,UAAI,aAAa;AAChB,eAAO;AAAA,UACN,YAAY;AAAA,UACZ,KAAK;AAAA,UACL,UAAU;AAAA,QACX;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;AAEA,MAAM,iBAAiB,CAAC,UAAkB;AACzC,SAAO,MACL,MAAM,GAAG,EACT,IAAI,CAAC,QAAQ,mBAAm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