New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customization for character replacement generated slug #1544
Customization for character replacement generated slug #1544
Conversation
…rg-roam#1543). Previously the replacement rules were harded-coded `org-roam-node-slug`. Now, the rules can be modifed via a custom variable.
1. All not alphanumeric characters are replaced by underscores | ||
2. Sequential underscores are replaced by a single underscore | ||
3. Starting underscores are removed | ||
4. Ending underscores are removed" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great. So much passive voice, though: are applied, are replaced (2×), are removed (2×). Why not go with apply, replace, and remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used passive voice because something is done with something. I will check the other docs and revise this text if necessary.
In v2, these won't be customizable options. The way to change how the slug is computed is to override the function entirely: (cl-defmethod org-roam-node-slug ((node org-roam-node))
"Return the slug of NODE."
;; My logic goes here
) I'm trying to prevent the proliferation of customize options, slug computation can be as complex or as simple as you want it to be. |
@jethrokuan Where do you draw the line between customize options and method overriding? Is it intended that the user should by default override methods to implement the desired behavior and, thus, that every method should work without customize options? |
The default is customize options, if the options can be easily enumerated. Slug computation is an open-ended configuration option: e.g. one may choose to include the file creation time, or tags in the slug in which case providing customize options would be far too messy. In this case overriding the method goes in line with adding properties to nodes: you can define arbitrary properties on a node and use them in org-roam templates e.g.:
and then in your templates use ${namespace} |
The user here. :) So, what shall I do? |
In a nutshell: You are supposed to redefine the original function as part of your configuration instead of relying on a customization option. So you more or less should copy this code to your init.el to get your desired - instead of _. Please make sure that this code is evaluated after org-roam was loaded : (cl-defmethod org-roam-node-slug ((node org-roam-node))
(let ((title (org-roam-node-title node)))
(cl-flet* ((nonspacing-mark-p (char)
(memq char org-roam-slug-trim-chars))
(strip-nonspacing-marks (s)
(ucs-normalize-NFC-string
(apply #'string (seq-remove #'nonspacing-mark-p
(ucs-normalize-NFD-string s)))))
(cl-replace (title pair)
(replace-regexp-in-string (car pair) (cdr pair) title)))
(let* ((pairs `(("[^[:alnum:][:digit:]]" . "-")
("--*" . "-")
("^-" . "")
("-$" . "")))
(slug (-reduce-from #'cl-replace (strip-nonspacing-marks title) pairs)))
(downcase slug))))) |
@aruscher Thank you! I customize a large number of functions and variables in my Emacs initialization file, but I have yet to use (or see) |
A small change needs to be made to the code snippet for it to work with Emacs 29. See here: d184001. |
This change allows to modify the replacement rules for the slug names.
Addresses feature request #1543