Skip to content

Commit 721735b

Browse files
alonsomoyatimvink
authored andcommitted
Custom href option
1 parent f184834 commit 721735b

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

docs/options.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins:
88
show_contribution: true
99
show_line_count: true
1010
show_email_address: true
11+
href: "mailto:{email}"
1112
count_empty_lines: true
1213
fallback_to_empty: false
1314
sort_authors_by: name
@@ -37,9 +38,15 @@ If this option is set to `true` (default: `false`) the number of lines per autho
3738
## `show_email_address`
3839

3940
If this option is set to `true` (default: `true`), then authors' names
40-
are rendered as a `mailto:` link pointing to their email address. If
41+
are rendered as (by default) a `mailto:{email}` link pointing to their email address. If
4142
set to `false`, they are shown in plain text.
4243

44+
## `href`
45+
46+
Given `show_email_adress` is set to `true`, you can customize the rendered link with the option `href`
47+
(default: `mailto:{email}`) using the f-string format (available `{email}` and `{name}`).
48+
This is useful to link to IM tools i.e. `href: "https://teams.microsoft.com/l/chat/0/0?users={email}"`.
49+
4350
## `count_empty_lines`
4451

4552
If this option is set to `true` (default: `false`) empty lines will count towards an authors' contribution.

mkdocs_git_authors_plugin/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class GitAuthorsPlugin(BasePlugin):
1717
("show_contribution", config_options.Type(bool, default=False)),
1818
("show_line_count", config_options.Type(bool, default=False)),
1919
("show_email_address", config_options.Type(bool, default=True)),
20+
("href", config_options.Type(str, default='mailto:{email}')),
2021
("count_empty_lines", config_options.Type(bool, default=True)),
2122
("fallback_to_empty", config_options.Type(bool, default=False)),
2223
("exclude", config_options.Type(list, default=[])),

mkdocs_git_authors_plugin/util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def page_authors_summary(page, config: dict):
5959
else ""
6060
)
6161
if page.repo().config("show_email_address"):
62-
author_name = "<a href='mailto:%s'>%s</a>" % (author.email(), author.name())
62+
href = page.repo().config("href").format(email=author.email(),
63+
name=author.name())
64+
author_name = "<a href='%s'>%s</a>" % (href, author.name())
6365
else:
6466
author_name = author.name()
6567
authors_summary.append("%s%s" % (author_name, contrib))
@@ -105,7 +107,9 @@ def site_authors_summary(authors, config: dict):
105107
lines = ": %s lines" % author.lines() if show_line_count else ""
106108
author_name = ""
107109
if show_email_address:
108-
author_name = '<a href="mailto:%s">%s</a>' % (author.email(), author.name())
110+
href = config["href"].format(email=author.email(),
111+
name=author.name())
112+
author_name = '<a href="%s">%s</a>' % (href, author.name())
109113
else:
110114
author_name = author.name()
111115
result += """
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitauthors_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-authors:
7+
href: "https://teams.microsoft.com/l/chat/0/0?users={email}"

tests/test_basic.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
SITES_THAT_SHOULD_SUCCEED = [
3131
"mkdocs.yml",
32+
"mkdocs_custom_href.yml",
3233
"mkdocs_complete_material_disabled.yml",
3334
"mkdocs_complete_material.yml",
3435
"mkdocs_exclude.yml",
@@ -92,6 +93,27 @@ def test_basic_working(tmp_path, mkdocs_file):
9293
assert re.search('<a href="mailto:[email protected]">Tim Vink</a>', contents)
9394

9495

96+
def test_custom_href(tmp_path):
97+
"""
98+
"""
99+
result = build_docs_setup("tests/basic_setup/mkdocs_custom_href.yml", tmp_path)
100+
assert result.exit_code == 0, (
101+
"'mkdocs build' command failed. Error: %s" % result.stdout
102+
)
103+
104+
index_file = tmp_path / "index.html"
105+
assert index_file.exists(), "%s does not exist" % index_file
106+
107+
contents = index_file.read_text()
108+
assert re.search("<span class='git-page-authors", contents)
109+
# Checking Page Authors
110+
assert re.search((r"<p>Page authors:.*<a href='https://teams.microsoft.com/l/chat/0/0\?"
111+
"[email protected]'>Tim Vink</a>.*<\/p>"), contents)
112+
# Checking Site Authors
113+
assert re.search(('<li><a href="https://teams.microsoft.com/l/chat/0/0\?'
114+
'[email protected]">Tim Vink</a><\/li>'), contents)
115+
116+
95117

96118
def test_no_email(tmp_path):
97119

0 commit comments

Comments
 (0)