Skip to content

Commit fc64840

Browse files
committed
👌 IMPROVE: External links in new tab (#856)
Add support to enable external links to open in a new tab. Expose the configuration option myst_links_external_new_tab, which if set will set all URL links to open in a new tab on the browser. Besides that, we allow the user to set the target and rel attributes when using the extension inline_attrs (#820). Closes #820 Closes #856 Signed-off-by: Marjus Cako <[email protected]>
1 parent 978e845 commit fc64840

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
@@ -233,6 +233,14 @@ def __repr__(self) -> str:
233233
},
234234
)
235235

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

myst_parser/mdit_to_docutils/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,12 @@ def render_link_url(
952952
"""
953953
ref_node = nodes.reference()
954954
self.add_line_and_source_path(ref_node, token)
955+
attribute_keys = ["class", "id", "reftitle", "target", "rel"]
956+
if self.md_config.links_external_new_tab:
957+
token.attrs["target"] = "_blank"
958+
token.attrs["rel"] = "noreferer noopener"
955959
self.copy_attributes(
956-
token, ref_node, ("class", "id", "reftitle"), aliases={"title": "reftitle"}
960+
token, ref_node, attribute_keys, aliases={"title": "reftitle"}
957961
)
958962
uri = cast(str, token.attrGet("href") or "")
959963
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_parse]
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)