From 8752c03a14334c7c6e753dd105f567c9c50e62c8 Mon Sep 17 00:00:00 2001 From: Henrique Mouta Date: Fri, 5 Apr 2024 13:27:43 +0100 Subject: [PATCH] Simplify generate smart links `if` condition --- build/content-helper/editor-sidebar.asset.php | 2 +- build/content-helper/editor-sidebar.js | 2 +- .../smart-linking/component.tsx | 23 +++++++------------ 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/build/content-helper/editor-sidebar.asset.php b/build/content-helper/editor-sidebar.asset.php index aa877e5b9..1805e42a7 100644 --- a/build/content-helper/editor-sidebar.asset.php +++ b/build/content-helper/editor-sidebar.asset.php @@ -1 +1 @@ - array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => '8f2597111b0af624758d'); + array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'ca76a3b8ba9fdb6aab99'); diff --git a/build/content-helper/editor-sidebar.js b/build/content-helper/editor-sidebar.js index dd303fecf..d35d24f7d 100644 --- a/build/content-helper/editor-sidebar.js +++ b/build/content-helper/editor-sidebar.js @@ -1,4 +1,4 @@ -!function(){"use strict";var e={251:function(e,t,n){var r=n(196),s=Symbol.for("react.element"),i=Symbol.for("react.fragment"),a=Object.prototype.hasOwnProperty,l=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,o={key:!0,ref:!0,__self:!0,__source:!0};function c(e,t,n){var r,i={},c=null,u=null;for(r in void 0!==n&&(c=""+n),void 0!==t.key&&(c=""+t.key),void 0!==t.ref&&(u=t.ref),t)a.call(t,r)&&!o.hasOwnProperty(r)&&(i[r]=t[r]);if(e&&e.defaultProps)for(r in t=e.defaultProps)void 0===i[r]&&(i[r]=t[r]);return{$$typeof:s,type:e,key:c,ref:u,props:i,_owner:l.current}}t.Fragment=i,t.jsx=c,t.jsxs=c},893:function(e,t,n){e.exports=n(251)},196:function(e){e.exports=window.React}},t={};function n(r){var s=t[r];if(void 0!==s)return s.exports;var i=t[r]={exports:{}};return e[r](i,i.exports,n),i.exports}n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,{a:t}),t},n.d=function(e,t){for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},function(){n.d({},{W:function(){return Xt}});var e,t,r,s=n(893),i=window.wp.components,a=window.wp.data,l=window.wp.editPost,o=window.wp.element,c=window.wp.i18n,u=n(196),p=window.wp.primitives,d=(0,u.createElement)(p.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},(0,u.createElement)(p.Path,{fillRule:"evenodd",d:"M11.25 5h1.5v15h-1.5V5zM6 10h1.5v10H6V10zm12 4h-1.5v6H18v-6z",clipRule:"evenodd"})),f=window.wp.plugins,g=function(){function e(){this._tkq=[],this.isLoaded=!1,this.isEnabled=!1,"undefined"!=typeof wpParselyTracksTelemetry&&(this.isEnabled=!0,this.loadTrackingLibrary())}return e.getInstance=function(){return window.wpParselyTelemetryInstance||Object.defineProperty(window,"wpParselyTelemetryInstance",{value:new e,writable:!1,configurable:!1,enumerable:!1}),window.wpParselyTelemetryInstance},e.prototype.loadTrackingLibrary=function(){var e=this,t=document.createElement("script");t.async=!0,t.src="//stats.wp.com/w.js",t.onload=function(){e.isLoaded=!0,e._tkq=window._tkq||[]},document.head.appendChild(t)},e.trackEvent=function(t){return n=this,r=arguments,i=function(t,n){var r;return void 0===n&&(n={}),function(e,t){var n,r,s,i,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(l){return function(o){return function(l){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,l[0]&&(a=0)),a;)try{if(n=1,r&&(s=2&l[0]?r.return:l[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,l[1])).done)return s;switch(r=0,s&&(l=[2&l[0],s.value]),l[0]){case 0:case 1:s=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!((s=(s=a.trys).length>0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]=1e4&&(clearInterval(i),n("Telemetry library not loaded"))}),100);else n("Telemetry not enabled")}))},e.prototype.trackEvent=function(t,n){var r;this.isLoaded?(0!==t.indexOf(e.TRACKS_PREFIX)&&(t=e.TRACKS_PREFIX+t),this.isEventNameValid(t)?(n=this.prepareProperties(n),null===(r=this._tkq)||void 0===r||r.push(["recordEvent",t,n])):console.error("Error tracking event: Invalid event name")):console.error("Error tracking event: Telemetry not loaded")},e.prototype.isTelemetryEnabled=function(){return this.isEnabled},e.prototype.isProprietyValid=function(t){return e.PROPERTY_REGEX.test(t)},e.prototype.isEventNameValid=function(t){return e.EVENT_NAME_REGEX.test(t)},e.prototype.prepareProperties=function(e){return(e=this.sanitizeProperties(e)).parsely_version=wpParselyTracksTelemetry.version,wpParselyTracksTelemetry.user&&(e._ut=wpParselyTracksTelemetry.user.type,e._ui=wpParselyTracksTelemetry.user.id),wpParselyTracksTelemetry.vipgo_env&&(e.vipgo_env=wpParselyTracksTelemetry.vipgo_env),this.sanitizeProperties(e)},e.prototype.sanitizeProperties=function(e){var t=this,n={};return Object.keys(e).forEach((function(r){t.isProprietyValid(r)&&(n[r]=e[r])})),n},e.TRACKS_PREFIX="wpparsely_",e.EVENT_NAME_REGEX=/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/,e.PROPERTY_REGEX=/^[a-z_][a-z0-9_]*$/,e}(),h=(g.trackEvent,function(){return(0,s.jsx)(i.SVG,{"aria-hidden":"true",version:"1.1",viewBox:"0 0 15 15",width:"15",height:"15",xmlns:"http://www.w3.org/2000/svg",children:(0,s.jsx)(i.Path,{d:"M0 14.0025V11.0025L7.5 3.5025L10.5 6.5025L3 14.0025H0ZM12 5.0025L13.56 3.4425C14.15 2.8525 14.15 1.9025 13.56 1.3225L12.68 0.4425C12.09 -0.1475 11.14 -0.1475 10.56 0.4425L9 2.0025L12 5.0025Z"})})}),y=function(e){var t=e.size,n=void 0===t?24:t,r=e.className,a=void 0===r?"wp-parsely-icon":r;return(0,s.jsxs)(i.SVG,{className:a,height:n,viewBox:"0 0 60 65",width:n,xmlns:"http://www.w3.org/2000/svg",children:[(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M23.72,51.53c0-.18,0-.34-.06-.52a13.11,13.11,0,0,0-2.1-5.53A14.74,14.74,0,0,0,19.12,43c-.27-.21-.5-.11-.51.22l-.24,3.42c0,.33-.38.35-.49,0l-1.5-4.8a1.4,1.4,0,0,0-.77-.78,23.91,23.91,0,0,0-3.1-.84c-1.38-.24-3.39-.39-3.39-.39-.34,0-.45.21-.25.49l2.06,3.76c.2.27,0,.54-.29.33l-4.51-3.6a3.68,3.68,0,0,0-2.86-.48c-1,.16-2.44.46-2.44.46a.68.68,0,0,0-.39.25.73.73,0,0,0-.14.45S.41,43,.54,44a3.63,3.63,0,0,0,1.25,2.62L6.48,50c.28.2.09.49-.23.37l-4.18-.94c-.32-.12-.5,0-.4.37,0,0,.69,1.89,1.31,3.16a24,24,0,0,0,1.66,2.74,1.34,1.34,0,0,0,1,.52l5,.13c.33,0,.41.38.1.48L7.51,58c-.31.1-.34.35-.07.55a14.29,14.29,0,0,0,3.05,1.66,13.09,13.09,0,0,0,5.9.5,25.13,25.13,0,0,0,4.34-1,9.55,9.55,0,0,1-.08-1.2,9.32,9.32,0,0,1,3.07-6.91"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M59.7,41.53a.73.73,0,0,0-.14-.45.68.68,0,0,0-.39-.25s-1.43-.3-2.44-.46a3.64,3.64,0,0,0-2.86.48l-4.51,3.6c-.26.21-.49-.06-.29-.33l2.06-3.76c.2-.28.09-.49-.25-.49,0,0-2,.15-3.39.39a23.91,23.91,0,0,0-3.1.84,1.4,1.4,0,0,0-.77.78l-1.5,4.8c-.11.32-.48.3-.49,0l-.24-3.42c0-.33-.24-.43-.51-.22a14.74,14.74,0,0,0-2.44,2.47A13.11,13.11,0,0,0,36.34,51c0,.18,0,.34-.06.52a9.26,9.26,0,0,1,3,8.1,24.1,24.1,0,0,0,4.34,1,13.09,13.09,0,0,0,5.9-.5,14.29,14.29,0,0,0,3.05-1.66c.27-.2.24-.45-.07-.55l-3.22-1.17c-.31-.1-.23-.47.1-.48l5-.13a1.38,1.38,0,0,0,1-.52A24.6,24.6,0,0,0,57,52.92c.61-1.27,1.31-3.16,1.31-3.16.1-.33-.08-.49-.4-.37l-4.18.94c-.32.12-.51-.17-.23-.37l4.69-3.34A3.63,3.63,0,0,0,59.46,44c.13-1,.24-2.47.24-2.47"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M46.5,25.61c0-.53-.35-.72-.8-.43l-4.86,2.66c-.45.28-.56-.27-.23-.69l4.66-6.23a2,2,0,0,0,.28-1.68,36.51,36.51,0,0,0-2.19-4.89,34,34,0,0,0-2.81-3.94c-.33-.41-.74-.35-.91.16l-2.28,5.68c-.16.5-.6.48-.59-.05l.28-8.93a2.54,2.54,0,0,0-.66-1.64S35,4.27,33.88,3.27,30.78.69,30.78.69a1.29,1.29,0,0,0-1.54,0s-1.88,1.49-3.12,2.59-2.48,2.35-2.48,2.35A2.5,2.5,0,0,0,23,7.27l.27,8.93c0,.53-.41.55-.58.05l-2.29-5.69c-.17-.5-.57-.56-.91-.14a35.77,35.77,0,0,0-3,4.2,35.55,35.55,0,0,0-2,4.62,2,2,0,0,0,.27,1.67l4.67,6.24c.33.42.23,1-.22.69l-4.87-2.66c-.45-.29-.82-.1-.82.43a18.6,18.6,0,0,0,.83,5.07,20.16,20.16,0,0,0,5.37,7.77c3.19,3,5.93,7.8,7.45,11.08A9.6,9.6,0,0,1,30,49.09a9.31,9.31,0,0,1,2.86.45c1.52-3.28,4.26-8.11,7.44-11.09a20.46,20.46,0,0,0,5.09-7,19,19,0,0,0,1.11-5.82"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M36.12,58.44A6.12,6.12,0,1,1,30,52.32a6.11,6.11,0,0,1,6.12,6.12"})]})},v=function(){return v=Object.assign||function(e){for(var t,n=1,r=arguments.length;nhere.',"wp-parsely"):i.code===A.ParselySuggestionsApiOpenAiError||i.code===A.ParselySuggestionsApiOpenAiUnavailable?i.message=(0,c.__)("The Parse.ly API returned an internal server error. Please retry with a different input, or try again later.","wp-parsely"):i.code===A.HttpRequestFailed&&i.message.includes("cURL error 28")?i.message=(0,c.__)("The Parse.ly API did not respond in a timely manner. Please try again later.","wp-parsely"):i.code===A.ParselySuggestionsApiSchemaError?i.message=(0,c.__)("The Parse.ly API returned a validation error. Please try again with different parameters.","wp-parsely"):i.code===A.ParselySuggestionsApiNoData?i.message=(0,c.__)("The Parse.ly API couldn't find any relevant data to fulfill the request. Please retry with a different input.","wp-parsely"):i.code===A.ParselySuggestionsApiOpenAiSchema?i.message=(0,c.__)("The Parse.ly API returned an incorrect response. Please try again later.","wp-parsely"):i.code===A.ParselySuggestionsApiAuthUnavailable&&(i.message=(0,c.__)("The Parse.ly API is currently unavailable. Please try again later.","wp-parsely")),i}return G(t,e),t.prototype.Message=function(e){return void 0===e&&(e=null),[A.PluginCredentialsNotSetMessageDetected,A.PluginSettingsSiteIdNotSet,A.PluginSettingsApiSecretNotSet].includes(this.code)?B(e):(this.code===A.FetchError&&(this.hint=this.Hint((0,c.__)("This error can sometimes be caused by ad-blockers or browser tracking protections. Please add this site to any applicable allow lists and try again.","wp-parsely"))),this.code!==A.ParselyApiForbidden&&this.code!==A.ParselySuggestionsApiNoAuthentication||(this.hint=this.Hint((0,c.__)("Please ensure that the Site ID and API Secret given in the plugin's settings are correct.","wp-parsely"))),this.code===A.HttpRequestFailed&&(this.hint=this.Hint((0,c.__)("The Parse.ly API cannot be reached. Please verify that you are online.","wp-parsely"))),(0,s.jsx)(I,{className:null==e?void 0:e.className,testId:"error",children:"

".concat(this.message,"

").concat(this.hint?this.hint:"")}))},t.prototype.Hint=function(e){return'

'.concat((0,c.__)("Hint:","wp-parsely")," ").concat(e,"

")},t}(Error);function z(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var W,U=function(e){var t=e.value,n=e.onChange,r=e.max,a=e.min,l=e.suffix,o=e.size,c=e.label,u=e.initialPosition,p=e.disabled,d=e.className;return(0,s.jsxs)("div",{className:"parsely-inputrange-control ".concat(d||""),children:[(0,s.jsx)(i.__experimentalHeading,{className:"parsely-inputrange-control__label",level:3,children:c}),(0,s.jsxs)("div",{className:"parsely-inputrange-control__controls",children:[(0,s.jsx)(i.__experimentalNumberControl,{disabled:p,value:t,suffix:(0,s.jsx)(i.__experimentalInputControlSuffixWrapper,{children:l}),size:null!=o?o:"__unstable-large",min:a,max:r,onChange:function(e){var t=parseInt(e,10);isNaN(t)||n(t)}}),(0,s.jsx)(i.RangeControl,{disabled:p,value:t,showTooltip:!1,initialPosition:u,onChange:function(e){n(e)},withInputField:!1,min:a,max:r})]})]})},$=function(){return $=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0)){var e={maxLinksPerPost:p.SmartLinking.MaxLinks,maxLinkWords:p.SmartLinking.MaxLinkWords};H(e)}}),[H,p]);var se=(0,a.useSelect)((function(e){var t=e("core/block-editor"),r=t.getSelectedBlock,s=t.getBlock,i=t.getBlocks,a=e("core/editor"),l=a.getEditedPostContent,o=a.getCurrentPostAttribute;return{allBlocks:i(),selectedBlock:n?s(n):r(),postContent:l(),postPermalink:o("link")}}),[n]),ie=se.allBlocks,ae=se.selectedBlock,le=se.postContent,oe=se.postPermalink,ce=function(e){return te(void 0,void 0,void 0,(function(){var t,n,r,s;return ne(this,(function(i){switch(i.label){case 0:t=[],i.label=1;case 1:return i.trys.push([1,7,,12]),[4,U((n=b||!ae)?W.All:W.Selected)];case 2:return i.sent(),a=oe.replace(/^https?:\/\//i,""),r=["http://"+a,"https://"+a],!(null==ae?void 0:ae.originalContent)||n?[3,4]:[4,J.generateSmartLinks(null==ae?void 0:ae.originalContent,T,j,r)];case 3:return t=i.sent(),[3,6];case 4:return[4,J.generateSmartLinks(le,T,j,r)];case 5:t=i.sent(),i.label=6;case 6:return[3,12];case 7:if((s=i.sent()).code&&s.code===A.ParselyAborted)throw s.numRetries=3-e,s;return e>0&&s.retryFetch?(console.error(s),[4,q(!0)]):[3,11];case 8:return i.sent(),[4,X()];case 9:return i.sent(),[4,ce(e-1)];case 10:return[2,i.sent()];case 11:throw s;case 12:return[4,M(t)];case 13:return i.sent(),[2,t]}var a}))}))},ue=function(e,t,n,r){var s=oe.replace(/^https?:\/\//,"").replace(/\/+$/,"");t=t.filter((function(e){return!e.href.includes(s)||(console.warn("PCH Smart Linking: Skipping self-reference link: ".concat(e.href)),!1)})),e.forEach((function(e){var s=!1;if(e.innerBlocks&&e.innerBlocks.length)ue(e.innerBlocks,t,n,r);else if(e.originalContent){var i=e.originalContent,a=(new DOMParser).parseFromString(i,"text/html").body.firstChild;a&&a instanceof HTMLElement&&(t.forEach((function(e){var t=function(e,t){for(var n,r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,{acceptNode:function(n){var r;if(!n.textContent||!n.textContent.includes(t))return NodeFilter.FILTER_REJECT;for(var s=n.parentNode;s&&s!==e;){if("A"===s.nodeName&&!(null===(r=s.textContent)||void 0===r?void 0:r.includes(t)))return NodeFilter.FILTER_REJECT;s=s.parentNode}return NodeFilter.FILTER_ACCEPT}}),s=[];n=r.nextNode();)s.push(n);return s}(a,e.text),r="".concat(e.text,"#").concat(e.offset);n[r]||(n[r]={encountered:0,linked:0}),t.forEach((function(t){var i;if(t.textContent){var a=n[r];if(a.linked>=1)return;for(var l=new RegExp(z(e.text),"g"),o=void 0;null!==(o=l.exec(t.textContent));)if(a.encountered++,a.encountered===e.offset+1){var c=document.createElement("a");c.href=e.href,c.title=e.title,c.textContent=o[0];var u=document.createRange();if(u.setStart(t,o.index),u.setEnd(t,o.index+o[0].length),u.deleteContents(),u.insertNode(c),t.textContent&&o.index+o[0].length[type="button"]').forEach((function(e){e.setAttribute("disabled","disabled")}))},he=function(){document.querySelectorAll('.edit-post-header__settings>[type="button"]').forEach((function(e){e.removeAttribute("disabled")})),(0,a.dispatch)("core/editor").unlockPostSaving("wp-parsely-block-overlay")};return(0,s.jsx)("div",{className:"wp-parsely-smart-linking",children:(0,s.jsxs)(i.PanelRow,{className:t,children:[(0,s.jsxs)("div",{className:"smart-linking-text",children:[(0,c.__)("Automatically insert links to your most relevant, top performing content.","wp-parsely"),(0,s.jsxs)(i.Button,{href:"https://docs.parse.ly/plugin-content-helper/#h-smart-linking-beta",target:"_blank",variant:"link",children:[(0,c.__)("Learn more about Parse.ly AI","wp-parsely"),(0,s.jsx)(V,{icon:F,size:18,className:"parsely-external-link-icon"})]})]}),P&&(0,s.jsx)(i.Notice,{status:"info",isDismissible:!0,onRemove:function(){return B(null)},className:"wp-parsely-content-helper-error",children:P.Message()}),null!==S&&(0,s.jsx)(i.Notice,{status:"success",isDismissible:!0,onRemove:function(){return M(null)},className:"wp-parsely-smart-linking-suggested-links",children:(0,c.sprintf)(/* translators: 1 - number of smart links generated */ +!function(){"use strict";var e={251:function(e,t,n){var r=n(196),s=Symbol.for("react.element"),i=Symbol.for("react.fragment"),a=Object.prototype.hasOwnProperty,l=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,o={key:!0,ref:!0,__self:!0,__source:!0};function c(e,t,n){var r,i={},c=null,u=null;for(r in void 0!==n&&(c=""+n),void 0!==t.key&&(c=""+t.key),void 0!==t.ref&&(u=t.ref),t)a.call(t,r)&&!o.hasOwnProperty(r)&&(i[r]=t[r]);if(e&&e.defaultProps)for(r in t=e.defaultProps)void 0===i[r]&&(i[r]=t[r]);return{$$typeof:s,type:e,key:c,ref:u,props:i,_owner:l.current}}t.Fragment=i,t.jsx=c,t.jsxs=c},893:function(e,t,n){e.exports=n(251)},196:function(e){e.exports=window.React}},t={};function n(r){var s=t[r];if(void 0!==s)return s.exports;var i=t[r]={exports:{}};return e[r](i,i.exports,n),i.exports}n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,{a:t}),t},n.d=function(e,t){for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},function(){n.d({},{W:function(){return Xt}});var e,t,r,s=n(893),i=window.wp.components,a=window.wp.data,l=window.wp.editPost,o=window.wp.element,c=window.wp.i18n,u=n(196),p=window.wp.primitives,d=(0,u.createElement)(p.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24"},(0,u.createElement)(p.Path,{fillRule:"evenodd",d:"M11.25 5h1.5v15h-1.5V5zM6 10h1.5v10H6V10zm12 4h-1.5v6H18v-6z",clipRule:"evenodd"})),f=window.wp.plugins,g=function(){function e(){this._tkq=[],this.isLoaded=!1,this.isEnabled=!1,"undefined"!=typeof wpParselyTracksTelemetry&&(this.isEnabled=!0,this.loadTrackingLibrary())}return e.getInstance=function(){return window.wpParselyTelemetryInstance||Object.defineProperty(window,"wpParselyTelemetryInstance",{value:new e,writable:!1,configurable:!1,enumerable:!1}),window.wpParselyTelemetryInstance},e.prototype.loadTrackingLibrary=function(){var e=this,t=document.createElement("script");t.async=!0,t.src="//stats.wp.com/w.js",t.onload=function(){e.isLoaded=!0,e._tkq=window._tkq||[]},document.head.appendChild(t)},e.trackEvent=function(t){return n=this,r=arguments,i=function(t,n){var r;return void 0===n&&(n={}),function(e,t){var n,r,s,i,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return i={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(l){return function(o){return function(l){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,l[0]&&(a=0)),a;)try{if(n=1,r&&(s=2&l[0]?r.return:l[0]?r.throw||((s=r.return)&&s.call(r),0):r.next)&&!(s=s.call(r,l[1])).done)return s;switch(r=0,s&&(l=[2&l[0],s.value]),l[0]){case 0:case 1:s=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,r=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!((s=(s=a.trys).length>0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]=1e4&&(clearInterval(i),n("Telemetry library not loaded"))}),100);else n("Telemetry not enabled")}))},e.prototype.trackEvent=function(t,n){var r;this.isLoaded?(0!==t.indexOf(e.TRACKS_PREFIX)&&(t=e.TRACKS_PREFIX+t),this.isEventNameValid(t)?(n=this.prepareProperties(n),null===(r=this._tkq)||void 0===r||r.push(["recordEvent",t,n])):console.error("Error tracking event: Invalid event name")):console.error("Error tracking event: Telemetry not loaded")},e.prototype.isTelemetryEnabled=function(){return this.isEnabled},e.prototype.isProprietyValid=function(t){return e.PROPERTY_REGEX.test(t)},e.prototype.isEventNameValid=function(t){return e.EVENT_NAME_REGEX.test(t)},e.prototype.prepareProperties=function(e){return(e=this.sanitizeProperties(e)).parsely_version=wpParselyTracksTelemetry.version,wpParselyTracksTelemetry.user&&(e._ut=wpParselyTracksTelemetry.user.type,e._ui=wpParselyTracksTelemetry.user.id),wpParselyTracksTelemetry.vipgo_env&&(e.vipgo_env=wpParselyTracksTelemetry.vipgo_env),this.sanitizeProperties(e)},e.prototype.sanitizeProperties=function(e){var t=this,n={};return Object.keys(e).forEach((function(r){t.isProprietyValid(r)&&(n[r]=e[r])})),n},e.TRACKS_PREFIX="wpparsely_",e.EVENT_NAME_REGEX=/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/,e.PROPERTY_REGEX=/^[a-z_][a-z0-9_]*$/,e}(),h=(g.trackEvent,function(){return(0,s.jsx)(i.SVG,{"aria-hidden":"true",version:"1.1",viewBox:"0 0 15 15",width:"15",height:"15",xmlns:"http://www.w3.org/2000/svg",children:(0,s.jsx)(i.Path,{d:"M0 14.0025V11.0025L7.5 3.5025L10.5 6.5025L3 14.0025H0ZM12 5.0025L13.56 3.4425C14.15 2.8525 14.15 1.9025 13.56 1.3225L12.68 0.4425C12.09 -0.1475 11.14 -0.1475 10.56 0.4425L9 2.0025L12 5.0025Z"})})}),y=function(e){var t=e.size,n=void 0===t?24:t,r=e.className,a=void 0===r?"wp-parsely-icon":r;return(0,s.jsxs)(i.SVG,{className:a,height:n,viewBox:"0 0 60 65",width:n,xmlns:"http://www.w3.org/2000/svg",children:[(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M23.72,51.53c0-.18,0-.34-.06-.52a13.11,13.11,0,0,0-2.1-5.53A14.74,14.74,0,0,0,19.12,43c-.27-.21-.5-.11-.51.22l-.24,3.42c0,.33-.38.35-.49,0l-1.5-4.8a1.4,1.4,0,0,0-.77-.78,23.91,23.91,0,0,0-3.1-.84c-1.38-.24-3.39-.39-3.39-.39-.34,0-.45.21-.25.49l2.06,3.76c.2.27,0,.54-.29.33l-4.51-3.6a3.68,3.68,0,0,0-2.86-.48c-1,.16-2.44.46-2.44.46a.68.68,0,0,0-.39.25.73.73,0,0,0-.14.45S.41,43,.54,44a3.63,3.63,0,0,0,1.25,2.62L6.48,50c.28.2.09.49-.23.37l-4.18-.94c-.32-.12-.5,0-.4.37,0,0,.69,1.89,1.31,3.16a24,24,0,0,0,1.66,2.74,1.34,1.34,0,0,0,1,.52l5,.13c.33,0,.41.38.1.48L7.51,58c-.31.1-.34.35-.07.55a14.29,14.29,0,0,0,3.05,1.66,13.09,13.09,0,0,0,5.9.5,25.13,25.13,0,0,0,4.34-1,9.55,9.55,0,0,1-.08-1.2,9.32,9.32,0,0,1,3.07-6.91"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M59.7,41.53a.73.73,0,0,0-.14-.45.68.68,0,0,0-.39-.25s-1.43-.3-2.44-.46a3.64,3.64,0,0,0-2.86.48l-4.51,3.6c-.26.21-.49-.06-.29-.33l2.06-3.76c.2-.28.09-.49-.25-.49,0,0-2,.15-3.39.39a23.91,23.91,0,0,0-3.1.84,1.4,1.4,0,0,0-.77.78l-1.5,4.8c-.11.32-.48.3-.49,0l-.24-3.42c0-.33-.24-.43-.51-.22a14.74,14.74,0,0,0-2.44,2.47A13.11,13.11,0,0,0,36.34,51c0,.18,0,.34-.06.52a9.26,9.26,0,0,1,3,8.1,24.1,24.1,0,0,0,4.34,1,13.09,13.09,0,0,0,5.9-.5,14.29,14.29,0,0,0,3.05-1.66c.27-.2.24-.45-.07-.55l-3.22-1.17c-.31-.1-.23-.47.1-.48l5-.13a1.38,1.38,0,0,0,1-.52A24.6,24.6,0,0,0,57,52.92c.61-1.27,1.31-3.16,1.31-3.16.1-.33-.08-.49-.4-.37l-4.18.94c-.32.12-.51-.17-.23-.37l4.69-3.34A3.63,3.63,0,0,0,59.46,44c.13-1,.24-2.47.24-2.47"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M46.5,25.61c0-.53-.35-.72-.8-.43l-4.86,2.66c-.45.28-.56-.27-.23-.69l4.66-6.23a2,2,0,0,0,.28-1.68,36.51,36.51,0,0,0-2.19-4.89,34,34,0,0,0-2.81-3.94c-.33-.41-.74-.35-.91.16l-2.28,5.68c-.16.5-.6.48-.59-.05l.28-8.93a2.54,2.54,0,0,0-.66-1.64S35,4.27,33.88,3.27,30.78.69,30.78.69a1.29,1.29,0,0,0-1.54,0s-1.88,1.49-3.12,2.59-2.48,2.35-2.48,2.35A2.5,2.5,0,0,0,23,7.27l.27,8.93c0,.53-.41.55-.58.05l-2.29-5.69c-.17-.5-.57-.56-.91-.14a35.77,35.77,0,0,0-3,4.2,35.55,35.55,0,0,0-2,4.62,2,2,0,0,0,.27,1.67l4.67,6.24c.33.42.23,1-.22.69l-4.87-2.66c-.45-.29-.82-.1-.82.43a18.6,18.6,0,0,0,.83,5.07,20.16,20.16,0,0,0,5.37,7.77c3.19,3,5.93,7.8,7.45,11.08A9.6,9.6,0,0,1,30,49.09a9.31,9.31,0,0,1,2.86.45c1.52-3.28,4.26-8.11,7.44-11.09a20.46,20.46,0,0,0,5.09-7,19,19,0,0,0,1.11-5.82"}),(0,s.jsx)(i.Path,{fill:"#5ba745",d:"M36.12,58.44A6.12,6.12,0,1,1,30,52.32a6.11,6.11,0,0,1,6.12,6.12"})]})},v=function(){return v=Object.assign||function(e){for(var t,n=1,r=arguments.length;nhere.',"wp-parsely"):i.code===A.ParselySuggestionsApiOpenAiError||i.code===A.ParselySuggestionsApiOpenAiUnavailable?i.message=(0,c.__)("The Parse.ly API returned an internal server error. Please retry with a different input, or try again later.","wp-parsely"):i.code===A.HttpRequestFailed&&i.message.includes("cURL error 28")?i.message=(0,c.__)("The Parse.ly API did not respond in a timely manner. Please try again later.","wp-parsely"):i.code===A.ParselySuggestionsApiSchemaError?i.message=(0,c.__)("The Parse.ly API returned a validation error. Please try again with different parameters.","wp-parsely"):i.code===A.ParselySuggestionsApiNoData?i.message=(0,c.__)("The Parse.ly API couldn't find any relevant data to fulfill the request. Please retry with a different input.","wp-parsely"):i.code===A.ParselySuggestionsApiOpenAiSchema?i.message=(0,c.__)("The Parse.ly API returned an incorrect response. Please try again later.","wp-parsely"):i.code===A.ParselySuggestionsApiAuthUnavailable&&(i.message=(0,c.__)("The Parse.ly API is currently unavailable. Please try again later.","wp-parsely")),i}return G(t,e),t.prototype.Message=function(e){return void 0===e&&(e=null),[A.PluginCredentialsNotSetMessageDetected,A.PluginSettingsSiteIdNotSet,A.PluginSettingsApiSecretNotSet].includes(this.code)?B(e):(this.code===A.FetchError&&(this.hint=this.Hint((0,c.__)("This error can sometimes be caused by ad-blockers or browser tracking protections. Please add this site to any applicable allow lists and try again.","wp-parsely"))),this.code!==A.ParselyApiForbidden&&this.code!==A.ParselySuggestionsApiNoAuthentication||(this.hint=this.Hint((0,c.__)("Please ensure that the Site ID and API Secret given in the plugin's settings are correct.","wp-parsely"))),this.code===A.HttpRequestFailed&&(this.hint=this.Hint((0,c.__)("The Parse.ly API cannot be reached. Please verify that you are online.","wp-parsely"))),(0,s.jsx)(I,{className:null==e?void 0:e.className,testId:"error",children:"

".concat(this.message,"

").concat(this.hint?this.hint:"")}))},t.prototype.Hint=function(e){return'

'.concat((0,c.__)("Hint:","wp-parsely")," ").concat(e,"

")},t}(Error);function z(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var W,U=function(e){var t=e.value,n=e.onChange,r=e.max,a=e.min,l=e.suffix,o=e.size,c=e.label,u=e.initialPosition,p=e.disabled,d=e.className;return(0,s.jsxs)("div",{className:"parsely-inputrange-control ".concat(d||""),children:[(0,s.jsx)(i.__experimentalHeading,{className:"parsely-inputrange-control__label",level:3,children:c}),(0,s.jsxs)("div",{className:"parsely-inputrange-control__controls",children:[(0,s.jsx)(i.__experimentalNumberControl,{disabled:p,value:t,suffix:(0,s.jsx)(i.__experimentalInputControlSuffixWrapper,{children:l}),size:null!=o?o:"__unstable-large",min:a,max:r,onChange:function(e){var t=parseInt(e,10);isNaN(t)||n(t)}}),(0,s.jsx)(i.RangeControl,{disabled:p,value:t,showTooltip:!1,initialPosition:u,onChange:function(e){n(e)},withInputField:!1,min:a,max:r})]})]})},$=function(){return $=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0&&s[s.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!s||l[1]>s[0]&&l[1]0)){var e={maxLinksPerPost:p.SmartLinking.MaxLinks,maxLinkWords:p.SmartLinking.MaxLinkWords};H(e)}}),[H,p]);var se=(0,a.useSelect)((function(e){var t=e("core/block-editor"),r=t.getSelectedBlock,s=t.getBlock,i=t.getBlocks,a=e("core/editor"),l=a.getEditedPostContent,o=a.getCurrentPostAttribute;return{allBlocks:i(),selectedBlock:n?s(n):r(),postContent:l(),postPermalink:o("link")}}),[n]),ie=se.allBlocks,ae=se.selectedBlock,le=se.postContent,oe=se.postPermalink,ce=function(e){return te(void 0,void 0,void 0,(function(){var t,n,r,s;return ne(this,(function(i){switch(i.label){case 0:t=[],i.label=1;case 1:return i.trys.push([1,4,,9]),[4,U((n=b||!ae)?W.All:W.Selected)];case 2:return i.sent(),a=oe.replace(/^https?:\/\//i,""),r=["http://"+a,"https://"+a],[4,J.generateSmartLinks((null==ae?void 0:ae.originalContent)&&!n?ae.originalContent:le,T,j,r)];case 3:return t=i.sent(),[3,9];case 4:if((s=i.sent()).code&&s.code===A.ParselyAborted)throw s.numRetries=3-e,s;return e>0&&s.retryFetch?(console.error(s),[4,q(!0)]):[3,8];case 5:return i.sent(),[4,X()];case 6:return i.sent(),[4,ce(e-1)];case 7:return[2,i.sent()];case 8:throw s;case 9:return[4,M(t)];case 10:return i.sent(),[2,t]}var a}))}))},ue=function(e,t,n,r){var s=oe.replace(/^https?:\/\//,"").replace(/\/+$/,"");t=t.filter((function(e){return!e.href.includes(s)||(console.warn("PCH Smart Linking: Skipping self-reference link: ".concat(e.href)),!1)})),e.forEach((function(e){var s=!1;if(e.innerBlocks&&e.innerBlocks.length)ue(e.innerBlocks,t,n,r);else if(e.originalContent){var i=e.originalContent,a=(new DOMParser).parseFromString(i,"text/html").body.firstChild;a&&a instanceof HTMLElement&&(t.forEach((function(e){var t=function(e,t){for(var n,r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,{acceptNode:function(n){var r;if(!n.textContent||!n.textContent.includes(t))return NodeFilter.FILTER_REJECT;for(var s=n.parentNode;s&&s!==e;){if("A"===s.nodeName&&!(null===(r=s.textContent)||void 0===r?void 0:r.includes(t)))return NodeFilter.FILTER_REJECT;s=s.parentNode}return NodeFilter.FILTER_ACCEPT}}),s=[];n=r.nextNode();)s.push(n);return s}(a,e.text),r="".concat(e.text,"#").concat(e.offset);n[r]||(n[r]={encountered:0,linked:0}),t.forEach((function(t){var i;if(t.textContent){var a=n[r];if(a.linked>=1)return;for(var l=new RegExp(z(e.text),"g"),o=void 0;null!==(o=l.exec(t.textContent));)if(a.encountered++,a.encountered===e.offset+1){var c=document.createElement("a");c.href=e.href,c.title=e.title,c.textContent=o[0];var u=document.createRange();if(u.setStart(t,o.index),u.setEnd(t,o.index+o[0].length),u.deleteContents(),u.insertNode(c),t.textContent&&o.index+o[0].length[type="button"]').forEach((function(e){e.setAttribute("disabled","disabled")}))},he=function(){document.querySelectorAll('.edit-post-header__settings>[type="button"]').forEach((function(e){e.removeAttribute("disabled")})),(0,a.dispatch)("core/editor").unlockPostSaving("wp-parsely-block-overlay")};return(0,s.jsx)("div",{className:"wp-parsely-smart-linking",children:(0,s.jsxs)(i.PanelRow,{className:t,children:[(0,s.jsxs)("div",{className:"smart-linking-text",children:[(0,c.__)("Automatically insert links to your most relevant, top performing content.","wp-parsely"),(0,s.jsxs)(i.Button,{href:"https://docs.parse.ly/plugin-content-helper/#h-smart-linking-beta",target:"_blank",variant:"link",children:[(0,c.__)("Learn more about Parse.ly AI","wp-parsely"),(0,s.jsx)(V,{icon:F,size:18,className:"parsely-external-link-icon"})]})]}),P&&(0,s.jsx)(i.Notice,{status:"info",isDismissible:!0,onRemove:function(){return B(null)},className:"wp-parsely-content-helper-error",children:P.Message()}),null!==S&&(0,s.jsx)(i.Notice,{status:"success",isDismissible:!0,onRemove:function(){return M(null)},className:"wp-parsely-smart-linking-suggested-links",children:(0,c.sprintf)(/* translators: 1 - number of smart links generated */ (0,c.__)("Successfully added %s smart links.","wp-parsely"),y>0?y:S.length)}),(0,s.jsx)(Q,{disabled:_,selectedBlock:null==ae?void 0:ae.clientId,onSettingChange:function(e,t){var n;f({SmartLinking:ee(ee({},p.SmartLinking),(n={},n[e]=t,n))}),"MaxLinks"===e?Z(t):"MaxLinkWords"===e&&$(t)}}),(0,s.jsx)("div",{className:"smart-linking-generate",children:(0,s.jsx)(i.Button,{onClick:function(){return te(void 0,void 0,void 0,(function(){var e,t,n,r,s;return ne(this,(function(i){switch(i.label){case 0:return[4,I(!0)];case 1:return i.sent(),[4,M(null)];case 2:return i.sent(),[4,B(null)];case 3:return i.sent(),g.trackEvent("smart_linking_generate_pressed",{is_full_content:b,selected_block:null!==(s=null==ae?void 0:ae.name)&&void 0!==s?s:"none",context:l}),[4,de(b?"all":null==ae?void 0:ae.clientId)];case 4:i.sent(),e=setTimeout((function(){var e;I(!1),g.trackEvent("smart_linking_generate_timeout",{is_full_content:b,selected_block:null!==(e=null==ae?void 0:ae.name)&&void 0!==e?e:"none",context:l}),fe(b?"all":null==ae?void 0:ae.clientId)}),18e4),t=C,i.label=5;case 5:return i.trys.push([5,7,9,14]),[4,ce(3)];case 6:return function(e){var t;g.trackEvent("smart_linking_applied",{is_full_content:b||!ae,selected_block:null!==(t=null==ae?void 0:ae.name)&&void 0!==t?t:"none",links_count:e.length,context:l});var n={},r=[];ue(ae&&!b?[ae]:ie,e,n,r),pe(r);var s=Object.values(n).reduce((function(e,t){return e+t.linked}),0);v(s),m("success",/* translators: %d: number of smart links applied */ (0,c.sprintf)((0,c.__)("%s smart links successfully applied.","wp-parsely"),s),{type:"snackbar",isDismissible:!0})}(i.sent()),[3,14];case 7:return n=i.sent(),r=(0,c.__)("There was a problem applying smart links.","wp-parsely"),n.code&&n.code===A.ParselyAborted&&(r=(0,c.sprintf)(/* translators: %d: number of retry attempts, %s: attempt plural */ (0,c.__)("The Smart Linking process was cancelled after %1$d %2$s.","wp-parsely"),n.numRetries,(0,c._n)("attempt","attempts",n.numRetries,"wp-parsely")),n.message=r),[4,B(n)];case 8:return i.sent(),m("error",r,{type:"snackbar",isDismissible:!0}),[3,14];case 9:return[4,I(!1)];case 10:return i.sent(),[4,U(t)];case 11:return i.sent(),[4,q(!1)];case 12:return i.sent(),[4,fe(b?"all":null==ae?void 0:ae.clientId)];case 13:return i.sent(),clearTimeout(e),[7];case 14:return[2]}}))}))},variant:"primary",isBusy:_,disabled:_,children:L?(0,c.sprintf)(/* translators: %1$d: number of retry attempts, %2$d: maximum number of retries */ diff --git a/src/content-helper/editor-sidebar/smart-linking/component.tsx b/src/content-helper/editor-sidebar/smart-linking/component.tsx index e4cc36274..b04cf520c 100644 --- a/src/content-helper/editor-sidebar/smart-linking/component.tsx +++ b/src/content-helper/editor-sidebar/smart-linking/component.tsx @@ -322,21 +322,14 @@ export const SmartLinkingPanel = ( { const urlExclusionList = generateProtocolVariants( postPermalink ); - if ( selectedBlock?.originalContent && ! generatingFullContent ) { - generatedLinks = await SmartLinkingProvider.generateSmartLinks( - selectedBlock?.originalContent, - maxLinkWords, - maxLinks, - urlExclusionList - ); - } else { - generatedLinks = await SmartLinkingProvider.generateSmartLinks( - postContent, - maxLinkWords, - maxLinks, - urlExclusionList - ); - } + generatedLinks = await SmartLinkingProvider.generateSmartLinks( + ( selectedBlock?.originalContent && ! generatingFullContent ) + ? selectedBlock.originalContent + : postContent, + maxLinkWords, + maxLinks, + urlExclusionList + ); } catch ( err: any ) { // eslint-disable-line @typescript-eslint/no-explicit-any // If the request was aborted, throw the AbortError to be handled elsewhere. if ( err.code && err.code === ContentHelperErrorCode.ParselyAborted ) {