Skip to content
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

Conversation

aruscher
Copy link

@aruscher aruscher commented Jun 8, 2021

This change allows to modify the replacement rules for the slug names.

Addresses feature request #1543

…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"
Copy link

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?

Copy link
Author

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.

@rudolf-adamkovic
Copy link

Addresses feature request #1543

This resolves the problem, so we can go with "Close #1543".

@jethrokuan
Copy link
Member

jethrokuan commented Jun 9, 2021

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.

@aruscher
Copy link
Author

aruscher commented Jun 9, 2021

@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?

@jethrokuan
Copy link
Member

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.:

(cl-defmethod org-roam-namespace ((node org-roam-node))
  "Return the namespace of NODE."
  ;; Do something with folder structure maybe
)

and then in your templates use ${namespace}

@rudolf-adamkovic
Copy link

The user here. :) So, what shall I do?

@aruscher
Copy link
Author

aruscher commented Jun 13, 2021

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 aruscher closed this Jun 13, 2021
@rudolf-adamkovic
Copy link

@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) cl-defmethod, cl-flet*, and the like. Hence, I got confused.

@jimmerricks
Copy link

A small change needs to be made to the code snippet for it to work with Emacs 29. See here: d184001.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants