feat: prepare latex server-side
This commit is contained in:
parent
cc63e16d9c
commit
1082f29143
3 changed files with 56 additions and 3 deletions
|
|
@ -43,13 +43,17 @@ export default class CollesController {
|
||||||
.preload('subject')
|
.preload('subject')
|
||||||
.preload('room')
|
.preload('room')
|
||||||
.first()
|
.first()
|
||||||
// TODO: Include BJID and BJSecret !
|
|
||||||
if (!colle) {
|
if (!colle) {
|
||||||
return response.notFound({ message: 'Colle not found' })
|
return response.notFound({ message: 'Colle not found' })
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
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' })
|
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 = {
|
const colleData = {
|
||||||
studentId: student.id,
|
studentId: student.id,
|
||||||
examinerId: examiner.id,
|
examinerId: examiner.id,
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export default class User extends BaseModel {
|
||||||
return `${this.firstName} ${this.lastName}`
|
return `${this.firstName} ${this.lastName}`
|
||||||
}
|
}
|
||||||
|
|
||||||
@column()
|
@column({ serializeAs: null })
|
||||||
declare email: string
|
declare email: string
|
||||||
|
|
||||||
@column.dateTime({ autoCreate: true })
|
@column.dateTime({ autoCreate: true })
|
||||||
|
|
|
||||||
|
|
@ -92,4 +92,45 @@ export class ColleService {
|
||||||
favoriteColles,
|
favoriteColles,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pre-process HTML content to render LaTeX
|
||||||
|
private removeTrailingLines(htmlString: string) {
|
||||||
|
return htmlString.replace(/(<br\s*\/?>\s*)+$/gi, '').trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
private extractLatexImages(html: string) {
|
||||||
|
const imgRegex = /<img[^>]+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, ' ')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue