-
Notifications
You must be signed in to change notification settings - Fork 143
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
Enforce consistent formatting for Markdown docs #231
Comments
Hi @lambda-fairy it's mdformat author here! It seems you have a bit of rust code in the Markdown docs. FYI, if you consider mdformat, it's very simple to create a plugin to run I can do this for you in like 30 mins if you find it useful (it's basically a copy paste of the Go plugin) |
Hi @hukkinj1! No rush, but that would be great! I think mdformat is my leading option at the moment, as the opinionated approach + semantic line breaks matches what I'm looking for. We probably want to call |
Hey, actually already did the plugin 😄 pip install mdformat-rustfmt I did some comparison of prettier and mdformat output using this repository as input. The intention was for me to learn if there's ways in which mdformat could improve. You'll find the learnings here executablebooks/mdformat#110. Maybe this makes choosing the right tool easier for you? Unfortunately points 5 and 6 from executablebooks/mdformat#110 hit this repo quite hard, and CHANGELOG.md is uglified quite a bit. You may want to consider excluding that file or choosing prettier until those issues are resolved. EDIT: Point 5 is now fixed in mdformat 0.5.5. So the only ugly bit remaining is point 6 (i.e. square bracket escapes). |
Thanks 😄 The changelog isn't a priority for me, so I'm happy with excluding it for now. My greater concern (which I realized just now) is with the "hidden setup code" feature. I want to run the code snippets with doctest (#257), but most of the snippets need extra setup code (e.g. imports) that I don't want to show in the rendered documentation. The standard solution in Rust is to prefix the setup lines with I looked into how rustfmt handles code snippets inside doc comments. (Rust doc comments are Markdown, so whatever approach is taken there should transfer to mdformat.) It looks like they comment out the setup code, format the snippet, then uncomment it back. That should be easy enough to reimplement ourselves. |
If I understood correctly, I think something like this would do what you describe: https://github.com/hukkinj1/mdformat-rustfmt/pull/1/files This doesn't seem to change formatting of https://github.com/lambda-fairy/maud docs however. Maybe you'd be able to provide a rust snippet for a test case (input and expected output)? |
Yep, that's what I had in mind, thanks!
Sure! I think Maud actually isn't such a good test case, as most of the examples are macro calls which rustfmt ignores 😛 Here's a test input: # let x=a();b();c();
let y=d();e();f(); Output: # let x=a();b();c();
let y = d();
e();
f(); I also found another potential blocker. By default, rustfmt parses its input in item scope, i.e. as a complete module. But most documentation examples are in statement scope, i.e. only make sense inside of a function, so are rejected by rustfmt. For example, this doesn't work:
But this works:
This is by design, as mentioned in the readme:
I'm not sure how best to solve this. We might need to propose a change to rustfmt... |
Thanks this works perfectly (when wrapped in a
The first thing that comes to my mind is a similar hack that we did with the setup code. That is, do something like:
I haven't tested this. What do you think, could this work? I'm afraid this might be destructive in some weird cases. 😄 Can you think of any? |
Yeah, I considered that as well, but unfortunately you're right about the weird cases...
So while this wrapping could work in a pinch, I think long term we'll need changes in rustfmt to do this properly. I'm not familiar with the rustfmt roadmap, though, so I don't know what such a change will look like 😅 |
Ah I see, damn. The first two (indentation related) problems might not be that bad. Even if completely ignored, it just means that the target width is 4 chars narrower. If the default is something like 80 then the plugin would use 76 for statement scope code blocks. That is IMHO not very significant at all, and the plugin (and the ability to format statement scope code) might still be net positive like 99.9% of the time. The raw string issue is the kind of case that I was more afraid of. I'd hate to be changing abstract syntax in any way. |
I like the effort you're making here and hope you don't mind I've made a PR on mdformat-rustfmt hukkin/mdformat-rustfmt#3 to get some stuff working needed to use it on the amethyst docs. I may tackle wrapping the code in |
so I think just increasing |
Hi @ezpuzz and thanks for the PR! Would be great if @lambda-fairy had the time to have a quick look at it as I'm not well-versed in many things Rust. Already wrote down some thoughts there though.
Sounds great!
IIRC The indentation problem is the lesser concern to me though. I'm more worried about the raw string issue raised by @lambda-fairy . I think a fairly simple way to tackle that is to make a regex to detect a multiline raw string, e.g. import re
RE_RUST_MULTILINE_RAW_STRING = re.compile(r'r#*".*\n.*"', flags=re.DOTALL) and if the regex finds any matches, just raise an exception (in which case mdformat doesn't apply formatting to the code block). In the odd case this happens, I feel its better to not do anything than to apply potentially AST breaking changes. Edit: Btw I made an issue about statement scope code formatting. We might want to continue there to not completely hijack @lambda-fairy's issue 😄 Edit 2: |
mdformat will preserve semantic line breaks, so we may as well commit to using them. cc #231
mdformat will preserve semantic line breaks, so we may as well commit to using them. cc #231
I don't think they add much, and they conflict with mdformat which escapes square brackets (hukkin/mdformat#112). cc #231
I don't think they add much, and they conflict with mdformat which escapes square brackets (hukkin/mdformat#112). cc #231
https://prettier.io/docs/en/install.html
or https://pypi.org/project/mdformat/
The text was updated successfully, but these errors were encountered: