Skip to content

Commit

Permalink
firefox: add support for nested bookmarks
Browse files Browse the repository at this point in the history
Change type of `firefox.profile.<name>.bookmarks` to allow for nested
bookmarks with user defined order.
  • Loading branch information
h7x4 authored and rycee committed Jul 18, 2022
1 parent 4c5106e commit 64c745f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 40 deletions.
128 changes: 96 additions & 32 deletions modules/programs/firefox.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let

mkUserJs = prefs: extraPrefs: bookmarks:
let
prefs' = lib.optionalAttrs ({ } != bookmarks) {
prefs' = lib.optionalAttrs ([ ] != bookmarks) {
"browser.bookmarks.file" = toString (firefoxBookmarksFile bookmarks);
"browser.places.importBookmarksHTML" = true;
} // prefs;
Expand All @@ -58,13 +58,36 @@ let

firefoxBookmarksFile = bookmarks:
let
mapper = _: entry: ''
<DT><A HREF="${escapeXML entry.url}" ADD_DATE="0" LAST_MODIFIED="0"${
lib.optionalString (entry.keyword != null)
" SHORTCUTURL=\"${escapeXML entry.keyword}\""
}>${escapeXML entry.name}</A>
'';
bookmarksEntries = lib.attrsets.mapAttrsToList mapper bookmarks;
indent = level:
lib.concatStringsSep "" (map (lib.const " ") (lib.range 1 level));

bookmarkToHTML = indentLevel: bookmark:
''
${indent indentLevel}<DT><A HREF="${
escapeXML bookmark.url
}" ADD_DATE="0" LAST_MODIFIED="0"${
lib.optionalString (bookmark.keyword != null)
" SHORTCUTURL=\"${escapeXML bookmark.keyword}\""
}>${escapeXML bookmark.name}</A>'';

directoryToHTML = indentLevel: directory: ''
${indent indentLevel}<DT><H3>${escapeXML directory.name}</H3>
${indent indentLevel}<DL><p>
${allItemsToHTML (indentLevel + 1) directory.bookmarks}
${indent indentLevel}</p></DL>'';

itemToHTMLOrRecurse = indentLevel: item:
if item ? "url" then
bookmarkToHTML indentLevel item
else
directoryToHTML indentLevel item;

allItemsToHTML = indentLevel: bookmarks:
lib.concatStringsSep "\n"
(map (itemToHTMLOrRecurse indentLevel) bookmarks);

bookmarkEntries = allItemsToHTML 1
(if isAttrs bookmarks then lib.attrValues bookmarks else bookmarks);
in pkgs.writeText "firefox-bookmarks.html" ''
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
Expand All @@ -74,7 +97,7 @@ let
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>
<DL><p>
${concatStrings bookmarksEntries}
${bookmarkEntries}
</p></DL>
'';

Expand Down Expand Up @@ -226,37 +249,78 @@ in {
};

bookmarks = mkOption {
type = types.attrsOf (types.submodule ({ config, name, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = "Bookmark name.";
};

keyword = mkOption {
type = types.nullOr types.str;
default = null;
description = "Bookmark search keyword.";
type = let
bookmarkSubmodule = types.submodule ({ config, name, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = "Bookmark name.";
};

keyword = mkOption {
type = types.nullOr types.str;
default = null;
description = "Bookmark search keyword.";
};

url = mkOption {
type = types.str;
description = "Bookmark url, use %s for search terms.";
};
};
}) // {
description = "bookmark submodule";
};

url = mkOption {
type = types.str;
description = "Bookmark url, use %s for search terms.";
bookmarkType = types.addCheck bookmarkSubmodule (x: x ? "url");

directoryType = types.submodule ({ config, name, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = "Directory name.";
};

bookmarks = mkOption {
type = types.listOf bookmarkType;
default = [ ];
description = "Bookmarks within directory.";
};
};
}) // {
description = "directory submodule";
};
}));
default = { };
in with types;
either (attrsOf bookmarkType)
(listOf (either bookmarkType directoryType));
default = [ ];
example = literalExpression ''
{
wikipedia = {
[
{
name = "wikipedia";
keyword = "wiki";
url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go";
};
"kernel.org" = {
}
{
name = "kernel.org";
url = "https://www.kernel.org";
};
}
}
{
name = "Nix sites";
bookmarks = [
{
name = "homepage";
url = "https://nixos.org/";
}
{
name = "wiki";
url = "https://nixos.wiki/";
}
];
}
]
'';
description = ''
Preloaded bookmarks. Note, this may silently overwrite any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>
<DL><p>
<DT><A HREF="https://www.kernel.org" ADD_DATE="0" LAST_MODIFIED="0">kernel.org</A>
<DT><A HREF="https://en.wikipedia.org/wiki/Special:Search?search=%s&amp;go=Go" ADD_DATE="0" LAST_MODIFIED="0" SHORTCUTURL="wiki">wikipedia</A>

<DT><A HREF="https://en.wikipedia.org/wiki/Special:Search?search=%s&amp;go=Go" ADD_DATE="0" LAST_MODIFIED="0" SHORTCUTURL="wiki">wikipedia</A>
<DT><A HREF="https://www.kernel.org" ADD_DATE="0" LAST_MODIFIED="0">kernel.org</A>
<DT><H3>Nix sites</H3>
<DL><p>
<DT><A HREF="https://nixos.org/" ADD_DATE="0" LAST_MODIFIED="0">homepage</A>
<DT><A HREF="https://nixos.wiki/" ADD_DATE="0" LAST_MODIFIED="0">wiki</A>
</p></DL>
</p></DL>
27 changes: 22 additions & 5 deletions tests/modules/programs/firefox/profile-settings.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,30 @@ lib.mkIf config.test.enableBig {
profiles.bookmarks = {
id = 2;
settings = { "general.smoothScroll" = false; };
bookmarks = {
wikipedia = {
bookmarks = [
{
name = "wikipedia";
keyword = "wiki";
url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go";
};
"kernel.org" = { url = "https://www.kernel.org"; };
};
}
{
name = "kernel.org";
url = "https://www.kernel.org";
}
{
name = "Nix sites";
bookmarks = [
{
name = "homepage";
url = "https://nixos.org/";
}
{
name = "wiki";
url = "https://nixos.wiki/";
}
];
}
];
};
};

Expand Down

0 comments on commit 64c745f

Please sign in to comment.