mattermost-mobile/app/utils/latex.ts
KobeBergmans 21adcf6085
Latex rendering with react-native-math-view (#5651)
* Added a Latex code window that displays the given code using katex

* Latex is now displayed without having to go to the next screen

* MathView test

* MathView WIP:
Displays math good, but in verry small font
View does not load if there is a syntax error and we are hard stuck in the app

* Math view now is rendered properly, few working points:
Text is not fixed size, it renders in a weird size
Multi Line subscript is not working

* Latex is now rendered properly

* Fixed Error message and moved helper function to utils

* Added some padding and vertical scroll on latex screen

* Updated config option to work on real phone too

* Removed unused code and fixed tests

* Added LatexInline and tried to modify parser, not yet finished though. InlineLatexOption is not yet coded for

* Bugfixes and better line break checking

* Few margin changes for ios, also updated pod files

* padding is now dependend on system

* Add platform import

* Reverted package-lock change

* Added tests

* Bugfixes, UI tweaks and added unit test for latex util

* Added enableLatex to markdown options instead getting the state and config

* Changed the rendering a little bit, it is better but still far from perfect

* Width and height is now correctely displayed. Still not clean enough though

* Inline latex is now rendered properly for most equations

* Inline latex is now working properly

* tests for escaping and unescaping functions

* Added inline latex setting support

* Added inline latex tests

* Delete unused comment

* Added extra regex rule: there must be no word character in front of the inline latex code

* Upgraded math-view package and deleted duplicate entry from gemfile.lock

* Inline error functions are now class functions

* Updated error color to theme error color

* Latex screen now extends the code screen

* Markdown changes:
Now uses dimensions instead of windowdimensions
Removed inline latex function
extracted latex rendering into separate function

* updated latex util test

* Hardcoded latex language in latex code block

* Added proptypes and cleaned up latex code block

* Bugfix latex split lines

* ÃDeleted language prop from latex code block

* Changed commonmark packages to personal repo's

* Latex rendering now goes by commonmark

* Deleted unused function and tests

* Commonmark packages back to normal versions with patch package

* Updated commonmark patch file to right file and changed package lock to point at right repo

* Updated patch files to latest version

* Updated commonmark patch

* Fixed linter errors

* Integrety of commonmark fix

* First fix of ios problems

* Updated snapshot

Co-authored-by: Kobe Bergmans <kobe.bergmans@student.kuleuven.be>
Co-authored-by: Administrator <admin@Macintosh-3.localdomain>
Co-authored-by: kobe bergmans <kobebergmans@kobes-MacBook-Pro.local>
2022-04-19 09:16:38 -04:00

96 lines
2.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/**
* Splits up latex code in an array consisting of each line of latex code.
* A latex linebreak is denoted by `\\`.
* A line is not broken in 2 cases:
* - The linebreak is in between brackets.
* - The linebreak occurs inbetween a `\begin` and `\end` statement.
*/
export function splitLatexCodeInLines(content: string): string[] {
let outLines = content.split('\\\\');
let i = 0;
while (i < outLines.length) {
if (testLatexLineBreak(outLines[i])) { //Line has no linebreak in between brackets
i += 1;
} else if (i < outLines.length - 2) {
outLines = outLines.slice(0, i).concat([outLines[i] + '\\\\' + outLines[i + 1]], outLines.slice(i + 2));
} else if (i === outLines.length - 2) {
outLines = outLines.slice(0, i).concat([outLines[i] + '\\\\' + outLines[i + 1]]);
} else {
break;
}
}
return outLines.map((line) => line.trim());
}
function testLatexLineBreak(latexCode: string): boolean {
/**
* These checks are special because they need to check if the braces and statements are not escaped
*/
//Check for cases
let beginCases = 0;
let endCases = 0;
let i = 0;
while (i < latexCode.length) {
const firstBegin = latexCode.indexOf('\\begin{', i);
const firstEnd = latexCode.indexOf('\\end{', i);
if (firstBegin === -1 && firstEnd === -1) {
break;
}
if (firstBegin !== -1 && (firstBegin < firstEnd || firstEnd === -1)) {
if (latexCode[firstBegin - 1] !== '\\') {
beginCases += 1;
}
i = firstBegin + '\\begin{'.length;
} else {
if (latexCode[firstEnd - 1] !== '\\') {
endCases += 1;
}
i = firstEnd + '\\end{'.length;
}
}
if (beginCases !== endCases) {
return false;
}
//Check for braces
let curlyOpenCases = 0;
let curlyCloseCases = 0;
i = 0;
while (i < latexCode.length) {
const firstBegin = latexCode.indexOf('{', i);
const firstEnd = latexCode.indexOf('}', i);
if (firstBegin === -1 && firstEnd === -1) {
break;
}
if (firstBegin !== -1 && (firstBegin < firstEnd || firstEnd === -1)) {
if (latexCode[firstBegin - 1] !== '\\') {
curlyOpenCases += 1;
}
i = firstBegin + 1;
} else {
if (latexCode[firstEnd - 1] !== '\\') {
curlyCloseCases += 1;
}
i = firstEnd + 1;
}
}
if (curlyOpenCases !== curlyCloseCases) {
return false;
}
return true;
}