Skip to content

Instantly share code, notes, and snippets.

@silenius
Created September 22, 2021 10:56
Show Gist options
  • Save silenius/c48c953a72d28b11459491d7754cb6b6 to your computer and use it in GitHub Desktop.
Save silenius/c48c953a72d28b11459491d7754cb6b6 to your computer and use it in GitHub Desktop.
export function get_base_url(localized=false) {
const host_url = document.body.getAttribute('data-host_url');
let base_url;
if (localized) {
const prefix = document.body.getAttribute('data-script_name_');
const edit_lang = document.body.getAttribute('data-edit_locale');
if (prefix !== null && edit_lang) {
base_url = host_url + prefix + '/' + edit_lang;
}
}
if (!base_url) {
const prefix = document.body.getAttribute('data-script_name');
base_url = host_url + prefix;
}
if (!base_url.endsWith('/')) {
base_url += '/';
}
return base_url;
}
export function site_url(args, { base_url = null, localized = true } = {}) {
let url;
if (!base_url) {
base_url = get_base_url(localized);
}
if (typeof(args) === 'string') {
url = [args];
} else if (typeof(args) === 'number') {
url = [args.toString()];
} else {
url = args;
}
if (base_url) {
url.splice(0, 0, base_url);
}
return url.join('/').replace(/([^:]\/)\/+/g, "$1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment