Skip to content

Commit 6daae24

Browse files
👌 IMPROVE: Allow for opening external links in new tabs (#856) (#857)
Add support to enable external links to open in a new tab. Add the configuration option `myst_links_external_new_tab`, which if set to `True` will set all URL links to open in a new tab on the browser. Also allow the user to set the `target` and `rel` attributes on links, when using the `inline_attrs` extension. Co-authored-by: Chris Sewell <[email protected]>
1 parent abcc087 commit 6daae24

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

myst_parser/config/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def __repr__(self) -> str:
234234
},
235235
)
236236

237+
links_external_new_tab: bool = dc.field(
238+
default=False,
239+
metadata={
240+
"validator": instance_of(bool),
241+
"help": "Open all external links in a new tab",
242+
},
243+
)
244+
237245
url_schemes: Dict[str, Optional[UrlSchemeType]] = dc.field(
238246
default_factory=lambda: {
239247
"http": None,

myst_parser/mdit_to_docutils/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,12 @@ def render_link_url(
953953
"""
954954
ref_node = nodes.reference()
955955
self.add_line_and_source_path(ref_node, token)
956+
attribute_keys = ["class", "id", "reftitle", "target", "rel"]
957+
if self.md_config.links_external_new_tab:
958+
token.attrs["target"] = "_blank"
959+
token.attrs["rel"] = "noreferer noopener"
956960
self.copy_attributes(
957-
token, ref_node, ("class", "id", "reftitle"), aliases={"title": "reftitle"}
961+
token, ref_node, attribute_keys, aliases={"title": "reftitle"}
958962
)
959963
uri = cast(str, token.attrGet("href") or "")
960964
implicit_text: str | None = None

tests/test_renderers/fixtures/myst-config.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ a
301301
[ref]{#id3 .e .f}
302302

303303
[ref]: https://example.com
304+
305+
[text3](https://example.com){target="g"}
306+
307+
[text4](https://example.com){rel="h"}
304308
.
305309
<document source="<string>">
306310
<paragraph>
@@ -319,6 +323,12 @@ a
319323
<paragraph>
320324
<reference classes="e f" ids="id3" names="id3" refuri="https://example.com">
321325
ref
326+
<paragraph>
327+
<reference refuri="https://example.com" target="g">
328+
text3
329+
<paragraph>
330+
<reference refuri="https://example.com" rel="h">
331+
text4
322332
.
323333

324334
[attrs_inline_image] --myst-enable-extensions=attrs_inline
@@ -507,3 +517,13 @@ content
507517
<string>:1: (WARNING/2) Unknown directive type: 'unknown' [myst.directive_unknown]
508518
<string>:6: (WARNING/2) 'admonition': Unknown option keys: ['a'] (allowed: ['class', 'name']) [myst.directive_option]
509519
.
520+
521+
[links-external-new-tab] --myst-links-external-new-tab="true"
522+
.
523+
[text](https://example.com)
524+
.
525+
<document source="<string>">
526+
<paragraph>
527+
<reference refuri="https://example.com" rel="noreferer noopener" target="_blank">
528+
text
529+
.

0 commit comments

Comments
 (0)