From b52225bcf1c4866af6b37b6ce3f806b27a7ef5e5 Mon Sep 17 00:00:00 2001 From: Sahatsawat Kanpai Date: Sun, 19 Jan 2025 21:23:13 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=A0=20pre-process=20each=20mathml=20el?= =?UTF-8?q?ement=20on=20the=20response=20html=20text=20into=20a=20ckeditor?= =?UTF-8?q?5-mathlive=20native?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + src/import.ts | 18 +++++++++++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0c69e58..9c8f3d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "@esi_package/ckeditor5-mathlive": "^0.0.16", + "mathml-to-latex": "^1.4.3", "temml": "^0.10.32" }, "devDependencies": { @@ -6368,6 +6369,15 @@ } } }, + "node_modules/@xmldom/xmldom": { + "version": "0.8.10", + "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.8.10.tgz", + "integrity": "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", @@ -13434,6 +13444,15 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/mathml-to-latex": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/mathml-to-latex/-/mathml-to-latex-1.4.3.tgz", + "integrity": "sha512-RfMRrpIBEk/BweXKZx0Mb3u5nA6/vKASYeVnclAed6pEhnfaCi1jfDqXYBEbzRsS+ORd1jB88Easa1Ov7majKQ==", + "license": "MIT", + "dependencies": { + "@xmldom/xmldom": "^0.8.10" + } + }, "node_modules/mdast-util-from-markdown": { "version": "0.8.5", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-0.8.5.tgz", diff --git a/package.json b/package.json index b24ef14..801f7cf 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,7 @@ }, "dependencies": { "@esi_package/ckeditor5-mathlive": "^0.0.16", + "mathml-to-latex": "^1.4.3", "temml": "^0.10.32" } } diff --git a/src/import.ts b/src/import.ts index 69f8dac..c211814 100644 --- a/src/import.ts +++ b/src/import.ts @@ -2,10 +2,11 @@ import { Plugin, ButtonView } from 'ckeditor5'; import ckeditor5Icon from '../theme/icons/ckeditor.svg'; -import { asBlob } from 'html-docx-js-typescript'; -import { saveAs } from 'file-saver'; +// import { asBlob } from 'html-docx-js-typescript'; +// import { saveAs } from 'file-saver'; -import Temml from 'temml'; +// import Temml from 'temml'; +import { MathMLToLaTeX } from 'mathml-to-latex'; export default class Import extends Plugin { public static get pluginName() { @@ -64,6 +65,17 @@ export default class Import extends Plugin { // console.log("Converted HTML:", htmlText); const htmlDoc = new DOMParser().parseFromString(htmlText, "text/html"); const body = htmlDoc.body; + // pre-process mathml to latex + // get all mathml elements + // replace all mathml element in the body with script type math/tex with inside content latex + const mathmlElements = body.querySelectorAll("math"); + mathmlElements.forEach(mathmlElement => { + const latex = MathMLToLaTeX.convert(mathmlElement.outerHTML); + const scriptElement = document.createElement("script"); + scriptElement.type = "math/tex"; + scriptElement.textContent = latex; + mathmlElement.replaceWith(scriptElement); + }); this.editor.setData(body.innerHTML); } catch (error) { console.error("Error uploading file:", error);