From 1ce08dbe40487785e33dd2206250e3bef6f7abbd Mon Sep 17 00:00:00 2001 From: David Ball Date: Mon, 26 Aug 2024 01:35:09 -0400 Subject: [PATCH] Patch Squidex Markdown editor image src paths to render as paths within our app. --- src/config.ts | 4 ++-- src/lib/rendering.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 25231a6..4a78938 100644 --- a/src/config.ts +++ b/src/config.ts @@ -78,8 +78,8 @@ export const getWebhookPort = () => env.WEBHOOK_PORT ? parseInt(env.WEBHOOK_PORT export const getSquidexAppName = () => env.SQUIDEX_APP_NAME || undefined; export const getSquidexClientId = () => env.SQUIDEX_CLIENT_ID || undefined; export const getSquidexClientSecret = () => env.SQUIDEX_CLIENT_SECRET || undefined; -export const getSquidexEnvironment = () => env.SQUIDEX_ENVIRONMENT || undefined; -export const getSquidexPublicUrl = () => env.SQUIDEX_PUBLIC_URL || getSquidexEnvironment(); +export const getSquidexEnvironment = () => trimSlashes(env.SQUIDEX_ENVIRONMENT||'') || undefined; +export const getSquidexPublicUrl = () => trimSlashes(env.SQUIDEX_PUBLIC_URL||'') || getSquidexEnvironment(); export const config: Config = { // AmazonProductAdvertisingAPIAccessKey: getAmazonProductAdvertisingAPIAccessKey(), diff --git a/src/lib/rendering.ts b/src/lib/rendering.ts index 804253d..3fa450f 100644 --- a/src/lib/rendering.ts +++ b/src/lib/rendering.ts @@ -1,6 +1,7 @@ import markdownIt from "markdown-it"; import markdownItAttrs from "markdown-it-attrs"; import vm from 'node:vm'; +import { config } from "../config"; export const md = markdownIt().use(markdownItAttrs, { }); @@ -67,9 +68,15 @@ const renderCodeblock = (lang: string, template: string, templateContext?: any) return template; } +const replaceSquidexAssetPathWithOwn = (html: string) => { + let re = new RegExp(`src\\s*=\\s*["']${config.squidexPublicUrl?.replaceAll(`/`, `\\/`).replaceAll(`.`, `\\.`)||''}\\/api\\/assets/${config.squidexAppName||''}/([\\w-\\.\\/]*)["']`, 'g'); + return html.replaceAll(re, `src="${config.siteUrl}/img/$1"`); +} + export const renderMarkdown = (template: string, templateContext?: any) => { // render code blocks inside of the Markdown template = renderCodeblock('markdown', template, templateContext); template = md.render(interpolateString(template, templateContext)); + template = replaceSquidexAssetPathWithOwn(template); return template; } \ No newline at end of file