idyll
idyll copied to clipboard
Add equation syntax
It would be great to write $...$
or $$...$$
instead of [equation]...[/equation]
[equation display:true]...[/equation]
Don't know enough about lexing + parsing to understand what is considered the cleaner approach / best practice. Let's say we have both of these inputs:
[Equation]
y = \int x^2 dx
[/Equation]
$$
y = \int x^2 dx
$$
Is it better practice to:
- Handle this at lexer level, i.e. in lexer.js so that they both output identical tokens and are indistinguishable for parser.
- Handle this at parser level. So maybe lexer converts "$$" into some new token, let's say EQUATION, that we handle with equation token specific definition in grammar.ne, kind of like how there are currently defintions for ImageInline and LinkInline, so that the final ast is indistinguishable for both.
- Handle at some other level I don't know about
@hemu it would be best to add an additional rule in the lexer, similar for how we special case equation tags here: https://github.com/idyll-lang/idyll/blob/master/packages/idyll-compiler/src/lexer.js#L53-L66.
The regex is a little hairy, but the same logic should be applicable to the new $$
tokens.
@mathisonian I've started working on this here https://github.com/idyll-lang/idyll/pull/573, tests pass but so far haven't succeeded in testing this manually inside new idyll project/document - adding $$y=0$$
somewhere in text does nothing.
Edit: found the problem, it was regex.
Wouldn't [$...$]
and [$$...$$]
be better instead of $...$
and $$...$$
? That way it works beautifully :) but it's just me battling with the regex :D.
Inline equation [$ y = x^{\frac{\pi}{2}} $], or centered: [$$ y = 0 $$]
Or with some whitespaces for nicer formatting:
[$$
y = \int x^2 dx
$$]
Thanks for the work on this @michaltakac! Also saw your comment on #314, I think that would be awesome to add - we should discuss syntax for that as well. I'm totally in favor of getting changes in to improve the equation/latex experience, but it will probably will be a couple days before I can do a close review.
Edit: found the problem, it was regex.
A tale as old as time.
Arguments for [$$...$$]
vs [$$]... [/$$]
to keep things more consistent with the existing syntax?
Feeling that [$]...[/$]
and [$$]...[/$$]
would suit Idyll better - basically it would be a syntactic sugar for [equation /]
.
[$ $]
is easier to type than [$][/$]
. Both are surely better than $ $
, because it won't accidentally parse something as an equation.
Note that there's also other latex syntax: \( \)
for inline equations and \[ \]
for block equations.