maths / moodle-qtype_stack

Stack question type for Moodle
GNU General Public License v3.0
142 stars 149 forks source link

Markdown setting for question text format seems to be ignored #827

Open mkraska opened 2 years ago

mkraska commented 2 years ago

Hi, after switching to 4.4 I experience the following problem:

This is my question text. The whole question text is in Markdown format as set in the Marklar editor widget.

Gegeben: \\( a_1={@a1@}\, \mathrm{m/s^2} \\), \\( t_1={@t1@}\, \mathrm{s} \\), \\( t_2={@t2@}\, \mathrm{s} \\),

<p>Gegeben: \( a_1={@a1@}\, \mathrm{m/s^2} \), \( t_1={@t1@}\, \mathrm{s} \), \( t_2={@t2@}\, \mathrm{s} \),</p>

Geben Sie die folgenden gesuchten Werte als Zahlen mit Maßeinheit an, z.B. `100 km/h` :

\\( v_e= \\) [[input:S_ve]] [[validation:S_ve]] [[feedback:ve]]

\\( x_e= \\) [[input:S_xe]] [[validation:S_xe]] [[feedback:xe]]

image

Possibly the {@@} tag makes STACK think that the entire paragraph is html instead of markdown.

When explicitly tagging the line as html things get better.

I used this stuff a lot so if there is no fix I'd need to touch nearly every question in my courses (which I perhaps do anyway in order to make use of dynamic inclusion).

quiz-T-B5-MB-TM3-TM3 01 01 (l) a(t) example with units-20220803-2212.zip

aharjula commented 2 years ago

To me, in markdown, both \ and ( are things that need to be escaped, so the logic I wrote requires one to use \\\( if one wants to have STACK detect Math mode and act in the correct way. STACK does, however, note that markdown also has its HTML mode and acts in the traditional STACK way there.

mkraska commented 2 years ago

So it is by design that the behaviour in 4.4 is different from 4.3? I see two ways to fix such questions:

aharjula commented 2 years ago

Yes 4.3 did not support Markdown, 4.4 does but it expects one to be very clear with the syntax if one expects it to be able to detect math-mode shifts. By support, we now mean that we detect Markdown and Markdown HTML modes and math-mode shifts in them and apply escaping for injected values so that they function in those modes. All the way to the level of escaping needed for injections to function in Markdown tables. We also know not to apply Markdown escaping in places like [[jsxgraph]] blocks that happen to exist inside Markdown content, basically, the way injections behave is now tightly tied to the surroundings and we even allow switching the injection method by switching formatting at will within the text...

Some people used Markdown with 4.3, and it probably worked as long as the injected values were trivial and/or the injection sites were carefully chosen.

sangwinc commented 2 years ago

This is perhaps one area where the STACK docs could be updated to make clear to authors what is expected for each of HTML and markdown. (I also suspect we need more unit tests!).

aharjula commented 2 years ago

I am fairly certain that the castext2 branch had notes in the Authoring/CASText.md documentation about the rules of escaping and formats related to Markdown. Have to wonder why that file is now in master in the form it was back in 2020. In any case, it needs no more tests. The test that exists for markdown format already confirms that the escaping happens and that it goes through actual Markdown processing just as expected and that the math mode gets detected as expected.

Basically, what needs to be known by authors:

  1. If you do Markdown, remember that some things may need slashes and that there are many interesting characters that have interesting effects.
  2. Mixing LaTeX with Markdown is tricky as you have plenty of characters that may need to be escaped, e.g. is \( the start of math-mode or is it just the escaped (?
  3. In Markdown, one can also shift to the so-called HTML mode where one does not need those escapes, and things behave differently. This mode is pretty much equal to what STACK expected in the old times. Typically one shifts to HTML mode by placing an HTML tag at the start of the line.

Assuming that you declare through Moodle text-aread options or by using other means that your content is in Markdown format STACK will then figure out if some of it is in HTML mode and will escape values it injects accordingly. However, the normal way of STACK outputting \(...\)-wrappings around {@...@}-injections only when not in math mode already will need to be able to detect whether you are in math mode and it assumes that all the rules related to escaping are followed, and no exceptions (or relaxed rules) are in play i.e. \( won't start a math mode in its detection logic if we are in Markdown-formatted context. Both \ and ( are special in Markdown and both need to be escaped, so if you want STACK to detect that math mode shift you need to use triple-slashes \\\(.

Note that you do not see those extra slashes in the output, and sometimes some slashes can be omitted but they are important both for us to unambiguously detect things and for the Markdown processor to process the content after we have injected those values or expressions.