side-notes/shared/key-builder.js

44 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

// Shared URL-key derivation for note lookups.
// Used by the side panel (load/save), the notes-list click handler
// (tab matching), and background navigation detection.
const DEFAULT_CONFIG = {
// Default: scheme://domain/path?search, hash excluded.
buildKey(url) {
try {
const u = new URL(url);
return `${u.protocol}//${u.host}${u.pathname}${u.search}`;
} catch {
// If URL parsing fails, use the raw string as-is.
return url;
}
},
};
let _config = { ...DEFAULT_CONFIG };
/**
* Derive a storage key from a page URL.
* @param {string} url
* @param {object} [config] — optional override (for testing / future UI changes)
* @returns {string}
*/
export function buildNoteKey(url, config = _config) {
return config.buildKey(url);
}
/**
* Swap the key-derivation rule at runtime.
* @param {object} newConfig — must expose a `buildKey(url)` function
*/
export function setKeyConfig(newConfig) {
_config = { ...newConfig };
}
/**
* Return the current config (read-only copy).
*/
export function getKeyConfig() {
return { ..._config };
}