From b04e001db1ac380874684e753c4f784f5a36b05c Mon Sep 17 00:00:00 2001 From: boojack Date: Mon, 28 Nov 2022 19:52:03 +0800 Subject: [PATCH] fix: image url host missing (#623) --- web/src/helpers/utils.ts | 6 ++++++ web/src/labs/marked/parser/Image.ts | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/web/src/helpers/utils.ts b/web/src/helpers/utils.ts index bbc314eed..90dfc57ea 100644 --- a/web/src/helpers/utils.ts +++ b/web/src/helpers/utils.ts @@ -134,3 +134,9 @@ export const parseHTMLToRawText = (htmlStr: string): string => { const text = tempEl.innerText; return text; }; + +export function absolutifyLink(rel: string): string { + const anchor = document.createElement("a"); + anchor.setAttribute("href", rel); + return anchor.href; +} diff --git a/web/src/labs/marked/parser/Image.ts b/web/src/labs/marked/parser/Image.ts index 3432e5e26..c8d2de353 100644 --- a/web/src/labs/marked/parser/Image.ts +++ b/web/src/labs/marked/parser/Image.ts @@ -1,4 +1,5 @@ import { escape } from "lodash-es"; +import { absolutifyLink } from "../../../helpers/utils"; export const IMAGE_REG = /!\[.*?\]\((.+?)\)/; @@ -8,8 +9,9 @@ const renderer = (rawStr: string): string => { return rawStr; } + const imageUrl = absolutifyLink(escape(matchResult[1])); // NOTE: Get image blob from backend to avoid CORS. - return ``; + return ``; }; export default {