Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to include custom js? #2650

Closed
hesxenon opened this issue Jul 28, 2024 · 8 comments · Fixed by #2663
Closed

How to include custom js? #2650

hesxenon opened this issue Jul 28, 2024 · 8 comments · Fixed by #2663
Labels
enhancement Improved functionality good first issue Easier issue for first time contributors help wanted Contributions are especially encouraged
Milestone

Comments

@hesxenon
Copy link

Search terms

include js, include script, custom script source

Question

I'm developing a webcomponent library and I noticed that HTML that is inlined in the documentation is rendered. Would be quite nice to have the webcomponents render but for that I'd have to include the bundled script somehow. Is there a way to do that?

@hesxenon hesxenon added the question Question about functionality label Jul 28, 2024
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Jul 28, 2024

Currently only via a plugin which uses a hook to add it to the page like https://typedoc.org/api/documents/Renderer.Custom_Themes.html#md:hooks

I've been meaning to add a customJs option which behaves like customCss does for a while, just haven't gotten around to it

@Gerrit0 Gerrit0 added enhancement Improved functionality help wanted Contributions are especially encouraged good first issue Easier issue for first time contributors and removed question Question about functionality labels Jul 28, 2024
@Aryakoste
Copy link
Contributor

Hello @Gerrit0 I would like to work on this issue.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Aug 6, 2024

Go for it!

@hesxenon
Copy link
Author

@Aryakoste thanks for your work on this, but maybe it would be nice to have something like

"customJs": {
  src: "./dist/index.js",
  type: "module"
}

it seems the way this is currently implemented this only allows for non-esm scripts?

@Gerrit0 would you like me to open another issue for this or re-open this issue?

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Nov 22, 2024

I'm becoming really wary of adding too much behavior via options - TypeDoc already has so many...

This is also really easy to do with a plugin...

import { Application, JSX } from "typedoc";
import fs from "fs";
import path from "path";

/** @param {Application} app */
export function load(app) {
    app.renderer.hooks.on("head.end", (ctx) =>
        JSX.createElement("script", { src: ctx.relativeURL("./assets/custom.js"), type: "module" }),
    );

    app.renderer.on("beginRender", (event) => {
        fs.copyFileSync("path/to/custom.js", path.join(event.outputDirectory, "assets/custom.js"));
    });
}

If we add an option for type, okay... what about defer? async? crossorigin? integrity?

@hesxenon
Copy link
Author

hesxenon commented Nov 23, 2024

absolutely, I get that. I mean I would probably re-use the lib.dom.ts declarations and do something like

customJs: Array<
  Partial<
    PickByValueType<HTMLScriptElement, string | number | boolean>
  > & {src: string}
>

so that consumers can specify any valid script attribute at the cost of being able to specify a string only.

But speaking of plugins: is there a guide to plugin development somewhere? (beside the advice to look at existing plugins)

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Nov 23, 2024

Unfortunately that API would require a hardcoded special case in the options parser, which is not something I want to add. There are a number of similar options that it'd be convenient to have within objects, but the parser as is doesn't lend itself to it.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Nov 23, 2024

For plugin docs, there is the internal-docs folder in this repository and https://typedoc.org/guides/development/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality good first issue Easier issue for first time contributors help wanted Contributions are especially encouraged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants