Skip to content

Commit

Permalink
Bugfix for referencing attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablosanqt committed Oct 19, 2024
1 parent b01e9ea commit aa6421c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,48 @@ export class LectureAttachmentReferenceAction extends TextEditorAction {
}

insertLectureReference(editor: TextEditor, lecture: LectureWithDetails): void {
this.replaceTextAtCurrentSelection(editor, `[lecture]${lecture.title}(${this.metisService.getLinkForLecture(lecture.id.toString())})[/lecture]`);
this.replaceTextAtCurrentSelection(
editor,
`[lecture]${this.sanitizeStringForMarkdownEditor(lecture.title)}(${this.metisService.getLinkForLecture(lecture.id.toString())})[/lecture]`,
);
}

insertAttachmentReference(editor: TextEditor, attachment: Attachment): void {
const shortLink = attachment.link?.split('attachments/')[1];
this.replaceTextAtCurrentSelection(editor, `[attachment]${attachment.name}(${shortLink})[/attachment]`);
this.replaceTextAtCurrentSelection(editor, `[attachment]${this.sanitizeStringForMarkdownEditor(attachment.name)}(${shortLink})[/attachment]`);
}

insertSlideReference(editor: TextEditor, attachmentUnit: AttachmentUnit, slide: Slide): void {
const shortLink = slide.slideImagePath?.split('attachments/')[1];
// Remove the trailing slash and the file name.
const shortLinkWithoutFileName = shortLink?.replace(new RegExp(`[^/]*${'.png'}`), '').replace(/\/$/, '');
this.replaceTextAtCurrentSelection(editor, `[slide]${attachmentUnit.name} Slide ${slide.slideNumber}(${shortLinkWithoutFileName})[/slide]`);
this.replaceTextAtCurrentSelection(
editor,
`[slide]${this.sanitizeStringForMarkdownEditor(attachmentUnit.name)} Slide ${slide.slideNumber}(${shortLinkWithoutFileName})[/slide]`,
);
}

insertAttachmentUnitReference(editor: TextEditor, attachmentUnit: AttachmentUnit): void {
const shortLink = attachmentUnit.attachment?.link!.split('attachments/')[1];
this.replaceTextAtCurrentSelection(editor, `[lecture-unit]${attachmentUnit.name}(${shortLink})[/lecture-unit]`);
this.replaceTextAtCurrentSelection(editor, `[lecture-unit]${this.sanitizeStringForMarkdownEditor(attachmentUnit.name)}(${shortLink})[/lecture-unit]`);
}

/**
* Takes a string and removes any symbols used for syntax in the markdown editor.
* @param {string} str - The string for sanitization.
*/
private sanitizeStringForMarkdownEditor(str: string | undefined): string | undefined {
if (str === undefined) {
return str;
}

const symbolsToRemove = ['(', ')', '[', ']'];
let newString = str;

symbolsToRemove.forEach((symbolToRemove) => {
newString = newString.replaceAll(symbolToRemove, '');
});

return newString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { Lecture } from 'app/entities/lecture.model';
import { LectureAttachmentReferenceAction } from 'app/shared/monaco-editor/model/actions/communication/lecture-attachment-reference.action';
import { LectureUnitType } from 'app/entities/lecture-unit/lectureUnit.model';
import { ReferenceType } from 'app/shared/metis/metis.util';
import { Attachment } from 'app/entities/attachment.model';
import dayjs from 'dayjs/esm';

describe('MonacoEditorCommunicationActionIntegration', () => {
let comp: MonacoEditorComponent;
Expand Down Expand Up @@ -276,6 +278,64 @@ describe('MonacoEditorCommunicationActionIntegration', () => {
expect(comp.getText()).toBe(`[lecture]${lecture.title}(${metisService.getLinkForLecture(lecture.id.toString())})[/lecture]`);
});

it('should reference an attachment without brackets', () => {
fixture.detectChanges();

const attachmentNameWithBrackets = 'Test (File) With [Brackets] And (More) [Bracket(s)]';
const attachmentNameWithoutBrackets = 'Test File With Brackets And More Brackets';

const newAttachment = {
id: 53,
name: attachmentNameWithBrackets,
link: '/api/files/attachments/lecture/4/Mein_Test_PDF3.pdf',
version: 1,
uploadDate: dayjs('2019-05-07T08:49:59+02:00'),
attachmentType: 'FILE',
} as Attachment;

comp.registerAction(lectureAttachmentReferenceAction);
const lecture = lectureAttachmentReferenceAction.lecturesWithDetails[0];
const shortLink = newAttachment.link?.split('attachments/')[1];
lectureAttachmentReferenceAction.executeInCurrentEditor({ reference: ReferenceType.ATTACHMENT, lecture: lecture, attachment: newAttachment });
expect(comp.getText()).toBe(`[attachment]${attachmentNameWithoutBrackets}(${shortLink})[/attachment]`);
});

it('should reference a lecture without brackets', () => {
fixture.detectChanges();

const lectureNameWithBrackets = 'Test (Lecture) With [Brackets] And (More) [Bracket(s)]';
const lectureNameWithoutBrackets = 'Test Lecture With Brackets And More Brackets';

comp.registerAction(lectureAttachmentReferenceAction);
const lecture = lectureAttachmentReferenceAction.lecturesWithDetails[0];
const previousTitle = lecture.title;
lecture.title = lectureNameWithBrackets;
lectureAttachmentReferenceAction.executeInCurrentEditor({ reference: ReferenceType.LECTURE, lecture });
lecture.title = previousTitle;
expect(comp.getText()).toBe(`[lecture]${lectureNameWithoutBrackets}(${metisService.getLinkForLecture(lecture.id.toString())})[/lecture]`);
});

it('should reference an attachment unit without brackets', () => {
fixture.detectChanges();

const attachmentUnitNameWithBrackets = 'Test (AttachmentUnit) With [Brackets] And (More) [Bracket(s)]';
const attachmentUnitNameWithoutBrackets = 'Test AttachmentUnit With Brackets And More Brackets';

comp.registerAction(lectureAttachmentReferenceAction);
const lecture = lectureAttachmentReferenceAction.lecturesWithDetails[2];
const attachmentUnit = lecture.attachmentUnits![0];
const previousName = attachmentUnit.name;
attachmentUnit.name = attachmentUnitNameWithBrackets;
const attachmentUnitFileName = 'Metis-Attachment.pdf';
lectureAttachmentReferenceAction.executeInCurrentEditor({
reference: ReferenceType.ATTACHMENT_UNITS,
lecture,
attachmentUnit,
});
attachmentUnit.name = previousName;
expect(comp.getText()).toBe(`[lecture-unit]${attachmentUnitNameWithoutBrackets}(${attachmentUnitFileName})[/lecture-unit]`);
});

it('should reference an attachment', () => {
fixture.detectChanges();
comp.registerAction(lectureAttachmentReferenceAction);
Expand Down

0 comments on commit aa6421c

Please sign in to comment.