Skip to content

Commit c341438

Browse files
authored
feat: add image proxy for crepe (#1586)
* feat: add image proxy for crepe * chore: f
1 parent 4862bf1 commit c341438

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Diff for: packages/components/src/image-block/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ImageBlockConfig {
1010
uploadPlaceholderText: string
1111
captionPlaceholderText: string
1212
onUpload: (file: File) => Promise<string>
13+
proxyDomURL?: (url: string) => Promise<string> | string
1314
}
1415

1516
export const defaultImageBlockConfig: ImageBlockConfig = {

Diff for: packages/components/src/image-block/view/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@ export const imageBlockView = $view(
1717
'milkdown-image-block'
1818
) as HTMLElement & ImageComponentProps
1919
const config = ctx.get(imageBlockConfig.key)
20+
const proxyDomURL = config.proxyDomURL
2021
const bindAttrs = (node: Node) => {
21-
dom.src = node.attrs.src
22+
if (!proxyDomURL) {
23+
dom.src = node.attrs.src
24+
} else {
25+
const proxiedURL = proxyDomURL(node.attrs.src)
26+
if (typeof proxiedURL === 'string') {
27+
dom.src = proxiedURL
28+
} else {
29+
proxiedURL.then((url) => {
30+
dom.src = url
31+
})
32+
}
33+
}
2234
dom.ratio = node.attrs.ratio
2335
dom.caption = node.attrs.caption
2436

Diff for: packages/components/src/image-inline/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface InlineImageConfig {
88
confirmButton: () => ReturnType<typeof html>
99
uploadPlaceholderText: string
1010
onUpload: (file: File) => Promise<string>
11+
proxyDomURL?: (url: string) => Promise<string> | string
1112
}
1213

1314
export const defaultInlineImageConfig: InlineImageConfig = {

Diff for: packages/components/src/image-inline/view.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@ export const inlineImageView = $view(
1717
'milkdown-image-inline'
1818
) as HTMLElement & InlineImageComponentProps
1919
const config = ctx.get(inlineImageConfig.key)
20+
const proxyDomURL = config.proxyDomURL
2021
const bindAttrs = (node: Node) => {
21-
dom.src = node.attrs.src
22+
if (!proxyDomURL) {
23+
dom.src = node.attrs.src
24+
} else {
25+
const proxiedURL = proxyDomURL(node.attrs.src)
26+
if (typeof proxiedURL === 'string') {
27+
dom.src = proxiedURL
28+
} else {
29+
proxiedURL.then((url) => {
30+
dom.src = url
31+
})
32+
}
33+
}
2234
dom.alt = node.attrs.alt
2335
dom.title = node.attrs.title
2436
}

0 commit comments

Comments
 (0)