diff --git a/src/shared/utils/helpers/insertAfterMedia.test.ts b/src/shared/utils/helpers/insertAfterMedia.test.ts new file mode 100644 index 000000000..55fe0b97a --- /dev/null +++ b/src/shared/utils/helpers/insertAfterMedia.test.ts @@ -0,0 +1,85 @@ +import { insertAfterMedia } from './insertAfterMedia'; + +describe('insertAfterMedia', () => { + it('should insert the string after the first line containing content that does not contain solely media', () => { + const markdown = `![Image](image.jpg) +Image + + +This line does not contain media. +This line also does not contain media.`; + const inserted = 'Inserted string'; + + const result = insertAfterMedia(markdown, inserted); + + expect(result).toBe(`![Image](image.jpg) +Image + + +Inserted string +This line does not contain media. +This line also does not contain media.`); + }); + + it('should append the string to the end if there is no line containing content that does not contain solely media', () => { + const markdown = `![Image](image.jpg) +Image + +`; + const inserted = 'Inserted string'; + + const result = insertAfterMedia(markdown, inserted); + + expect(result).toBe(`![Image](image.jpg) +Image + + +Inserted string`); + }); + + it('should insert the string before a line containing both media and text content', () => { + const markdown = `![Image](image.jpg) +Image + + +![Image](image.jpg) This line contains text and media. +This line does not contain media.`; + const inserted = 'Inserted string'; + + const result = insertAfterMedia(markdown, inserted); + + expect(result).toBe(`![Image](image.jpg) +Image + + +Inserted string +![Image](image.jpg) This line contains text and media. +This line does not contain media.`); + }); + + it('should insert the string after a line that contains media wrapped in an alignment block', () => { + const markdown = `::: hljs-center +![Image](image.jpg) +Image + + +::: +::: hljs-center Image ::: +::: hljs-right This line does not contain media. ::: +This line also does not contain media.`; + const inserted = 'Inserted string'; + + const result = insertAfterMedia(markdown, inserted); + + expect(result).toBe(`::: hljs-center +![Image](image.jpg) +Image + + +::: +::: hljs-center Image ::: +Inserted string +::: hljs-right This line does not contain media. ::: +This line also does not contain media.`); + }); +}); diff --git a/src/shared/utils/helpers/insertAfterMedia.ts b/src/shared/utils/helpers/insertAfterMedia.ts index 1c76b3c35..64149c8c9 100644 --- a/src/shared/utils/helpers/insertAfterMedia.ts +++ b/src/shared/utils/helpers/insertAfterMedia.ts @@ -14,7 +14,10 @@ export const insertAfterMedia = (markdown: string, inserted: string) => { let i: number; for (i = 0; i < lines.length; i++) { const lineWithoutMedia = lines[i] - .replaceAll(/^:::\s+hljs-\S+|:::$|<(img|video|audio)[^>]+>|!\[[^\]]*\]\([^)]*\)/gi, '') + .replaceAll( + /^:::\s+hljs-\S+|:::$|]+>|]+>.*<\/video>|]+>.*<\/audio>|!\[[^\]]*\]\([^)]*\)/gi, + '', + ) .trim(); if (lineWithoutMedia) break; }