remarkjs / remark

markdown processor powered by plugins part of the @unifiedjs collective
https://remark.js.org
MIT License
7.66k stars 358 forks source link

Escaped pipes in input markdown confuses remark #929

Closed ahmedelgabri closed 2 years ago

ahmedelgabri commented 2 years ago

Initial checklist

Affected packages and versions

remark 14.0.2

Link to runnable example

https://codesandbox.io/s/red-dream-iqwrh?file=/src/index.js

Also, a repo is here https://github.com/ahmedelgabri/remark-issue

Steps to reproduce

  1. add this table to foo.md
| Name  | Type                   |
| :---- | :--------------------- |
| `foo` | `string` \| `string`[] |
  1. Process the file with remark without any plugins. this is the script that I'm using
#!/usr/bin/env node

import path from 'node:path'
import fs from 'node:fs'
import {remark} from 'remark'
import {visit} from 'unist-util-visit'

const OUT = './out'
const INPUT = './foo.md'

if (fs.existsSync(OUT)) {
  fs.rmSync(OUT, {recursive: true})
  fs.mkdirSync(OUT)
} else {
  fs.mkdirSync(OUT)
}

remark().process(fs.readFileSync(INPUT, 'utf8'), (err, newFile) => {
  if (err) throw err

  const newFilePath = path.resolve(OUT, INPUT)

  fs.writeFileSync(
    newFilePath,
    String(newFile),
    'utf8',
  )
})

Expected behavior

The table is returned as-is

| Name  | Type                   |
| :---- | :--------------------- |
| `foo` | `string` \| `string`[] |

Commonmark also renders this properly https://spec.commonmark.org/dingus/?text=%7C%20Name%20%7C%20Type%20%7C%0A%7C%20%3A------%20%7C%20%3A------%20%7C%0A%7C%20%60foo%60%20%7C%20%60string%60%20%5C%7C%20%60string%5B%5D%60%20%7C

Actual behavior

Notice that the escaped pipe is not escaped anymore & instead the opening square bracket is escaped.

| Name  | Type                   |
| :---- | :--------------------- |
| `foo` | `string` | `string`\[] |

And if you later format this with prettier it will lead to this.

| Name  | Type     |
| :---- | :------- | ----------- |
| `foo` | `string` | `string`\[] |

Runtime

Node v16

Package manager

yarn v1

OS

macOS

Build and bundle tools

Other (please specify in steps to reproduce)

wooorm commented 2 years ago

Tables are a GFM feature, not part of CommonMark. GFM is not supported by remark by default. This should work when you use remark-gfm

ahmedelgabri commented 2 years ago

@wooorm thanks this fixes the issue, but I wonder if the current behavior is wrong still. Shouldn't remark return the input as is? Because theoretically in my case I don't even need remark-gfm

github-actions[bot] commented 2 years ago

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.

wooorm commented 2 years ago

As "normal" markdown doesn't use pipes for any special meaning, it's superfluous, and superfluous things are dropped

wooorm commented 2 years ago

While GFM is common, "normal" markdown is also common. Similar to, say, escaped dollars. They could perhaps mean something when a math extension is used. But we'd need to know about that.

github-actions[bot] commented 2 years ago

Hi! Thanks for reaching out! Because we treat issues as our backlog, we close issues that are questions since they don’t represent a task to be completed.

See our support docs for how and where to ask questions.

Thanks, — bb

ahmedelgabri commented 2 years ago

Thanks @wooorm & enjoy your weekend & Happy Holidays :)

wooorm commented 2 years ago

Thanks Ahmed, you too, and stay safe :)