-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/stacks
- Loading branch information
Showing
10 changed files
with
97 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
src/app/shared/components/template/components/latex/latex.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
import { Component, OnInit } from "@angular/core"; | ||
import { Component, computed } from "@angular/core"; | ||
import { TemplateBaseComponent } from "src/app/shared/components/template/components/base"; | ||
|
||
import { DomSanitizer } from "@angular/platform-browser"; | ||
|
||
import DOMPurify from "dompurify"; | ||
import { latexToHtml } from "./latex.utils"; | ||
|
||
@Component({ | ||
selector: "template-latex-component", | ||
template: ` <span [innerHTML]="this._row.value | latex"> </span> `, | ||
template: ` <span [innerHTML]="parsedLatex()"> </span> `, | ||
}) | ||
/** Render text that includes LaTeX equations **/ | ||
export class TmplLatexComponent extends TemplateBaseComponent {} | ||
export class TmplLatexComponent extends TemplateBaseComponent { | ||
public parsedLatex = computed(() => { | ||
const value = this.value(); | ||
const html = latexToHtml(value); | ||
const sanitizedHtml = DOMPurify.sanitize(html); | ||
return this.domSanitizer.bypassSecurityTrustHtml(sanitizedHtml); | ||
}); | ||
|
||
constructor(private domSanitizer: DomSanitizer) { | ||
super(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/app/shared/components/template/components/latex/latex.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import katex from "katex"; | ||
import { extractMath, Segment } from "extract-math"; | ||
|
||
export function latexToHtml(latexInput: string) { | ||
let segments: Segment[] = []; | ||
if (latexInput && typeof latexInput === "string" && latexInput.length >= 0) { | ||
const dollarSignDelimitedSegments = extractMath(latexInput, { | ||
delimiters: { | ||
inline: ["$", "$"], | ||
display: ["$$", "$$"], | ||
}, | ||
}); | ||
|
||
segments = | ||
dollarSignDelimitedSegments.length > 1 | ||
? dollarSignDelimitedSegments | ||
: extractMath(latexInput, { | ||
delimiters: { | ||
inline: ["\\(", "\\)"], | ||
display: ["\\[", "\\]"], | ||
}, | ||
}); | ||
|
||
let htmlString = `<p>`; | ||
|
||
// detect whether input string contains a LaTeX 'math' segment, and is therefore in LaTeX's 'paragraph mode', else is itself in 'math mode' | ||
if (segments.some((segment) => segment.math)) { | ||
for (const segment of segments) { | ||
if (segment.math) { | ||
htmlString += katex.renderToString(segment.raw, { | ||
displayMode: segment.type === "display", | ||
throwOnError: true, | ||
}); | ||
} else { | ||
htmlString += segment.value; | ||
} | ||
} | ||
} else { | ||
htmlString += katex.renderToString(latexInput, { | ||
throwOnError: true, | ||
}); | ||
} | ||
htmlString += `</p>`; | ||
|
||
return htmlString; | ||
} | ||
return latexInput; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/app/shared/components/template/components/qr-code/qr-code.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<div class="image-wrapper"> | ||
<img src="{{ _row.value | qrCode | async }}" alt="{{ _row.value }}" /> | ||
<img src="{{ qrCodeData() | async }}" [alt]="_row.value" /> | ||
</div> |
18 changes: 16 additions & 2 deletions
18
src/app/shared/components/template/components/qr-code/qr-code.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
import { Component } from "@angular/core"; | ||
import { Component, computed, effect } from "@angular/core"; | ||
import QRCode from "qrcode"; | ||
import { TemplateBaseComponent } from "../base"; | ||
|
||
@Component({ | ||
selector: "plh-qr-code", | ||
templateUrl: "./qr-code.component.html", | ||
styleUrls: ["./qr-code.component.scss"], | ||
}) | ||
export class TmplQRCodeComponent extends TemplateBaseComponent {} | ||
export class TmplQRCodeComponent extends TemplateBaseComponent { | ||
/** | ||
* Computed signal used to generate QR code from value input | ||
* | ||
* NOTE - as computed signals do not accept async values this method | ||
* returns a promise which should be handled via async pipe | ||
* */ | ||
public qrCodeData = computed((): Promise<string> => { | ||
const value = this.value(); | ||
if (value && typeof value === "string") { | ||
return QRCode.toDataURL(value); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters