UNPKG

@wordpress/block-library

Version:
41 lines (40 loc) • 1.6 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_data = require("@wordpress/data"); let _wordpress_core_data = require("@wordpress/core-data"); // packages/block-library/src/utils/use-resolved-url.ts /** * Resolves a link against a base URL, the way a browser resolves an `href`. * * @param url The link, as stored in a block's attributes. * @param baseUrl The URL that relative links and hash links resolve against. * @return The absolute URL, or `undefined` when the link can't be resolved, * such as a relative link with no base URL. */ function getResolvedUrl(url, baseUrl) { if (!url) return; try { return new URL(url, baseUrl).href; } catch { return; } } /** * Resolves a link against the site's home URL. A page-relative link, such as * `#faq` or `./about`, depends on the page it's rendered on, which a block * doesn't know, so the home page stands in for it. * * @param url The link, as stored in a block's attributes. * @return The absolute URL, or `undefined` when the link can't be resolved. */ function useResolvedUrl(url) { const needsBaseUrl = !!url && getResolvedUrl(url) === void 0; const homeUrl = (0, _wordpress_data.useSelect)((select) => needsBaseUrl ? select(_wordpress_core_data.store).getEntityRecord("root", "__unstableBase")?.home : void 0, [needsBaseUrl]); return getResolvedUrl(url, homeUrl && homeUrl.replace(/\/?$/, "/")); } exports.getResolvedUrl = getResolvedUrl; exports.useResolvedUrl = useResolvedUrl; //# sourceMappingURL=use-resolved-url.cjs.map