Skip to content

Commit e35ee9a

Browse files
committed
2 parents a99ab84 + 4e79c05 commit e35ee9a

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ export default class MarkdownPlugin extends AdminForthPlugin {
1919

2020
// Placeholder for future Upload Plugin API integration.
2121
// For now, treat all extracted URLs as plugin-owned public URLs.
22-
isPluginPublicUrl(_url: string): boolean {
23-
// todo: here we need to check that host name is same as upload plugin, probably create upload plugin endpoint
24-
// should handle cases that user might define custom preview url
25-
// and that local storage has no host name, here, the fact of luck of hostname might be used as
26-
return true;
22+
async isUrlFromPlugin(url: string): Promise<boolean> {
23+
if (!this.uploadPlugin) return false;
24+
try {
25+
const uploadPlugin = this.uploadPlugin as any;
26+
if (typeof uploadPlugin.isInternalUrl === 'function') {
27+
return await uploadPlugin.isInternalUrl(url);
28+
} else {
29+
throw new Error ('Please update upload plugin and storage adapter')
30+
}
31+
} catch (err) {
32+
console.error(`[MarkdownPlugin] Error checking URL ${url}:`, err);
33+
}
34+
return false;
2735
}
2836

2937
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
@@ -175,18 +183,22 @@ export default class MarkdownPlugin extends AdminForthPlugin {
175183

176184
const shouldTrackUrl = (url: string) => {
177185
try {
178-
return this.isPluginPublicUrl(url);
186+
return this.isUrlFromPlugin(url);
179187
} catch (err) {
180188
console.error('Error checking URL ownership', url, err);
181189
return false;
182190
}
183191
};
184192

185-
const getKeyFromTrackedUrl = (rawUrl: string): string | null => {
193+
const getKeyFromTrackedUrl = async (rawUrl: string): Promise<string | null> => {
186194
const srcTrimmed = rawUrl.trim().replace(/^<|>$/g, '');
187195
if (!srcTrimmed || srcTrimmed.startsWith('data:') || srcTrimmed.startsWith('javascript:')) {
188196
return null;
189197
}
198+
const isInternal = await this.isUrlFromPlugin(srcTrimmed);
199+
if (!isInternal) {
200+
return null;
201+
}
190202
if (!shouldTrackUrl(srcTrimmed)) {
191203
return null;
192204
}
@@ -232,7 +244,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
232244
return cleaned || null;
233245
};
234246

235-
function getAttachmentMetas(markdown: string): AttachmentMeta[] {
247+
async function getAttachmentMetas(markdown: string): Promise<AttachmentMeta[]> {
236248
if (!markdown) {
237249
return [];
238250
}
@@ -249,7 +261,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
249261
const srcRaw = match[2];
250262
const titleRaw = normalizeAttachmentTitleForDb((match[3] ?? match[4]) ?? null);
251263

252-
const key = getKeyFromTrackedUrl(srcRaw);
264+
const key = await getKeyFromTrackedUrl(srcRaw);
253265
if (!key) {
254266
continue;
255267
}
@@ -262,7 +274,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
262274
let srcMatch: RegExpExecArray | null;
263275
while ((srcMatch = htmlSrcRegex.exec(markdown)) !== null) {
264276
const srcRaw = srcMatch[1] ?? srcMatch[2] ?? srcMatch[3] ?? '';
265-
const key = getKeyFromTrackedUrl(srcRaw);
277+
const key = await getKeyFromTrackedUrl(srcRaw);
266278
if (!key) {
267279
continue;
268280
}
@@ -397,7 +409,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
397409

398410
(resourceConfig.hooks.create.afterSave).push(async ({ record, adminUser }: { record: any, adminUser: AdminUser }) => {
399411
// find all s3Paths in the html
400-
const metas = getAttachmentMetas(record[this.options.fieldName]);
412+
const metas = await getAttachmentMetas(record[this.options.fieldName]);
401413
const keys = metas.map(m => m.key);
402414
process.env.HEAVY_DEBUG && console.log('📸 Found attachment keys', keys);
403415
// create attachment records
@@ -423,7 +435,7 @@ export default class MarkdownPlugin extends AdminForthPlugin {
423435
]);
424436
const existingKeys = existingAparts.map((a: any) => a[this.options.attachments.attachmentFieldName]);
425437

426-
const metas = getAttachmentMetas(record[this.options.fieldName]);
438+
const metas = await getAttachmentMetas(record[this.options.fieldName]);
427439
const newKeys = metas.map(m => m.key);
428440

429441
process.env.HEAVY_DEBUG && console.log('📸 Existing keys (from db)', existingKeys)

0 commit comments

Comments
 (0)