From ce64fda1e96fc1eb30fa84a00961d75da2aef36c Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 7 Dec 2022 23:35:00 -0500 Subject: [PATCH] improvement: replace doc links in sidebar also --- lib/mix/tasks/replace_doc_links.ex | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/mix/tasks/replace_doc_links.ex b/lib/mix/tasks/replace_doc_links.ex index c5769bb3..d9c8f6a3 100644 --- a/lib/mix/tasks/replace_doc_links.ex +++ b/lib/mix/tasks/replace_doc_links.ex @@ -162,6 +162,13 @@ defmodule Mix.Tasks.Ash.ReplaceDocLinks do :link, %{type: "module", item: item, name_override: name, library: library}, _ -> ~s(#{name || item}) + :link, %{type: "function", item: item, name_override: name, library: library}, _ -> + parts = String.split(item, ".") + function = List.last(parts) + module = parts |> :lists.droplast() |> Enum.join(".") + + ~s(#{name || item}) + :link, %{type: "library", name_override: name, library: library}, _ -> ~s(#{name || library}) @@ -171,6 +178,41 @@ defmodule Mix.Tasks.Ash.ReplaceDocLinks do File.write!(file, new_contents) end) + + "doc/dist/*.js" + |> Path.wildcard() + |> Enum.filter(&String.contains?(&1, "sidebar")) + |> Enum.each(fn file -> + current_project = to_string(Mix.Project.config()[:app]) + + new_contents = + file + |> File.read!() + |> Spark.DocIndex.render_replacements(fn + :mix_dep, %{library: ^current_project}, _context -> + case Version.parse(Mix.Project.config()[:version]) do + {:ok, %Version{pre: pre, build: build}} when pre != [] or not is_nil(build) -> + ~s({:#{current_project}, "~> #{Mix.Project.config()[:version]}"}) + + {:ok, %Version{major: major, minor: minor}} -> + ~s({:#{current_project}, "~> #{major}.#{minor}"}) + + _ -> + ~s({:#{current_project}, "~> x.y.z"}) + end + + :mix_dep, %{library: library}, _context -> + library + + :link, %{name_override: name, item: item}, _context -> + name || item + + _, %{text: text}, _ -> + text + end) + + File.write!(file, new_contents) + end) end def doc_indexes do