Skip to content

Commit 66b2c53

Browse files
committed
Add markdownLinkExternal option
Resolves #2679
1 parent 3dc7e96 commit 66b2c53

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

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

78
### Bug Fixes

src/lib/internationalization/translatable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ export const translatable = {
270270
"Set the CNAME file text, it's useful for custom domains on GitHub Pages",
271271
help_sourceLinkExternal:
272272
"Specifies that source links should be treated as external links to be opened in a new tab",
273+
help_markdownLinkExternal:
274+
"Specifies that http[s]:// links in comments and markdown files should be treated as external links to be opened in a new tab",
273275
help_githubPages:
274276
"Generate a .nojekyll file to prevent 404 errors in GitHub Pages. Defaults to `true`",
275277
help_hostedBaseUrl:

src/lib/output/themes/MarkedPlugin.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class MarkedPlugin extends ContextAwareRendererComponent {
3636
@Option("markdownItOptions")
3737
accessor markdownItOptions!: Record<string, unknown>;
3838

39+
@Option("markdownLinkExternal")
40+
accessor markdownLinkExternal!: boolean;
41+
3942
private parser?: markdown;
4043

4144
/**
@@ -263,6 +266,14 @@ export class MarkedPlugin extends ContextAwareRendererComponent {
263266
const token = tokens[idx];
264267
const href = token.attrGet("href")?.replace(/^#(?:md:)?(.+)/, "#md:$1");
265268
if (href) {
269+
// Note: This doesn't catch @link tags to reflections as those
270+
// will be relative links. This will likely have to change with
271+
// the introduction of support for customized routers whenever
272+
// that becomes a real thing.
273+
if (this.markdownLinkExternal && /https?:\/\//i.test(href)) {
274+
token.attrSet("target", "_blank");
275+
}
276+
266277
token.attrSet("href", href);
267278
}
268279
return self.renderToken(tokens, idx, options);

src/lib/utils/options/declaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export interface TypeDocOptionMap {
122122
disableSources: boolean;
123123
sourceLinkTemplate: string;
124124
sourceLinkExternal: boolean;
125+
markdownLinkExternal: boolean;
125126
disableGit: boolean;
126127
gitRevision: string;
127128
gitRemote: string;

src/lib/utils/options/sources/typedoc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
430430
help: (i18n) => i18n.help_sourceLinkExternal(),
431431
type: ParameterType.Boolean,
432432
});
433+
options.addDeclaration({
434+
name: "markdownLinkExternal",
435+
help: (i18n) => i18n.help_markdownLinkExternal(),
436+
type: ParameterType.Boolean,
437+
defaultValue: true,
438+
});
433439
options.addDeclaration({
434440
name: "githubPages",
435441
help: (i18n) => i18n.help_githubPages(),
@@ -440,7 +446,7 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
440446
name: "hostedBaseUrl",
441447
help: (i18n) => i18n.help_hostedBaseUrl(),
442448
validate(value, i18n) {
443-
if (!/https?:\/\//.test(value)) {
449+
if (!/https?:\/\//i.test(value)) {
444450
throw new Error(i18n.hostedBaseUrl_must_start_with_http());
445451
}
446452
},

0 commit comments

Comments
 (0)