Skip to content

Commit 691368a

Browse files
Copilotcamilamacedo86
authored andcommitted
Improve docs layout and collapse code from tutorials
Assisted-by: Cursor
1 parent 41ff901 commit 691368a

File tree

26 files changed

+319
-55
lines changed

26 files changed

+319
-55
lines changed

docs/book/book.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ title = "The Kubebuilder Book"
77
[output.html]
88
smart-punctuation = true
99
additional-css = ["theme/css/markers.css", "theme/css/custom.css", "theme/css/version-dropdown.css"]
10+
additional-js = ["functions/copy-code.js"]
1011
git-repository-url = "https://github.com/kubernetes-sigs/kubebuilder"
1112
edit-url-template = "https://github.com/kubernetes-sigs/kubebuilder/edit/master/docs/book/{path}"
13+
# Enable copy button on code blocks
14+
copy-fonts = true
15+
# Improve code block rendering
16+
code-block-copy-button = true
1217

1318
[preprocessor.literatego]
1419
command = "./litgo.sh"

docs/book/functions/copy-code.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Add copy buttons to code blocks
3+
* Enhances usability by allowing users to easily copy code snippets
4+
*/
5+
6+
(function() {
7+
'use strict';
8+
9+
// Wait for page to load
10+
if (document.readyState === 'loading') {
11+
document.addEventListener('DOMContentLoaded', addCopyButtons);
12+
} else {
13+
addCopyButtons();
14+
}
15+
16+
function addCopyButtons() {
17+
// Find all code blocks in .literate sections and regular pre blocks
18+
var codeBlocks = document.querySelectorAll('pre:has(code)');
19+
20+
codeBlocks.forEach(function(pre) {
21+
// Skip if button already exists or if it's inside a summary (collapse header)
22+
if (pre.querySelector('.copy-button') || pre.closest('summary')) {
23+
return;
24+
}
25+
26+
// Create buttons container
27+
var buttonsDiv = document.createElement('div');
28+
buttonsDiv.className = 'buttons';
29+
buttonsDiv.style.cssText = 'position: absolute; right: 5px; top: 5px; display: flex; gap: 4px;';
30+
31+
// Create copy button
32+
var copyButton = document.createElement('button');
33+
copyButton.className = 'fa fa-copy copy-button';
34+
copyButton.setAttribute('aria-label', 'Copy code to clipboard');
35+
copyButton.setAttribute('title', 'Copy');
36+
copyButton.style.cssText = 'cursor: pointer; padding: 4px 8px; background: var(--sidebar-bg); border: 1px solid var(--sidebar-fg); border-radius: 3px; color: var(--sidebar-fg); font-size: 14px;';
37+
38+
// Make pre position relative so button can be absolutely positioned
39+
if (window.getComputedStyle(pre).position === 'static') {
40+
pre.style.position = 'relative';
41+
}
42+
43+
// Add click handler
44+
copyButton.addEventListener('click', function() {
45+
var code = pre.querySelector('code');
46+
var text = code ? code.innerText : pre.innerText;
47+
48+
// Use clipboard API
49+
if (navigator.clipboard) {
50+
navigator.clipboard.writeText(text).then(function() {
51+
copyButton.innerHTML = '<i class="fa fa-check"></i>';
52+
copyButton.setAttribute('title', 'Copied!');
53+
setTimeout(function() {
54+
copyButton.innerHTML = '<i class="fa fa-copy"></i>';
55+
copyButton.setAttribute('title', 'Copy');
56+
}, 2000);
57+
}).catch(function(err) {
58+
console.error('Failed to copy: ', err);
59+
});
60+
} else {
61+
// Fallback for older browsers
62+
var textarea = document.createElement('textarea');
63+
textarea.value = text;
64+
textarea.style.position = 'fixed';
65+
textarea.style.opacity = '0';
66+
document.body.appendChild(textarea);
67+
textarea.select();
68+
try {
69+
document.execCommand('copy');
70+
copyButton.innerHTML = '<i class="fa fa-check"></i>';
71+
copyButton.setAttribute('title', 'Copied!');
72+
setTimeout(function() {
73+
copyButton.innerHTML = '<i class="fa fa-copy"></i>';
74+
copyButton.setAttribute('title', 'Copy');
75+
}, 2000);
76+
} catch (err) {
77+
console.error('Failed to copy: ', err);
78+
}
79+
document.body.removeChild(textarea);
80+
}
81+
});
82+
83+
buttonsDiv.appendChild(copyButton);
84+
pre.insertBefore(buttonsDiv, pre.firstChild);
85+
});
86+
}
87+
})();
88+

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func main() {
198198
os.Exit(1)
199199
}
200200

201-
// +kubebuilder:docs-gen:collapse=old stuff
201+
// +kubebuilder:docs-gen:collapse=Remaining code from main.go
202202

203203
if err := (&controller.CronJobReconciler{
204204
Client: mgr.GetClient(),
@@ -239,5 +239,5 @@ func main() {
239239
setupLog.Error(err, "problem running manager")
240240
os.Exit(1)
241241
}
242-
// +kubebuilder:docs-gen:collapse=old stuff
242+
// +kubebuilder:docs-gen:collapse=Remaining code from main.go
243243
}

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type Clock interface {
6868
Now() time.Time
6969
}
7070

71-
// +kubebuilder:docs-gen:collapse=Clock
71+
// +kubebuilder:docs-gen:collapse=Clock Code Implementation
7272

7373
// Definitions to manage status conditions
7474
const (

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ var _ = Describe("CronJob controller", func() {
208208

209209
})
210210

211+
// +kubebuilder:docs-gen:collapse=Remaining code from cronjob_controller_test.go
212+
211213
/*
212214
After writing all this code, you can run `go test ./...` in your `controllers/` directory again to run your new test!
213215
*/

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ var _ = AfterSuite(func() {
168168
cancel()
169169
err := testEnv.Stop()
170170
Expect(err).NotTo(HaveOccurred())
171+
// +kubebuilder:docs-gen:collapse=Remaining code from suite_test.go
171172
})
172173

173174
/*

docs/book/src/cronjob-tutorial/testdata/project/internal/webhook/v1/cronjob_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
batchv1 "tutorial.kubebuilder.io/project/api/v1"
3737
)
3838

39-
// +kubebuilder:docs-gen:collapse=Go imports
39+
// +kubebuilder:docs-gen:collapse=Imports
4040

4141
/*
4242
Next, we'll setup a logger for the webhooks.
@@ -277,4 +277,4 @@ func validateCronJobName(cronjob *batchv1.CronJob) *field.Error {
277277
return nil
278278
}
279279

280-
// +kubebuilder:docs-gen:collapse=Validate object name
280+
// +kubebuilder:docs-gen:collapse=validateCronJobName() Code Implementation

docs/book/src/multiversion-tutorial/testdata/project/api/v1/cronjob_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ type CronJobStatus struct {
131131
Conditions []metav1.Condition `json:"conditions,omitempty"`
132132
}
133133

134-
// +kubebuilder:docs-gen:collapse=old stuff
134+
// +kubebuilder:docs-gen:collapse=Remaining code from cronjob_types.go
135135

136136
/*
137137
Since we'll have more than one version, we'll need to mark a storage version.
@@ -183,4 +183,4 @@ func init() {
183183
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
184184
}
185185

186-
// +kubebuilder:docs-gen:collapse=old stuff
186+
// +kubebuilder:docs-gen:collapse=Remaining code from cronjob_types.go

docs/book/src/multiversion-tutorial/testdata/project/api/v2/cronjob_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type CronJobSpec struct {
8484
// +kubebuilder:validation:Minimum=0
8585
FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"`
8686

87-
// +kubebuilder:docs-gen:collapse=The rest of Spec
87+
// +kubebuilder:docs-gen:collapse=CronJobSpec Full Code
8888

8989
}
9090

docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ func main() {
204204
setupLog.Error(err, "unable to create controller", "controller", "CronJob")
205205
os.Exit(1)
206206
}
207-
// +kubebuilder:docs-gen:collapse=existing setup
208207

209208
/*
210209
Our existing call to SetupWebhookWithManager registers our conversion webhooks with the manager, too.
@@ -242,5 +241,5 @@ func main() {
242241
setupLog.Error(err, "problem running manager")
243242
os.Exit(1)
244243
}
245-
// +kubebuilder:docs-gen:collapse=existing setup
244+
// +kubebuilder:docs-gen:collapse=Remaining code from main.go
246245
}

0 commit comments

Comments
 (0)