Skip to content

Commit

Permalink
clean up plugins, add plausible
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Oct 8, 2024
1 parent 58ee0c1 commit a80bce3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 66 deletions.
64 changes: 0 additions & 64 deletions tools/api-plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Application, JSX, ParameterType, ReflectionKind } from "typedoc";

const optionSiteName = "plausibleSiteDomain";
const optionSiteOrigin = "plausibleSiteOrigin";

/**
* This plugin is used to load a plugin that throws an error
* @param {import("typedoc").Application} app
Expand All @@ -21,67 +18,6 @@ export function load(app) {
}
}
});

// Keywords support
// from https://github.com/matteobruni/typedoc-plugins/blob/main/plugins/keywords/src/index.tsx
app.options.addDeclaration({
name: "keywords",
type: ParameterType.Array,
help: "Website keywords",
});

app.renderer.hooks.on("head.begin", (ctx) => {
/** @type string[] */
const keywords = ctx.options.getValue("keywords");

// convert to JSX.createElement:
return JSX.createElement("meta", {
name: "keywords",
content: keywords.join(", "),
});
});

// Plausible support
// from https://gitlab.com/8hobbies/typedoc-plugin-plausible/-/blob/master/index.ts?ref_type=heads

app.options.addDeclaration({
name: optionSiteName,
type: ParameterType.String,
help: `Domain name used by Plausible Analytics.`,
});
app.options.addDeclaration({
name: optionSiteOrigin,
type: ParameterType.String,
help: `Base URL to get Plausible Analytics script from and report to. Should be everything but 'script.js'`,
defaultValue: "plausible.io/js/",
});
app.renderer.hooks.on("head.end", (ctx) => {
const plausibleSiteDomain = ctx.options.getValue(optionSiteName);
if (typeof plausibleSiteDomain !== "string") {
throw TypeError(
`Unexpected ${optionSiteName} type: ${JSON.stringify(plausibleSiteDomain)}`,
);
}
const plausibleSiteOrigin = ctx.options.getValue(optionSiteOrigin);
if (typeof plausibleSiteOrigin !== "string") {
throw TypeError(
`Unexpected ${optionSiteOrigin} type: ${JSON.stringify(plausibleSiteOrigin)}`,
);
}
const plausibleSrc = !plausibleSiteOrigin.endsWith("/")
? `${plausibleSiteOrigin}/`
: plausibleSiteOrigin;
if (plausibleSiteDomain === "") {
// No site specified.
return JSX.createElement(JSX.Fragment, {});
}

return JSX.createElement("script", {
defer: true,
"data-domain": plausibleSiteDomain,
src: `https://${plausibleSrc}script.js`,
});
});
}

const threejsDocsLinkCache = new Map();
Expand Down
27 changes: 27 additions & 0 deletions tools/api-plugins/keywords/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Application, JSX, ParameterType, ReflectionKind } from "typedoc";

/**
* @param {import("typedoc").Application} app
*/
export function load(app) {

// Keywords support
// from https://github.com/matteobruni/typedoc-plugins/blob/main/plugins/keywords/src/index.tsx
app.options.addDeclaration({
name: "keywords",
type: ParameterType.Array,
help: "Website keywords",
});

app.renderer.hooks.on("head.begin", (ctx) => {
/** @type string[] */
const keywords = ctx.options.getValue("keywords");

// convert to JSX.createElement:
return JSX.createElement("meta", {
name: "keywords",
content: keywords.join(", "),
});
});

}
81 changes: 81 additions & 0 deletions tools/api-plugins/plausible/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Application, JSX, ParameterType, ReflectionKind } from "typedoc";

const optionSiteName = "plausibleSiteDomain";
const optionSiteOrigin = "plausibleSiteOrigin";

/**
* This plugin is used to load a plugin that throws an error
* @param {import("typedoc").Application} app
*/
export function load(app) {

// Plausible support
// from https://gitlab.com/8hobbies/typedoc-plugin-plausible/-/blob/master/index.ts?ref_type=heads

app.options.addDeclaration({
name: optionSiteName,
type: ParameterType.String,
help: `Domain name used by Plausible Analytics.`,
});
app.options.addDeclaration({
name: optionSiteOrigin,
type: ParameterType.String,
help: `Base URL to get Plausible Analytics script from and report to. Should be everything but 'script.js'`,
defaultValue: "plausible.io/js/",
});
app.renderer.hooks.on("head.end", (ctx) => {

const plausibleSiteDomain = ctx.options.getValue(optionSiteName);
if (typeof plausibleSiteDomain !== "string") {
throw TypeError(
`Unexpected ${optionSiteName} type: ${JSON.stringify(plausibleSiteDomain)}`,
);
}
const plausibleSiteOrigin = ctx.options.getValue(optionSiteOrigin);
if (typeof plausibleSiteOrigin !== "string") {
throw TypeError(
`Unexpected ${optionSiteOrigin} type: ${JSON.stringify(plausibleSiteOrigin)}`,
);
}
const plausibleSrc = !plausibleSiteOrigin.endsWith("/")
? `${plausibleSiteOrigin}/`
: plausibleSiteOrigin;
if (plausibleSiteDomain === "") {
// No site specified.
return JSX.createElement(JSX.Fragment, {});
}

return [
JSX.createElement("script", {
defer: true,
"data-domain": plausibleSiteDomain,
src: `https://${plausibleSrc}script.manual.outbound-links.pageview-props.js`,
}),
JSX.createElement("script", null, JSX.createElement(JSX.Raw, {
html: "window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }"
})),
JSX.createElement("script", null, JSX.createElement(JSX.Raw, {
html: `
// the start of the URL has this form:
// /docs/api/@needle-tools/engine/3.48.3/<actually interesting part>
// so we need to extract the interesting part.
const url = window.location.pathname;
const parts = url.split("/");
// find index of the part that looks like a version (contains two dots and the first two parts are numbers)
const versionIndex = parts.findIndex((part, index) => {
const subParts = part.split(".");
return subParts.length === 3 && !isNaN(subParts[0]) && !isNaN(subParts[1]);
});
const interestingPart = parts.slice(versionIndex + 1).join("/");
const engineVersion = parts[versionIndex];
console.log("Plausible PageView", interestingPart, engineVersion);
plausible('pageview', {
u: "https://api.needle.tools/" + interestingPart + window.location.search,
props: { version: engineVersion },
});
`
}),
),
];
});
}
9 changes: 7 additions & 2 deletions tools/build_api_docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ async function produceDocs(packageDir, outputDirectory) {
packageDir + "/src/engine-components",
packageDir + "/src/engine-components-experimental",
packageDir + "/src/engine-schemes",
packageDir + "/src/needle-engine.ts",
],
tsconfig: "./tools/api-plugins/tsconfig.json",
// don't include references multiple times
Expand Down Expand Up @@ -231,6 +230,8 @@ async function produceDocs(packageDir, outputDirectory) {
// "typedoc-plugin-keywords",
"typedoc-plugin-extras",
"./tools/api-plugins/index.js",
"./tools/api-plugins/keywords/index.js",
"./tools/api-plugins/plausible/index.js",
],
keywords: ["typescript", "library", "threejs", "webgl", "engine", "browser", "webxr", "api"],
footerDate: true,
Expand All @@ -241,7 +242,11 @@ async function produceDocs(packageDir, outputDirectory) {
blockTags: [...TypeDoc.OptionDefaults.blockTags, "@link", "@obsolete", "@validate"],
validation: {
invalidLink: true,
}
},

// analytics
plausibleSiteDomain: "api.needle.tools",
plausibleSiteOrigin: "analytics.needle.tools/js/",
});

// inline sources plugin
Expand Down

0 comments on commit a80bce3

Please sign in to comment.