atom-community / markdown-preview-plus

Markdown Preview + Community Features
https://atom.io/packages/markdown-preview-plus
Other
370 stars 85 forks source link

Opening a preview causes ex-mode to malfunction #86

Closed chaserhkj closed 9 years ago

chaserhkj commented 9 years ago

Testing with sync preview feature, I encountered some wierd issues.

At /home/hkj/.atom/packages/ex-mode/lib/ex-state.coffee:90

TypeError: undefined is not a function
  at Ex.write (/home/hkj/.atom/packages/ex-mode/lib/ex.coffee:153:24)
  at Ex.w (/home/hkj/.atom/packages/ex-mode/lib/ex.coffee:174:5)
  at /home/hkj/.atom/packages/ex-mode/lib/ex.coffee:1:1
  at Command.execute (/home/hkj/.atom/packages/ex-mode/lib/command.coffee:153:7)
  at ExState.processOpStack (/home/hkj/.atom/packages/ex-mode/lib/ex-state.coffee:53:17)
  at ExState.pushOperations (/home/hkj/.atom/packages/ex-mode/lib/ex-state.coffee:43:6)
  at ExViewModel.ViewModel.confirm (/home/hkj/.atom/packages/ex-mode/lib/view-model.coffee:13:14)
  at ExViewModel.module.exports.ExViewModel.confirm (/home/hkj/.atom/packages/ex-mode/lib/ex-view-model.coffee:35:5)
  at ExViewModel.confirm (/home/hkj/.atom/packages/ex-mode/lib/ex-view-model.coffee:1:1)
  at ex-command-mode-input.ExCommandModeInputElement.confirm (/home/hkj/.atom/packages/ex-mode/lib/ex-normal-mode-input-element.coffee:46:16)
  at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (/usr/share/atom/resources/app.asar/src/command-registry.js:245:29)
  at /usr/share/atom/resources/app.asar/src/command-registry.js:3:61
  at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (/usr/share/atom/resources/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:524:16)
  at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (/usr/share/atom/resources/app.asar/node_modules/atom-keymap/lib/keymap-manager.js:347:22)
  at HTMLDocument.module.exports.WindowEventHandler.onKeydown (/usr/share/atom/resources/app.asar/src/window-event-handler.js:177:20)

Not only did ex-mode go wrong, vim-mode is not acting well after this, either. Things like dding a line would leave a d in the place of the deleted line and other wierd things would happen.

I am not sure about where this issue belong, but since it's triggered by this new feature in MPP, I'm submitting it here.

# Math Examples

## The Lorenz Equations

The Lorenz system is a system of ordinary differential equations (the Lorenz equations) first studied by Edward Lorenz. It is notable for having chaotic solutions for certain parameter values and initial conditions. In particular, the Lorenz attractor is a set of chaotic solutions of the Lorenz system which, when plotted, resemble a butterfly or figure eight.

$$\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}$$

## The Cauchy-Schwarz Inequality

In mathematics, the Cauchy–Schwarz inequality is a useful inequality encountered in many different settings, such as linear algebra, analysis, probability theory, and other areas. It is considered to be one of the most important inequalities in all of mathematics. It has a number of generalizations, among them Hölder's inequality.

The inequality for sums was published by Augustin-Louis Cauchy (1821), while the corresponding inequality for integrals was first proved by Viktor Bunyakovsky (1859). The modern proof of the integral inequality was given by Hermann Amandus Schwarz (1888).

$$\begin{equation*}
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
\end{equation*}$$

## A Cross Product Formula

In mathematics, the cross product or vector product is a binary operation on two vectors in three-dimensional space and is denoted by the symbol ×. The cross product a × b of two linearly independent vectors a and b is a vector that is perpendicular to both and therefore normal to the plane containing them. It has many applications in mathematics, physics, engineering, and computer programming.

$$\begin{equation*}
\mathbf{V}_1 \times \mathbf{V}_2 =  \begin{vmatrix}
\mathbf{i} & \mathbf{j} & \mathbf{k} \\
\frac{\partial X}{\partial u} &  \frac{\partial Y}{\partial u} & 0 \\
\frac{\partial X}{\partial v} &  \frac{\partial Y}{\partial v} & 0
\end{vmatrix}
\end{equation*}$$

## The probability of getting \(k\) heads when flipping \(n\) coins is

Probability is the measure of the likeliness that an event will occur. Probability is quantified as a number between 0 and 1 (where 0 indicates impossibility and 1 indicates certainty). The higher the probability of an event, the more certain we are that the event will occur. A simple example is the toss of a fair coin. Since the two outcomes are equally probable, the probability of "heads" equals the probability of "tails", so the probability is 1/2 (or 50%) chance of either "heads" or "tails".

$$\begin{equation*}
P(E)   = {n \choose k} p^k (1-p)^{ n-k}
\end{equation*}$$

## A Rogers-Ramanujan Identity

In mathematics, the Rogers–Ramanujan identities are two identities related to basic hypergeometric series, first discovered and proved by Leonard James Rogers (1894). They were subsequently rediscovered (without a proof) by Srinivasa Ramanujan some time before 1913. Ramanujan had no proof, but rediscovered Rogers's paper in 1917, and they then published a joint new proof (Rogers & Ramanujan 1919). Issai Schur (1917) independently rediscovered and proved the identities.

$$\begin{equation*}
1 + \frac{q^2}{(1-q)}+\frac{q^6}{(1-q)(1-q^2)}+\cdots =
\prod_{j=0}^{\infty}\frac{1}{(1-q^{5j+2})(1-q^{5j+3})},
\quad\quad \text{for $|q|<1$}.
\end{equation*}$$

## Maxwell's Equations

Maxwell's equations are a set of partial differential equations that, together with the Lorentz force law, form the foundation of classical electrodynamics, classical optics, and electric circuits. These fields in turn underlie modern electrical and communications technologies. Maxwell's equations describe how electric and magnetic fields are generated and altered by each other and by charges and currents. They are named after the Scottish physicist and mathematician James Clerk Maxwell, who published an early form of those equations between 1861 and 1862.

$$\begin{align}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\   \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}$$

## Inline Typesetting (Mixing Markdown and TeX)

While display equations look good for a page of samples, the ability to mix math and *formatted* **text** in a paragraph is also important.

This expression $\sqrt{3x-1}+(1+x)^2$ is an example of a TeX inline equation in a **[Markdown-formatted](http://daringfireball.net/projects/markdown/)** sentence.  

In the example above, I couldn't get anything beyond the first displayed math block synced through this feature. ( Only the 1st 2 headings and the 1st paragraph could be synced )

Galadirith commented 9 years ago

@chaserhkj Thanks for the post :D

On the first issue I can't see any errors that would relate to MPP. Does this problem only happen after syncing? and in which direction? Does ex-mode/vim-mode work as expected when you don't sync source/preview? I grab ex-mode and test it out myself. I should not that Atom v1.0.10 was released right around the same time as MPP v2.0.0 so it might be an Atom issue, but I havn't seen any issues on ex-mode or vim-mode pop up related to this so I would not conclude that until we're certain it's not MPP :D

On the second issue, the reason why sync is failing is actually not a bug but because with 2.0.0 we've migrated to markdown-it and to handle equations we are using markdown-it-math which uses a strickter syntax, so you need to place the $$ on separate lines. So this:

$$\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}$$

will no longer work, you should use this:

$$
\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}
$$

I hope that's not a deal breaker. It is very possible in an upcoming release we'll add support back in for the original style, but we don't have any date for that yet. But if that's some thing that is important to you then please let me know :D

I'll update you when I've look more in to the ex-mode issue :D

leipert commented 9 years ago

@chaserhkj @Galadirith

Regarding the math mode, I wrote several issues in markdown-it-math:

Galadirith commented 9 years ago

@chaserhkj Thanks so much for your patience :D I'm still investigating the ex-mode/vim-mode issue which I can repeat locally. Can I ask if you previously had MPP 1.7.0 installed and did it cause you an issue then?

Galadirith commented 9 years ago

Ok so for reference I have isolated the problem to a particular commit. Everything is working fine at https://github.com/Galadirith/markdown-preview-plus/commit/fa1015b9355d7db388dad13aef4756c176c06508 but not at https://github.com/Galadirith/markdown-preview-plus/commit/7697921502586256ef4c4f0ea210a08a7c6f17f6 after #70 was merged. Going back through the commits of #70 it turns out the issue was introduced by https://github.com/Galadirith/markdown-preview-plus/compare/fb4d3faf4a7c0461dc7758980139ad84cc7142b2...c0583b3b14adb74ab51503211e609687a2d52510.

So to summarise that, it appears that the problem was introduced when markdown-it was initially integrated. Its really peculiar as as far as I can tell right now ex-mode really shouldn't be worrying about this. But we've narrowed it down which is great :D

Galadirith commented 9 years ago

So far I am not able to reproduce any problems with vim-mode commands such as dd. For now I will just focus on investigating ex-mode but if @chaserhkj you do experience those problems what would be incredibly helpful is a minimal set of steps to reproduce the problem. Thanks @chaserhkj :D

Galadirith commented 9 years ago

Ok wow so this is going down a rather interesting path :D So I have now isolated the exact line that is causing a problem in ex-mode, lib/ex.coffee#L153. So it appears that the method Promise.defer() exists before opening a preview but doesn't after!? :D

Now it seems that ex-mode is using a depreciated style of Promise as described over at MDN. My best guess right now is that some module within markdown-it and its dependencies is overwriting the global Promise object. Very interesting :D

So still more work to do but we're getting ever closer still :D

leipert commented 9 years ago

WAITWAITWAIT.

leipert commented 9 years ago

Maybe it is the polyfills in https://github.com/runarberg/markdown-it-math ?

leipert commented 9 years ago

Nvm it maybe somewhere else :+1:

Galadirith commented 9 years ago

@leipert I think you were really close. I don't think its the polyfill used by markdown-it-math directly, but one of its dependencies ascii2mathml loads babel/polyfill which looks to be the culprit:

This will emulate a full ES6 environment

If I simply set var ascii2mathmml = null in markdown-it-math/index.js#L5 then that appears to cure the problem.

Ok great found the source :D lets think about how to resolve this.

leipert commented 9 years ago

I will write to the ascii2mathml maintainer.

Galadirith commented 9 years ago

Thanks @leipert :D

As a suggestion, it looks like @runarberg maintains both markdown-it-math and ascii2mathml. Maybe they'd be open to simply lazy loading ascii2mathml in markdown-it-math but maybe there is a better solution in the core of ascii2mathml.

leipert commented 9 years ago

@Galadirith I just opened a lazyload PR and wrote an issue in ascii2mathml.

If you have anything to add to those, go a head (your english is faster than mine ;) )

Galadirith commented 9 years ago

Thanks @leipert they look good to me so I would have nothing to add :D

(The disadvantage of [maybe] writing faster is that you end up writing crazy long comments like this, you are effortlessly succinct. The poor souls who have to read my comments :D)

Galadirith commented 9 years ago

Oh my goodness, @runarberg has already merged, wow, and checked locally ex-mode is working just fine :D So we'll need to make sure to bump the version of markdown-it-math to a 3.x.x compatible version. That was fun :D

@chaserhkj this fix will roll out on Sunday, thanks for your patience :D

leipert commented 9 years ago

@Galadirith #95 is on the way

Galadirith commented 9 years ago

@leipert awesome :D

runarberg commented 9 years ago

@Galadirith actually you need "markdown-it-math": "^3.0.1" because the .1 patch has the lazy loading.

leipert commented 9 years ago

Thanks, @runarberg. We did that here: https://github.com/Galadirith/markdown-preview-plus/commit/3d07a2a8d3f74bc9aa5626f60cead20864d6fa3c

chaserhkj commented 9 years ago

On atom 1.0.11 with MPP 2.0.1, confirming this is fixed.

Sorry for late reply and feedback, for I am not close enough to network yesterday, but thank all of you for the quick and fine work you've done! ^_^