Skip to content

Run examples in Markdown through Prettier #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gaearon opened this issue Nov 20, 2017 · 10 comments
Closed

Run examples in Markdown through Prettier #310

gaearon opened this issue Nov 20, 2017 · 10 comments

Comments

@gaearon
Copy link
Member

gaearon commented Nov 20, 2017

Not sure how to do it but I think I've seen some tweet saying you can run Markdown files through Prettier and it will format JS blocks.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 20, 2017

Kind of related to this, but, I was planning to write a little Gatsby Remark plug-in that let us directly embed examples from the examples dir. These examples (all files in this directory) could be run through Prettier automatically.

This won't solve all problems, since it's a bit too much overhead to create an example file for a couple of lines, but it would help with the larger ones.

@clemmy
Copy link
Contributor

clemmy commented Nov 20, 2017

@alexkrolick
Copy link
Collaborator

One thing I noticed is that some of the js code blocks have special tags like js{2, 7} - might want to watch out to make sure Prettier recognizes them.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 23, 2017

First step to what I described would be to get Prettier running on our example code, which I've done with PR #331.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 24, 2017

Regarding Prettier 1.8's ability to format Markdown files - it looks like it chokes on the ones we have here because of the "frontmatter" header blocks, eg:

content/warnings/unknown-prop.md
[error] content/warnings/unknown-prop.md: SyntaxError: Invalid left-hand side in assignment (1:3)
[error] > 1 | ---
[error] | ^^^^^^^^^^^^^^^^^^^^
[error] > 2 | title: Unknown Prop Warning
[error] | ^^^^^^
[error] 3 | layout: single
[error] 4 | permalink: warnings/unknown-prop.html
[error] 5 | ---
error Command failed with exit code 2.

@bvaughn
Copy link
Contributor

bvaughn commented Nov 24, 2017

Related PR #336

@bvaughn
Copy link
Contributor

bvaughn commented Nov 29, 2017

We might be able to draw some inspiration from this project in terms of running only the code blocks through Prettier. Not sure how straight forward this would be to do, given that multiple Gatsby plugins touch our code blocks. It might also complicate line-highlights since those are line-number based (at least until the Gatsby plugin supports the newer inline comment syntax).

@alexkrolick
Copy link
Collaborator

This seems to be working now even with frontmatter:

npx prettier --parser=markdown --write "content/docs/**/*.md"
content/docs/accessibility.md 249ms
content/docs/add-react-to-a-new-app.md 20ms
content/docs/add-react-to-an-existing-app.md 40ms
content/docs/addons-animation.md 159ms
content/docs/addons-create-fragment.md 26ms
...

The line-highlights are still problematic though, since the markdown parser doesn't recognize javascript{2-4} as a JS block (nor do editors...).

@ghost
Copy link

ghost commented Mar 16, 2019

@bvaughn and anybody reading this thread: prettier-markdown recommends to use prettier directly

Prettier now includes support for prettifying Markdown (including code blocks!), so as such, I'd recommend using the official Prettier instead. See the release notes for more information

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this issue Jul 1, 2020
@ashutosh887
Copy link

@bvaughn @gaearon Please close the Issue if resolved
if not, I would like to work on the issue

BetterZxx pushed a commit to BetterZxx/react.dev that referenced this issue Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants