Skip to content
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

Set up MarkdownCodeBlockFormatter #1419

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
inputs: ["mix.exs", "{exercises,bin}/**/*.{ex,exs}"]
plugins: [MarkdownCodeBlockFormatter],
inputs: ["mix.exs", "{exercises,bin}/**/*.{ex,exs}", "{concepts,exercises,docs,reference}/**/*.md", "*.md"]
]
2 changes: 1 addition & 1 deletion concepts/anonymous-functions/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Anonymous functions may be created with the [`&` capture shorthand][kernel-captu
fn x, y -> abs(x) + abs(y) end

# We can write:
& abs(&1) + abs(&2)
&(abs(&1) + abs(&2))
```

- The `&` capture operator can also be used to [_capture_ existing named function][capture]:
Expand Down
12 changes: 8 additions & 4 deletions concepts/basics/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
Elixir is a dynamically-typed language, meaning that the type of a variable is only checked at runtime. Using the match `=` operator, we can bind a value of any type to a variable name:

```elixir
count = 1 # Bound an integer value of 1
count = 2 # You may re-bind variables
# Bound an integer value of 1
count = 1
# You may re-bind variables
count = 2

count = false # You may re-bind any type to a variable
# You may re-bind any type to a variable
count = false

message = "Success!" # Strings can be created by enclosing characters within double quotes
# Strings can be created by enclosing characters within double quotes
message = "Success!"
```

## Modules
Expand Down
3 changes: 2 additions & 1 deletion concepts/binaries/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

```elixir
<<255>> == <<0xFF>>
<<256>> == <<0>> # Overflowing bits are truncated
# Overflowing bits are truncated
<<256>> == <<0>>
<<256::size(16)>> == <<1, 0>>

<<"Hello">> == <<72, 101, 108, 108, 111>>
Expand Down
3 changes: 2 additions & 1 deletion concepts/binaries/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Binary literals are defined using the bitstring special form `<<>>`. When defini

```elixir
<<255>> == <<0xFF>>
<<256>> == <<0>> # Overflowing bits are truncated
# Overflowing bits are truncated
<<256>> == <<0>>
<<2, 4, 6, 8, 10, 12, 14, 16>> == <<0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10>>
```

Expand Down
7 changes: 5 additions & 2 deletions concepts/booleans/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ Boolean operators use _short-circuit evaluation_, which means that the expressio
Each of the operators has a different precedence, where `not/1` is evaluated before `and/2` and `or/2`. Brackets can be used to evaluate one part of the expression before the others:

```elixir
not true and false # => false
not (true and false) # => true
not true and false
# => false

not (true and false)
# => true
```

When writing a function that returns a boolean value, it is [idiomatic to end the function name][naming] with a `?`. The same convention can be used for variables that store boolean values.
Expand Down
2 changes: 2 additions & 0 deletions concepts/docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ If you have Elixir installed on your computer, you can use it in [the interactiv

In `iex`, you can type [`h`][iex-h], followed by a space and a module name or a function name, to read its documentation.

[]: # (elixir-formatter-disable-next-block)

```elixir
iex()> h String.upcase/1
# def upcase(string, mode \\ :default)
Expand Down
1 change: 1 addition & 0 deletions concepts/enum/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Enum.reduce([4, 20, 31, 9, 2], nil, fn x, acc ->
x <= acc -> acc
end
end)

# => 31
```

Expand Down
1 change: 1 addition & 0 deletions concepts/exceptions/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule MyCustomizedError do
case value do
[] ->
%MyCustomizedError{}

_ ->
%MyCustomizedError{message: "Alert: " <> value}
end
Expand Down
1 change: 1 addition & 0 deletions concepts/exceptions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule MyCustomizedError do
case value do
[] ->
%MyCustomizedError{}

_ ->
%MyCustomizedError{message: "Alert: " <> value}
end
Expand Down
2 changes: 1 addition & 1 deletion concepts/file/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Reading a file with [`File.read/1`][file-read] is going to load the whole file i

```elixir
File.stream!("file.txt")
|> Stream.map(& &1 <> "!")
|> Stream.map(&(&1 <> "!"))
|> Stream.into(File.stream!("new_file.txt"))
|> Stream.run()
```
Expand Down
1 change: 1 addition & 0 deletions concepts/io/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ When this happens, you might want to use [`IO.inspect/2`][io-inspect] instead.
|> IO.inspect(label: "step 1")
|> String.downcase()
|> IO.inspect(label: "step 2")

# > step 1: "HELLO"
# > step 2: "hello"
# => "hello"
Expand Down
4 changes: 4 additions & 0 deletions concepts/keyword-lists/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Keyword.keyword?([{"month", "April"}])

If you want to use characters other than letters, numbers, and `_` in the key, you need to wrap it in quotes. However, that does not make it a string - it is still an atom.

[]: # (elixir-formatter-disable-next-block)

```elixir
Keyword.keyword?(["day of week": "Monday"])
# => true
Expand Down Expand Up @@ -59,6 +61,8 @@ if age >= 16, do: "beer", else: "no beer"

This may look like `if` accepts two arguments, but the `do:` and `else:` pair is actually a single argument - a keyword list. The same code could be written as:

[]: # (elixir-formatter-disable-next-block)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where desired, formatting can be turned off. For example here we want to show off a non-standard way to format this code to prove a point.


```elixir
if age >= 16, [do: "beer", else: "no beer"]
# or
Expand Down
2 changes: 2 additions & 0 deletions concepts/links/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pid = spawn_link(fn -> :timer.sleep(50_000) end)
self()
|> Process.info()
|> Keyword.get(:links)

# => [#PID<0.126.0>]
```

Expand All @@ -69,6 +70,7 @@ The current value of the flag can be checked using [`Process.info/1`][process-in
pid
|> Process.info()
|> Keyword.get(:trap_exit)

# => false
```

Expand Down
8 changes: 6 additions & 2 deletions concepts/list-comprehensions/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

[Comprehension][for] provide a facility for transforming _Enumerables_ easily and declaratively. They are _syntactic sugar_ for iterating through enumerables in Elixir.

[]: # (elixir-formatter-disable-next-block)

```elixir
for s <- ["a", "b", "hello", "c"], # 1. generator
String.length(s) == 1, # 2. filter
into: "", # 3. collectable
String.length(s) == 1, # 2. filter
into: "", # 3. collectable
do: String.upcase(s)

# => "ABC"
Expand All @@ -29,6 +31,7 @@ for x <- [0, 1],
y <- [0, 1] do
{x, y}
end

# => [{0, 0}, {0, 1}, {1, 0}, {1, 1}]
```

Expand All @@ -49,6 +52,7 @@ for pair <- [a: "string"],
{atom, str} = pair do
str
end

# => ["string"]
```

Expand Down
2 changes: 2 additions & 0 deletions concepts/list-comprehensions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ for {atom, number} <- [a: 1, b: 2, c: 3, d: 4],
rem(number, 2) == 0 do
atom
end

# => [:b, :d]
```

Expand All @@ -33,5 +34,6 @@ for x <- [0, 1],
y <- [0, 1] do
{x, y}
end

# => [{0, 0}, {0, 1}, {1, 0}, {1, 1}]
```
12 changes: 8 additions & 4 deletions concepts/lists/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ Lists can be written in literal form, head-tail notation, (which uses the `cons`

# Head-tail Notation
[]
[1 | []] # same as [1]
[1 | [2 | [3 | []]]] # same as [1, 2, 3]
# same as [1]
[1 | []]
# same as [1, 2, 3]
[1 | [2 | [3 | []]]]

# Mixed
[1 | [2, 3]] # same as [1, 2, 3]
# same as [1, 2, 3]
[1 | [2, 3]]
```

There can also be more than one element before the _cons_ (`|`) operator.
Expand Down Expand Up @@ -46,7 +49,8 @@ We can achieve the same result by prepending an element to the reversed list, an
[1, 2, 3] ++ [4] ++ [5] ++ [6]

# Prepend to the start of a list (faster, due to the nature of linked lists)
[6 | [5 | [4 | [3, 2, 1]]]] # then reverse!
[6 | [5 | [4 | [3, 2, 1]]]]
# then reverse!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this comment go at the bottom?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it manually because the "reverse" part is not part of this code. The code only shows list creation, and then it's implied that you need to reverse later. Hence the comment should be last.

```

There are several common `Kernel` functions for lists:
Expand Down
4 changes: 2 additions & 2 deletions concepts/maps/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
# A map with the string key "a" associated to the float value 2.0
%{"a" => 2.0}

# A map with the map key %{} with the list value [1,2,3]
%{%{} => [1,2,3]}
# A map with the map key %{} with the list value [1 ,2, 3]
%{%{} => [1, 2, 3]}

# A map with keys of different types
%{:a => 1, "b" => 2}
Expand Down
1 change: 0 additions & 1 deletion concepts/module-attributes-as-constants/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ In Elixir, we can define module attributes which can be used as constants in our

```elixir
defmodule Example do

# Defines the attribute as the value 1
@constant_number 1

Expand Down
1 change: 1 addition & 0 deletions concepts/multiple-clause-functions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Elixir offers _multiple function clauses_ and _guards_ to write:
def number(n) when n == 7 do
"Awesome, that's my favorite"
end

def number(_n) do
"That's not my favorite"
end
Expand Down
2 changes: 2 additions & 0 deletions concepts/nil/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ favorite_color = nil

`nil` is an atom, but it is usually written as `nil`, not `:nil`. The boolean values `true` and `false` are atoms too.

[]: # (elixir-formatter-disable-next-block)

```elixir
nil === :nil
# => true
Expand Down
2 changes: 1 addition & 1 deletion concepts/pipe-operator/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ It is a matter of personal preference when to use the pipe operator and when not

```elixir
# do
String.split("hello" , "")
String.split("hello", "")

# don't
"hello" |> String.split("")
Expand Down
1 change: 1 addition & 0 deletions concepts/pipe-operator/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ The `|>` operator is called the pipe operator. It can be used to chain function
"hello"
|> String.upcase()
|> Kernel.<>("?!")

# => "HELLO?!"
```
2 changes: 1 addition & 1 deletion concepts/ranges/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Enum.to_list(9..1)
# => [9, 8, 7, 6, 5, 4, 3, 2, 1]

Enum.map(?A..?F, & <<&1>>)
Enum.map(?A..?F, &<<&1>>)
["A", "B", "C", "D", "E", "F"]
```

Expand Down
1 change: 1 addition & 0 deletions concepts/strings/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Any Elixir expression is valid inside the interpolation. If a string is given, t
# => "And lists, too!"

"But not functions: #{fn x -> x end}"

# => ** (Protocol.UndefinedError) protocol String.Chars not implemented for #Function<7.126501267/1 in :erl_eval.expr/5> of type Function
# (elixir 1.10.1) lib/string/chars.ex:3: String.Chars.impl_for!/1
# (elixir 1.10.1) lib/string/chars.ex:22: String.Chars.to_string/1
Expand Down
1 change: 1 addition & 0 deletions concepts/tail-call-recursion/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Tail-recursive functions allow for [tail call optimization][tail-call-optimizati
def reverse(list), do: do_reverse(list, [])

defp do_reverse([], acc), do: acc

defp do_reverse([head | tail], acc) do
do_reverse(tail, [head | acc])
end
Expand Down
1 change: 1 addition & 0 deletions concepts/tasks/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ If you need to run tasks that perform different operations, you might need `Task
inputs
|> Enum.map(&Task.async(fn -> function.(&1) end))
|> Task.await_many(1_000)

# => ** (exit) exited in: Task.await_many([...], 1000)
# ** (EXIT) time out
# (elixir 1.11.3) lib/task.ex:725: Task.await_many/5
Expand Down
1 change: 1 addition & 0 deletions concepts/try-rescue-else-after/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ else
after
:some_action
end

# => :success
```
1 change: 1 addition & 0 deletions concepts/try-rescue-else-after/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ else
after
:some_action
end

# => :success
```
2 changes: 2 additions & 0 deletions concepts/try-rescue/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

For example:

[]: # (elixir-formatter-disable-next-block)

```elixir
try do #1
raise RuntimeError, "error" #2
Expand Down
2 changes: 2 additions & 0 deletions concepts/try-rescue/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Elixir provides a construct for rescuing from errors using `try .. rescue`

[]: # (elixir-formatter-disable-next-block)

```elixir
try do #1
raise RuntimeError, "error" #2
Expand Down
13 changes: 9 additions & 4 deletions concepts/with/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ Let's say that you want to validate a username with several checks. You might re
case check_ascii(username) do
{:ok, username} ->
case check_starts_with_letter(username) do
{:ok, username} -> {:ok, username}
{:error, "usernames may only start with a letter"} = err -> err
end
{:error, "usernames may only contain ascii letters"} = err -> err
{:ok, username} ->
{:ok, username}

{:error, "usernames may only start with a letter"} = err ->
err
end

{:error, "usernames may only contain ascii letters"} = err ->
err
end
```

Expand Down
2 changes: 2 additions & 0 deletions docs/REPRESENTER_NORMALIZATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Types are written without.

### Before

[]: # (elixir-formatter-disable-next-block)

```elixir
defmodule Fake do
@type foo() :: integer() | atom
Expand Down
Loading
Loading