Skip to content

Commit

Permalink
Add markdownLinkExternal option
Browse files Browse the repository at this point in the history
Resolves #2679
  • Loading branch information
Gerrit0 committed Aug 23, 2024
1 parent 3dc7e96 commit 66b2c53
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- Added `markdownLinkExternal` option to treat `http[s]://` links in markdown documents and comments as external to be opened in a new tab, #2679.
- Added `navigation.excludeReferences` option to prevent re-exports from appearing in the left hand navigation, #2685.

### Bug Fixes
Expand Down
2 changes: 2 additions & 0 deletions src/lib/internationalization/translatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export const translatable = {
"Set the CNAME file text, it's useful for custom domains on GitHub Pages",
help_sourceLinkExternal:
"Specifies that source links should be treated as external links to be opened in a new tab",
help_markdownLinkExternal:
"Specifies that http[s]:// links in comments and markdown files should be treated as external links to be opened in a new tab",
help_githubPages:
"Generate a .nojekyll file to prevent 404 errors in GitHub Pages. Defaults to `true`",
help_hostedBaseUrl:
Expand Down
11 changes: 11 additions & 0 deletions src/lib/output/themes/MarkedPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class MarkedPlugin extends ContextAwareRendererComponent {
@Option("markdownItOptions")
accessor markdownItOptions!: Record<string, unknown>;

@Option("markdownLinkExternal")
accessor markdownLinkExternal!: boolean;

private parser?: markdown;

/**
Expand Down Expand Up @@ -263,6 +266,14 @@ export class MarkedPlugin extends ContextAwareRendererComponent {
const token = tokens[idx];
const href = token.attrGet("href")?.replace(/^#(?:md:)?(.+)/, "#md:$1");
if (href) {
// Note: This doesn't catch @link tags to reflections as those
// will be relative links. This will likely have to change with
// the introduction of support for customized routers whenever
// that becomes a real thing.
if (this.markdownLinkExternal && /https?:\/\//i.test(href)) {
token.attrSet("target", "_blank");
}

token.attrSet("href", href);
}
return self.renderToken(tokens, idx, options);
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 @@ -122,6 +122,7 @@ export interface TypeDocOptionMap {
disableSources: boolean;
sourceLinkTemplate: string;
sourceLinkExternal: boolean;
markdownLinkExternal: boolean;
disableGit: boolean;
gitRevision: string;
gitRemote: string;
Expand Down
8 changes: 7 additions & 1 deletion src/lib/utils/options/sources/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
help: (i18n) => i18n.help_sourceLinkExternal(),
type: ParameterType.Boolean,
});
options.addDeclaration({
name: "markdownLinkExternal",
help: (i18n) => i18n.help_markdownLinkExternal(),
type: ParameterType.Boolean,
defaultValue: true,
});
options.addDeclaration({
name: "githubPages",
help: (i18n) => i18n.help_githubPages(),
Expand All @@ -440,7 +446,7 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
name: "hostedBaseUrl",
help: (i18n) => i18n.help_hostedBaseUrl(),
validate(value, i18n) {
if (!/https?:\/\//.test(value)) {
if (!/https?:\/\//i.test(value)) {
throw new Error(i18n.hostedBaseUrl_must_start_with_http());
}
},
Expand Down

0 comments on commit 66b2c53

Please sign in to comment.