Skip to content

Commit 59e9b43

Browse files
authored
Merge pull request #26151 from feloy/feloy-api-reference-links
api-reference shortcode + script to test links
2 parents fa882c6 + 7d0e235 commit 59e9b43

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

content/en/docs/contribute/style/hugo-shortcodes/index.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ You can also include a full definition:
8383
which renders as:
8484
{{< glossary_definition term_id="cluster" length="all" >}}
8585

86+
## Links to API Reference
87+
88+
You can link to a page of the Kubernetes API reference using the `api-reference` shortcode, for example to the {{< api-reference page="workload-resources/pod-v1" >}} reference:
89+
90+
```
91+
{{</* api-reference page="workload-resources/pod-v1" */>}}
92+
```
93+
94+
The content of the `page` parameter is the suffix of the URL of the API reference page.
95+
96+
97+
You can link to a specific place into a page by specifying an `anchor` parameter, for example to the {{< api-reference page="workload-resources/pod-v1" anchor="PodSpec" >}} reference or the {{< api-reference page="workload-resources/pod-v1" anchor="environment-variables" >}} section of the page:
98+
99+
```
100+
{{</* api-reference page="workload-resources/pod-v1" anchor="PodSpec" */>}}
101+
{{</* api-reference page="workload-resources/pod-v1" anchor="environment-variables" */>}}
102+
```
103+
104+
105+
You can change the text of the link by specifying a `text` parameter, for example by linking to the {{< api-reference page="workload-resources/pod-v1" anchor="environment-variables" text="Environment Variables">}} section of the page:
106+
107+
```
108+
{{</* api-reference page="workload-resources/pod-v1" anchor="environment-variables" text="Environment Variable" */>}}
109+
```
110+
111+
112+
86113
## Table captions
87114

88115
You can make tables more accessible to screen readers by adding a table caption. To add a [caption](https://www.w3schools.com/tags/tag_caption.asp) to a table, enclose the table with a `table` shortcode and specify the caption with the `caption` parameter.

layouts/shortcodes/api-reference.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{ $base := "docs/reference/kubernetes-api" }}
2+
{{ $pageArg := .Get "page" }}
3+
{{ $anchorArg := .Get "anchor" }}
4+
{{ $textArg := .Get "text" }}
5+
{{ $page := site.GetPage "page" (printf "%s/%s" $base $pageArg) }}
6+
{{ $metadata := $page.Params.api_metadata }}
7+
<a href="{{ $page.URL }}{{if $anchorArg}}#{{ $anchorArg }}{{end}}">{{if $textArg}}{{ $textArg }}{{else if $anchorArg}}{{ $anchorArg }}{{else}}{{ $metadata.kind }}{{end}}</a>

scripts/linkchecker.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
# + /docs/bar : is a redirect entry, or
3636
# + /docs/bar : is something we don't understand
3737
#
38+
# + {{ < api-reference page="" anchor="" ... > }}
39+
# + {{ < api-reference page="" > }}
3840

3941
import argparse
4042
import glob
@@ -72,7 +74,8 @@
7274
RESULT = {}
7375
# Cached redirect entries
7476
REDIRECTS = {}
75-
77+
# Cached anchors in target pages
78+
ANCHORS = {}
7679

7780
def new_record(level, message, target):
7881
"""Create new checking record.
@@ -330,6 +333,44 @@ def check_target(page, anchor, target):
330333
msg = "Link may be wrong for the anchor [%s]" % anchor
331334
return new_record("WARNING", msg, target)
332335

336+
def check_anchor(target_page, anchor):
337+
"""Check if an anchor is defined in the target page
338+
339+
:param target_page: The target page to check
340+
:param anchor: Anchor string to find in the target page
341+
"""
342+
if target_page not in ANCHORS:
343+
try:
344+
with open(target_page, "r") as f:
345+
data = f.readlines()
346+
except Exception as ex:
347+
print("[Error] failed in reading markdown file: " + str(ex))
348+
return
349+
content = "\n".join(strip_comments(data))
350+
anchor_pattern1 = r"<a name=\"(.*?)\""
351+
regex1 = re.compile(anchor_pattern1)
352+
anchor_pattern2 = r"{#(.*?)}"
353+
regex2 = re.compile(anchor_pattern2)
354+
ANCHORS[target_page] = regex1.findall(content) + regex2.findall(content)
355+
return anchor in ANCHORS[target_page]
356+
357+
def check_apiref_target(target, anchor):
358+
"""Check a link to an API reference page.
359+
360+
:param target: The link target string to check
361+
:param anchor: Anchor string from the content page
362+
"""
363+
base = os.path.join(ROOT, "content", "en", "docs", "reference", "kubernetes-api")
364+
ok = check_file_exists(base + "/", target)
365+
if not ok:
366+
return new_record("ERROR", "API reference page not found", target)
367+
368+
if anchor is None:
369+
return
370+
371+
target_page = os.path.join(base, target)+".md"
372+
if not check_anchor(target_page, anchor):
373+
return new_record("ERROR", "Anchor not found in API reference page", target+"#"+anchor)
333374

334375
def validate_links(page):
335376
"""Find and validate links on a content page.
@@ -355,6 +396,27 @@ def validate_links(page):
355396
r = check_target(page, m[0], m[1])
356397
if r:
357398
records.append(r)
399+
400+
# searches for pattern: {{< api-reference page="" anchor=""
401+
apiref_pattern = r"{{ *< *api-reference page=\"([^\"]*?)\" *anchor=\"(.*?)\""
402+
regex = re.compile(apiref_pattern)
403+
404+
matches = regex.findall(content)
405+
for m in matches:
406+
r = check_apiref_target(m[0], m[1])
407+
if r:
408+
records.append(r)
409+
410+
# searches for pattern: {{< api-reference page=""
411+
apiref_pattern = r"{{ *< *api-reference page=\"([^\"]*?)\""
412+
regex = re.compile(apiref_pattern)
413+
414+
matches = regex.findall(content)
415+
for m in matches:
416+
r = check_apiref_target(m, None)
417+
if r:
418+
records.append(r)
419+
358420
if len(records):
359421
RESULT[page] = records
360422

0 commit comments

Comments
 (0)