Skip to content

Commit 52d0b1e

Browse files
authored
Added GitHub buttons, terraform and github action icons, fixed broken example links (#558)
Added GitHub buttons, terraform and github action icons, fixed broken example links
1 parent daf39ec commit 52d0b1e

File tree

14 files changed

+279
-102
lines changed

14 files changed

+279
-102
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"prism-react-renderer": "^1.3.5",
2525
"react": "^17.0.2",
2626
"react-dom": "^17.0.2",
27+
"react-github-btn": "^1.4.0",
2728
"sass": "^1.57.1"
2829
},
2930
"devDependencies": {

scripts/docs-collator/AbstractRenderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def _pre_rendering_fixes(self, repo, module_download_dir):
2020
content = io.read_file_to_string(readme_yaml_file)
2121
content = rendering.remove_targets_md(content)
2222
content = rendering.rename_name(repo, content)
23+
content = rendering.fix_links_to_examples(repo, content)
2324
io.save_string_to_file(readme_yaml_file, content)
2425

2526
def _post_rendering_fixes(self, repo, readme_md_file):

scripts/docs-collator/utils/rendering.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import re
22

3+
TAG_REGEX = re.compile('(<)([0-9a-zA-Z._-]+)(>)', re.IGNORECASE)
4+
BR_REGEX = re.compile(re.escape('<br>'), re.IGNORECASE)
5+
SIDEBAR_LABEL_REGEX = re.compile('sidebar_label: .*', re.IGNORECASE)
6+
CUSTOM_EDIT_URL_REGEX = re.compile('custom_edit_url: .*', re.IGNORECASE)
7+
NAME_REGEX = re.compile('name: .*', re.IGNORECASE)
8+
39

410
def fix_self_non_closing_br_tags(content):
5-
regex = re.compile(re.escape('<br>'), re.IGNORECASE)
6-
return regex.sub('<br/>', content)
11+
return BR_REGEX.sub('<br/>', content)
712

813

914
def fix_custom_non_self_closing_tags_in_pre(content):
1015
lines = content.splitlines()
11-
tag_regex = re.compile('(<)([0-9a-zA-Z._-]+)(>)', re.IGNORECASE)
1216
fixed_lines = []
1317

1418
for line in lines:
@@ -20,7 +24,7 @@ def fix_custom_non_self_closing_tags_in_pre(content):
2024

2125
for group in groups:
2226
before = group
23-
after = tag_regex.sub(r"&lt;\2&gt;", group)
27+
after = TAG_REGEX.sub(r"&lt;\2&gt;", group)
2428
line = line.replace(f'<pre>{before}</pre>', f'<pre>{after}</pre>')
2529

2630
fixed_lines.append(line)
@@ -29,15 +33,13 @@ def fix_custom_non_self_closing_tags_in_pre(content):
2933

3034

3135
def fix_sidebar_label(content, repo):
32-
regex = re.compile('sidebar_label: .*', re.IGNORECASE)
3336
provider, module_name = parse_terraform_repo_name(repo.name)
34-
return regex.sub(f'sidebar_label: {module_name}', content)
37+
return SIDEBAR_LABEL_REGEX.sub(f'sidebar_label: {module_name}', content)
3538

3639

3740
def fix_github_edit_url(content, repo):
38-
regex = re.compile('custom_edit_url: .*', re.IGNORECASE)
3941
github_edit_url = f"custom_edit_url: https://github.com/{repo.full_name}/blob/{repo.default_branch}/README.yaml"
40-
return regex.sub(github_edit_url, content)
42+
return CUSTOM_EDIT_URL_REGEX.sub(github_edit_url, content)
4143

4244

4345
def remove_logo_from_the_bottom(content):
@@ -56,8 +58,12 @@ def remove_prefix(string, prefix):
5658

5759

5860
def rename_name(repo, content):
59-
regex = re.compile('name: .*', re.IGNORECASE)
60-
return regex.sub(f'name: {repo.name}', content)
61+
return NAME_REGEX.sub(f'name: {repo.name}', content)
62+
63+
64+
def fix_links_to_examples(repo, content):
65+
return re.sub(r"(\[examples/.*])\((examples/.*)\)",
66+
rf"\1(https://github.com/{repo.full_name}/tree/{repo.default_branch}/\2)", content)
6167

6268

6369
def parse_terraform_repo_name(name):

scripts/render-docs-for-github-actions.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ DOWNLOAD_TMP_DIR="${DOWNLOAD_TMP_DIR:-tmp/github-actions}"
1414

1515
python scripts/docs-collator/render_docs_for_github_actions.py \
1616
--download-dir "${DOWNLOAD_TMP_DIR}" \
17-
--output-dir "${RENDERED_DOCS_DIR}"
17+
--output-dir "${RENDERED_DOCS_DIR}" \
18+
--excludes 'github-action-ci-terraform'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from "react";
2+
import GitHubButton from 'react-github-btn'
3+
4+
export default function GitHubButtons({custom_edit_url}) {
5+
if (custom_edit_url == undefined) {
6+
return (<div/>);
7+
} else {
8+
const url = new URL(custom_edit_url);
9+
const parts = url.pathname.split('/');
10+
11+
let repoFullName = parts[1] + '/' + parts[2];
12+
13+
return (
14+
<div className="github_buttons">
15+
<ul>
16+
<li>
17+
<GitHubButton
18+
href={'https://github.com/' + repoFullName}
19+
aria-label="Open GitHub">GitHub</GitHubButton>
20+
</li>
21+
22+
<li>
23+
<GitHubButton
24+
href={'https://github.com/' + repoFullName + '/issues'}
25+
data-show-count="true"
26+
aria-label={'Issue ' + repoFullName + ' on GitHub'}>Issues</GitHubButton>
27+
</li>
28+
29+
<li>
30+
<GitHubButton
31+
href={'https://github.com/' + repoFullName + '/fork'}
32+
data-icon="octicon-repo-forked"
33+
data-show-count="true"
34+
aria-label={'Fork ' + repoFullName + ' on GitHub'}>Forks</GitHubButton>
35+
</li>
36+
37+
<li>
38+
<GitHubButton
39+
href={'https://github.com/' + repoFullName}
40+
data-icon="octicon-star"
41+
data-show-count="true"
42+
aria-label={'Star ' + repoFullName + ' on GitHub'}>Stars</GitHubButton>
43+
</li>
44+
</ul>
45+
</div>);
46+
}
47+
}

src/css/custom.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,19 @@ svg.landingLogo path {
262262
.footer__copyright {
263263
margin-left: 0;
264264
}
265+
266+
.github_buttons > ul {
267+
list-style-type: none;
268+
padding-left: 0px;
269+
margin-top: 5px;
270+
margin-bottom: 10px;
271+
}
272+
273+
.github_buttons > ul > li {
274+
display: inline-block;
275+
margin-right: 10px;
276+
}
277+
278+
.markdown h1:first-child {
279+
--ifm-h1-font-size: 2.5rem;
280+
}

src/css/custom.orig.css

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/theme/DocCard/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import React from 'react';
2+
import clsx from 'clsx';
3+
import Link from '@docusaurus/Link';
4+
import {
5+
findFirstCategoryLink,
6+
useDocById,
7+
} from '@docusaurus/theme-common/internal';
8+
import isInternalUrl from '@docusaurus/isInternalUrl';
9+
import {translate} from '@docusaurus/Translate';
10+
import styles from './styles.module.css';
11+
function CardContainer({href, children}) {
12+
return (
13+
<Link
14+
href={href}
15+
className={clsx('card padding--lg', styles.cardContainer)}>
16+
{children}
17+
</Link>
18+
);
19+
}
20+
function CardLayout({href, icon, title, description}) {
21+
return (
22+
<CardContainer href={href}>
23+
<h2 className={clsx('text--truncate', styles.cardTitle)} title={title}>
24+
{icon} {title}
25+
</h2>
26+
{description && (
27+
<p
28+
className={clsx('text--truncate', styles.cardDescription)}
29+
title={description}>
30+
{description}
31+
</p>
32+
)}
33+
</CardContainer>
34+
);
35+
}
36+
function ImageCardLayout({href, icon, title, description}) {
37+
return (
38+
<CardContainer href={href}>
39+
<h2 className={clsx('text--truncate', styles.cardTitle)} title={title}>
40+
<img src={icon} style={{ maxWidth: 16, minWidth: 16 }}/> {title}
41+
</h2>
42+
{description && (
43+
<p
44+
className={clsx('text--truncate', styles.cardDescription)}
45+
title={description}>
46+
{description}
47+
</p>
48+
)}
49+
</CardContainer>
50+
);
51+
}
52+
function CardCategory({item}) {
53+
const href = findFirstCategoryLink(item);
54+
// Unexpected: categories that don't have a link have been filtered upfront
55+
if (!href) {
56+
return null;
57+
}
58+
return (
59+
<ImageCardLayout
60+
href={href}
61+
icon="/images/folder.svg"
62+
title={item.label}
63+
description={translate(
64+
{
65+
message: '{count} items',
66+
id: 'theme.docs.DocCard.categoryDescription',
67+
description:
68+
'The default description for a category card in the generated index about how many items this category includes',
69+
},
70+
{count: item.items.length},
71+
)}
72+
/>
73+
);
74+
}
75+
function CardLink({item}) {
76+
const icon = isInternalUrl(item.href) ? '/images/terraform-color-16.svg' : '🔗';
77+
const doc = useDocById(item.docId ?? undefined);
78+
return (
79+
<CardLayout
80+
href={item.href}
81+
icon={icon}
82+
title={item.label}
83+
description={doc?.description}
84+
/>
85+
);
86+
}
87+
function ImageCardLink({item, image}) {
88+
const doc = useDocById(item.docId ?? undefined);
89+
return (
90+
<ImageCardLayout
91+
href={item.href}
92+
icon={image}
93+
title={item.label}
94+
description={doc?.description}
95+
/>
96+
);
97+
}
98+
export default function DocCard({item}) {
99+
switch (item.type) {
100+
case 'link':
101+
if(item.href.startsWith('/components/catalog/') || item.href.startsWith('/modules/catalog/')) {
102+
return <ImageCardLink item={item} image='/images/terraform.svg' />
103+
} else if(item.href.startsWith('/github-actions/catalog/')) {
104+
return <ImageCardLink item={item} image='/images/github-actions.svg' />
105+
} else {
106+
return <CardLink item={item} />;
107+
}
108+
case 'category':
109+
return <CardCategory item={item} />;
110+
default:
111+
throw new Error(`unknown item type ${JSON.stringify(item)}`);
112+
}
113+
}

0 commit comments

Comments
 (0)