fenjalien / obsidian-typst

Renders typst code blocks in Obsidian into images using Typst through the power of WASM!
Apache License 2.0
335 stars 16 forks source link

"TypeError: Cannot read properties of undefined (reading 'length')" in math blocks while using rtl plugins #40

Open YonatanRubin opened 8 months ago

YonatanRubin commented 8 months ago

I am not sure if this plugin is the cause for the problem or the rtl plugin, but since this plugin shows the error I assume this one. while using either dynamic rtl or obsidian-rtl with the math block options on. writing math blocks seem to most of the time show the error "TypeError: Cannot read properties of undefined (reading 'length')" on some blocks, but only when in read mode. this issue persisted both on android and linux.

in edit mode: Screenshot_20240108_081610_Obsidian

in read mode: Screenshot_20240108_081556_Obsidian

after disabling the rtl plugin: Screenshot_20240108_082054_Obsidian

d-kaue commented 8 months ago

I can't reproduce this with the ObsidianRTL plugin. Are you sure you're getting an error with that one?

The DynamicRTL plugin seems to be deprecated.

YonatanRubin commented 8 months ago

yes. I recreated it now in a new vault having only obsidian-rtl and typst-renderer. I used the following file to check if the direction matters, and got it even when the page was set to ltr (which is weird because it is the first time it behaved like this that I have seen)

test for only ltr in my code.
a few lines
inline $f_x (t) = integral_0^oo e^(k/2) d t$
block :
$$f_x (t) = integral_0^oo e^(k/2) d t$$
multiline block:
$$f_x (t) = integral_0^oo e^(k/2) d t\ 
F'_x (t)=f_x (t)
$$
d-kaue commented 8 months ago

with your example I was able to reproduce it.

e.g. this is OK.

Lorem $Alpha$ ipsum.

Lorem $Beta$ ipsum.

but this will get an error.

Lorem $Alpha$ ipsum.
Lorem $Beta$ ipsum.

Obsidian recognises it as a single <p> element and insert a <br> line break. In this case obsidian-rtl creates a <div>.

with obsidian-rtl enabled with obsidian-rtl

with obsidian-rtl disabled without obsidian-rtl

obsidian-rtl separates each element inside the <p> element that contains a <br> line break and creates a <div> in each element. the typst compiler is probably losing the reference of the element it is rendering.

To be honest, I don't know anything about Right-to-Left. The content inside each inline maths have to change position?

fenjalien commented 8 months ago

I can reproduce this as well, it looks like at some point the custom HTML element that renders the Typst source code gets reset or recreated. The error is from the source string being undefined but still being read as a string. This shouldn't happen as it gets given the source code so I'm not entirely sure whats happening.

YonatanRubin commented 8 months ago

what do you mean by change position? if you mean change position in relation to the normal text then probably (since a line with n chars before the math block should position it either n pixels to the correct alignment). if you mean compared to itself then no, the math should be in the same direction orientation and position.

d-kaue commented 8 months ago

I can reproduce this as well, it looks like at some point the custom HTML element that renders the Typst source code gets reset or recreated. The error is from the source string being undefined but still being read as a string. This shouldn't happen as it gets given the source code so I'm not entirely sure whats happening.

I've been debugging for a while, and it seems to be a problem with the CustomElements lifecycle. obsidian-rtl modifies before draw, but after obsidian-typst creating the element.

In the TypstRenderElement class the draw method only has an idea of the fontSize and this.size because it is defined inside it, everything else that is passed in the .compile is undefined.

YonatanRubin commented 3 months ago

obsidian just added better rtl support, the obsidian rtl plugin no longer gives this error!