goessner / markdown-it-texmath

Support TeX math equations with your Markdown documents.
MIT License
156 stars 28 forks source link

Math blocks should work even when not preceded by empty line #16

Closed tomtung closed 4 years ago

tomtung commented 5 years ago

Latex blocks only work when they are preceded by an empty line. This behavior is not consistent with other blocks (e.g. code, table, etc.).

For example, the first math block in the following Markdown document won't render:

Code block works:
``` js
var foo = function (bar) {
  return bar++;
};

console.log(foo(5));
Table works: Option Description
data path to data files to supply the data that will be passed into templates.
engine engine to be used for processing templates. Handlebars is the default.
ext extension to be used for dest files.

Math block doesn't work: $$\frac{1}{2}$$

Unless an empty line precedes it:

$$\frac{1}{2}$$

goessner commented 5 years ago

You are right at a first glance, but ...

code blocks and tables are intrinsic markdown elements and recognized per se as block elements by the markdown parser (here markdown-it).

$$ is a custom element and treated as inline text by default (simple linebreaks are ignored as you might know). So in order

we need an empty line before block formulas signalling the beginning of a new paragraph to the parser.

This is in fact somewhat inconsistent compared to intrinsic block elements. I am simply not aware of a working consistent solution. If you might be, please tell me.

Thanks

alfazaz commented 4 years ago

@goessner Hello !

Sorry for this little "necromancy"... but I would like to adapt markdown-it-texmath used on my nodebb forum to have display mode working also without an empty line before the starting $$...

You say that

$$ is a custom element and treated as inline text by default (simple linebreaks are ignored as you might know)

but, in the markdown plugin in nodebb, there is an option to "Treat newlines as single line breaks" and it works...

Can you give me some guidelines to adapt your texmath.js ? For example, the string processed by applyRule contains "text\n$$formula$$" and it should give text followed by the formula in display mode. I tried things but without success (markdown-it background is obscure for me but I have time to work on it with the... covid-19 :-/ )...