Patch Squidex Markdown editor image src paths to render as paths within our app. #3

Merged
daball merged 1 commits from native-advertising into main 2024-08-26 02:07:49 -04:00
2 changed files with 9 additions and 2 deletions
Showing only changes of commit 1ce08dbe40 - Show all commits

View File

@ -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(),

View File

@ -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;
}