derpycoder

derpycoder

I created Table of Contents using Floki, with Header nesting. How to simplify the logic?

Hey Guys,

I created a table of contents, inspired by TailwindCSS docs.

The headers extraction logic is server side, and highlights and collapse or expand is done with JS.

Table of Contents


Here’s how I extract the headers, and nest them within their parent headers. In JavaScript it would have taken fewer lines to achieve, however since I am new to Elixir, I don’t know how to reduce the following code even further.


defp extract_headers(function_component) do
    function_component.static
    |> Floki.parse_fragment!()
    |> Floki.find("article")
    |> Enum.at(0)
    |> Floki.children()
    |> Enum.filter(&is_tuple/1)
    |> Enum.filter(fn each -> Tuple.to_list(each) |> Enum.count() == 3 end)
    |> Enum.filter(fn {name, _, _} -> name in ~w(h2 h3 h4) end)
    |> Enum.map(fn
      {header, meta, [title | _rest]} ->
        {
          header,
          meta |> Enum.find(&(elem(&1, 0) == "id")) |> elem(1),
          title |> String.replace(~r"\n\s+", "")
        }
    end)
    |> Enum.reduce([], fn
      {"h2", id, title}, acc ->
        [%{header: "h2", id: id, title: title, children: []} | acc]

      {"h3", id, title}, [head | tail] ->
        [
          %{
            head
            | children: [%{header: "h3", id: id, title: title, children: []} | head.children]
          }
          | tail
        ]

      {"h4", id, title}, [head | tail] ->
        [child_head | child_tail] = head.children

        [
          %{
            head
            | children: [
                %{
                  child_head
                  | children: [
                      %{header: "h4", id: id, title: title, children: nil} | child_head.children
                    ]
                }
                | child_tail
              ]
          }
          | tail
        ]
    end)
    |> Enum.reverse()
end

Here’s how its called:


__MODULE__
|> apply(assigns.post.body, [assigns])
|> extract_headers()

This the list before the big reduce function:


[
  {"h2", "installation", "Installation"},
  {"h3", "linux", "Linux"},
  {"h4", "ubuntu", "Ubuntu"},
  {"h4", "fedora", "Fedora"},
  {"h3", "mac", "Mac"},
  {"h2", "simple-task", "Simple Task"},
  {"h3", "foo", "Foo"},
  {"h3", "bar", "Bar"}
]

And here’s how the reduce changes the above list:


%{
    id: "installation",
    header: "h2",
    title: "Installation",
    children: [
      %{id: "mac", header: "h3", title: "Mac", children: []},
      %{
        id: "linux",
        header: "h3",
        title: "Linux",
        children: [
          %{id: "fedora", header: "h4", title: "Fedora", children: nil},
          %{id: "ubuntu", header: "h4", title: "Ubuntu", children: nil}
        ]
      }
    ]
  },
  %{
    id: "simple-task",
    header: "h2",
    title: "Simple Task",
    children: [
      %{id: "bar", header: "h3", title: "Bar", children: []},
      %{id: "foo", header: "h3", title: "Foo", children: []}
    ]
  },

Is there a better way to create nesting?

Marked As Solved

sodapopcan

sodapopcan

Ah yes, you can simplify further by removing the guard. I’d would also pattern matching instead of the last elem, though that’s very much up to personal preference:

{_, id} = Enum.find(&match?({"id", _}, &1)

And of course, put_in could help clean up the nesting part a bit. There is also pathex if you want to go the library route. I still can’t put much mental energy into that tonight as it’s crazy late here and haven’t been able to sleep.

Also Liked

derpycoder

derpycoder

Full Implementation

Table of contents, component:

attr :headers, :list

def table_of_contents(assigns) do
  ~H"""
  <div
    id="table-of-contents"
    class="sticky top-[calc(var(--header-height))] py-1 pr-5"
    data-file={__ENV__.file}
    data-line={__ENV__.line}
    phx-hook={Application.fetch_env!(:derpy_tools, :show_inspector?) && "SourceInspector"}
  >
    <h5
      id="toc"
      class="text-slate-900 font-semibold mb-2 text-sm leading-6 dark:text-slate-100"
      phx-hook="TableOfContents"
    >
      On this page
    </h5>
    <a
      href="#"
      class="block py-1 font-medium hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-300"
    >
      <i class="hero-chevron-up w-5.5 h-5.5 text-slate-500 dark:text-navy-100" /> Top
    </a>
     <.nested_header
      headers={@headers}
      class="max-h-[calc(100svh-(var(--header-height)))] overflow-auto"
    />
  </div>
  """
end

attr :id, :string, default: nil
attr :headers, :list
attr :class, :string, default: nil
attr :parent, :list, default: []

def nested_header(assigns) do
  ~H"""
  <ul id={@id} class={["space-y-1 font-inter font-medium list-none not-prose", @class]}>
    <li
      :for={%{header: header, id: id, title: title, children: children} <- @headers}
      class="not-prose"
    >
      <a
        id={"#{id}-link"}
        key={id}
        href={"##{id}"}
        tabindex="0"
        data-parent={@parent |> Enum.join(">")}
        class={[
          "block py-1 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-300",
          case header do
            "h2" -> "font-semibold"
            "h3" -> "font-medium"
            "h4" -> "font-normal"
            "h5" -> "font-normal text-xs"
          end
        ]}
        phx-click={JS.toggle(to: "##{id}-container")}
      >
        <i :if={header in ~w{h3 h4 h5}} class="hero-chevron-right-mini" />
        <span><%= title %></span>
      </a>

      <.nested_header
        :if={children}
        id={"#{id}-container"}
        headers={children |> Enum.reverse()}
        class="pl-4 hidden"
        parent={[id, @parent |> Enum.join(">")]}
      />
    </li>
  </ul>
  """
end

Using the TOC component in Left Nav

Pass any function component to the parse_blog function, and it’ll work.

attr :post, :map
attr :class, :string, default: nil

def left_nav(assigns) do
  parsed_blog =
    __MODULE__
    |> apply(assigns.post.body, [assigns])
    |> parse_blog()

  assigns =
    assigns
    |> assign(
      headers: extract_headers(parsed_blog),
      reading_time: reading_time(parsed_blog)
    )

  ~H"""
  <aside class={["flex flex-col", @class]}>
    <span><%= @reading_time %></span>
    <.table_of_contents headers={@headers} />
  </aside>
  """
end

Parsing-related code in private functions:

defp parse_blog(function_component) do
  function_component.static
  |> Floki.parse_fragment!()
  |> Floki.find("article")
end

defp extract_headers([parsed_blog]) do
  parsed_blog
  |> Floki.children()
  |> Enum.filter(&match?({name, _, _} when name in ~w(h2 h3 h4 h5), &1))
  |> Enum.map(fn
    {header, meta, [{"a", _, [title | _]} | _rest]} ->
      {_, id} = Enum.find(meta, &match?({"id", _}, &1))
      {header, id, title |> String.trim()}

     {header, meta, [title | _rest]} ->
      {_, id} = Enum.find(meta, &match?({"id", _}, &1))
      {header, id, title |> String.trim()}
  end)
  |> Enum.reduce([], fn
    {"h2", id, title}, acc ->
      [%{header: "h2", id: id, title: title, children: []} | acc]

    {"h3", id, title}, acc ->
      children = path(0 / :children)

      Pathex.set!(acc, children, [
        %{header: "h3", id: id, title: title, children: []} | Pathex.get(acc, children)
      ])

    {"h4", id, title}, acc ->
      children = path(0 / :children / 0 / :children)

      Pathex.set!(acc, children, [
        %{header: "h4", id: id, title: title, children: []} | Pathex.get(acc, children)
      ])

    {"h5", id, title}, acc ->
      children = path(0 / :children / 0 / :children / 0 / :children)

      Pathex.set!(acc, children, [
        %{header: "h5", id: id, title: title, children: nil} | Pathex.get(acc, children)
      ])
  end)
  |> Enum.reverse()
end

defp reading_time(parsed_blog) do
  parsed_blog
  |> Floki.text()
  |> String.replace(~r/@|#|\$|%|&|\^|:|_|!|,/u, " ")
  |> String.split()
  |> Enum.count()
  |> div(@wpm)
  |> Timex.Duration.from_minutes()
  |> Timex.Format.Duration.Formatter.format(:humanized)
end

JS Hook

const TableOfContents = {
  mounted() {
    if (location.hash) {
      const hash = location.hash.replace("#", "");
      const header = document.getElementById(hash);

      header &&
        header.scrollIntoView({
          behavior: "instant",
          block: "start",
          inline: "end",
        });

      highlightNav(
        hash,
        [
          "hover:text-slate-900",
          "dark:text-slate-400",
          "dark:hover:text-slate-300",
        ],
        ["text-sky-500", "dark:text-sky-400"]
      );
    }
    window.addEventListener("hashchange", handleHashChange);
  },
  destroyed() {
    window.removeEventListener("hashchange", handleHashChange);
  },
};

function handleHashChange(event) {
  if (event.oldURL.includes("#")) {
    const hash = event.oldURL.split("#").pop();

    highlightNav(
      hash,
      ["text-sky-500", "dark:text-sky-400"],
      [
        "hover:text-slate-900",
        "dark:text-slate-400",
        "dark:hover:text-slate-300",
      ]
    );
  }

  if (location.hash) {
    const hash = location.hash.replace("#", "");

    highlightNav(
      hash,
      [
        "hover:text-slate-900",
        "dark:text-slate-400",
        "dark:hover:text-slate-300",
      ],
      ["text-sky-500", "dark:text-sky-400"]
    );
  }
}

function highlightNav(hash, remove, add) {
  const nav = document.getElementById(`${hash}-link`);

  if (nav) {
    nav.classList.remove(...remove);
    nav.classList.add(...add);

    const { parent } = nav.dataset;

    if (parent) {
      parent.split(">").forEach((parentId) => {
        const parent = document.getElementById(`${parentId}-link`);

        if (parent) {
          parent.classList.remove(...remove);
          parent.classList.add(...add);
        }

        const parentContainer = document.getElementById(
          `${parentId}-container`
        );

        if (parentContainer) {
          parentContainer.classList.remove("hidden");
          if (location.hash == `#${hash}`) parentContainer.style.display = null;
        }
      });
    }
  }
}

export default TableOfContents;

Example headers, with self linking.

<h2 id="installation" class="group flex whitespace-nowrap not-prose">
    <a href="#installation" class="relative flex items-center">
      Installation
      <span class="absolute -ml-8 opacity-0 group-hover:opacity-100 transition-opacity duration-500 group-focus:opacity-100 flex h-6 w-6 items-center justify-center rounded-md text-slate-400 shadow-sm ring-1 ring-slate-900/5 hover:text-slate-700 hover:shadow hover:ring-slate-900/10 dark:bg-slate-700 dark:text-slate-300 dark:shadow-none dark:ring-0">
        <svg width="12" height="12" fill="none" aria-hidden="true">
          <path
            d="M3.75 1v10M8.25 1v10M1 3.75h10M1 8.25h10"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
          >
          </path>
        </svg>
      </span>
    </a>
  </h2>
  <h3 id="linux">Linux</h3>
  <h4 id="ubuntu" class="group whitespace-nowrap not-prose">
    <a href="#ubuntu" class="relative flex items-center">
      Ubuntu
      <span class="absolute -ml-8 opacity-0 group-hover:opacity-100 transition-opacity duration-500 group-focus:opacity-100 flex h-6 w-6 items-center justify-center rounded-md text-slate-400 shadow-sm ring-1 ring-slate-900/5 hover:text-slate-700 hover:shadow hover:ring-slate-900/10 dark:bg-slate-700 dark:text-slate-300 dark:shadow-none dark:ring-0">
        <svg width="12" height="12" fill="none" aria-hidden="true">
          <path
            d="M3.75 1v10M8.25 1v10M1 3.75h10M1 8.25h10"
            stroke="currentColor"
            stroke-width="1.5"
            stroke-linecap="round"
          >
          </path>
        </svg>
      </span>
    </a>
  </h4>

Sorry about the wall of text, but I don’t know how to string some nice description for the code.
Perhaps I’ll write a blog post about it.

Features:

  1. The nav stays selected, even on page refresh.
  2. Only the highlight part is JS, rest is happening on the Elixir side.
  3. Even the parent nav-links, light/open up, when it’s child is selected.

Rest you can figure out the features and kinks.

Thanks everyone. :upside_down_face:


P.S. I left the reading time estimator in the code, in case someone finds better way to do that thing. :hatching_chick:

sodapopcan

sodapopcan

I would need to put a little more thought into the nesting part, but for now the stuff before it could be cleaned up a bit.

|> Enum.filter(&is_tuple/1)
|> Enum.filter(fn each -> Tuple.to_list(each) |> Enum.count() == 3 end)
|> Enum.filter(fn {name, _, _} -> name in ~w(h2 h3 h4) end)

Whenever you see type checking and size checks in loops that can often be consolidated with pattern matching. The following should be equivalent:

|> Enum.filter(&match?({name, _, _} when name in ~w(h2 h3 h4), &1))

But I think for would do better here and you can even include the following map too! You’d have to break the pipe, though I think that’s a good thing in this situation. This is how I would probably write everything before the big reduce:

[menu] =
  function_component.static
  |> Floki.parse_fragment!()
  |> Floki.find("article")

menu_items = Floki.children(menu)

for {header, meta, [title | _]} <- menu_items,
    header in ~w(h2 h3 h4) do
  {
    header,
    meta |> Enum.find(&(elem(&1, 0) == "id")) |> elem(1), # I can't mentally parse what this is doing atm
    String.trim(title) # Pretty sure `trim` is what you want here
  }
end

Sorry I can’t provide help on the nesting part as I haven’t done anything like that in a bit. I’m curious, though, and hopefully someone else can. I would actually probably write something like that just because it’s simple and explicit (since there will never be more than 3 levels), I would just extract some variables so it’s not so jarring to read.

Good job on the component—it looks really nice!

sodapopcan

sodapopcan

Oh it represents me too, lol! And then I’ve worked with people who would go so far as to add extra overhead just to make pipelines work. Personally, I actually prefer functional pipelines if they are clear, but comprehensions can often read much better, so I don’t shy away from them. I’ve said this recently already, but I really feel that Elixir provides such a nice succinct set of builtin constructs that there is no reason not to get to know all of them and use each where appropriate.

derpycoder

derpycoder

@sodapopcan,

Pathex is awesome! I was wrong about it.

This is the reducer, after Pathex treatment!

|> Enum.reduce([], fn
  {"h2", id, title}, acc ->
    [%{header: "h2", id: id, title: title, children: []} | acc]

  {"h3", id, title}, acc ->
    children = path(0 / :children)

    Pathex.set!(acc, children, [
      %{header: "h3", id: id, title: title, children: []} | Pathex.get(acc, children)
    ])

  {"h4", id, title}, acc ->
    children = path(0 / :children / 0 / :children)

    Pathex.set!(acc, children, [
      %{header: "h4", id: id, title: title, children: nil} | Pathex.get(acc, children)
    ])

  {"h5", id, title}, acc ->
    children = path(0 / :children / 0 / :children / 0 / :children)

    Pathex.set!(acc, children, [
      %{header: "h5", id: id, title: title, children: nil} | Pathex.get(acc, children)
    ])
end)

I was able to add h5 as well, with ease!


P.S. If anyone wants the code for this Table of Contents implementation, I can paste the whole shebang, here! (The HEEx template, JS Hook and this logic.)

fceruti

fceruti

Just some food for thought: Those five operations can be expressed with something like this:

titles = 
  for {header, meta, [title | _rest]} when header in ~w(h2 h3 h4) <- children, reduce: [] do
    [head | tail] = acc -> 
       id = meta |> Enum.find(&(elem(&1, 0) == "id")) |> elem(1)
       title = title |> String.replace(~r"\n\s+", "")

      case header do
        "h2" -> [%{header: "h2", id: id, title: title, children: []} | acc]
        "h3" -> [
          %{ head | children: [%{header: "h3", id: id, title: title, children: []} | head.children] | tail
        ]
        ...
      end
  end

Not only it will shorter & easier to reason about, but also more performant.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement