diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 1529cb2..ce7d4b4 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -25,6 +25,13 @@ const config: Config = { 'docusaurus-plugin-image-zoom', './plugins/docusaurus-plugin-unified-tags', './plugins/docusaurus-plugin-recent-articles', + [ + './plugins/docusaurus-plugin-plausible-custom', + { + domain: 'docs.tellserv.fr', + scriptSrc: 'https://plausible.tellserv.fr/js/script.js', + }, + ], ], title: 'TellServ Tech Blog', diff --git a/plugins/docusaurus-plugin-plausible-custom.js b/plugins/docusaurus-plugin-plausible-custom.js new file mode 100644 index 0000000..b0ada88 --- /dev/null +++ b/plugins/docusaurus-plugin-plausible-custom.js @@ -0,0 +1,52 @@ +module.exports = function (_context, options) { + const { domain, scriptSrc } = options; + + if (!domain) { + throw new Error('You must specify the `domain` option for plausible-custom plugin.'); + } + + if (!scriptSrc) { + throw new Error('You must specify the `scriptSrc` option for plausible-custom plugin.'); + } + + const isProd = process.env.NODE_ENV === 'production'; + + return { + name: 'docusaurus-plugin-plausible-custom', + + injectHtmlTags() { + if (!isProd) { + return {}; + } + + return { + headTags: [ + { + tagName: 'link', + attributes: { + key: 'plausible-custom-preconnect', + rel: 'preconnect', + href: new URL(scriptSrc).origin, + }, + }, + { + tagName: 'script', + attributes: { + key: 'plausible-custom-script', + defer: true, + 'data-domain': domain, + src: scriptSrc, + }, + }, + { + tagName: 'script', + attributes: { + key: 'plausible-custom-events', + }, + innerHTML: 'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) };', + }, + ], + }; + }, + }; +};