Skip to content

Commit 4d9d4d4

Browse files
committed
Fix last edge cases of link behavior
1 parent a11ba23 commit 4d9d4d4

7 files changed

+57
-18
lines changed

content/policy-archive/policy-version-1-0.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Archive Notice
22

33
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is
4-
retained for historical purposes only.
4+
retained for historical purposes only.</p>
55

66
Version 1.0 of the Chrome Root Program Policy was superseded by Version [1.1](./policy-version-1-1.md) on June 1, 2022.
77

content/policy-archive/policy-version-1-1.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Archive Notice
22

33
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is
4-
retained for historical purposes only.
4+
retained for historical purposes only.</p>
55

66
Version 1.1 of the Chrome Root Program Policy was superseded by Version [1.2](./policy-version-1-2.md) on September 1, 2022.
77

content/policy-archive/policy-version-1-2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Archive Notice
22

33
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is
4-
retained for historical purposes only.
4+
retained for historical purposes only.</p>
55

66
Version 1.2 of the Chrome Root Program Policy was superseded by Version [1.3](./policy-version-1-3.md) on January 6, 2023.
77

content/policy-archive/policy-version-1-3.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Archive Notice
22

3-
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.
3+
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.</p>
44

55
Version 1.3 of the Chrome Root Program Policy was superseded by Version [1.4](./policy-version-1-4.md) on March 3, 2023.
66

content/policy-archive/policy-version-1-4.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Archive Notice
22

3-
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.
3+
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.</p>
44

55
Version 1.4 of the Chrome Root Program Policy was superseded by Version [1.5](policy-version-1-5.md) on January 16, 2024.
66

content/policy-archive/policy-version-1-5.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## Archive Notice
22

3-
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.
3+
<p><strong><span style="color:#FF0000">IMPORTANT:</span></strong> This page is retained for historical purposes only.</p>
44

5-
Version 1.5 of the Chrome Root Program Policy was superseded by Version [1.6](../policy.md) on February 15, 2025.
5+
Version 1.5 of the Chrome Root Program Policy was superseded by Version [1.6](../index.md) on February 15, 2025.
66

77
For the latest version of the Chrome Root Program Policy, see <a href="https://g.co/chrome/root-policy">https://g.co/chrome/root-policy</a>.</p>
88

src/generate.py

+50-11
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,57 @@ def absolute_url(cls, base_url, path):
4242

4343

4444
class RemoveMdExtensionTreeprocessor(Treeprocessor):
45+
46+
def __init__(self, md, base_url, dir_path):
47+
super().__init__(md)
48+
self.base_url = urllib.parse.urlparse(base_url)
49+
self.dir_path = dir_path
50+
4551
def run(self, root):
46-
"""Iterate over all <a> tags in the HTML tree and remove `.md` from their href."""
52+
"""Iterate over all <a> tags in the HTML tree and remove `.md` from
53+
their href. If the link is in a relative directory, resolve the full
54+
link using base URL"""
4755
for element in root.iter("a"):
4856
href = element.get("href")
4957
if href and href.endswith(".md") and not href.startswith("http"):
50-
# Strip off the last 3 chars (".md")
51-
element.set("href", href[:-3])
58+
full_path = os.path.normpath(
59+
os.path.join(self.base_url.path, self.dir_path, href)
60+
)
61+
print("href, full_path:", href, full_path)
62+
target = urllib.parse.urlunparse(
63+
(
64+
self.base_url.scheme or "http",
65+
self.base_url.netloc,
66+
full_path,
67+
self.base_url.params,
68+
self.base_url.query,
69+
self.base_url.fragment,
70+
)
71+
)
72+
if target.endswith("/index.md"):
73+
target = target[:-9]
74+
else:
75+
# Strip off the last 3 chars (".md")
76+
target = target[:-3]
77+
element.set("href", target)
5278

5379

5480
class RemoveMdExtension(Extension):
81+
82+
def __init__(self, base_url, dir_path):
83+
super().__init__()
84+
self.base_url = base_url
85+
self.dir_path = dir_path
86+
5587
def extendMarkdown(self, md):
88+
print("base_url, dir_path", self.base_url, self.dir_path)
5689
md.treeprocessors.register(
57-
RemoveMdExtensionTreeprocessor(md), "remove_md_extension", priority=15
90+
RemoveMdExtensionTreeprocessor(md, self.base_url, self.dir_path),
91+
"remove_md_extension",
92+
priority=15,
5893
)
5994

6095

61-
def make_extension(**kwargs):
62-
"""Entry point for the extension."""
63-
return RemoveMdExtension(**kwargs)
64-
65-
6696
def replace_extension(filename, old, new):
6797
parts = filename.rsplit(
6898
f".{old}", 1
@@ -100,7 +130,14 @@ def render_file(input_path, output_path, env, page_context={}):
100130
# Convert Markdown to HTML
101131
html_content = markdown.markdown(
102132
md_content,
103-
extensions=["tables", "toc", "attr_list", RemoveMdExtension()],
133+
extensions=[
134+
"tables",
135+
"toc",
136+
"attr_list",
137+
RemoveMdExtension(
138+
page_context.get("base_url"), page_context.get("dir_path")
139+
),
140+
],
104141
)
105142

106143
# Wrap with a template
@@ -169,7 +206,9 @@ def render_markdown(input_dir, output_dir, env, page_context={}) -> ConversionRe
169206
if should_render:
170207
# Ensure the parent directory (or in the case of .md, the subfolder) exists.
171208
os.makedirs(os.path.dirname(output_path), exist_ok=True)
172-
render_file(input_path, output_path, env, page_context=page_context)
209+
context = page_context.copy()
210+
context["dir_path"] = relative_path
211+
render_file(input_path, output_path, env, page_context=context)
173212
converted += 1
174213
else:
175214
os.makedirs(os.path.dirname(output_path), exist_ok=True)

0 commit comments

Comments
 (0)