Skip to content

Add Smartcat Translator integration #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions integrations/smartcat/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
2 changes: 2 additions & 0 deletions integrations/smartcat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
package-lock.json
21 changes: 21 additions & 0 deletions integrations/smartcat/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: smartcat
title: Smartcat
organization: gitbook
visibility: unlisted
description: 'Add Smartcat to your GitBook to translate your content.'
script: src/index.tsx
scopes:
- space:script:inject
- site:script:inject
secrets: {}
contentSecurityPolicy:
script-src: https://cdn.smartcat-proxy.com;
configurations:
site:
properties:
site_tag:
type: string
title: Smartcat Site Tag
description: Available in Smartcat’s settings.
required:
- site_tag
17 changes: 17 additions & 0 deletions integrations/smartcat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "softwareone-smartcat",
"private": true,
"scripts": {
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
"typecheck": "tsc --noEmit",
"publish": "gitbook publish ."
},
"dependencies": {
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "^0.15.2",
"@gitbook/eslint-config": "*",
"@gitbook/tsconfig": "*"
}
}
35 changes: 35 additions & 0 deletions integrations/smartcat/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
createIntegration,
FetchPublishScriptEventCallback,
RuntimeContext,
RuntimeEnvironment,
} from '@gitbook/runtime';

import script from './script.raw.js';

type SmartcatRuntimeContext = RuntimeContext<
RuntimeEnvironment<
{},
{
site_tag?: string;
}
>
>;

export const handleFetchEvent: FetchPublishScriptEventCallback = async (
event,
{ environment }: SmartcatRuntimeContext
) => {
const smartCatScriptTag = environment.siteInstallation?.configuration?.site_tag;

return new Response(script.replace('<TO_REPLACE>', smartCatScriptTag), {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=604800',
},
});
};

export default createIntegration<SmartcatRuntimeContext>({
fetch_published_script: handleFetchEvent,
});
29 changes: 29 additions & 0 deletions integrations/smartcat/src/script.raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function loadScript() {
const smartCatScriptTag = '<TOREPLACE>';

const smartcatOrgIDRegExp =
'https://cdn.smartcat-proxy.com/([^/]+)/script-v1/__translator.js?hash=.*';

const smartcatHashRegExp =
'https://cdn.smartcat-proxy.com/[^/]+/script-v1/__translator.js?hash=([^/]+)';

const smartcatOrgID = smartCatScriptTag.match(smartcatOrgIDRegExp)[1];
const smartcatHash = smartCatScriptTag.match(smartcatHashRegExp)[1];

const divElement = document.createElement('div');
divElement.id = 'scLangSelectorContainer';
divElement.style.cssText = 'position:fixed; right:10px; bottom:10px; z-index:9999;';
document.body.appendChild(divElement);
const script = document.createElement('script');
script.id = 'sc-script';
script.src = `https://cdn.smartcat-proxy.com/${smartcatOrgID}/script-v1/__translator.js?hash=${smartcatHash}`;
document.head.appendChild(script);
}

(function () {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadScript);
} else {
loadScript();
}
})();
6 changes: 6 additions & 0 deletions integrations/smartcat/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@gitbook/tsconfig/integration.json",
"compilerOptions": {
"lib": ["ES6", "DOM"],
}
}
Loading