-
Hi, thanks for the great work. Only possible workaround for me is using HTML such that <a href="http://google.com/" target="_blank">Hello, google!</a> but this way somehow messed up context and I prefer to have a nice concise form: using It seems I missed something or |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
You probably forgot to activate the Attribute Lists extension: |
Beta Was this translation helpful? Give feedback.
-
The built-in privacy plugin now allows to automatically add |
Beta Was this translation helpful? Give feedback.
-
I have created the plugin to do that automatically. Plugin opens all outgoing links in a new tab. Just install the plugin using pip from PyPI: pip install mkdocs-open-in-new-tab And add one line of code to your plugins:
- search
- open-in-new-tab |
Beta Was this translation helpful? Give feedback.
-
@JakubAndrysek Looks like this plugin you created only works for python correct? |
Beta Was this translation helpful? Give feedback.
-
Here is a MkDocs "hook" that injects a "Python-Markdown Extension" which makes all external links use from typing import List
from urllib.parse import urlparse
import markdown
from mkdocs.config.defaults import MkDocsConfig
class ExternalLinksTreeProcessor(markdown.treeprocessors.Treeprocessor):
"""
Adds target="_blank" and rel="noopener" to external links.
"""
def __init__(self, md, ignore_domains: List[str]):
super().__init__(md)
# convert to set for faster lookup
self.ignore_domains = set(ignore_domains)
def run(self, root):
for element in root.iter():
if element.tag == "a":
href = element.get("href", "")
# parse the href and check if it's an absolute URL with http or https scheme
parsed_url = urlparse(href)
if parsed_url.scheme in ["http", "https"]:
# remove port, if present
domain = parsed_url.netloc.split(":")[0]
# skip if the domain is in the ignore list
if domain in self.ignore_domains:
continue
element.set("target", "_blank")
element.set("rel", "noopener")
class ExternalLinksExtension(markdown.Extension):
def __init__(self, **kwargs):
self.config = {
"ignore_domains": [[], "List of domains to ignore"],
}
super(ExternalLinksExtension, self).__init__(**kwargs)
def extendMarkdown(self, md: markdown.Markdown):
ignore_domains = self.getConfig("ignore_domains", [])
if not isinstance(ignore_domains, list):
raise ValueError("'ignore_domains' config must be a list")
md.treeprocessors.register(
ExternalLinksTreeProcessor(md, ignore_domains), "external_links", -1000
)
def on_config(config: MkDocsConfig, **kwargs):
# a list of domains to ignore
# WARNING: requires re-running `mkdocs serve` when changed
IGNORE_DOMAINS = ["example.com"]
# inject the markdown extension
config.markdown_extensions.append(
ExternalLinksExtension(ignore_domains=IGNORE_DOMAINS)
)
return config You can exclude specific domains by updating the All you need to do is put this file in your MkDocs repo, and reference it under hooks:
- path/to/external_links_md_extension.py |
Beta Was this translation helpful? Give feedback.
You probably forgot to activate the Attribute Lists extension:
https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown/#attribute-lists