Collapse namespace prefixes

When creating new pages, in order to avoid conflicts, it’s useful to add a prefix. E.g. Book/Book Title.

In Roam, there is an option to collapse the prefix, so when we link it’s shown as [[B/Book Title]]. It would be nice to have this option in Logseq as well

Hi @andrea, I know that some people like namespaces, but I have trouble seeing their value. They seem to me to be a way to add a type of folder hierarchy to Logseq pages, but this can also be done with tags like #book and then a page for #book or a query. Is there a benefit over that method that I’m missing?

Also, does Logseq even support namespaces, or does it just allow the use of / in page titles?

2 Likes

namespaces could be a nice addition imo.
It helps when you need different pages around the same master topic (same Title) that are automatically linked like : [[comic/Star Wars]] [[TV/Star Wars]] [[movies/Star Wars]] [[quotes/Star Wars]].

using tags like #book #movie doesn’t provide the same flexibility : once you have [[Star Wars]] you can’t create another page with the same title, and creating [[book-Star Wars]] doesn’t automatically create an unlinked-ref (afaik, unlinked refs are parsed from the content only, not from titles )

more details on namespaces, taken from : http://ruudhein.com/roam-namespaces

namespaces to categorize/disambiguate

Namespaces are, and are simply created, by using a backslash / in the page title. In our examples we might end up with “Book/How To Take Smart Notes” and “Article/How To Take Smart Notes”

Semi-Automatic Namespace Collation

Typing [[Book/How To Take Smart Notes]] somewhere will:

  1. Create the page Book/How To Take Smart Notes
  2. If you go to the page [[Book]], the link, your mention of the page, will show up under Unlinked References.
  3. If you go to the page [[How To Take Smart Notes]], the link, your mention of the page, shows up there under Unlinked References as well.

Folders on Export

When doing an export to Markdown, the namespaces will become folders, and the last part of the namespace will be a regular file in that folder.

Clean Display Options

The initial default for displaying namespace links is to show the whole link.
Using the shortcut sequence CTRL C followed by CTRL L you can cycle through that option, abbreviated, or isolated:

  1. [[Book/How To Take Smart Notes]]
  2. [[B/How To Take Smart Notes]]
  3. [[How To Take Smart Notes]]

The color of the links will change according, so visually it is readily apparent 2 & 3 are namespace links

I like the clean display options (it could be somewhat possible with aliases), but most importantly, the Semi-Automatic Namespace Collation would be great .

I think if unlinked refs were also evaluating TITLES:, we could partially emulate namesspaces using nested pages, as [[[[book]] LOTR]] [[[[movie]] LOTR]] would appear as unlinked refs inside [[LOTR]]

9 Likes

I think @cannibalox covered most of the reasons. One last reason, I’d add is that it makes search faster.

I like to track Goals and progress. I create a page for each goal with a namespace Goal. The goal pages are empty, but I refer to them from the daily page. Even if I don’t exactly remember the name of the page, typing Goal/ restricts the search and makes it much easier to find the right page

6 Likes

backlinking the related topic with recent discussion :

Totally agree. I already use namespaces for organizing my notes when searching and differentiating notes of the same name, but proper support for them would be great.

+1 for this – one of the only features that I still miss from Roam.

Namespaces are helpful when searching for pages of a particular type. For example, if I need to create a link to a project, I can easily see all projects when searching for it. Same for Books, People, etc. Tags do not help when performing a page search when adding a link.

There are two ways to do namespaces, adding type:: book to every page that describes a book or adding it to the page title, like described here. The latter can be searched for, using:

#+BEGIN_QUERY 
{:title [:h2 "📚 Books Namespace"]
:query [:find (pull ?page [*])
:in $ ?ns
:where
  [?page :block/name ?title]
  [(clojure.string/starts-with? ?title ?ns)] ]
 :inputs [ "book/" ]
}
#+END_QUERY

It will find all pages called book/book title (not Book/book title!).

It’s not optimal, but it’s a start.

PS, welcome @egfrenzy!

2 Likes

Huge +1 — while there isn’t a difference in function here, it cuts wayyyyyy down on visual noise, especially for folks like me who use a lot of namespaces.

I like Roam’s 3 options for simplifying the view (full, abbreviated, do not display namespaces). I use the “do not display” option most commonly. The UI color change helps to differentiate between normal pages.


CleanShot 2022-01-11 at 15.35.53@2x

3 Likes

You can try using aliases.

The syntax Text to be displayed

And also you can add the alias:: property to the namespace page to define a new way you can link to it. If you add alias::A guide to the good life to a page titled Book/Stoicism/A guide to the good life, you can link to it using [[A guide to the good life]]

1 Like

Decent workaround, actually! I’ll try to use this for some of the high-traffic ones. Would still like this as a preference, but aliases might be ok for now.

The problem with alias is that you have to create one for every page you whant to shorten and when you rename the name of the page, you have to change all the “sub pages” aliases one by one.

1 Like

I’ve released a custom.js snippet to collapse namespaces here (the prefixes are collapsed as ../ with a hardcoded color, to see the full page-name, either enterr Edit Mode or use the Hover Preview Tooltip) :

demo:

it’s bundled with another snippet to adjust the width of query tables, but it’s easy to extract/copy the namespace section only (just be careful to copy the mutation observer section at the top of tfile too).

4 Likes

Would it be possible to maintain last prefix? for example:

“region/country/company” to “…/country/company”

or select n last prefixes

thank you

sure, edit line 93, read this javascript - JS Get Second To Last Index Of - Stack Overflow

1 Like

Hello, I’m sorry to bother you, but since I don’t have a programming base, I refer to the stackoverflow page and modified“

nmsp[i].innerText.substring(nmsp[i].innerText.lastIndexOf("/", innerText.lastIndexOf("/")-1)); , but it doesn’t work.

Can I trouble you to give a demonstration?


您好,很抱歉打扰了,由于我并没有编程的基础,我参照 stackoverflow 页面并修改了:
nmsp[i].innerText.substring(nmsp[i].innerText.lastIndexOf("/", innerText.lastIndexOf("/")-1)); ,但是并没有成功。

能劳烦您给一个示范吗?

hey, sry for the late reply… it looks good though, not sure why it’s not working for you.

I replaced:

  nmsp[i].innerHTML =
          "<span style='color:rgb(133, 211, 81)'>..</span>" +
          nmsp[i].innerText.substring(
            nmsp[i].innerText.lastIndexOf("/"));  

with

    nmsp[i].innerHTML =
          "<span style='color:rgb(133, 211, 81)'>..</span>" +
          nmsp[i].innerText.substring(
            nmsp[i].innerText.lastIndexOf("/",
            nmsp[i].innerText.lastIndexOf("/")-1)
          );

and got: Logseq_Logseq_20220427_1531

Thank you for your code, now available! I think maybe I didn’t have the right format before.

I am really good creating at pbkac issues.

I copied the CSS and JS into my graph and refreshed and re-indexed but still not seeing the namespace modification.

Pages/Files used
image

JS Copied

CSS Copied


What am I missing?