From 00e1a4ff8a4fad39f14f5ab02ef8b6c2ec13cf7f Mon Sep 17 00:00:00 2001 From: Jeff Wu Date: Wed, 22 Mar 2017 21:46:27 -0700 Subject: [PATCH] fixes to katex - report errors - non-greedy match - handle inner newline - handle surrounding punctuation --- package.json | 4 ++-- src/plugins/latex/index.tsx | 31 ++++++++++++++----------------- typings/katex.d.ts | 3 --- typings/react-katex.d.ts | 4 ---- 4 files changed, 16 insertions(+), 26 deletions(-) delete mode 100644 typings/katex.d.ts delete mode 100644 typings/react-katex.d.ts diff --git a/package.json b/package.json index b1f1029f..cd4adf17 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "@types/immutable": "^3.8.4", "@types/jquery": "^2.0.34", + "@types/katex": "^0.5.0", "@types/lodash": "^4.14.36", "@types/react": "^0.14.36", "@types/react-dom": "^0.14.18", @@ -27,8 +28,7 @@ "katex": "^0.7.1", "lodash": "^4.15.0", "react": "^15.3.1", - "react-dom": "^15.3.1", - "react-katex": "^1.1.0" + "react-dom": "^15.3.1" }, "devDependencies": { "@types/mocha": "^2.2.32", diff --git a/src/plugins/latex/index.tsx b/src/plugins/latex/index.tsx index 8cfcf3fd..12e0d4c3 100644 --- a/src/plugins/latex/index.tsx +++ b/src/plugins/latex/index.tsx @@ -1,13 +1,15 @@ import React from 'react'; // tslint:disable-line no-unused-variable -/// import katex from 'katex'; -/// -import { InlineMath, BlockMath } from 'react-katex'; import 'katex/dist/katex.min.css'; import { Token, RegexTokenizerSplitter, EmitFn } from '../../assets/js/utils/token_scanner'; import { registerPlugin } from '../../assets/js/plugins'; +// ignore the group, allow whitespace, beginning of line, or open paren +const latexPreRegex = '(?:\\s|^|\\()'; +// ignore the group, allow whitespace, end of line, punctuation, or close paren +const latexPostRegex = '(?:\\s|$|\\.|,|!|\\?|\\))'; + registerPlugin( { name: 'LaTeX', @@ -15,12 +17,13 @@ registerPlugin( description: ` Lets you inline LaTeX between $ delimiters, or add block LaTeX between $$ delimiters. + Limited to what KaTeX supports. `, }, function(api) { api.registerHook('session', 'renderLineTokenHook', (tokenizer) => { return tokenizer.chain(RegexTokenizerSplitter( - new RegExp('(?:\\s|^)(\\$\\$.+\\$\\$)(?:\\s|$)'), + new RegExp(latexPreRegex + '(\\$\\$(\\n|.)+?\\$\\$)' + latexPostRegex), (token: Token, emitToken: EmitFn, emit: EmitFn) => { for (let i = 0; i < token.info.length; i++) { if (token.info[i].cursor) { @@ -31,18 +34,15 @@ registerPlugin( } } try { - katex.__parse(token.text.slice(2, -2)); - emit( - - {token.text.slice(2, -2)} - - ); + const html = katex.renderToString(token.text.slice(2, -2), { displayMode: true }); + emit(
); } catch (e) { + api.session.showMessage(e.message, { text_class: 'error' }); emitToken(token); } } )).chain(RegexTokenizerSplitter( - new RegExp('(?:\\s|^)(\\$.+\\$)(?:\\s|$)'), + new RegExp(latexPreRegex + '(\\$(\\n|.)+?\\$)' + latexPostRegex), (token: Token, emitToken: EmitFn, emit: EmitFn) => { for (let i = 0; i < token.info.length; i++) { if (token.info[i].cursor) { @@ -53,13 +53,10 @@ registerPlugin( } } try { - katex.__parse(token.text.slice(-1, 1)); - emit( - - {token.text.slice(1, -1)} - - ); + const html = katex.renderToString(token.text.slice(1, -1), { displayMode: false }); + emit(); } catch (e) { + api.session.showMessage(e.message, { text_class: 'error' }); emitToken(token); } } diff --git a/typings/katex.d.ts b/typings/katex.d.ts deleted file mode 100644 index 2fb6c72a..00000000 --- a/typings/katex.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare module 'katex' { - export const __parse: any; -} diff --git a/typings/react-katex.d.ts b/typings/react-katex.d.ts deleted file mode 100644 index 35bdd349..00000000 --- a/typings/react-katex.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -declare module 'react-katex' { - export const InlineMath: any; - export const BlockMath: any; -}