Allow images from any domain

This commit is contained in:
EGOIST 2022-05-23 18:54:48 +08:00
parent 6bfb48db4c
commit 8efb4e71d9
1 changed files with 2 additions and 12 deletions

View File

@ -1,17 +1,8 @@
import Markdown from "markdown-it"
import { R2_URL } from "~/lib/env"
import { getUserContentsUrl } from "~/lib/user-contents"
const isExternLink = (url: string) => /^https?:\/\//.test(url)
const ALLOW_IMAGE_ORIGINS = [
"user-images.githubusercontent.com",
"cdn.jsdelivr.net",
"images.unsplash.com",
"source.unsplash.com",
R2_URL.replace(/^https?\:\/\//, ""),
]
export const pluginImage = (md: Markdown) => {
const imageRule = md.renderer.rules.image!
md.renderer.rules.image = (tokens, idx, options, env, self) => {
@ -23,9 +14,8 @@ export const pluginImage = (md: Markdown) => {
}
if (isExternLink(url)) {
const { hostname } = new URL(url)
if (!ALLOW_IMAGE_ORIGINS.includes(hostname)) {
throw new Error(`Image from ${hostname} is not allowed`)
if (!url.startsWith("https:")) {
throw new Error(`External image url must start with https`)
}
return imageRule(tokens, idx, options, env, self)
}