Skip to content

Commit

Permalink
feat: add image proxy for crepe (Milkdown#1586)
Browse files Browse the repository at this point in the history
* feat: add image proxy for crepe

* chore: f
  • Loading branch information
Saul-Mirone authored Dec 15, 2024
1 parent 4862bf1 commit c341438
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/components/src/image-block/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ImageBlockConfig {
uploadPlaceholderText: string
captionPlaceholderText: string
onUpload: (file: File) => Promise<string>
proxyDomURL?: (url: string) => Promise<string> | string
}

export const defaultImageBlockConfig: ImageBlockConfig = {
Expand Down
14 changes: 13 additions & 1 deletion packages/components/src/image-block/view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,20 @@ export const imageBlockView = $view(
'milkdown-image-block'
) as HTMLElement & ImageComponentProps
const config = ctx.get(imageBlockConfig.key)
const proxyDomURL = config.proxyDomURL
const bindAttrs = (node: Node) => {
dom.src = node.attrs.src
if (!proxyDomURL) {
dom.src = node.attrs.src
} else {
const proxiedURL = proxyDomURL(node.attrs.src)
if (typeof proxiedURL === 'string') {
dom.src = proxiedURL
} else {
proxiedURL.then((url) => {
dom.src = url
})
}
}
dom.ratio = node.attrs.ratio
dom.caption = node.attrs.caption

Expand Down
1 change: 1 addition & 0 deletions packages/components/src/image-inline/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface InlineImageConfig {
confirmButton: () => ReturnType<typeof html>
uploadPlaceholderText: string
onUpload: (file: File) => Promise<string>
proxyDomURL?: (url: string) => Promise<string> | string
}

export const defaultInlineImageConfig: InlineImageConfig = {
Expand Down
14 changes: 13 additions & 1 deletion packages/components/src/image-inline/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,20 @@ export const inlineImageView = $view(
'milkdown-image-inline'
) as HTMLElement & InlineImageComponentProps
const config = ctx.get(inlineImageConfig.key)
const proxyDomURL = config.proxyDomURL
const bindAttrs = (node: Node) => {
dom.src = node.attrs.src
if (!proxyDomURL) {
dom.src = node.attrs.src
} else {
const proxiedURL = proxyDomURL(node.attrs.src)
if (typeof proxiedURL === 'string') {
dom.src = proxiedURL
} else {
proxiedURL.then((url) => {
dom.src = url
})
}
}
dom.alt = node.attrs.alt
dom.title = node.attrs.title
}
Expand Down

0 comments on commit c341438

Please sign in to comment.