From c469c653807ee5827908e4540d31d3c952b31609 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 13 May 2022 08:42:04 -0600 Subject: [PATCH 1/5] migrate katex to ts --- .../app/katex/client/{index.js => index.ts} | 81 +++++++++++++------ .../app/katex/server/{index.js => index.ts} | 0 apps/meteor/package.json | 1 + .../core-typings/src/IMessage/IMessage.ts | 17 ++++ yarn.lock | 8 ++ 5 files changed, 82 insertions(+), 25 deletions(-) rename apps/meteor/app/katex/client/{index.js => index.ts} (63%) rename apps/meteor/app/katex/server/{index.js => index.ts} (100%) diff --git a/apps/meteor/app/katex/client/index.js b/apps/meteor/app/katex/client/index.ts similarity index 63% rename from apps/meteor/app/katex/client/index.js rename to apps/meteor/app/katex/client/index.ts index 139b420af26bd..e9dde7b5ef713 100644 --- a/apps/meteor/app/katex/client/index.js +++ b/apps/meteor/app/katex/client/index.ts @@ -1,52 +1,71 @@ import { Random } from 'meteor/random'; -import katex from 'katex'; +import KatexPackage from 'katex'; import { unescapeHTML, escapeHTML } from '@rocket.chat/string-helpers'; - import 'katex/dist/katex.min.css'; import './style.css'; +import { IMessage } from '@rocket.chat/core-typings'; class Boundary { - length() { + start: number; + + end: number; + + length(): number { return this.end - this.start; } - extract(str) { + extract(str: string): string { return str.substr(this.start, this.length()); } } +type Delimiter = { + opener: string; + closer: string; + displayMode: boolean; + enabled: () => boolean; +}; + +type OpeningDelimiter = { options: Delimiter; pos: number }; + +type LatexBoundary = { outer: Boundary; inner: Boundary }; + class Katex { - constructor(katex, { dollarSyntax, parenthesisSyntax }) { + katex: KatexPackage; + + delimitersMap: Delimiter[]; + + constructor(katex: KatexPackage, { dollarSyntax, parenthesisSyntax }: { dollarSyntax: boolean; parenthesisSyntax: boolean }) { this.katex = katex; this.delimitersMap = [ { opener: '\\[', closer: '\\]', displayMode: true, - enabled: () => parenthesisSyntax, + enabled: (): boolean => parenthesisSyntax, }, { opener: '\\(', closer: '\\)', displayMode: false, - enabled: () => parenthesisSyntax, + enabled: (): boolean => parenthesisSyntax, }, { opener: '$$', closer: '$$', displayMode: true, - enabled: () => dollarSyntax, + enabled: (): boolean => dollarSyntax, }, { opener: '$', closer: '$', displayMode: false, - enabled: () => dollarSyntax, + enabled: (): boolean => dollarSyntax, }, ]; } - findOpeningDelimiter(str, start) { + findOpeningDelimiter(str: string, start: number): OpeningDelimiter | null { const matches = this.delimitersMap .filter((options) => options.enabled()) .map((options) => ({ @@ -70,7 +89,7 @@ class Katex { return match; } - getLatexBoundaries(str, { options: { closer }, pos }) { + getLatexBoundaries(str: string, { options: { closer }, pos }: OpeningDelimiter): LatexBoundary | null { const closerIndex = str.substr(pos + closer.length).indexOf(closer); if (closerIndex < 0) { return null; @@ -92,15 +111,17 @@ class Katex { } // Searches for the first latex block in the given string - findLatex(str) { + findLatex(str: string): (LatexBoundary & { options: Delimiter }) | null { let start = 0; let openingDelimiterMatch; while ((openingDelimiterMatch = this.findOpeningDelimiter(str, start++)) != null) { const match = this.getLatexBoundaries(str, openingDelimiterMatch); - if (match && match.inner.extract(str).trim().length) { - match.options = openingDelimiterMatch.options; - return match; + if (match?.inner.extract(str).trim().length) { + return { + ...match, + options: openingDelimiterMatch.options, + }; } } @@ -109,7 +130,7 @@ class Katex { // Breaks a message to what comes before, after and to the content of a // matched latex block - extractLatex(str, match) { + extractLatex(str: string, match: LatexBoundary): { before: string; latex: string; after: string } { const before = str.substr(0, match.outer.start); const after = str.substr(match.outer.end); let latex = match.inner.extract(str); @@ -123,9 +144,9 @@ class Katex { // Takes a latex math string and the desired display mode and renders it // to HTML using the KaTeX library - renderLatex = (latex, displayMode) => { + renderLatex = (latex: string, displayMode: Delimiter['displayMode']): string => { try { - return this.katex.renderToString(latex, { + return KatexPackage.renderToString(latex, { displayMode, macros: { '\\href': '\\@secondoftwo', @@ -137,11 +158,15 @@ class Katex { }; // Takes a string and renders all latex blocks inside it - render(str, renderFunction) { + render(str: string, renderFunction: (latex: string, displayMode: Delimiter['displayMode']) => string): string { let result = ''; while (this.findLatex(str) != null) { // Find the first latex block in the string const match = this.findLatex(str); + if (!match) { + continue; + } + const parts = this.extractLatex(str, match); // Add to the reuslt what comes before the latex block as well as @@ -155,7 +180,7 @@ class Katex { return result; } - renderMessage = (message) => { + renderMessage = (message: string | IMessage): string | IMessage => { if (typeof message === 'string') { return this.render(message, this.renderLatex); } @@ -170,7 +195,7 @@ class Katex { message.html = this.render(message.html, (latex, displayMode) => { const token = `=!=${Random.id()}=!=`; - message.tokens.push({ + message.tokens?.push({ token, text: this.renderLatex(latex, displayMode), }); @@ -181,11 +206,17 @@ class Katex { }; } -export const createKatexMessageRendering = (options) => { - const instance = new Katex(katex, options); - return (message) => instance.renderMessage(message); +export const createKatexMessageRendering = (options: { + dollarSyntax: boolean; + parenthesisSyntax: boolean; +}): ((message: string | IMessage) => string | IMessage) => { + const instance = new Katex(KatexPackage, options); + return (message): string | IMessage => instance.renderMessage(message); }; -export const getKatexHtml = (text, katex) => { +export const getKatexHtml = ( + text: string, + katex: { dollarSyntaxEnabled: boolean; parenthesisSyntaxEnabled: boolean }, +): string | IMessage => { return createKatexMessageRendering({ dollarSyntax: katex.dollarSyntaxEnabled, parenthesisSyntax: katex.parenthesisSyntaxEnabled })(text); }; diff --git a/apps/meteor/app/katex/server/index.js b/apps/meteor/app/katex/server/index.ts similarity index 100% rename from apps/meteor/app/katex/server/index.js rename to apps/meteor/app/katex/server/index.ts diff --git a/apps/meteor/package.json b/apps/meteor/package.json index 7f3ade827db7f..8ae672a41ea87 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -212,6 +212,7 @@ "@rocket.chat/ui-kit": "~0.31.11", "@slack/client": "^4.12.0", "@types/cookie": "^0.4.1", + "@types/katex": "^0.14.0", "@types/lodash": "^4.14.177", "@types/lodash.debounce": "^4.0.6", "@types/proxy-from-env": "^1.0.1", diff --git a/packages/core-typings/src/IMessage/IMessage.ts b/packages/core-typings/src/IMessage/IMessage.ts index 2a0e738fdd1cd..47e6c00ae7698 100644 --- a/packages/core-typings/src/IMessage/IMessage.ts +++ b/packages/core-typings/src/IMessage/IMessage.ts @@ -74,6 +74,19 @@ export type MessageTypesValues = | OmnichannelTypesValues | OtrSystemMessages; +export type TokenType = 'code' | 'inlinecode' | 'bold' | 'italic' | 'strike' | 'link'; +export type Token = { + token: string; + text: string; + type?: TokenType; + noHtml?: string; +} & TokenExtra; + +export type TokenExtra = { + highlight?: boolean; + noHtml?: string; +}; + export interface IMessage extends IRocketChatRecord { rid: RoomID; msg: string; @@ -140,6 +153,10 @@ export interface IMessage extends IRocketChatRecord { avatar?: string; emoji?: string; + + // Tokenization fields + tokens?: Token[]; + html?: string; } export type MessageSystem = { diff --git a/yarn.lock b/yarn.lock index 5276084b95bbc..eb97a65cafffb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4047,6 +4047,7 @@ __metadata: "@types/jsdom": ^16.2.12 "@types/jsdom-global": ^3.0.2 "@types/jsrsasign": ^9.0.3 + "@types/katex": ^0.14.0 "@types/ldapjs": ^2.2.1 "@types/less": ^3.0.2 "@types/lodash": ^4.14.177 @@ -6515,6 +6516,13 @@ __metadata: languageName: node linkType: hard +"@types/katex@npm:^0.14.0": + version: 0.14.0 + resolution: "@types/katex@npm:0.14.0" + checksum: 330e0d0337ba48c87f5b793965fbad673653789bf6e50dfe8d726a7b0cbefd37195055e31503aae629814aa79447e4f23a4b87ad1ac565c0d9a9d9978836f39b + languageName: node + linkType: hard + "@types/keyv@npm:^3.1.1": version: 3.1.4 resolution: "@types/keyv@npm:3.1.4" From f06e46c99c93c3706f0a69a80d8f897d1fae3160 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 13 May 2022 08:43:41 -0600 Subject: [PATCH 2/5] prettier --- packages/core-typings/src/IMessage/IMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-typings/src/IMessage/IMessage.ts b/packages/core-typings/src/IMessage/IMessage.ts index 47e6c00ae7698..64d68c1b15b39 100644 --- a/packages/core-typings/src/IMessage/IMessage.ts +++ b/packages/core-typings/src/IMessage/IMessage.ts @@ -153,7 +153,7 @@ export interface IMessage extends IRocketChatRecord { avatar?: string; emoji?: string; - + // Tokenization fields tokens?: Token[]; html?: string; From 7c74765d020199eaeaf7c3f0e1c027069f6daa58 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 24 May 2022 10:08:42 -0600 Subject: [PATCH 3/5] fix package.lock --- apps/meteor/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/package.json b/apps/meteor/package.json index 580d941d77522..95bddf5ef6357 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -210,9 +210,9 @@ "@rocket.chat/ui-contexts": "workspace:^", "@rocket.chat/ui-kit": "~0.31.12", "@slack/client": "^4.12.0", - "@types/katex": "^0.14.0", "@slack/rtm-api": "^6.0.0", "@types/cookie": "^0.5.1", + "@types/katex": "^0.14.0", "@types/lodash": "^4.14.182", "@types/lodash.debounce": "^4.0.6", "@types/proxy-from-env": "^1.0.1", From bc271b357ff47fa287a6edc3a0a388e30b487070 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 24 May 2022 11:02:16 -0600 Subject: [PATCH 4/5] fix typings --- apps/meteor/app/katex/client/index.ts | 50 ++++++++++++++----- apps/meteor/client/components/Katex.tsx | 2 +- .../client/startup/renderMessage/katex.ts | 2 +- apps/meteor/package.json | 2 +- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/apps/meteor/app/katex/client/index.ts b/apps/meteor/app/katex/client/index.ts index e9dde7b5ef713..a2cee08f78e4e 100644 --- a/apps/meteor/app/katex/client/index.ts +++ b/apps/meteor/app/katex/client/index.ts @@ -180,7 +180,11 @@ class Katex { return result; } - renderMessage = (message: string | IMessage): string | IMessage => { + public renderMessage(message: string): string; + + public renderMessage(message: IMessage): IMessage; + + public renderMessage(message: string | IMessage): string | IMessage { if (typeof message === 'string') { return this.render(message, this.renderLatex); } @@ -203,20 +207,40 @@ class Katex { }); return message; - }; + } } -export const createKatexMessageRendering = (options: { - dollarSyntax: boolean; - parenthesisSyntax: boolean; -}): ((message: string | IMessage) => string | IMessage) => { +export function createKatexMessageRendering( + options: { + dollarSyntax: boolean; + parenthesisSyntax: boolean; + }, + _isMessage: true, +): (message: IMessage) => IMessage; +export function createKatexMessageRendering( + options: { + dollarSyntax: boolean; + parenthesisSyntax: boolean; + }, + _isMessage: false, +): (message: string) => string; +export function createKatexMessageRendering( + options: { + dollarSyntax: boolean; + parenthesisSyntax: boolean; + }, + _isMessage: true | false, +): ((message: string) => string) | ((message: IMessage) => IMessage) { const instance = new Katex(KatexPackage, options); - return (message): string | IMessage => instance.renderMessage(message); -}; + if (_isMessage) { + return (message: IMessage): IMessage => instance.renderMessage(message); + } + return (message: string): string => instance.renderMessage(message); +} -export const getKatexHtml = ( - text: string, - katex: { dollarSyntaxEnabled: boolean; parenthesisSyntaxEnabled: boolean }, -): string | IMessage => { - return createKatexMessageRendering({ dollarSyntax: katex.dollarSyntaxEnabled, parenthesisSyntax: katex.parenthesisSyntaxEnabled })(text); +export const getKatexHtml = (text: string, katex: { dollarSyntaxEnabled: boolean; parenthesisSyntaxEnabled: boolean }): string => { + return createKatexMessageRendering( + { dollarSyntax: katex.dollarSyntaxEnabled, parenthesisSyntax: katex.parenthesisSyntaxEnabled }, + false, + )(text); }; diff --git a/apps/meteor/client/components/Katex.tsx b/apps/meteor/client/components/Katex.tsx index 6a66a11571919..a7cb3f279f4b1 100644 --- a/apps/meteor/client/components/Katex.tsx +++ b/apps/meteor/client/components/Katex.tsx @@ -11,7 +11,7 @@ type KatexProps = { }; const Katex = ({ text, options }: KatexProps): ReactElement => ( - + ); export default memo(Katex); diff --git a/apps/meteor/client/startup/renderMessage/katex.ts b/apps/meteor/client/startup/renderMessage/katex.ts index 005181c9b92e6..68b76006c1174 100644 --- a/apps/meteor/client/startup/renderMessage/katex.ts +++ b/apps/meteor/client/startup/renderMessage/katex.ts @@ -19,7 +19,7 @@ Meteor.startup(() => { }; import('../../../app/katex/client').then(({ createKatexMessageRendering }) => { - const renderMessage = createKatexMessageRendering(options); + const renderMessage = createKatexMessageRendering(options, true); callbacks.remove('renderMessage', 'katex'); callbacks.add('renderMessage', renderMessage, callbacks.priority.HIGH + 1, 'katex'); }); diff --git a/apps/meteor/package.json b/apps/meteor/package.json index 95bddf5ef6357..e85211fb48525 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -28,7 +28,7 @@ "obj:dev": "TEST_MODE=true yarn dev", "stylelint": "stylelint \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"", "stylelint:fix": "stylelint --fix \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"", - "typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=4092\" tsc --noEmit --skipLibCheck", + "typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=8092\" tsc --noEmit --skipLibCheck", "deploy": "npm run build && pm2 startOrRestart pm2.json", "coverage": "nyc -r html mocha --config ./.mocharc.js", "testci": "node .scripts/start.js", From df0c647fcc9b9bcd3539b26e0e785da12bbf2d29 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 24 May 2022 11:58:52 -0600 Subject: [PATCH 5/5] oops 2 --- apps/meteor/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/package.json b/apps/meteor/package.json index e85211fb48525..95bddf5ef6357 100644 --- a/apps/meteor/package.json +++ b/apps/meteor/package.json @@ -28,7 +28,7 @@ "obj:dev": "TEST_MODE=true yarn dev", "stylelint": "stylelint \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"", "stylelint:fix": "stylelint --fix \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"", - "typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=8092\" tsc --noEmit --skipLibCheck", + "typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=4092\" tsc --noEmit --skipLibCheck", "deploy": "npm run build && pm2 startOrRestart pm2.json", "coverage": "nyc -r html mocha --config ./.mocharc.js", "testci": "node .scripts/start.js",