Skip to content

(Feature request) Friendly share urls: turn title into shareAlias #499

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

Open
maphew opened this issue Oct 18, 2024 · 1 comment
Open

(Feature request) Friendly share urls: turn title into shareAlias #499

maphew opened this issue Oct 18, 2024 · 1 comment

Comments

@maphew
Copy link
Contributor

maphew commented Oct 18, 2024

Describe feature

Idea: a script with button/keystroke for "turn note title into share-alias". Also make it apply when changing a note's status to Shared, but it only acts if #shareAlias is missing/empty. In case of duplicate append something.

Changing the friendly URL should not be done automatically by the system, because the user might not be aware that changing the title will also affect the URL. Generally once a page is published it's a good idea to keep the URL the same in order not to break any hard links (inside the application, or bookmarked by viewers of the site).

The shareAlias would continue to be able to be changed by a deliberate act by the user, the same way it is now (which could be the user executing the putative titleToShareAlias function).

Originally posted by @maphew in TriliumNext/Docs#117 (comment)

@maphew
Copy link
Contributor Author

maphew commented May 3, 2025

The user guide has this example under Apply bulk actions >> Other:

To alter attributes of a note based on another attribute, such as setting the #shareAlias label to the title of the note:

note.setLabel("shareAlias", note.title)

From which I concocted the script below. I haven't figured out how to apply it though. It has no effect when used in bulk actions and I don't understand how to turn a script into a widget.

/**
 * Converts a string into a human-friendly kebab-case URL fragment.
 * Removes or replaces special characters, trims, and lowercases.
 * Example: "Title with: special %^&* chars" -> "title-with-special-chars"
 *
 * @param str - The input string to convert
 * @returns The kebab-case URL fragment
 */
function toKebab(str) {
  return str
    .normalize("NFKD")                  // Normalize unicode
    .replace(/[\u0300-\u036f]/g, '')    // Remove diacritics/accents
    .replace(/[^\w\s-]/g, '')           // Remove non-word, non-space, non-hyphen
    .trim()                             // Trim leading/trailing whitespace
    .replace(/[-\s]+/g, '-')            // Replace spaces and hyphens with single hyphen
    .toLowerCase();
}

console.log(toKebab(api.note.title));	// debug
api.note.setLabel("shareAlias", toKebab(api.note.title)); // #shareAlias=title-with-special-chars

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

No branches or pull requests

2 participants