Skip to content

Commit

Permalink
Add favicon to TypeDoc generated output via plugin script (#132)
Browse files Browse the repository at this point in the history
## Problem
Currently, our generated documentation for the Typescript client doesn't
have a favicon, which would be nice. TypeDoc doesn't provide a
straightforward way of providing a favicon asset, but they do provide
the ability to write custom plugin scripts to modify the generated
output.

TypeDoc custom theme documentation:
https://github.com/TypeStrong/typedoc/blob/master/internal-docs/custom-themes.md

## Solution

- Add an `/assets/` folder to the top level of the repo.
- Add the `favicon-32x32.png` image (generated from:
https://www.figma.com/file/4tZhnN7BbFM6yWqPGjjVbf/Favicon?type=design&node-id=0-1&mode=design&t=nNcepCxWYRcJWwWx-0)
- Add a new `docs-theme.mjs` script to be provided to TypeDoc at build
time.

The `docs-theme.mjs` script does two things:
- `onPageRendered` takes the `page` output from typedoc and injects a
`<link .../>` into `head`.
- Copies `favicon-32x32.png` from `./assets` to the new `/docs` folder.

**Note:** I was having issues writing a plain `.js` file for the script
(no imports warning, etc), but `.mjs` seems to work well given our
current TS setup. Would definitely love feedback here around improving
things though, I didn't play around with our configs much.

|Before|After|
|-----|-----|
|![Screenshot 2023-10-03 at 5 45 39
PM](https://github.com/pinecone-io/pinecone-ts-client/assets/119623786/f1bbacf1-8d75-45db-b9aa-7bf3515bdd80)|![Screenshot
2023-10-03 at 5 45 44
PM](https://github.com/pinecone-io/pinecone-ts-client/assets/119623786/b6fd603f-3bc9-4a16-8f61-38bdd35d55c7)|

## Type of Change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update
- [X] Infrastructure change (CI configs, etc)
- [ ] Non-code change (docs, etc)
- [ ] None of the above: (explain here)

## Test Plan
You should be able to test locally by pulling this branch down, and
running `npm run docs:build`. Make sure the generated output includes
the Pinecone logo in the tab. Inspect the page source to confirm the
presence of `favicon-32x32.png`.

Confirm the docs build step of the PR workflow run completes as
expected.
  • Loading branch information
austin-denoble authored Oct 9, 2023
1 parent b0eb619 commit 895c434
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"browser": true,
"es2021": true
"es2021": true,
"node": true
},
"ignorePatterns": ["dist", "src/pinecone-generated-ts-fetch", "src/v0"],
"extends": [
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions assets/docs-theme.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { copyFileSync } from 'fs';
import { join } from 'path';

/**
* This script is passed into typedoc at build time, and is used to hook into their rendering
* pipeline allowing us to modify the output.
*
* example: typedoc --plugin ./assets/docs-theme.mjs
* TypeDoc documentation: https://github.com/TypeStrong/typedoc/blob/master/internal-docs/custom-themes.md#hooks-v0228
*/

export const load = (app) => {
// See PageEvent: https://github.com/TypeStrong/typedoc/blob/f2d2abe054feca91b89c00c33e1d726bbda85dcb/src/lib/output/events.ts#L134
app.renderer.on('endPage', onPageRendered.bind(this));
// See RendererEvent: https://github.com/TypeStrong/typedoc/blob/f2d2abe054feca91b89c00c33e1d726bbda85dcb/src/lib/output/events.ts#L47
app.renderer.once('endRender', onRenderFinished.bind(this));
};

function onPageRendered(page) {
// after the page is rendered we want to insert a favicon into head
if (page && page.contents) {
page.contents = page.contents.replace(
'</head>',
'<link rel="icon" href="./favicon-32x32.png"/></head>'
);
}
}

function onRenderFinished() {
// rendering complete, copy favicon asset into /docs folder
if (process) {
const workingDir = process.cwd();
const startingFavIcon = join(workingDir, '/assets/favicon-32x32.png');
const endingFavIcon = join(workingDir, './docs', '/favicon-32x32.png');

copyFileSync(startingFavIcon, endingFavIcon);
}
}
Binary file added assets/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"build": "rm -rf dist/ && tsc",
"version": "jq --null-input --arg version $npm_package_version '{\"name\": \"@pinecone-database/pinecone\", \"version\": $version}' > src/version.json",
"docs:build": "typedoc",
"docs:build": "typedoc --plugin ./assets/docs-theme.mjs",
"format": "prettier --write .",
"lint": "eslint src/ --ext .ts",
"repl": "npm run build && node utils/replInit.ts",
Expand Down
2 changes: 1 addition & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"includeVersion": true,
"excludeInternal": true,
"out": "docs",
"customCss": "./docs-styles.css",
"customCss": "./assets/docs-styles.css",
"navigationLinks": {
"Github Repo": "https://github.com/pinecone-io/pinecone-ts-client",
"Pinecone Docs": "https://docs.pinecone.io"
Expand Down

0 comments on commit 895c434

Please sign in to comment.