Skip to content

Commit dea822e

Browse files
committed
feat: use MDEx for markdown
MDEx uses treesitter for syntax highlighting, so we automatically get tons of themes, plus it's way faster
1 parent 52e1ba6 commit dea822e

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

lib/tableau.ex

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
defmodule Tableau do
22
defdelegate live_reload(assigns), to: WebDevUtils.Components
3+
4+
def markdown(content, overrides \\ []) do
5+
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))
6+
7+
MDEx.to_html(content, Keyword.merge(config.markdown[:mdex], overrides))
8+
end
39
end

lib/tableau/config.ex

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ defmodule Tableau.Config do
55
* `:include_dir` - Directory that is just copied to the output directory. Defaults to `extra`.
66
* `:timezone` - Timezone to use when parsing date times. Defaults to `Etc/UTC`.
77
* `:url` - The URL of your website.
8+
* `:markdown` - keyword
9+
* `:mdex` - keyword - Options to pass to [MDEx](https://hexdocs.pm/mdex/MDEx.html#to_html/2)
810
"""
911

1012
import Schematic
1113

12-
defstruct [:url, include_dir: "extra", timezone: "Etc/UTC", reload_log: false]
14+
defstruct [
15+
:url,
16+
include_dir: "extra",
17+
timezone: "Etc/UTC",
18+
reload_log: false,
19+
markdown: [mdex: []]
20+
]
1321

1422
def new(config) do
1523
unify(schematic(), config)
@@ -22,6 +30,7 @@ defmodule Tableau.Config do
2230
optional(:include_dir) => str(),
2331
optional(:timezone) => str(),
2432
optional(:reload_log) => bool(),
33+
optional(:markdown) => list(oneof([tuple([:mdex, list()])])),
2534
url: str()
2635
},
2736
convert: false

lib/tableau/extensions/post_extension.ex

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ defmodule Tableau.PostExtension.Config do
33
Configuration for PostExtension.
44
55
## Options
6+
67
- `:enabled` - boolean - Extension is active or not.
78
- `:dir` - string - Directory to scan for markdown files. Defaults to `_posts`
89
- `:future` - boolean - Show posts that have dates later than the current timestamp, or time at which the site is generated.
910
- `:permalink` - string - Default output path for posts. Accepts `:title` as a replacement keyword, replaced with the post's provided title. If a post has a `:permalink` provided, that will override this value _for that post_.
10-
- `layout` - string - Elixir module providing page layout for posts. Default is nil
11+
- `:layout` - string - Elixir module providing page layout for posts. Default is nil
1112
"""
1213

1314
import Schematic
@@ -31,6 +32,14 @@ defmodule Tableau.PostExtension.Config do
3132
end
3233
end
3334

35+
defmodule Tableau.PostExtension.Posts.HTMLConverter do
36+
def convert(_filepath, body, _attrs, _opts) do
37+
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))
38+
39+
body |> MDEx.to_html(config.markdown[:mdex])
40+
end
41+
end
42+
3443
defmodule Tableau.PostExtension.Posts.Post do
3544
def build(filename, attrs, body) do
3645
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))
@@ -147,7 +156,8 @@ defmodule Tableau.PostExtension do
147156
from: "#{unquote(@config.dir)}/*.md",
148157
as: :posts,
149158
highlighters: [:makeup_elixir],
150-
parser: Tableau.PostExtension.Posts.Post
159+
parser: Tableau.PostExtension.Posts.Post,
160+
html_converter: Tableau.PostExtension.Posts.HTMLConverter
151161

152162
def posts(_opts \\ []) do
153163
@posts

mix.exs

+8-13
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,19 @@ defmodule Tableau.MixProject do
3232
# Run "mix help deps" to learn about dependencies.
3333
defp deps do
3434
[
35-
{:ex_doc, ">= 0.0.0", only: :dev},
36-
# {:file_system, "~> 0.2"},
37-
{:web_dev_utils, "~> 0.1"},
38-
{:libgraph, "~> 0.16.0"},
3935
{:bandit, "~> 1.0"},
40-
{:websock_adapter, "~> 0.5"},
36+
{:libgraph, "~> 0.16.0"},
37+
{:mdex, "~> 0.1"},
38+
{:nimble_publisher, "~> 1.0"},
4139
{:plug_static_index_html, "~> 1.0"},
4240
{:schematic, "~> 0.3.1"},
43-
{:nimble_publisher, "~> 1.0"},
44-
{:yaml_elixir, "~> 2.9"},
45-
{:makeup_elixir, ">= 0.0.0"},
4641
{:tz, "~> 0.26.2"},
42+
{:web_dev_utils, "~> 0.1"},
43+
{:websock_adapter, "~> 0.5"},
44+
{:yaml_elixir, "~> 2.9"},
4745

48-
# {:yaml_front_matter, "~> 1.0"},
49-
# {:jason, "~> 1.4"},
50-
# {:req, "~> 0.3", only: :test},
51-
# {:bypass, "~> 2.0", only: :test},
52-
# {:earmark, "~> 1.4"},
46+
# dev
47+
{:ex_doc, ">= 0.0.0", only: :dev},
5348
{:floki, "~> 0.34", only: :test}
5449
]
5550
end

mix.lock

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%{
22
"bandit": {:hex, :bandit, "1.0.0", "2bd87bbf713d0eed0090f2fa162cd1676198122e6c2b68a201c706e354a6d5e5", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "32acf6ac030fee1f99fd9c3fcf81671911ae8637e0a61c98111861b466efafdb"},
3+
"castore": {:hex, :castore, "1.0.4", "ff4d0fb2e6411c0479b1d965a814ea6d00e51eb2f58697446e9c41a97d940b28", [:mix], [], "hexpm", "9418c1b8144e11656f0be99943db4caf04612e3eaecefb5dae9a2a87565584f8"},
34
"earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"},
45
"earmark_parser": {:hex, :earmark_parser, "1.4.32", "fa739a0ecfa34493de19426681b23f6814573faee95dfd4b4aafe15a7b5b32c6", [:mix], [], "hexpm", "b8b0dd77d60373e77a3d7e8afa598f325e49e8663a51bcc2b88ef41838cca755"},
56
"ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"},
@@ -10,12 +11,14 @@
1011
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
1112
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
1213
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
14+
"mdex": {:hex, :mdex, "0.1.11", "83bac0b339811310362c86087c1ea1d37cf3190f41993a7de41fea81ccdbc8a1", [:mix], [{:rustler, "~> 0.29", [hex: :rustler, repo: "hexpm", optional: true]}, {:rustler_precompiled, "~> 0.6", [hex: :rustler_precompiled, repo: "hexpm", optional: false]}], "hexpm", "303510829f3c59295e13b27992ef542356db8276a3a514f1369ae91afb62f60b"},
1315
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
1416
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
15-
"nimble_publisher": {:hex, :nimble_publisher, "1.0.0", "e7c6b1dc0505a87dcb12e47427bce9f659eba36ab6f0afb8267d21f2ac701871", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "959ab4d9ff41a33a7269082ccf7e9fb76b840d850c872733dd4201591b6ea6f4"},
17+
"nimble_publisher": {:hex, :nimble_publisher, "1.1.0", "49dee0f30536140268996660a5927d0282946949c35c88ccc6da11a19231b4b6", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "80fb42d8d1e34f41ff29fc2a1ae6ab86ea7b764b3c2d38e5268a43cf33825782"},
1618
"plug": {:hex, :plug, "1.15.1", "b7efd81c1a1286f13efb3f769de343236bd8b7d23b4a9f40d3002fc39ad8f74c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "459497bd94d041d98d948054ec6c0b76feacd28eec38b219ca04c0de13c79d30"},
1719
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
1820
"plug_static_index_html": {:hex, :plug_static_index_html, "1.0.0", "840123d4d3975585133485ea86af73cb2600afd7f2a976f9f5fd8b3808e636a0", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "79fd4fcf34d110605c26560cbae8f23c603ec4158c08298bd4360fdea90bb5cf"},
21+
"rustler_precompiled": {:hex, :rustler_precompiled, "0.7.0", "5d0834fc06dbc76dd1034482f17b1797df0dba9b491cef8bb045fcaca94bcade", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "fdf43a6835f4e4de5bfbc4c019bfb8c46d124bd4635fefa3e20d9a2bbbec1512"},
1922
"schematic": {:hex, :schematic, "0.3.1", "be633c1472959dc0ace22dd0e1f1445b099991fec39f6d6e5273d35ebd217ac4", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "52c419b5c405286e2d0369b9ca472b00b850c59a8b0bdf0dd69172ad4418d5ea"},
2023
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
2124
"thousand_island": {:hex, :thousand_island, "1.0.0", "63fc8807d8607c9d74fa670996897c8c8a1f2022c8c68d024182e45249acd756", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "996320c72ba8f34d7be9b02900622e44341649f24359e0f67643e4dda8f23995"},

0 commit comments

Comments
 (0)