# Quiz Markdown Format

A quiz is a single Markdown file. The file is both a valid Markdown document (readable on
GitHub, in any editor) and the app's data source — no separate database or JSON export.

A file exercising every case below: [`quiz-example.md`](quiz-example.md).

## File structure

```
# <quiz title>            ← exactly one, first line
<!-- version: <version> -->  ← optional
<!-- date: <update date> --> ← optional

## <category title>       ← one or more

**<question>**            ← one or more per category
<!-- id: <question id> -->   ← optional
<!-- type: <type> -->        ← optional, default single

- <wrong option>
+ <correct option>
- <wrong option>

> <hint>                  ← optional
```

- Encoding is UTF-8; content is commonly non-Latin (Ukrainian).
- File name has no semantic meaning. The `# ` title is the display name of the quiz.
- Blank lines separate blocks. A blank line between the question and its options is
  required (otherwise Markdown renderers glue the list to the paragraph).

### Quiz title (`#`)

Exactly one level-1 heading, on the first line of the file. It names the whole set of
tests, e.g. `# Lorem Ipsum Quiz`.

### Quiz metadata

Two optional HTML comments in the comment block directly below the `#` title describe the file
itself:

```markdown
# Lorem Ipsum Quiz
<!-- version: 1.4.0 -->
<!-- date: 2016-12-07 17:38 -->

## Text questions
```

- `<!-- version: <value> -->`: free text identifying this revision of the file: a semver
  (`1.4.0`), a hash (`5d4187a`) or anything else. It is compared only for equality (a different
  value means the file changed), never ordered.
- `<!-- date: <value> -->`: when the file was last updated. Either a date, `YYYY-MM-DD`
  (`2016-12-11`), or a date and 24-hour time, `YYYY-MM-DD HH:MM` (`2016-12-07 17:38`), separated
  by one space. No timezone; the value is shown as written. Any other form is an error.
- Each appears at most once, in either order, on the lines right after the title, before the
  first `##` heading. Whitespace around the key and the value is ignored; the value is non-empty.
- Both are optional and independent. They never affect quiz, category or question ids.

### Category (`##`)

Level-2 headings split the quiz into subgroups, e.g. `## Text questions`. Every
question belongs to the nearest preceding `##`. A quiz with a single category still needs
one — questions never sit directly under the `#` title.

Deeper headings (`###` and below) are not part of the format.

## Question

A question is a paragraph wrapped in `**`, alone on its line:

```markdown
**Lorem ipsum dolor sit amet, consectetur adipiscing elit?**
```

The text between the markers may contain inline Markdown, including an image — an
image-only question is written as:

```markdown
**![Nemo enim ipsam](https://example.com/img/nemo-001.jpg)**
```

### Question id

An optional HTML comment on the line directly below the question overrides the generated
id:

```markdown
**What does "Eaque ipsa" look like?**
<!-- id: image-questions-eaque-002 -->
```

- Syntax: `<!-- id: <value> -->`. Whitespace around `id:` and the value is ignored.
- Ids must be unique within a file.
- When the comment is absent, the id is derived from the question title (see below).

Explicit ids matter when the same question text repeats within a quiz — a "what does X look
like?" question can be asked several times with different images, and each repetition needs
its own id. They also keep a question's identity (and any stored progress or statistics)
stable when its wording is edited.

### Hidden questions

A question whose correct answer isn't known yet can be kept in the file but hidden from the app
with an HTML comment in the comment block directly below the question:

```markdown
**Quis autem vel eum iure reprehenderit?**
<!-- id: lorem-ipsum-017 -->
<!-- hidden: no source confirms the answer -->

+ Sed quia non
- Numquam eius
- Modi tempora
```

- Syntax: `<!-- hidden: <reason> -->`. The reason is required and free text: it records why the
  question is hidden, so it can be found and resolved later. Whitespace around it is ignored.
- It sits in the comment block under the question line, before or after `<!-- id: … -->`, never
  elsewhere in the question's block.
- A hidden question is left out of tests, random draws, question counts and scores. On the
  answers list the app either leaves it out or shows it marked as hidden.
- A hidden question may have **no `+` option** (the answer is unknown) or keep a best-guess `+`.
  Ordering and matching questions always have a full order, and fill questions an answer for every
  blank; when hidden, it is the best guess.
  Every other rule still applies: it belongs to a `##` category, needs at least two options, and
  its id counts towards uniqueness.
- Deleting the line un-hides the question. Hiding and un-hiding never change the question's id,
  so its stored history survives.

Markdown viewers don't render HTML comments, so a hidden question looks like any other in the
rendered file. Find them with `grep -n '<!-- hidden' *.md`.

### Question type

An optional HTML comment in the same comment block says how the question is answered:

```markdown
**Put the steps of lorem ipsum in order**
<!-- id: lorem-ipsum-037 -->
<!-- type: ordering -->
```

- Syntax: `<!-- type: <value> -->`, at most once per question, anywhere in the comment block under
  the question line (next to `id`, `hidden`, `note`).
- Values (lowercase): `single` (the default; the comment may be omitted), `multiple`, `ordering`,
  `matching`, `fill`. Any other value is an error.
- The type decides how the option lines are read; see [Options](#options).
- Adding, changing or removing the type never changes the question's id.

The app explains the task itself (e.g. "Select all correct answers"), so files carry no hint
saying which type a question is.

### Generated ids

When no `<!-- id -->` comment is given, the id is a slug of the question title:

1. Strip Markdown markup (`**`, image/link syntax — an image contributes its alt text).
2. Transliterate Cyrillic to Latin (`в`→`v`, `щ`→`sch`, `є`→`ie`, …).
3. Lowercase.
4. Delete apostrophes (`'` and `’`) so they join the letters around them rather than splitting
   the word.
5. Replace every run of characters that is not an ASCII letter or digit with a single `-`.
6. Trim leading and trailing `-`.

`**Яка сільська садиба?**` → `yaka-sil-s-ka-sadiba`.

Ids are therefore always ASCII and safe in URLs and filenames. The transliteration table has no
entry for the soft sign, so `ь` falls through to step 5 and becomes a separator — that is where
the dash inside `sil-s-ka` comes from. The quirk is preserved deliberately: the table is
shared with the web version, and changing it would give every affected question a new id and
silently orphan its answer history.

Transliteration can also make two differently-spelled questions collide, which is another thing
explicit ids solve.

The same rule produces the quiz id from the `# ` title and the section id from each `## `
heading, so `# Тестова вікторина` → `testova-viktorina`.

## Options

Each option is a single line starting with `+ ` or `- `. What the markers mean depends on the
question type.

### Single choice (default)

`+ ` marks the correct option, `- ` a wrong one:

```markdown
+ Sed do eiusmod tempor
- Incididunt ut labore
- Dolore magna aliqua
- Ut enim ad minim
```

- Exactly one `+` per question.
- Two or more options; four is typical for text questions, three for image questions.
- Order in the file is the authoring order, not the presentation order; the app is free to
  shuffle. Don't rely on the correct answer's position.
- Both `-` and `+` are ordinary Markdown list markers, so options still render as bullet
  lists in any viewer. Note that switching markers starts a new list in Markdown, so the
  `+` line may render with extra spacing around it — cosmetic only.

### Multiple choice (`multiple`)

Same as single choice, but with **two or more** `+` options. The answer counts as correct only when
exactly the `+` options are selected.

```markdown
**Which of these are lorem ipsum words?**
<!-- type: multiple -->

+ Dolor
+ Amet
- Banana
+ Tempor
```

### Ordering (`ordering`)

One `- ` line per item, written **in the correct order**. No `+`: the order is the answer.

```markdown
**Put the steps of lorem ipsum in order**
<!-- type: ordering -->

- Lorem ipsum
- Dolor sit amet
- Consectetur adipiscing
- Sed do eiusmod
```

- Two or more items.
- The app shows the items shuffled (never already in the correct order) and the user drags them
  into order. The answer is correct only when the whole order matches.

### Matching (`matching`)

Two lists separated by one blank line. The first list (`- `) is the fixed column, the second (`+ `)
the movable one, written so that row *i* of the first list pairs with row *i* of the second:

```markdown
**Match each letter with its word**
<!-- type: matching -->

- L
- D
- S

+ Lorem
+ Dolor
+ Sit
```

- Both lists have the same length, two or more rows.
- Here `+` means "second column", not "correct". The different marker makes every Markdown viewer
  render two separate lists.
- The app keeps the first column in place and shows the second shuffled; the user reorders it so
  each row forms a pair. The answer is correct only when every pair is right.

### Fill in the blanks (`fill`)

The question text contains one or more **blanks**, each a run of three or more underscores (`___`).
One `+ ` line per blank gives its answer, **in the order the blanks appear**. There are no `-` lines:
nothing is wrong to choose from.

```markdown
**Lorem ipsum has ___ words. Each one is ___ letters long on average.**
<!-- type: fill -->

+ 5
+ 4,6 | 4.6
```

- A question with **no** `___` in its text has a single answer field after the question, and
  exactly one `+` line:

  ```markdown
  **What number does the dial show? ![550mw|Dial](assets/dial-001.png)**
  <!-- type: fill -->

  + 42 | 42°
  ```

- ` | ` (space, pipe, space) separates alternative accepted answers for one blank. The first one is
  the canonical answer, shown on answer lists and in exports.
- Checking: the typed text and each accepted answer are compared after trimming, collapsing runs
  of whitespace and lowercasing. Nothing else is normalised (`4,6` and `4.6` differ unless both
  are listed). The question counts as correct only when every blank is correct.
- In CommonMark a `___` with spaces around it is literal text, so the blanks show as gaps in any
  Markdown viewer, also inside `**…**`. Underscores inside image syntax don't count as blanks.

Items of ordering and matching questions are ordinary option text: they may contain inline
Markdown and images, and `;`, `—` or `→` have no special meaning in them.

## Hint

A question may carry one optional hint — a mnemonic, a rule of thumb, or a clarifying
picture. It is a Markdown blockquote, written after the options:

```markdown
**Quis nostrud exercitation ullamco laboris?**

- Nisi ut aliquip ex ea
+ Commodo consequat
- Duis aute irure dolor
- In reprehenderit in voluptate

> Ut enim ad minima veniam, quis nostrum.
```

- A hint is one or more lines beginning with `>`, and belongs to the nearest preceding
  question.
- Consecutive `>` lines **fold into a single paragraph**, joined with one space. This is what
  Markdown itself does with a multi-line blockquote, so the rendered file and the app agree.
- At most one hint per question: every `>` line inside a question's block folds into the same
  hint, however they are grouped.
- A bare `>` line carries no content — it is a spacer, as in Markdown.

Position inside the question's block is free: a hint may sit above the options, below them, or
directly after the comment block (but not between the two lists of a matching question). **Write it
after the options**, as the examples do —
that keeps the question and the list it introduces adjacent, which is how the file reads.

A hint is styled as an aside by every Markdown viewer, which is what makes the blockquote the
right marker: it is visibly not a question and not an option, with no syntax the reader has to
learn. In the app the hint is hidden behind a **Hint** button during a test — so it cannot spoil
the answer — and shown outright on the answers list, where nothing is hidden anyway.

### Hints with images

A hint takes the same inline image syntax, size tokens included (see below), and may be nothing
but an image:

```markdown
**Excepteur sint occaecat cupidatat non proident?**

+ Sunt in culpa
- Qui officia deserunt
- Mollit anim

> ![400mw|Itaque earum rerum](https://example.com/img/itaque-001.png)
```

Adding, editing or removing a hint never changes a question's id, so answer history survives it.

## Images

Images use Markdown image syntax with a pipe-delimited alt field:

```
![<token>|<token>|...](<url>)
```

Each token is either a **size token** or **display text**:

| Token   | Meaning     |
| ------- | ----------- |
| `100w`  | width       |
| `100h`  | height      |
| `100mw` | max-width   |
| `100mh` | max-height  |

A size token is a number followed by one of these suffixes; the number is in CSS pixels.
Any token that does not match becomes the alt / display text of the image. Token order is
free — `![550mw|Lorem](…)` and `![Lorem|550mw](…)` are equivalent. Multiple size
tokens may be combined (`![300w|200mh|Lorem](…)`); at most one non-size token is
expected.

With no pipe, the whole alt field is display text: `![Nemo enim ipsam](…)`.

Images are valid both as a question body and as an option, and text and image options may
be mixed in the same question:

```markdown
**What does "Eaque ipsa" look like?**
<!-- id: image-questions-eaque-002 -->

- ![550mw|Architecto beatae](https://example.com/img/architecto-002.jpg)
+ ![550mw|Eaque ipsa](https://example.com/img/eaque-002.jpg)
- Ab illo inventore
```

Urls may be absolute (`https://example.com/…`). Relative paths
are resolved by the app against wherever it serves quiz assets from.

## Validation rules

A file is well-formed when:

- The first non-empty line is a single `# ` heading, and there is only one in the file.
- Every question is preceded by a `##` heading.
- Every question line matches `**…**` with non-empty content.
- A `<!-- type: … -->` value is one of `single`, `multiple`, `ordering`, `matching`, `fill`.
- Options, by type:
  - single: at least two options, exactly one `+`;
  - multiple: at least two options, two or more of them `+`;
  - ordering: at least two `-` items and no `+`;
  - matching: a `-` list followed by a `+` list, separated by a blank line, of equal length (at
    least two rows);
  - fill: only `+` lines, non-empty, one per `___` blank in the question text (exactly one when the
    text has none).
- A hidden single or multiple question may have no `+` option.
- An `<!-- id: … -->`, `<!-- type: … -->` or `<!-- hidden: … -->` comment appears only in the
  comment block directly below a question line, at most once each per question.
- A `<!-- hidden: … -->` comment has a non-empty reason.
- A `<!-- version: … -->` or `<!-- date: … -->` comment appears only in the comment block directly
  below the `#` title, at most once each, with a non-empty value; a date is `YYYY-MM-DD` or
  `YYYY-MM-DD HH:MM`.
- A `>` hint line sits inside a question's block — never before the first question of a section.
- All question ids in the file — explicit and generated — are unique.

## Full example

```markdown
# Lorem Ipsum Quiz
<!-- version: 1.4.0 -->
<!-- date: 2016-12-07 17:38 -->

## Image questions

**What does "Eaque ipsa" look like?**
<!-- id: image-questions-eaque-001 -->

+ ![550mw|Eaque ipsa](https://example.com/img/eaque-001.jpg)
- ![550mw|Ab illo inventore](https://example.com/img/illo-001.jpg)
- ![550mw|Veritatis et quasi](https://example.com/img/veritatis-001.jpg)

## Text questions

**Quis nostrud exercitation ullamco laboris?**

- Nisi ut aliquip ex ea
+ Commodo consequat
- Duis aute irure dolor
- In reprehenderit in voluptate

> Ut enim ad minima veniam, quis nostrum.
```