Skip to content

Commit

Permalink
fix: render script if typedoc is >=0.24.5
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed May 10, 2023
1 parent df1becc commit ea31790
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> **Note**: TypeDoc natively supports this in [TypeStrong/typedoc#2153](https://github.com/TypeStrong/typedoc/issues/2153)
> **Note**: TypeDoc natively supports this in [v0.24.5](https://github.com/TypeStrong/typedoc/issues/2153)
# typedoc-plugin-copy-code-to-clipboard

Expand Down
59 changes: 59 additions & 0 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`load renders markup on "body.end" for typedoc v0.24.4 1`] = `
{
"children": [
{
"children": [
{
"children": [],
"props": {
"html": "style",
},
"tag": [Function],
},
],
"props": null,
"tag": "style",
},
{
"children": [
{
"children": [],
"props": {
"html": "(function(){scriptundefined();})();",
},
"tag": [Function],
},
],
"props": null,
"tag": "script",
},
],
"props": null,
"tag": Symbol(),
}
`;

exports[`load renders markup on "body.end" for typedoc v0.24.5 1`] = `
{
"children": [
{
"children": [
{
"children": [],
"props": {
"html": "style",
},
"tag": [Function],
},
],
"props": null,
"tag": "style",
},
false,
],
"props": null,
"tag": Symbol(),
}
`;
45 changes: 29 additions & 16 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import type { Application } from 'typedoc';
import { Application } from 'typedoc';

import { load } from '../src/index';

jest.mock('../src/script', () => 'script');
jest.mock('../src/style', () => 'style');

const { VERSION } = Application;

afterAll(() => {
Application.VERSION = VERSION;
});

describe('load', () => {
it('calls hook "body.end" with callback', () => {
const app = {
renderer: {
hooks: {
on: jest.fn(),
it.each(['0.24.4', '0.24.5'])(
'renders markup on "body.end" for typedoc v%s',
(version) => {
Application.VERSION = version;
const app = {
renderer: {
hooks: {
on: jest.fn(),
},
},
},
};
expect(load(app as unknown as Application)).toBe(undefined);
expect(app.renderer.hooks.on).toBeCalledTimes(1);
expect(app.renderer.hooks.on).toBeCalledWith(
'body.end',
expect.any(Function)
);
expect(app.renderer.hooks.on.mock.calls[0][1]()).toBeTruthy();
});
};
expect(load(app as unknown as Application)).toBe(undefined);
expect(app.renderer.hooks.on).toBeCalledTimes(1);
expect(app.renderer.hooks.on).toBeCalledWith(
'body.end',
expect.any(Function)
);
expect(app.renderer.hooks.on.mock.calls[0][1]()).toMatchSnapshot();
}
);
});
61 changes: 29 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"code block",
"clipboard"
],
"dependencies": {
"semver": "^7.5.0"
},
"devDependencies": {
"@commitlint/cli": "17.6.3",
"@commitlint/config-conventional": "17.6.3",
Expand All @@ -53,11 +56,11 @@
"prettier": "2.8.8",
"standard-version": "9.5.0",
"ts-jest": "29.1.0",
"typedoc": "0.24.1",
"typedoc": "0.24.7",
"typescript": "5.0.4"
},
"peerDependencies": {
"typedoc": "0.23 || 0.24"
"typedoc": "^0.24.7"
},
"files": [
"lib/"
Expand Down
27 changes: 27 additions & 0 deletions scripts/docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

DOCS_DIRECTORY='docs'

npm run docs

# https://developers.cloudflare.com/pages/platform/build-configuration/#environment-variables
if [[ $CF_PAGES == '1' ]]; then
# https://community.cloudflare.com/t/functions-dir-is-ignored-in-deploy/438211
# https://github.com/cloudflare/wrangler2/issues/2240#issuecomment-1343406897
FUNCTIONS='functions/'
RENAMED_FUNCTIONS='function/'
FUNCTIONS_DIRECTORY="$DOCS_DIRECTORY/$FUNCTIONS"
RENAMED_FUNCTIONS_DIRECTORY="$DOCS_DIRECTORY/$RENAMED_FUNCTIONS"

mv $FUNCTIONS_DIRECTORY $RENAMED_FUNCTIONS_DIRECTORY

if [[ $(uname) == 'Linux' ]]; then
sed -i "s|$FUNCTIONS|$RENAMED_FUNCTIONS|g" $DOCS_DIRECTORY/*.html
sed -i "s|$FUNCTIONS|$RENAMED_FUNCTIONS|g" $DOCS_DIRECTORY/assets/search.js
elif [[ $(uname) == 'Darwin' ]]; then
sed -i '' "s|$FUNCTIONS|$RENAMED_FUNCTIONS|g" $DOCS_DIRECTORY/*.html
sed -i '' "s|$FUNCTIONS|$RENAMED_FUNCTIONS|g" $DOCS_DIRECTORY/assets/search.js
fi

echo "Renamed $FUNCTIONS_DIRECTORY to $RENAMED_FUNCTIONS_DIRECTORY"
fi
14 changes: 10 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Application } from 'typedoc';
import semver from 'semver';
import { Application } from 'typedoc';
import { JSX } from 'typedoc';

import script from './script';
Expand All @@ -11,15 +12,20 @@ import { iife } from './utils';
* @param app - Reference to the application that is loading the plugin.
*/
export function load(app: Application): void {
// https://github.com/TypeStrong/typedoc/issues/2153
const hasNativeSupport = semver.gte(Application.VERSION, '0.24.5');

app.renderer.hooks.on('body.end', () => (
<>
<style>
<JSX.Raw html={style.trim()} />
</style>

<script>
<JSX.Raw html={iife(script)} />
</script>
{!hasNativeSupport && (
<script>
<JSX.Raw html={iife(script)} />
</script>
)}
</>
));
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"module": "commonjs",
"lib": ["dom"],
"declaration": true,
Expand Down

0 comments on commit ea31790

Please sign in to comment.