@wordpress/block-library
Version:
Block library for the WordPress editor.
34 lines (33 loc) • 1.38 kB
JavaScript
import { prependHTTPS } from "@wordpress/url";
import { NEW_TAB_REL, NEW_TAB_TARGET, NOFOLLOW_REL } from "./constants.mjs";
// packages/block-library/src/button/get-updated-link-attributes.js
/**
* Updates the link attributes.
*
* @param {Object} attributes The current block attributes.
* @param {string} attributes.rel The current link rel attribute.
* @param {string} attributes.url The current link url.
* @param {boolean} attributes.opensInNewTab Whether the link should open in a new window.
* @param {boolean} attributes.nofollow Whether the link should be marked as nofollow.
*/
function getUpdatedLinkAttributes({ rel = "", url = "", opensInNewTab, nofollow }) {
let newLinkTarget;
let updatedRel = rel;
if (opensInNewTab) newLinkTarget = NEW_TAB_TARGET;
else {
const relRegex = new RegExp(`\\b${NEW_TAB_REL}\\s*`, "g");
updatedRel = updatedRel?.replace(relRegex, "").trim();
}
if (nofollow) updatedRel = updatedRel?.includes(NOFOLLOW_REL) ? updatedRel : (updatedRel + ` ${NOFOLLOW_REL}`).trim();
else {
const relRegex = new RegExp(`\\b${NOFOLLOW_REL}\\s*`, "g");
updatedRel = updatedRel?.replace(relRegex, "").trim();
}
return {
url: prependHTTPS(url),
linkTarget: newLinkTarget,
rel: updatedRel || void 0
};
}
export { getUpdatedLinkAttributes };
//# sourceMappingURL=get-updated-link-attributes.mjs.map