/
embeds.ts
99 lines (88 loc) · 2.49 KB
/
embeds.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { EMBED_DEFINITIONS, EmbedDefinition } from '@tldraw/editor'
// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js
function escapeStringRegexp(string: string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string')
}
// Escape characters with special meaning either inside or outside character sets.
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}
/** @public */
export function matchEmbedUrl(url: string) {
const host = new URL(url).host.replace('www.', '')
for (const localEmbedDef of EMBED_DEFINITIONS) {
if (checkHostnames(localEmbedDef.hostnames, host)) {
const originalUrl = localEmbedDef.fromEmbedUrl(url)
if (originalUrl) {
return {
definition: localEmbedDef,
url: originalUrl,
embedUrl: url,
}
}
}
}
}
const globlikeRegExp = (input: string) => {
return input
.split('*')
.map((str) => escapeStringRegexp(str))
.join('.+')
}
const checkHostnames = (hostnames: readonly string[], targetHostname: string) => {
return !!hostnames.find((hostname) => {
const re = new RegExp(globlikeRegExp(hostname))
return targetHostname.match(re)
})
}
/** @public */
export function matchUrl(url: string) {
const host = new URL(url).host.replace('www.', '')
for (const localEmbedDef of EMBED_DEFINITIONS) {
if (checkHostnames(localEmbedDef.hostnames, host)) {
const embedUrl = localEmbedDef.toEmbedUrl(url)
if (embedUrl) {
return {
definition: localEmbedDef,
embedUrl,
url,
}
}
}
}
}
/** @public */
export type TLEmbedResult =
| {
definition: EmbedDefinition
url: string
embedUrl: string
}
| undefined
/**
* Tests whether an URL supports embedding and returns the result.
*
* @param inputUrl - The URL to match
* @public
*/
export function getEmbedInfoUnsafely(inputUrl: string): TLEmbedResult {
const result = matchUrl(inputUrl) ?? matchEmbedUrl(inputUrl)
return result
}
/**
* Tests whether an URL supports embedding and returns the result. If we encounter an error, we
* return undefined.
*
* @param inputUrl - The URL to match
* @public
*/
export function getEmbedInfo(inputUrl: string): TLEmbedResult {
try {
return getEmbedInfoUnsafely(inputUrl)
} catch (e) {
// Don't throw here! We'll throw it from the embed shape's shape util
console.error(e)
}
return undefined
}