diff --git a/packages/docusaurus-fathom/CHANGELOG.md b/packages/docusaurus-fathom/CHANGELOG.md new file mode 100644 index 0000000..420e6f2 --- /dev/null +++ b/packages/docusaurus-fathom/CHANGELOG.md @@ -0,0 +1 @@ +# Change Log diff --git a/packages/docusaurus-fathom/package.json b/packages/docusaurus-fathom/package.json new file mode 100644 index 0000000..182f46b --- /dev/null +++ b/packages/docusaurus-fathom/package.json @@ -0,0 +1,38 @@ +{ + "name": "@acid-info/docusaurus-fathom", + "version": "1.0.0-alpha.1", + "description": "Docusaurus fathom plugin", + "main": "lib/index.js", + "types": "src/plugin.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/acid-info/logos-docusaurus-plugins.git", + "directory": "packages/docusaurus-fathom" + }, + "license": "MIT", + "scripts": { + "build": "tsc --build", + "watch": "tsc --build --watch", + "prepublishOnly": "yarn build" + }, + "dependencies": { + "@docusaurus/core": "^2.4.1", + "@docusaurus/module-type-aliases": "^2.4.1", + "@docusaurus/types": "^2.4.1", + "@docusaurus/utils": "^2.4.1", + "@docusaurus/utils-common": "^2.4.1", + "@docusaurus/utils-validation": "^2.4.1", + "lodash": "^4.17.21", + "node-html-parser": "^6.1.5", + "object-hash": "^3.0.0", + "satori": "^0.10.1", + "sharp": "^0.32.1" + }, + "engines": { + "node": ">=16.14" + }, + "devDependencies": { + "@types/lodash": "^4.14.186" + }, + "gitHead": "d2ee08c6c0678f78ad70f5d04183f7f781d11563" +} diff --git a/packages/docusaurus-fathom/src/client/index.ts b/packages/docusaurus-fathom/src/client/index.ts new file mode 100644 index 0000000..f70e3d8 --- /dev/null +++ b/packages/docusaurus-fathom/src/client/index.ts @@ -0,0 +1,33 @@ +import { SITE_ID, SCRIPT_URL } from './options' + +declare global { + interface Window { + fathom: any + } +} + +;(function (f: any, a: Window, t: string, h: string) { + a[h] = + a[h] || + function () { + ;(a[h].q = a[h].q || []).push(arguments) + } + const o = f.createElement('script') + const m = f.getElementsByTagName('script')[0] + o.async = 1 + o.src = t + o.id = 'fathom-script' + m.parentNode.insertBefore(o, m) +})(document, window, SCRIPT_URL, 'fathom') + +const { fathom } = window as any +fathom('set', 'siteId', SITE_ID) +fathom('trackPageview') + +console.log('PLUGIN_SITE_ID', SITE_ID) + +export function onRouteDidUpdate({ location, previousLocation }) { + if (location.pathname !== previousLocation?.pathname) { + fathom('trackPageview') + } +} diff --git a/packages/docusaurus-fathom/src/client/options.ts b/packages/docusaurus-fathom/src/client/options.ts new file mode 100644 index 0000000..2691185 --- /dev/null +++ b/packages/docusaurus-fathom/src/client/options.ts @@ -0,0 +1,2 @@ +export const SITE_ID = '' +export const SCRIPT_URL = '' diff --git a/packages/docusaurus-fathom/src/client/types/plugin.types.ts b/packages/docusaurus-fathom/src/client/types/plugin.types.ts new file mode 100644 index 0000000..5e0aabe --- /dev/null +++ b/packages/docusaurus-fathom/src/client/types/plugin.types.ts @@ -0,0 +1,4 @@ +export type PluginOptions = { + siteId: string + scriptUrl: string +} diff --git a/packages/docusaurus-fathom/src/deps.d.ts b/packages/docusaurus-fathom/src/deps.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/docusaurus-fathom/src/index.ts b/packages/docusaurus-fathom/src/index.ts new file mode 100644 index 0000000..9de6fb3 --- /dev/null +++ b/packages/docusaurus-fathom/src/index.ts @@ -0,0 +1,25 @@ +import type { LoadContext, Plugin } from '@docusaurus/types' +import { PluginOptions } from './client/types/plugin.types' +import path from 'path' +import fs from 'fs-extra' + +export default function fathomPlugin( + context: LoadContext, + options: PluginOptions, +): Plugin { + const { siteId, scriptUrl } = options + const dir = path.join(context.generatedFilesDir, 'docusaurus-fathom/default') + fs.ensureDirSync(dir) + + fs.writeFileSync( + path.join(dir, 'options.ts'), + `export const SITE_ID = '${siteId}'\nexport const SCRIPT_URL = '${scriptUrl}'`, + ) + + return { + name: 'docusaurus-fathom', + getClientModules: () => [path.resolve(__dirname, './client/index.js')], + } +} + +export { type PluginOptions } diff --git a/packages/docusaurus-fathom/src/plugin.d.ts b/packages/docusaurus-fathom/src/plugin.d.ts new file mode 100644 index 0000000..ccd487e --- /dev/null +++ b/packages/docusaurus-fathom/src/plugin.d.ts @@ -0,0 +1 @@ +export type * from './index' diff --git a/packages/docusaurus-fathom/tsconfig.client.json b/packages/docusaurus-fathom/tsconfig.client.json new file mode 100644 index 0000000..1085444 --- /dev/null +++ b/packages/docusaurus-fathom/tsconfig.client.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "noEmit": false, + "composite": true, + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo-client", + "rootDir": "src", + "outDir": "lib", + "module": "esnext", + "target": "esnext", + "lib": ["DOM"] + }, + "include": ["src/client", "src/*.d.ts"], + "exclude": ["**/__tests__/**"] +} diff --git a/packages/docusaurus-fathom/tsconfig.json b/packages/docusaurus-fathom/tsconfig.json new file mode 100644 index 0000000..ac2d96a --- /dev/null +++ b/packages/docusaurus-fathom/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "../../tsconfig.json", + "references": [{ "path": "./tsconfig.client.json" }], + "compilerOptions": { + "noEmit": false, + "incremental": true, + "tsBuildInfoFile": "./lib/.tsbuildinfo", + "rootDir": "src", + "outDir": "lib", + "lib": ["DOM"] + }, + "include": ["src"], + "exclude": ["src/theme", "**/__tests__/**"] +} diff --git a/packages/docusaurus-playground/docusaurus.config.js b/packages/docusaurus-playground/docusaurus.config.js index 8ed3f61..651e6c8 100644 --- a/packages/docusaurus-playground/docusaurus.config.js +++ b/packages/docusaurus-playground/docusaurus.config.js @@ -54,6 +54,8 @@ const config = { ], ], + // clientModules: ['./src/fathom.ts'], + plugins: [ [ '@docusaurus/plugin-content-blog', @@ -69,6 +71,13 @@ const config = { authorsMapPath: 'authors.yml', }), ], + [ + '@acid-info/docusaurus-fathom', + { + siteId: 'SIDE_ID_2', + scriptUrl: 'SCRIPT_URL_2', + }, + ], ], themeConfig: diff --git a/packages/docusaurus-playground/src/fathom.ts b/packages/docusaurus-playground/src/fathom.ts new file mode 100644 index 0000000..c45a4b0 --- /dev/null +++ b/packages/docusaurus-playground/src/fathom.ts @@ -0,0 +1,33 @@ +const SCRIPT_URL = '//fathom.status.im/tracker.js' +const SITE_ID = 'FUTMI' + +declare global { + interface Window { + fathom: any + } +} + +;(function (f: any, a: Window, t: string, h: string) { + a[h] = + a[h] || + function () { + ;(a[h].q = a[h].q || []).push(arguments) + } + const o = f.createElement('script') + const m = f.getElementsByTagName('script')[0] + o.async = 1 + o.src = t + o.id = 'fathom-script' + m.parentNode.insertBefore(o, m) +})(document, window, SCRIPT_URL, 'fathom') + +const { fathom } = window as any +fathom('set', 'siteId', SITE_ID) +fathom('trackPageview') + +export function onRouteDidUpdate({ location, previousLocation }) { + console.log('vvxcvxcv') + if (location.pathname !== previousLocation?.pathname) { + fathom('trackPageview') + } +} diff --git a/packages/logos-docusaurus-preset/src/index.ts b/packages/logos-docusaurus-preset/src/index.ts index eaae3c4..144d109 100644 --- a/packages/logos-docusaurus-preset/src/index.ts +++ b/packages/logos-docusaurus-preset/src/index.ts @@ -45,6 +45,7 @@ export default function logosPreset( context: LoadContext, options: PluginOptions, ): Preset { + //@ts-ignore const docsEnabled = options.docs !== false const siteConfig: typeof context.siteConfig = defaultsDeep(