JiHong88 / suneditor

Pure javascript based WYSIWYG html editor, with no dependencies.
http://suneditor.com
MIT License
1.77k stars 316 forks source link

Get latex original content from math nodes #1137

Open mendeseduda opened 1 year ago

mendeseduda commented 1 year ago

There's any way to get only math content as the original latex? So basically, I want to maintain core.getContents() behavior, but instead of html katex, get other html inserted plus the original latex for each node.

Context: I have a CMS where I can edit content using this editor and then save it into a database. One of the problems here is the size of the latex content after compiled by katex and some errors when re-rendering the katex html at the other app.

Current behavior:

From this:

image

Get this:

<h1>Should be a h1</h1><p>​<span class="__se__katex katex" contenteditable="false" data-exp="x = \frac{1}{2}" data-font-size="1em" style="font-size: 1em;"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>x</mi><mo>=</mo><mfrac><mn>1</mn><mn>2</mn></mfrac></mrow><annotation encoding="application/x-tex">x = \frac{1}{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">2</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span>​​<br></p>

Desired behavior

From this:

image

Get something like this:

<h1>Should be a h1</h1>
<p>$$x = \frac{1}{2}$$<\p>

Version

Suneditor: 2.44.3

JiHong88 commented 1 year ago

You need to get it with 'getContents" and edit it yourself. const dom = this._d.createRange().createContextualFragment(editorInstance.getContent()); const katex = dom.querySelectorAll('.sekatex');