feat: implement fathom

This commit is contained in:
jinhojang6
2023-06-27 23:03:59 +09:00
parent 8c0d5ee41d
commit 966c6b3636
13 changed files with 177 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
# Change Log
+38
View File
@@ -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"
}
@@ -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')
}
}
@@ -0,0 +1,2 @@
export const SITE_ID = ''
export const SCRIPT_URL = ''
@@ -0,0 +1,4 @@
export type PluginOptions = {
siteId: string
scriptUrl: string
}
View File
+25
View File
@@ -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<undefined> {
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 }
+1
View File
@@ -0,0 +1 @@
export type * from './index'
@@ -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__/**"]
}
+14
View File
@@ -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__/**"]
}
@@ -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:
@@ -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')
}
}
@@ -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(