Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New renderer fails in preview mode sometimes #12

Closed
OverHash opened this issue Jul 27, 2023 · 8 comments
Closed

New renderer fails in preview mode sometimes #12

OverHash opened this issue Jul 27, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@OverHash
Copy link

OverHash commented Jul 27, 2023

Just updated from the previous version of this plugin and it appears to fail to render some typst blocks in preview mode:
image

In edit mode (working correctly):

image

The raw source:
image

I can't seem to find a solid repro for this issue, but it appears to happen quite consistently in one vault.
The full error is

Uncaught ["unknown variable: NaNpt"]

Strangely, modifying the source typst block which fails to render, toggling to preview, and changing back to the original source will allow for it to render correctly (for a while).. Is this a cache issue somehow?

I notice that for a given page, the content at the top is much more likely to render successfully than the content at the bottom. It seems to be arbitrary where the content will fail to render -- so seems like there is a race condition almost going on somewhere.

@OverHash
Copy link
Author

Perhaps it's something to do with this section of code..

obsidian-typst/main.ts

Lines 209 to 219 in f01148c

async processThenCompileTypst(path: string, source: string, size: number, display: boolean, fontSize: number) {
const dpr = window.devicePixelRatio;
const pxToPt = (px: number) => (px * dpr * (72 / 96)).toString() + "pt"
const sizing = `#let (WIDTH, HEIGHT, SIZE) = (${display ? pxToPt(size) : "auto"}, ${!display ? pxToPt(size) : "auto"}, ${pxToPt(fontSize)})`
return this.compileToTypst(
path,
`${sizing}\n${this.settings.preamable.shared}\n${source}`,
size,
display
)
}

Specifically,

const pxToPt = (px: number) => (px * dpr * (72 / 96)).toString() + "pt" 

@fenjalien fenjalien added the bug Something isn't working label Jul 27, 2023
@fenjalien
Copy link
Owner

I can't seem to reproduce it from the source you've given, but as you've said it appears to be rare. If you see it happen again can you open the console (Ctrl+shift+I) show me the logs/erros in there?

Its not pxToPt's fault its either size or fontSize being 0 or NaN which shouldn't necessarily be possible...

Have you also modified the preambles in anyway?

@OverHash
Copy link
Author

I can reproduce this in a new plain vault with Typst Renderer installed and the math override option turned on.

From the developer console:
image

Uncaught ["unknown variable: NaNpt"]     plugin:typst:89 
> eval @ plugin:typst:89

Uncaught ["unknown variable: NaNpt"]     1eb76e4a-8651-4f2d-a8bf-c5b19ad17424:1

It looks like the Typst renderer gets particularly tripped up after it successfully embeds another note:

# Asymptotic Notation Rules
- We can **drop constants**: If $c > 0$ then $c f$ is $Theta(f)$.

![Big o Types](big-o-types)

# Asymptotic Notation Rules
- We can **drop constants**: If $c > 0$ then $c f$ is $Theta(f)$.

In this example, the first bullet point will render successfully, but the latter will not. I need to have a decent amount of notes before and after this section of my note file before the Typst renderer will fail.

Let me know how you would like me to transmit you the obsidian repro of this (I'd rather not share it publicly on GitHub) -- e.g., through a GPG public key I can sign to or if you're on the Typst Discord.

@OverHash
Copy link
Author

OverHash commented Jul 27, 2023

Just added a debug statement in the piece of the code that I referenced in my comment -- the output after the embedded note part of my note is as expected:

path="/586f8912-f3a8-4455-8a4a-3729469c2cc1.typ"

source="#set page(margin: 0pt)
#set align(horizon)
$c > 0$"

size="NaN"
display="false"
fontSize="NaN"
pxToPtSize="NaNpt"
pxToPtFontSize="NaNpt"

So looks like size and fontSize is NaN sometimes.

It looks like

let fontSize = parseFloat(this.getCssPropertyValue("--font-text-size"));

is failing (i.e., this is where fontSize is getting set to NaN).

@fenjalien
Copy link
Owner

I don't know why fontSize or size is sometimes NaN for you. It normally means that the element has been detached from the document in which case it shouldn't be able to render. Do you have any other plugins that might be causing this?

@OverHash
Copy link
Author

Just sent the repro files in Discord.

@fenjalien
Copy link
Owner

I could reproduce the error with the supplied reproduction files, as of 05b1a9a the error no longer occurs so this is fixed :)

@OverHash
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants