UNPKG

ntms

Version:

A dead simple way to add i18n to your Next.js app using the Notion API and Deepl

3 lines (2 loc) 6.56 kB
var e=function(){function e(){this.timeoutHandleForKey=void 0,this.timeoutHandleForKey={}}var t=e.prototype;return t.clearTimeout=function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){clearTimeout(this.timeoutHandleForKey[e])}),t.updateTimeout=function(e,t,n){this.clearTimeout(e),this.timeoutHandleForKey[e]=setTimeout(function(){n()},t)},e}(),t={},n=function(){function n(){this.keyTimeout=void 0,this.keyTimeout=new e}var r=n.prototype;return r.get=function(e){return t[e]},r.remove=function(e){this.keyTimeout.clearTimeout(e),delete t[e]},r.set=function(e,n){var r,o=this;Object.assign(t,((r={})[e]=n,r)),this.keyTimeout.updateTimeout(e,6e4,function(){return o.remove(e)})},n}(),r=require("node-fetch-cache").fetchBuilder.withCache(new n),o=function(e,t,n,o,i){try{var a={method:e,headers:{Authorization:process.env.NOTION_API_KEY,"Notion-Version":"2021-05-13",Accept:"application/json","Content-Type":"application/json"}};return i&&Object.assign(a,{body:i}),Promise.resolve(r("https://api.notion.com/v1/"+t+"/"+n+"/"+(o||""),a)).then(function(e){return e.json()})}catch(e){return Promise.reject(e)}},i=require("htmlparser2"),a=function(e){return e.map(function(e){return e.plain_text}).join("")},c=function(e,t){try{return Promise.resolve(r(process.env.DEEPL_URL,{method:"POST",body:(n={auth_key:process.env.DEEPL_API_KEY,text:e,tag_handling:"xml",ignore_tags:"pre,mustach",target_lang:t},o=[],"object"==typeof n&&null!==n&&Object.keys(n).map(function(e){var t=n[e];if(null!=t&&!(t instanceof Function)){var r=encodeURIComponent(e)+"="+encodeURIComponent(t);o.push(r)}}),o.join("&")),headers:{"content-type":"application/x-www-form-urlencoded"}})).then(function(e){return Promise.resolve(e.json()).then(function(e){return e.translations.map(function(e){return e.text}).join("")})})}catch(e){return Promise.reject(e)}var n,o};function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var s=function(e){var t=e.id,n=e.rich_text,r=e.locale,a=e.target_lang;try{var s=function(e){for(var t,n,r,o,i,a,c,u,s,l,f=[],h=e.length,p=0;p<h;p++)f.push((l=void 0,r=(t=e[p]).href,o=(n=t.annotations).bold,i=n.code,a=n.color,c=n.italic,u=n.strikethrough,s=n.underline,l=t.plain_text.replace(/{{/g,"<mustach>").replace(/}}/g,"</mustach>").replace(/\(\(/g,"<plural>").replace(/\)\)/g,"</plural>"),o&&(l="<b>"+l+"</b>"),i&&(l="<pre>"+l+"</pre>"),u&&(l="<strike>"+l+"</span>"),s&&(l="<u>"+l+"</u>"),c&&(l="<i>"+l+"</i>"),a&&"default"!==a&&(l='<mark data-color="'+a+'">'+l+"</mark>"),r&&(l='<a href="'+r+'">'+l+"</a>"),"<span>"+l+"</span>"));return f.join("")}(n);return Promise.resolve(c(s,a)).then(function(e){var n=function(e){var t,n=[],r=new i.Parser({onopentag:function(e,n){"span"===e&&(t={type:"text",plain_text:"",annotations:{bold:!1,italic:!1,strikethrough:!1,underline:!1,code:!1,color:"default"},href:null}),"mustach"==e&&(t.plain_text+="{{"),"plural"==e&&(t.plain_text+="(("),"b"===e&&(t.annotations.bold=!0),"pre"===e&&(t.annotations.code=!0),"strike"===e&&(t.annotations.strikethrough=!0),"u"===e&&(t.annotations.underline=!0),"mark"===e&&Object.keys(n).forEach(function(e){return"data-color"===e&&(t.annotations.color=n[e])}),"a"===e&&(t.href=n.href)},ontext:function(e){t.plain_text+=e},onclosetag:function(e){"mustach"==e&&(t.plain_text+="}}"),"plural"==e&&(t.plain_text+="))"),"span"===e&&n.push(t)}});return r.write(e),r.end(),n}(e);return Promise.resolve(function(e){var t=e.id,n=e.newRichText,r=e.locale;try{var i;return console.log({id:t,newRichText:n,locale:r}),Promise.resolve(o("patch","pages",t,void 0,JSON.stringify({properties:(i={},i[r]={rich_text:n.map(function(e){var t=e.annotations,n=void 0===t?{}:t,r=e.plain_text,o=e.href;return{type:"text",annotations:u({},n,{color:n.color||"default"}),text:{link:o?{url:o}:void 0,content:r}}})},i)})))}catch(e){return Promise.reject(e)}}({id:t,newRichText:n,locale:r})).then(function(){return n})})}catch(e){return Promise.reject(e)}},l=require("p-ratelimit"),f=["BG","CS","DA","DE","EL","EN-GB","EN-US","EN","ES","ET","FI","FR","HU","IT","JA","LT","LV","NL","PL","PT-PT","PT-BR","RO","RU","SK","SL","SV","ZH"],h=(0,l.pRateLimit)({interval:1e3,rate:10}),p=function(e){var t=e.deepl,n=e.results,r=e.locale,o=e.defaultLocale,i=e.defaultLocaleTranslations,c=e.notionLocales;try{var u={};return Promise.resolve(Promise.all(n.map(function(e){var n=e.properties,l=e.id;return h(function(){try{var e=function(){var e,t=function(e){return e.some(function(e){return Object.keys(e.annotations).some(function(t){var n=e.annotations[t];return"color"===t?!!n&&"default"!==n:n})||!!e.href})}(p)?p:a(p);Object.assign(u,((e={})[h]=t,e))},h=a(n.key.title),p=n[r]&&n[r].rich_text||[],d=a(p),m=0===p.length||0===d.length,v=function(){if(m){var e=(null==t?void 0:t[r])||f.find(function(e){return e===r.toUpperCase()}),a=!!i&&!!process.env.DEEPL_API_KEY&&!!process.env.DEEPL_URL&&e&&f.includes(e)&&c.includes(r),u=function(){if(a)return Promise.resolve(s({rich_text:n[o].rich_text,locale:r,id:l,target_lang:e})).then(function(e){p=e||n[o].rich_text});p=n[o].rich_text}();if(u&&u.then)return u.then(function(){})}}();return Promise.resolve(v&&v.then?v.then(e):e())}catch(e){return Promise.reject(e)}})}))).then(function(){return u})}catch(e){return Promise.reject(e)}};exports.deepl=c,exports.fetchTranslations=function(e,t){var n=t.locale,r=t.defaultLocale,i=t.deepl;try{if(!process.env.NOTION_API_KEY)throw new Error("A notion API key must be defined");if(void 0===r)throw new Error("A default locale must be defined inside your next.config.js file under the ntms object");if(void 0===n)throw new Error("This locale is undefined, please make sure your i18n config inside next.config.js handle it");var c={};return Promise.resolve(Promise.all(e.map(function(e){try{return Promise.resolve(o("get","databases",e)).then(function(t){var s=t.title,l=Object.keys(t.properties).filter(function(e){return"key"!==e});return Promise.resolve(o("post","databases",e,"query")).then(function(e){var t=e.results,o=a(s),f={notionLocales:l,deepl:i,results:t,defaultLocale:r};return Promise.resolve(p(u({locale:r},f))).then(function(e){return Promise.resolve(p(u({locale:n,defaultLocaleTranslations:e},f))).then(function(e){var t;Object.assign(c,((t={})[o]=e,t))})})})})}catch(e){return Promise.reject(e)}}))).then(function(){return c})}catch(e){return Promise.reject(e)}},exports.translate=s; //# sourceMappingURL=main.js.map