@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
63 lines (62 loc) • 2.29 kB
JavaScript
import { combineUrlAndPath as d } from "@scalar/helpers/url/merge-urls";
import { inject as p, ref as c, toValue as a } from "vue";
const w = Symbol(), v = c(!1), U = c(""), S = c(""), y = () => {
const { isIntersectionEnabled: g, hash: i, hashPrefix: o, basePath: s, generateHeadingSlug: f } = p(w, {
isIntersectionEnabled: v,
hash: U,
hashPrefix: S,
basePath: void 0,
generateHeadingSlug: void 0
}), l = (e) => {
if (!a(s))
return "";
const t = new RegExp("^" + a(s) + "/?");
return decodeURIComponent(e.replace(t, ""));
}, h = () => a(s) ? l(window.location.pathname) : (
// Must remove the prefix from the hash as the internal hash value should be pure
decodeURIComponent(window.location.hash.replace(/^#/, "")).slice(o.value.length)
);
return {
hash: i,
/** Sets the prefix for the hash */
setHashPrefix: (e) => {
o.value = e;
},
/**
* Gets the full hash with the prefix
* @param hashTarget The hash to target with the return
* @returns The full hash
*/
getFullHash: (e = i.value) => `${o.value}${e}`,
/**
* Gets the hashed url with the prefix
* @param replacementHash The hash to replace the current hash with
* @param url The url to get the hashed url from
* @returns The hashed url
*/
getHashedUrl: (e, t = window.location.href, r = window.location.search) => {
const n = new URL(t), u = a(s);
return typeof u == "string" ? n.pathname = d(u, e) : n.hash = o.value + e, n.search = r, n.toString();
},
/**
* Replaces the URL state with the new url and hash
* Replacement is used so that hash changes don't trigger the url hash watcher and cause a scroll
*/
replaceUrlState: (e, t = window.location.href) => {
const r = new URL(t), n = a(s);
typeof n == "string" ? r.pathname = d(n, e) : r.hash = o.value + e, i.value = e, window.history.replaceState({}, "", r);
},
getReferenceId: h,
getHeadingId: (e) => {
const t = a(f);
return typeof t == "function" ? `${t(e)}` : e.slug ? `description/${e.slug}` : "";
},
getPathRoutingId: l,
isIntersectionEnabled: g,
updateHash: () => i.value = h()
};
};
export {
w as NAV_STATE_SYMBOL,
y as useNavState
};