dify/web/app/components/base/markdown-blocks/utils.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

30 lines
970 B
TypeScript

import { ALLOW_UNSAFE_DATA_SCHEME, MARKETPLACE_API_PREFIX } from '@/config'
type MdastNode = {
tagName?: string
children?: MdastNode[]
[key: string]: unknown
}
export const hasImageChild = (children: MdastNode[] | undefined): boolean => {
return (
children?.some((child) => {
if (child.tagName === 'img') return true
return child.children ? hasImageChild(child.children) : false
}) ?? false
)
}
export const isValidUrl = (url: string): boolean => {
const validPrefixes = ['http:', 'https:', '//', 'mailto:']
if (ALLOW_UNSAFE_DATA_SCHEME) validPrefixes.push('data:')
return validPrefixes.some((prefix) => url.startsWith(prefix))
}
export const getMarkdownImageURL = (url: string, pathname?: string) => {
const regex = /(^\.\/_assets|^_assets)/
if (regex.test(url))
return `${MARKETPLACE_API_PREFIX}${MARKETPLACE_API_PREFIX.endsWith('/') ? '' : '/'}plugins/${pathname ?? ''}${url.replace(regex, '/_assets')}`
return url
}