rundel / md4r

An R wrapper for the md4c markdown parsing library
https://rundel.github.io/md4r/
Other
4 stars 1 forks source link

Single vs. multiple list items between headers #6

Closed krlmlr closed 6 months ago

krlmlr commented 6 months ago

I wonder what causes the difference in the roundtrip between the two examples.

library(md4r)

md1 <- "
# Header 1

- `my_fun()` replaces `my_other_fun()` (#123).

# Header 2
"

md2 <- "
# Header 1

- `my_fun()` replaces `my_other_fun()` (#123).

- `my_fun()` replaces `my_other_fun()` (#123).

# Header 2
"

writeLines(to_md(parse_md(md1)))
#> # Header 1
#>  - `my_fun()`
#>  replaces 
#> `my_other_fun()`
#>  (#123).
#> 
#> # Header 2
writeLines(to_md(parse_md(md2)))
#> # Header 1
#>  - `my_fun()` replaces `my_other_fun()` (#123).
#> 
#>  - `my_fun()` replaces `my_other_fun()` (#123).
#> 
#> # Header 2

Created on 2024-02-18 with reprex v2.1.0

rundel commented 6 months ago

For whatever reason it seems like the md1 example is missing a md_block_p around the bullet's text, while the md_block_p exists for both of the bullets in md2. I would guess that this is an upstream issue with md4c.

parse_md(md1)
#> md_block_doc [flags: "MD_DIALECT_COMMONMARK"]
#> ├── md_block_h [level: 1]
#> │   └── md_text_normal - "Header 1"
#> ├── md_block_ul [tight: 1, mark: "-"]
#> │   └── md_block_li
#> │       ├── md_span_code
#> │       │   └── md_text_code - "my_fun()"
#> │       ├── md_text_normal - " replaces "
#> │       ├── md_span_code
#> │       │   └── md_text_code - "my_other_fun()"
#> │       └── md_text_normal - " (#123)."
#> └── md_block_h [level: 1]
#>     └── md_text_normal - "Header 2"
parse_md(md2)
#> md_block_doc [flags: "MD_DIALECT_COMMONMARK"]
#> ├── md_block_h [level: 1]
#> │   └── md_text_normal - "Header 1"
#> ├── md_block_ul [tight: 0, mark: "-"]
#> │   ├── md_block_li
#> │   │   └── md_block_p
#> │   │       ├── md_span_code
#> │   │       │   └── md_text_code - "my_fun()"
#> │   │       ├── md_text_normal - " replaces "
#> │   │       ├── md_span_code
#> │   │       │   └── md_text_code - "my_other_fun()"
#> │   │       └── md_text_normal - " (#123)."
#> │   └── md_block_li
#> │       └── md_block_p
#> │           ├── md_span_code
#> │           │   └── md_text_code - "my_fun()"
#> │           ├── md_text_normal - " replaces "
#> │           ├── md_span_code
#> │           │   └── md_text_code - "my_other_fun()"
#> │           └── md_text_normal - " (#123)."
#> └── md_block_h [level: 1]
#>     └── md_text_normal - "Header 2"
rundel commented 6 months ago

In all fairness the code for assembling a md_block_li is a bit convoluted so there may be a way to fix it at that level as well.

krlmlr commented 6 months ago

I think the paragraph only gets added if there is space between two bullet points. Simpler reprex:

library(md4r)

md <- "
# Header 1

- `my_fun()` replaces `my_other_fun()` (#123).
- `my_fun()` replaces `my_other_fun()` (#123).
"

writeLines(to_md(parse_md(md)))
#> # Header 1
#> 
#> - `my_fun()`
#>  replaces 
#> `my_other_fun()`
#>  (#123).
#> - `my_fun()`
#>  replaces 
#> `my_other_fun()`
#>  (#123).

Created on 2024-03-03 with reprex v2.1.0

krlmlr commented 6 months ago

Even simpler:

library(md4r)

md <- "
- `my_fun()` replaces
- `my_fun()` replaces
"

writeLines(to_md(parse_md(md)))
#> - `my_fun()`
#>  replaces
#> - `my_fun()`
#>  replaces

Created on 2024-03-03 with reprex v2.1.0

krlmlr commented 6 months ago

Closing in favor of #8.