Skip to content

Commit

Permalink
Add an option for external source links
Browse files Browse the repository at this point in the history
REsolves #2415
  • Loading branch information
Gerrit0 committed Oct 28, 2023
1 parent 9fbeeab commit 99f7a12
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Features

- Added `--sourceLinkExternal` option to render source code links as external, #2415.

## v0.25.2 (2023-10-08)

### Features
Expand Down
2 changes: 1 addition & 1 deletion src/lib/output/themes/default/partials/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function footer(context: DefaultThemeRenderContext) {
<div class="tsd-generator">
<p>
{"Generated using "}
<a href="https://typedoc.org/" rel="noopener" target="_blank">
<a href="https://typedoc.org/" target="_blank">
TypeDoc
</a>
</p>
Expand Down
53 changes: 33 additions & 20 deletions src/lib/output/themes/default/partials/member.sources.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext";
import { JSX } from "../../../../utils";
import type { DeclarationReflection, SignatureReflection } from "../../../../models";
import type { DeclarationReflection, SignatureReflection, SourceReference } from "../../../../models";

function sourceLink(context: DefaultThemeRenderContext, item: SourceReference) {
if (!item.url) {
return (
<li>
Defined in {item.fileName}:{item.line}
</li>
);
}

if (context.options.getValue("sourceLinkExternal")) {
return (
<li>
{"Defined in "}
<a href={item.url} class="external" target="_blank">
{item.fileName}:{item.line}
</a>
</li>
);
}

return (
<li>
{"Defined in "}
<a href={item.url}>
{item.fileName}:{item.line}
</a>
</li>
);
}

export const memberSources = (
context: DefaultThemeRenderContext,
Expand Down Expand Up @@ -32,25 +62,8 @@ export const memberSources = (
</p>,
);
}
if (props.sources) {
sources.push(
<ul>
{props.sources.map((item) =>
item.url ? (
<li>
{"Defined in "}
<a href={item.url}>
{item.fileName}:{item.line}
</a>
</li>
) : (
<li>
Defined in {item.fileName}:{item.line}
</li>
),
)}
</ul>,
);
if (props.sources?.length) {
sources.push(<ul>{props.sources.map((item) => sourceLink(context, item))}</ul>);
}

if (sources.length === 0) {
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 @@ -110,6 +110,7 @@ export interface TypeDocOptionMap {
includeVersion: boolean;
disableSources: boolean;
sourceLinkTemplate: string;
sourceLinkExternal: boolean;
disableGit: boolean;
gitRevision: string;
gitRemote: string;
Expand Down
35 changes: 20 additions & 15 deletions src/lib/utils/options/sources/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
help: "Disable setting the source of a reflection when documenting it.",
type: ParameterType.Boolean,
});
options.addDeclaration({
name: "sourceLinkTemplate",
help: "Specify a link template to be used when generating source urls. If not set, will be automatically created using the git remote. Supports {path}, {line}, {gitRevision} placeholders.",
});
options.addDeclaration({
name: "gitRevision",
help: "Use specified revision instead of the last revision for linking to GitHub/Bitbucket source files. Has no effect if disableSources is set.",
});
options.addDeclaration({
name: "gitRemote",
help: "Use the specified remote for linking to GitHub/Bitbucket source files. Has no effect if disableGit or disableSources is set.",
defaultValue: "origin",
});
options.addDeclaration({
name: "disableGit",
help: "Assume that all can be linked to with the sourceLinkTemplate, sourceLinkTemplate must be set if this is enabled. {path} will be rooted at basePath",
type: ParameterType.Boolean,
});
options.addDeclaration({
name: "basePath",
help: "Specifies the base path to be used when displaying file paths.",
Expand Down Expand Up @@ -344,23 +362,10 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
help: "Set the CNAME file text, it's useful for custom domains on GitHub Pages.",
});
options.addDeclaration({
name: "sourceLinkTemplate",
help: "Specify a link template to be used when generating source urls. If not set, will be automatically created using the git remote. Supports {path}, {line}, {gitRevision} placeholders.",
});
options.addDeclaration({
name: "disableGit",
help: "Assume that all can be linked to with the sourceLinkTemplate, sourceLinkTemplate must be set if this is enabled. {path} will be rooted at basePath",
name: "sourceLinkExternal",
help: "Specifies that source links should be treated as external links to be opened in a new tab.",
type: ParameterType.Boolean,
});
options.addDeclaration({
name: "gitRevision",
help: "Use specified revision instead of the last revision for linking to GitHub/Bitbucket source files. Has no effect if disableSources is set.",
});
options.addDeclaration({
name: "gitRemote",
help: "Use the specified remote for linking to GitHub/Bitbucket source files. Has no effect if disableGit or disableSources is set.",
defaultValue: "origin",
});
options.addDeclaration({
name: "githubPages",
help: "Generate a .nojekyll file to prevent 404 errors in GitHub Pages. Defaults to `true`.",
Expand Down

0 comments on commit 99f7a12

Please sign in to comment.