Fix validation for local files. (#2447)

Allow urls for local files. This addresses the comment from
[here](https://github.com/tldraw/tldraw/pull/2428#issuecomment-1886221841).

### Change Type

- [x] `patch` — Bug fix
- [ ] `minor` — New feature
- [ ] `major` — Breaking change
- [ ] `dependencies` — Changes to package dependencies[^1]
- [ ] `documentation` — Changes to the documentation only[^2]
- [ ] `tests` — Changes to any test code only[^2]
- [ ] `internal` — Any other changes that don't affect the published
package[^2]
- [ ] I don't know

[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version

### Test Plan

1. Local images example should now work. We use images from the public
folder there.
This commit is contained in:
Mitja Bezenšek 2024-01-15 13:33:46 +01:00 committed by GitHub
parent 29044867dd
commit 9c91b2c4cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -617,6 +617,13 @@ function parseUrl(str: string) {
try {
return new URL(str)
} catch (error) {
if (str.startsWith('/') || str.startsWith('./')) {
try {
return new URL(str, 'http://example.com')
} catch (error) {
throw new ValidationError(`Expected a valid url, got ${JSON.stringify(str)}`)
}
}
throw new ValidationError(`Expected a valid url, got ${JSON.stringify(str)}`)
}
}