sqlfmt icon indicating copy to clipboard operation
sqlfmt copied to clipboard

feat: Support parsing of SQL code blocks in Markdown files

Open michael-the1 opened this issue 8 months ago • 5 comments

Closes https://github.com/tconbeer/sqlfmt/issues/593

There are two caveats that would be good to consider:

  1. Because we parse to AST -> rebuild from AST, we can actually lose some trivial whitespace characters. mistletoe keeps all the important whitespace, but can toss out some unnecessary whitespace, such as trailing whitespace or double newlines. A good example is in the README.md of this very repo:
 ~/sqlfmt
❯ poetry run sqlfmt README.md
1 file formatted.
0 files left unchanged.
README.md formatted.

 ~/sqlfmt
❯ git diff
diff --git a/README.md b/README.md
index 7d292f9..5a3f457 100755
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@


 sqlfmt formats your dbt SQL files so you don't have to. It is similar in nature to black, gofmt,
-and rustfmt (but for SQL).
+and rustfmt (but for SQL).

 1. **sqlfmt promotes collaboration.** An auto-formatter makes it easier to collaborate with your team and solicit contributions from new people. You will never have to mention (or argue about) code style in code reviews again.
 2. **sqlfmt is fast.** Forget about formatting your code, and spend your time on business logic instead. sqlfmt processes hundreds of files per second and only operates on files that have changed since the last run.
@@ -138,7 +138,6 @@ Config for other integrations is detailed in the docs linked below:
 - [SQLFluff](https://docs.sqlfmt.com/integrations/sqlfluff)
 - [VSCode](https://docs.sqlfmt.com/integrations/vs-code)

-
 ## The sqlfmt style
 The only thing you can configure with sqlfmt is the desired line length of the formatted file. You can do this with the `--line-length` or `-l` options. The default is 88.

I think this is acceptable since it does not affect readability and should not affect output (i.e. when it's rendered to HTML).

  1. Nested sql blocks are not formatted. I think this is acceptable. See example:

``````markdown

# Hello

```sql
SELECT 1 -- this is not formatted
```

``````

michael-the1 avatar Jun 03 '24 14:06 michael-the1