Skip to content

Commit

Permalink
馃憣 IMPROVE: Allow for opening external links in new tabs (#856) (#857)
Browse files Browse the repository at this point in the history
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 <chrisj_sewell@hotmail.com>
  • Loading branch information
marjus45 and chrisjsewell committed Mar 26, 2024
1 parent abcc087 commit 6daae24
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions myst_parser/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ def __repr__(self) -> str:
},
)

links_external_new_tab: bool = dc.field(
default=False,
metadata={
"validator": instance_of(bool),
"help": "Open all external links in a new tab",
},
)

url_schemes: Dict[str, Optional[UrlSchemeType]] = dc.field(
default_factory=lambda: {
"http": None,
Expand Down
6 changes: 5 additions & 1 deletion myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,12 @@ def render_link_url(
"""
ref_node = nodes.reference()
self.add_line_and_source_path(ref_node, token)
attribute_keys = ["class", "id", "reftitle", "target", "rel"]
if self.md_config.links_external_new_tab:
token.attrs["target"] = "_blank"
token.attrs["rel"] = "noreferer noopener"
self.copy_attributes(
token, ref_node, ("class", "id", "reftitle"), aliases={"title": "reftitle"}
token, ref_node, attribute_keys, aliases={"title": "reftitle"}
)
uri = cast(str, token.attrGet("href") or "")
implicit_text: str | None = None
Expand Down
20 changes: 20 additions & 0 deletions tests/test_renderers/fixtures/myst-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ a
[ref]{#id3 .e .f}

[ref]: https://example.com

[text3](https://example.com){target="g"}

[text4](https://example.com){rel="h"}
.
<document source="<string>">
<paragraph>
Expand All @@ -319,6 +323,12 @@ a
<paragraph>
<reference classes="e f" ids="id3" names="id3" refuri="https://example.com">
ref
<paragraph>
<reference refuri="https://example.com" target="g">
text3
<paragraph>
<reference refuri="https://example.com" rel="h">
text4
.

[attrs_inline_image] --myst-enable-extensions=attrs_inline
Expand Down Expand Up @@ -507,3 +517,13 @@ content
<string>:1: (WARNING/2) Unknown directive type: 'unknown' [myst.directive_unknown]
<string>:6: (WARNING/2) 'admonition': Unknown option keys: ['a'] (allowed: ['class', 'name']) [myst.directive_option]
.

[links-external-new-tab] --myst-links-external-new-tab="true"
.
[text](https://example.com)
.
<document source="<string>">
<paragraph>
<reference refuri="https://example.com" rel="noreferer noopener" target="_blank">
text
.

0 comments on commit 6daae24

Please sign in to comment.