diff --git a/app/controllers/colles_controller.ts b/app/controllers/colles_controller.ts index 8eba953..e9bf3a6 100644 --- a/app/controllers/colles_controller.ts +++ b/app/controllers/colles_controller.ts @@ -43,13 +43,17 @@ export default class CollesController { .preload('subject') .preload('room') .first() - // TODO: Include BJID and BJSecret ! if (!colle) { return response.notFound({ message: 'Colle not found' }) } + return { success: true, - data: colle, + data: { + ...colle.serialize(), + bjid: colle.bjid, + bjsecret: colle.bjsecret, + }, } } @@ -67,6 +71,14 @@ export default class CollesController { return response.badRequest({ message: 'Invalid date format' }) } + // Prepare the content and comment for rendering + if (payload.comment) { + payload.comment = this.service.prepareHtmlForRendering(payload.comment) + } + if (payload.content) { + payload.content = this.service.prepareHtmlForRendering(payload.content) + } + const colleData = { studentId: student.id, examinerId: examiner.id, diff --git a/app/models/user.ts b/app/models/user.ts index 56d5170..d6146da 100644 --- a/app/models/user.ts +++ b/app/models/user.ts @@ -22,7 +22,7 @@ export default class User extends BaseModel { return `${this.firstName} ${this.lastName}` } - @column() + @column({ serializeAs: null }) declare email: string @column.dateTime({ autoCreate: true }) diff --git a/app/services/colle_service.ts b/app/services/colle_service.ts index ee20a23..917312d 100644 --- a/app/services/colle_service.ts +++ b/app/services/colle_service.ts @@ -92,4 +92,45 @@ export class ColleService { favoriteColles, } } + + // Pre-process HTML content to render LaTeX + private removeTrailingLines(htmlString: string) { + return htmlString.replace(/(\s*)+$/gi, '').trim() + } + + private extractLatexImages(html: string) { + const imgRegex = /]+src="(https:\/\/latex\.codecogs\.com\/gif\.latex\?(=?.*?))"[^>]*>/g + let parts = [] + let latexMatches: string[] = [] + let lastIndex = 0 + + html.replace(imgRegex, (match, _, latex, index) => { + parts.push(html.slice(lastIndex, index)) // Add HTML before image + latexMatches.push(decodeURIComponent(latex)) // Extract and decode LaTeX + lastIndex = index + match.length + return '' + }) + + parts.push(html.slice(lastIndex)) // Add remaining HTML after last image + + return { parts, latexMatches } + } + + prepareHtmlForRendering(html: string) { + const strippedHtml = this.removeTrailingLines(html) + const { parts, latexMatches } = this.extractLatexImages(strippedHtml) + + const outputHtml = parts + .map((part, i) => { + if (!latexMatches[i]) { + return part + } + return `${part}$$${latexMatches[i]}$$` + }) + .join('') + + // Remove all "\," from string + const regex = /\\,/g + return outputHtml.replace(regex, ' ') + } }