Skip to content

Commit

Permalink
feat: use MDEx for markdown
Browse files Browse the repository at this point in the history
MDEx uses treesitter for syntax highlighting, so we automatically get tons of themes, plus it's way faster
  • Loading branch information
mhanberg committed Oct 31, 2023
1 parent 52e1ba6 commit dea822e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/tableau.ex
@@ -1,3 +1,9 @@
defmodule Tableau do
defdelegate live_reload(assigns), to: WebDevUtils.Components

def markdown(content, overrides \\ []) do
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))

MDEx.to_html(content, Keyword.merge(config.markdown[:mdex], overrides))
end
end
11 changes: 10 additions & 1 deletion lib/tableau/config.ex
Expand Up @@ -5,11 +5,19 @@ defmodule Tableau.Config do
* `:include_dir` - Directory that is just copied to the output directory. Defaults to `extra`.
* `:timezone` - Timezone to use when parsing date times. Defaults to `Etc/UTC`.
* `:url` - The URL of your website.
* `:markdown` - keyword
* `:mdex` - keyword - Options to pass to [MDEx](https://hexdocs.pm/mdex/MDEx.html#to_html/2)
"""

import Schematic

defstruct [:url, include_dir: "extra", timezone: "Etc/UTC", reload_log: false]
defstruct [
:url,
include_dir: "extra",
timezone: "Etc/UTC",
reload_log: false,
markdown: [mdex: []]
]

def new(config) do
unify(schematic(), config)
Expand All @@ -22,6 +30,7 @@ defmodule Tableau.Config do
optional(:include_dir) => str(),
optional(:timezone) => str(),
optional(:reload_log) => bool(),
optional(:markdown) => list(oneof([tuple([:mdex, list()])])),
url: str()
},
convert: false
Expand Down
14 changes: 12 additions & 2 deletions lib/tableau/extensions/post_extension.ex
Expand Up @@ -3,11 +3,12 @@ defmodule Tableau.PostExtension.Config do
Configuration for PostExtension.
## Options
- `:enabled` - boolean - Extension is active or not.
- `:dir` - string - Directory to scan for markdown files. Defaults to `_posts`
- `:future` - boolean - Show posts that have dates later than the current timestamp, or time at which the site is generated.
- `: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_.
- `layout` - string - Elixir module providing page layout for posts. Default is nil
- `:layout` - string - Elixir module providing page layout for posts. Default is nil
"""

import Schematic
Expand All @@ -31,6 +32,14 @@ defmodule Tableau.PostExtension.Config do
end
end

defmodule Tableau.PostExtension.Posts.HTMLConverter do
def convert(_filepath, body, _attrs, _opts) do
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))

body |> MDEx.to_html(config.markdown[:mdex])
end
end

defmodule Tableau.PostExtension.Posts.Post do
def build(filename, attrs, body) do
{:ok, config} = Tableau.Config.new(Map.new(Application.get_env(:tableau, :config, %{})))
Expand Down Expand Up @@ -147,7 +156,8 @@ defmodule Tableau.PostExtension do
from: "#{unquote(@config.dir)}/*.md",
as: :posts,
highlighters: [:makeup_elixir],
parser: Tableau.PostExtension.Posts.Post
parser: Tableau.PostExtension.Posts.Post,
html_converter: Tableau.PostExtension.Posts.HTMLConverter

def posts(_opts \\ []) do
@posts
Expand Down
21 changes: 8 additions & 13 deletions mix.exs
Expand Up @@ -32,24 +32,19 @@ defmodule Tableau.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ex_doc, ">= 0.0.0", only: :dev},
# {:file_system, "~> 0.2"},
{:web_dev_utils, "~> 0.1"},
{:libgraph, "~> 0.16.0"},
{:bandit, "~> 1.0"},
{:websock_adapter, "~> 0.5"},
{:libgraph, "~> 0.16.0"},
{:mdex, "~> 0.1"},
{:nimble_publisher, "~> 1.0"},
{:plug_static_index_html, "~> 1.0"},
{:schematic, "~> 0.3.1"},
{:nimble_publisher, "~> 1.0"},
{:yaml_elixir, "~> 2.9"},
{:makeup_elixir, ">= 0.0.0"},
{:tz, "~> 0.26.2"},
{:web_dev_utils, "~> 0.1"},
{:websock_adapter, "~> 0.5"},
{:yaml_elixir, "~> 2.9"},

# {:yaml_front_matter, "~> 1.0"},
# {:jason, "~> 1.4"},
# {:req, "~> 0.3", only: :test},
# {:bypass, "~> 2.0", only: :test},
# {:earmark, "~> 1.4"},
# dev
{:ex_doc, ">= 0.0.0", only: :dev},
{:floki, "~> 0.34", only: :test}
]
end
Expand Down
5 changes: 4 additions & 1 deletion mix.lock
@@ -1,5 +1,6 @@
%{
"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"},
"castore": {:hex, :castore, "1.0.4", "ff4d0fb2e6411c0479b1d965a814ea6d00e51eb2f58697446e9c41a97d940b28", [:mix], [], "hexpm", "9418c1b8144e11656f0be99943db4caf04612e3eaecefb5dae9a2a87565584f8"},
"earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.32", "fa739a0ecfa34493de19426681b23f6814573faee95dfd4b4aafe15a7b5b32c6", [:mix], [], "hexpm", "b8b0dd77d60373e77a3d7e8afa598f325e49e8663a51bcc2b88ef41838cca755"},
"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"},
Expand All @@ -10,12 +11,14 @@
"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"},
"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"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"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"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"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"},
"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"},
"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"},
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
"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"},
"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"},
"schematic": {:hex, :schematic, "0.3.1", "be633c1472959dc0ace22dd0e1f1445b099991fec39f6d6e5273d35ebd217ac4", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "52c419b5c405286e2d0369b9ca472b00b850c59a8b0bdf0dd69172ad4418d5ea"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"thousand_island": {:hex, :thousand_island, "1.0.0", "63fc8807d8607c9d74fa670996897c8c8a1f2022c8c68d024182e45249acd756", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "996320c72ba8f34d7be9b02900622e44341649f24359e0f67643e4dda8f23995"},
Expand Down

0 comments on commit dea822e

Please sign in to comment.