Skip to content

Commit

Permalink
👌 IMPROVE: External links in new tab (#856)
Browse files Browse the repository at this point in the history
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 <marios.cako@hpe.com>
  • Loading branch information
marjus45 committed Jan 5, 2024
1 parent 330e96a commit 1c83a62
Show file tree
Hide file tree
Showing 2 changed files with 13 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 @@ -233,6 +233,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 @@ -952,8 +952,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

0 comments on commit 1c83a62

Please sign in to comment.