extern crate micromark;
use micromark::{micromark, micromark_with_options, Constructs, Options};
use pretty_assertions::assert_eq;
#[test]
fn gfm_table() -> Result<(), String> {
let gfm = Options {
constructs: Constructs::gfm(),
..Options::default()
};
assert_eq!(
micromark("| a |\n| - |\n| b |"),
"
| a |\n| - |\n| b |
",
"should ignore tables by default"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n| b |", &gfm)?,
"",
"should support tables"
);
assert_eq!(
micromark_with_options("| a |", &gfm)?,
"| a |
",
"should not support a table w/ the head row ending in an eof (1)"
);
assert_eq!(
micromark_with_options("| a", &gfm)?,
"| a
",
"should not support a table w/ the head row ending in an eof (2)"
);
assert_eq!(
micromark_with_options("a |", &gfm)?,
"a |
",
"should not support a table w/ the head row ending in an eof (3)"
);
assert_eq!(
micromark_with_options("| a |\n| - |", &gfm)?,
"",
"should support a table w/ a delimiter row ending in an eof (1)"
);
assert_eq!(
micromark_with_options("| a\n| -", &gfm)?,
"",
"should support a table w/ a delimiter row ending in an eof (2)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n| b |", &gfm)?,
"",
"should support a table w/ a body row ending in an eof (1)"
);
assert_eq!(
micromark_with_options("| a\n| -\n| b", &gfm)?,
"",
"should support a table w/ a body row ending in an eof (2)"
);
assert_eq!(
micromark_with_options("a|b\n-|-\nc|d", &gfm)?,
"\n\n\na | \nb | \n
\n\n\n\nc | \nd | \n
\n\n
",
"should support a table w/ a body row ending in an eof (3)"
);
assert_eq!(
micromark_with_options("| a \n| -\t\n| b | ", &gfm)?,
"",
"should support rows w/ trailing whitespace (1)"
);
assert_eq!(
micromark_with_options("| a | \n| - |", &gfm)?,
"",
"should support rows w/ trailing whitespace (2)"
);
assert_eq!(
micromark_with_options("| a |\n| - | ", &gfm)?,
"",
"should support rows w/ trailing whitespace (3)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n| b | ", &gfm)?,
"",
"should support rows w/ trailing whitespace (4)"
);
assert_eq!(
micromark_with_options("||a|\n|-|-|", &gfm)?,
"",
"should support empty first header cells"
);
assert_eq!(
micromark_with_options("|a||\n|-|-|", &gfm)?,
"",
"should support empty last header cells"
);
assert_eq!(
micromark_with_options("a||b\n-|-|-", &gfm)?,
"",
"should support empty header cells"
);
assert_eq!(
micromark_with_options("|a|b|\n|-|-|\n||c|", &gfm)?,
"\n\n\na | \nb | \n
\n\n\n\n | \nc | \n
\n\n
",
"should support empty first body cells"
);
assert_eq!(
micromark_with_options("|a|b|\n|-|-|\n|c||", &gfm)?,
"\n\n\na | \nb | \n
\n\n\n\nc | \n | \n
\n\n
",
"should support empty last body cells"
);
assert_eq!(
micromark_with_options("a|b|c\n-|-|-\nd||e", &gfm)?,
"\n\n\na | \nb | \nc | \n
\n\n\n\nd | \n | \ne | \n
\n\n
",
"should support empty body cells"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n- b", &gfm)?,
"\n",
"should support a list after a table"
);
assert_eq!(
micromark_with_options("> | a |\n| - |", &gfm)?,
"\n| a |\n| - |
\n
",
"should not support a lazy delimiter row (1)"
);
assert_eq!(
micromark_with_options("> a\n> | b |\n| - |", &gfm)?,
"\na\n| b |\n| - |
\n
",
"should not support a lazy delimiter row (2)"
);
assert_eq!(
micromark_with_options("| a |\n> | - |", &gfm)?,
"| a |
\n\n| - |
\n
",
"should not support a piercing delimiter row"
);
assert_eq!(
micromark_with_options("> a\n> | b |\n|-", &gfm)?,
"\na\n| b |\n|-
\n
",
"should not support a lazy body row (2)"
);
assert_eq!(
micromark_with_options("> | a |\n> | - |\n| b |", &gfm)?,
"\n\n
\n| b |
",
"should not support a lazy body row (1)"
);
assert_eq!(
micromark_with_options("> a\n> | b |\n> | - |\n| c |", &gfm)?,
"\na
\n\n
\n| c |
",
"should not support a lazy body row (2)"
);
assert_eq!(
micromark_with_options("> | A |\n> | - |\n> | 1 |\n| 2 |", &gfm)?,
"\n\n
\n| 2 |
",
"should not support a lazy body row (3)"
);
assert_eq!(
micromark_with_options(" - d\n - e", &gfm)?,
micromark(" - d\n - e"),
"should not change how lists and lazyness work"
);
assert_eq!(
micromark_with_options("| a |\n | - |", &gfm)?,
"",
"should form a table if the delimiter row is indented w/ 3 spaces"
);
assert_eq!(
micromark_with_options("| a |\n | - |", &gfm)?,
"| a |\n| - |
",
"should not form a table if the delimiter row is indented w/ 4 spaces"
);
assert_eq!(
micromark_with_options("| a |\n | - |", &Options {
constructs: Constructs {
code_indented: false,
..Constructs::gfm()
},
..Options::default()
})?,
"",
"should form a table if the delimiter row is indented w/ 4 spaces and indented code is turned off"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n> block quote?", &gfm)?,
"\n\nblock quote?
\n
",
"should be interrupted by a block quote"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n>", &gfm)?,
"\n\n
",
"should be interrupted by a block quote (empty)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n- list?", &gfm)?,
"\n",
"should be interrupted by a list"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n-", &gfm)?,
"\n",
"should be interrupted by a list (empty)"
);
assert_eq!(
micromark_with_options(
"| a |\n| - |\n",
&Options {
allow_dangerous_html: true,
constructs: Constructs::gfm(),
..Options::default()
}
)?,
"\n",
"should be interrupted by HTML (flow)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n\tcode?", &Options {
allow_dangerous_html: true,
constructs: Constructs::gfm(),
..Options::default()
})?,
"\ncode?\n
",
"should be interrupted by code (indented)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n```js\ncode?", &Options {
allow_dangerous_html: true,
constructs: Constructs::gfm(),
..Options::default()
})?,
"\ncode?\n
\n",
"should be interrupted by code (fenced)"
);
assert_eq!(
micromark_with_options(
"| a |\n| - |\n***",
&Options {
allow_dangerous_html: true,
constructs: Constructs::gfm(),
..Options::default()
}
)?,
"\n
",
"should be interrupted by a thematic break"
);
assert_eq!(
micromark_with_options("| a |\n| - |\n# heading?", &gfm)?,
"\nheading?
",
"should be interrupted by a heading (ATX)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\nheading\n=", &gfm)?,
"\n\n\na | \n
\n\n\n\nheading | \n
\n\n= | \n
\n\n
",
"should *not* be interrupted by a heading (setext)"
);
assert_eq!(
micromark_with_options("| a |\n| - |\nheading\n---", &gfm)?,
"\n\n\na | \n
\n\n\n\nheading | \n
\n\n
\n
",
"should *not* be interrupted by a heading (setext), but interrupt if the underline is also a thematic break"
);
assert_eq!(
micromark_with_options("| a |\n| - |\nheading\n-", &gfm)?,
"\n\n\na | \n
\n\n\n\nheading | \n
\n\n
\n",
"should *not* be interrupted by a heading (setext), but interrupt if the underline is also an empty list item bullet"
);
assert_eq!(
micromark_with_options("a\nb\n-:", &gfm)?,
"a
\n",
"should support a single head row"
);
assert_eq!(
micromark_with_options("> | a |\n> | - |", &gfm)?,
"\n\n
",
"should support a table in a container"
);
assert_eq!(
micromark_with_options("> | a |\n| - |", &gfm)?,
"\n| a |\n| - |
\n
",
"should not support a lazy delimiter row if the head row is in a container"
);
assert_eq!(
micromark_with_options("| a |\n> | - |", &gfm)?,
"| a |
\n\n| - |
\n
",
"should not support a “piercing” container for the delimiter row, if the head row was not in that container"
);
assert_eq!(
micromark_with_options("> | a |\n> | - |\n| c |", &gfm)?,
"\n\n
\n| c |
",
"should not support a lazy body row if the head row and delimiter row are in a container"
);
assert_eq!(
micromark_with_options("> | a |\n| - |\n> | c |", &gfm)?,
"\n| a |\n| - |\n| c |
\n
",
"should not support a lazy delimiter row if the head row and a further body row are in a container"
);
assert_eq!(
micromark_with_options(
r###"# Align
## An empty initial cell
| | a|c|
|--|:----:|:---|
|a|b|c|
|a|b|c|
## Missing alignment characters
| a | b | c |
| |---|---|
| d | e | f |
* * *
| a | b | c |
|---|---| |
| d | e | f |
## Incorrect characters
| a | b | c |
|---|-*-|---|
| d | e | f |
## Two alignments
|a|
|::|
|a|
|:-:|
## Two at the start or end
|a|
|::-|
|a|
|-::|
## In the middle
|a|
|-:-|
## A space in the middle
|a|
|- -|
## No pipe
a
:-:
a
:-
a
-:
## A single colon
|a|
|:|
a
:
## Alignment on empty cells
| a | b | c | d | e |
| - | - | :- | -: | :-: |
| f |
"###,
&gfm
)?,
r###"Align
An empty initial cell
Missing alignment characters
| a | b | c |
| |---|---|
| d | e | f |
| a | b | c |
|---|---| |
| d | e | f |
Incorrect characters
| a | b | c |
|---|-*-|---|
| d | e | f |
Two alignments
|a|
|::|
Two at the start or end
|a|
|::-|
|a|
|-::|
In the middle
|a|
|-:-|
A space in the middle
|a|
|- -|
No pipe
A single colon
|a|
|:|
a
:
Alignment on empty cells
"###,
"should match alignment like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Tables
| a | b | c |
| - | - | - |
| d | e | f |
## No body
| a | b | c |
| - | - | - |
## One column
| a |
| - |
| b |
"###,
&gfm
)?,
r###"Tables
No body
One column
"###,
"should match basic like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Tables in things
## In lists
* Unordered:
| A | B |
| - | - |
| 1 | 2 |
1. Ordered:
| A | B |
| - | - |
| 1 | 2 |
* Lazy?
| A | B |
| - | - |
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
## In block quotes
> W/ space:
> | A | B |
> | - | - |
> | 1 | 2 |
>W/o space:
>| A | B |
>| - | - |
>| 1 | 2 |
> Lazy?
> | A | B |
> | - | - |
> | 1 | 2 |
>| 3 | 4 |
| 5 | 6 |
### List interrupting delimiters
a |
- |
a
-|
a
|-
"###,
&gfm
)?,
r###"Tables in things
In lists
-
Ordered:
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
In block quotes
W/ space:
W/o space:
Lazy?
| 5 | 6 |
List interrupting delimiters
a |
"###,
"should match containers like GitHub"
);
assert_eq!(
micromark_with_options(
r###"| a |
| - |
| - |
| 1 |
"###,
&gfm
)?,
r###"
"###,
"should match a double delimiter row like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Examples from GFM
## A
| foo | bar |
| --- | --- |
| baz | bim |
## B
| abc | defghi |
:-: | -----------:
bar | baz
## C
| f\|oo |
| ------ |
| b `\|` az |
| b **\|** im |
## D
| abc | def |
| --- | --- |
| bar | baz |
> bar
## E
| abc | def |
| --- | --- |
| bar | baz |
bar
bar
## F
| abc | def |
| --- |
| bar |
## G
| abc | def |
| --- | --- |
| bar |
| bar | baz | boo |
## H
| abc | def |
| --- | --- |
"###,
&gfm
)?,
r###"Examples from GFM
A
B
C
D
bar
E
bar
F
| abc | def |
| --- |
| bar |
G
H
"###,
"should match examples from the GFM spec like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Grave accents
## Grave accent in cell
| A | B |
|--------------|---|
| ` | C |
## Escaped grave accent in “inline code” in cell
| A |
|-----|
| `\` |
## “Empty” inline code
| 1 | 2 | 3 |
|---|------|----|
| a | `` | |
| b | `` | `` |
| c | ` | ` |
| d | `|` |
| e | `\|` | |
| f | \| | |
## Escaped pipes in code in cells
| `\|\\` |
| --- |
| `\|\\` |
`\|\\`
"###,
&Options {
allow_dangerous_html: true,
..gfm.clone()
}
)?,
r###"Grave accents
Grave accent in cell
Escaped grave accent in “inline code” in cell
“Empty” inline code
1 |
2 |
3 |
a |
`` |
|
b |
`` |
`` |
c |
` |
` |
d |
` |
` |
e |
| |
|
f |
| |
|
Escaped pipes in code in cells
\|\\
"###,
"should match grave accent like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Code
## Indented delimiter row
a
|-
a
|-
## Indented body
| a |
| - |
| C |
| D |
| E |
"###,
&gfm
)?,
r###"Code
Indented delimiter row
a
|-
Indented body
| E |
"###,
"should match indent like GitHub"
);
assert_eq!(
micromark_with_options(
r###"## Blank line
a
:-
b
c
## Block quote
a
:-
b
> c
## Code (fenced)
a
:-
b
```
c
```
## Code (indented)
a
:-
b
c
## Definition
a
:-
b
[c]: d
## Heading (atx)
a
:-
b
# c
## Heading (setext) (rank 1)
a
:-
b
==
c
## Heading (setext) (rank 2)
a
:-
b
--
c
## HTML (flow, kind 1: raw)
a
:-
b
a
## HTML (flow, kind 2: comment)
a
:-
b
## HTML (flow, kind 3: instruction)
a
:-
b
c ?>
## HTML (flow, kind 4: declaration)
a
:-
b
## HTML (flow, kind 5: cdata)
a
:-
b
## HTML (flow, kind 6: basic)
a
:-
b
## HTML (flow, kind 7: complete)
a
:-
b
## List (ordered, 1)
a
:-
b
1. c
## List (ordered, other)
a
:-
b
2. c
## List (unordered)
a
:-
b
* c
## List (unordered, blank)
a
:-
b
*
c
## List (unordered, blank start)
a
:-
b
*
c
## Thematic break
a
:-
b
***
"###,
&Options {
allow_dangerous_html: true,
..gfm.clone()
}
)?,
r###"Blank line
c
Block quote
c
Code (fenced)
c
Code (indented)
c
Definition
Heading (atx)
c
Heading (setext) (rank 1)
Heading (setext) (rank 2)
HTML (flow, kind 1: raw)
a
HTML (flow, kind 2: comment)
HTML (flow, kind 3: instruction)
c ?>
HTML (flow, kind 4: declaration)
HTML (flow, kind 5: cdata)
HTML (flow, kind 6: basic)
HTML (flow, kind 7: complete)
List (ordered, 1)
- c
List (ordered, other)
- c
List (unordered)
List (unordered, blank)
c
List (unordered, blank start)
Thematic break
"###,
"should match interrupt like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Loose
## Loose
Header 1 | Header 2
-------- | --------
Cell 1 | Cell 2
Cell 3 | Cell 4
## One “column”, loose
a
-
b
## No pipe in first row
a
| - |
"###,
&gfm
)?,
r###"Loose
Loose
Header 1 |
Header 2 |
Cell 1 |
Cell 2 |
Cell 3 |
Cell 4 |
One “column”, loose
a
b
No pipe in first row
"###,
"should match loose tables like GitHub"
);
assert_eq!(
micromark_with_options(
r###"# Some more escapes
| Head |
| ------------- |
| A | Alpha |
| B \| Bravo |
| C \\| Charlie |
| D \\\| Delta |
| E \\\\| Echo |
Note: GH has a bug where in case C and E, the escaped escape is treated as a
normal escape: .
"###,
&gfm
)?,
r###"Some more escapes
Head |
A |
B | Bravo |
C \ |
D \| Delta |
E \\ |
Note: GH has a bug where in case C and E, the escaped escape is treated as a
normal escape: https://github.com/github/cmark-gfm/issues/277.
"###,
"should match loose escapes like GitHub"
);
Ok(())
}