Skip to content

Commit

Permalink
Add useHostedBaseUrlForAbsoluteLinks option
Browse files Browse the repository at this point in the history
Closes #940
  • Loading branch information
Gerrit0 committed Jun 9, 2024
1 parent bb34bd5 commit 0ea1c2b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Update website docs - consider if reworking website to just be a TypeDoc generated site is a good idea
`@license`, `@import`, `@hideGroups` `@hideCategories` sitemapBaseUrl, markedOptions -> markdownItOptions, markdownItLoader, navigation
sort - documents-first, documents-last, alphabetical-ignoring-documents
searchInDocuments
searchInDocuments, useHostedBaseUrlForAbsoluteLinks
- #2147, add `referencedProperties` to a `ReferenceType` which is a set of name/comment pairs

### Breaking Changes
Expand Down Expand Up @@ -50,6 +50,7 @@
- TypeDoc will now warn if mutually exclusive modifier tags are specified for a comment (e.g. both `@alpha` and `@beta`), #2056.
- Added support for JSDoc `@hideconstructor` tag.
This tag should only be used to work around TypeScript#58653, prefer the more general `@hidden`/`@ignore` tag to hide members normally, #2577.
- Added `--useHostedBaseUrlForAbsoluteLinks` option to use the `--hostedBaseUrl` option to produce absolute links to pages on a site, #940.
- Fixed an issue where the "On This Page" section would include markdown if the page contained headings contained markdown.
- TypeDoc will now warn if a block tag is used which is not defined by the `--blockTags` option.
- Added three new sort strategies `documents-first`, `documents-last`, and `alphabetical-ignoring-documents` to order markdown documents.
Expand Down
10 changes: 10 additions & 0 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ export class Application extends ChildableComponent<
"You can define/override local locales with the `locales` option, or contribute them to TypeDoc!" as TranslatedString,
);
}

if (
this.options.getValue("useHostedBaseUrlForAbsoluteLinks") &&
!this.options.getValue("hostedBaseUrl")
) {
this.logger.warn(
this.i18n.useHostedBaseUrlForAbsoluteLinks_requires_hostedBaseUrl(),
);
this.options.setValue("useHostedBaseUrlForAbsoluteLinks", false);
}
}

private setOptions(options: Partial<TypeDocOptions>, reportErrors = true) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/internationalization/translatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ export const translatable = {
"Generate a .nojekyll file to prevent 404 errors in GitHub Pages. Defaults to `true`.",
help_hostedBaseUrl:
"Specify a base URL to be used in generating a sitemap.xml in our output folder and canonical links. If not specified, no sitemap will be generated.",
help_useHostedBaseUrlForAbsoluteLinks:
"If set, TypeDoc will produce absolute links to pages on your site using the hostedBaseUrl option.",
help_gaID:
"Set the Google Analytics tracking ID and activate tracking code.",
help_hideGenerator: "Do not print the TypeDoc link at the end of the page.",
Expand Down Expand Up @@ -362,6 +364,8 @@ export const translatable = {
"highlightLanguages contains invalid languages: {0}, run typedoc --help for a list of supported languages.",
hostedBaseUrl_must_start_with_http:
"hostedBaseUrl must start with http:// or https://",
useHostedBaseUrlForAbsoluteLinks_requires_hostedBaseUrl:
"The useHostedBaseUrlForAbsoluteLinks option requires that hostedBaseUrl be set",
option_0_must_be_an_object: "The '{0}' option must be a non-array object.",
option_0_must_be_a_function: "The '{0}' option must be a function.",
option_0_must_be_object_with_urls: `{0} must be an object with string labels as keys and URL values.`,
Expand Down
17 changes: 16 additions & 1 deletion src/lib/output/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from "../models/reflections/index";
import type { Renderer } from "./renderer";
import { RendererEvent, PageEvent } from "./events";
import { Option } from "../utils";

export { Component };

Expand Down Expand Up @@ -39,6 +40,14 @@ export abstract class ContextAwareRendererComponent extends RendererComponent {
*/
protected urlPrefix = /^(http|ftp)s?:\/\//;

private get hostedBaseUrl() {
const url = this.application.options.getValue("hostedBaseUrl");
return !url || url.endsWith("/") ? url : url + "/";
}

@Option("useHostedBaseUrlForAbsoluteLinks")
private accessor useHostedBaseUrlForAbsoluteLinks!: boolean;

/**
* Create a new ContextAwareRendererPlugin instance.
*
Expand Down Expand Up @@ -67,7 +76,13 @@ export abstract class ContextAwareRendererComponent extends RendererComponent {
const key = `${this.location}:${absolute}`;
let path = this.absoluteToRelativePathMap.get(key);
if (path) return path;
path = Path.posix.relative(this.location, absolute) || ".";

if (this.useHostedBaseUrlForAbsoluteLinks) {
path = new URL(absolute, this.hostedBaseUrl).toString();
} else {
path = Path.posix.relative(this.location, absolute) || ".";
}

this.absoluteToRelativePathMap.set(key, path);
return path;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/output/themes/default/assets/typedoc/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function buildNav() {
.pipeThrough(new DecompressionStream("gzip"));
const nav: NavigationElement[] = await new Response(json).json();

BASE_URL = container.dataset.base + "/";
BASE_URL = container.dataset.base!;
if (!BASE_URL.endsWith("/")) BASE_URL += "/";
container.innerHTML = "";
for (const el of nav) {
buildNavElement(el, container, []);
Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/options/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export interface TypeDocOptionMap {
cname: string;
githubPages: boolean;
hostedBaseUrl: string;
useHostedBaseUrlForAbsoluteLinks: boolean;
cacheBust: boolean;
gaID: string;
hideGenerator: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils/options/sources/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
}
},
});
options.addDeclaration({
name: "useHostedBaseUrlForAbsoluteLinks",
help: (i18n) => i18n.help_useHostedBaseUrlForAbsoluteLinks(),
type: ParameterType.Boolean,
});
options.addDeclaration({
name: "gaID",
help: (i18n) => i18n.help_gaID(),
Expand Down

0 comments on commit 0ea1c2b

Please sign in to comment.