-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: support for embedding code snippets #22226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mdelapenya
wants to merge
3
commits into
docker:main
Choose a base branch
from
mdelapenya:embedding-code-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ Testcontainers provide support for the most popular languages, and Docker sponso | |
|
||
The rest are community-driven and maintained by independent contributors. | ||
|
||
{{< embedded language="go" source="/git-src/testcontainers-go/modules/redis/examples_test.go" id="runRedisContainer" >}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before merging this PR, this line is just an example on how to use it, so it should be removed from here. |
||
|
||
### Prerequisites | ||
|
||
Testcontainers requires a Docker-API compatible container runtime. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{{ $language := .Get "language" }} | ||
{{ $source := .Get "source" }} | ||
{{ $options := .Get "options" }} | ||
{{ $id := .Get "id" }} | ||
{{ $startTag := printf "START %s" $id }} | ||
{{ $endTag := printf "END %s" $id }} | ||
|
||
{{ if and $source (strings.Contains $source "testcontainers-go") }} | ||
{{/* | ||
If the source is a testcontainers-go file, we need to use a different tag. In example: | ||
|
||
// runRedisContainer { | ||
// ... | ||
// } | ||
*/}} | ||
{{ $startTag = printf "// %s {" $id }} | ||
{{ $endTag = printf "// }" }} | ||
{{ end }} | ||
|
||
{{ with $source | readFile }} | ||
{{ $snippet := . }} | ||
|
||
{{ if $id }} | ||
{{ $lines := split $snippet "\n" }} | ||
|
||
{{ $startl := -1 }} | ||
{{ $endl := -1 }} | ||
|
||
{{/* Find the lines that ends with the start and end tags. */}} | ||
{{ range $index, $line := $lines }} | ||
{{ if hasSuffix (strings.TrimSpace $line) $startTag }} | ||
{{ $startl = $index }} | ||
{{ else if hasSuffix (strings.TrimSpace $line) $endTag }} | ||
{{ $endl = $index }} | ||
{{ end }} | ||
{{ end }} | ||
|
||
{{/* Let's add some basic assertions. */}} | ||
{{ if lt $startl 0 }} | ||
{{ errorf "Named snippet '%s' is missing START tag (searched %d lines)" $id (len $lines) }} | ||
{{ end }} | ||
|
||
{{ if lt $endl 0 }} | ||
{{ errorf "Named snippet '%s' is missing END tag (searched %d lines)" $id (len $lines) }} | ||
{{ end }} | ||
|
||
{{ if le $endl $startl }} | ||
{{ errorf "Named snippet '%s': END tag (line %d) must come after START tag (line %d)" $id $endl $startl }} | ||
{{ end }} | ||
|
||
{{/* Size of the snippet in number of lines. */}} | ||
{{ $snippetLen := sub (sub $endl $startl) 1 }} | ||
|
||
{{/* Create slice with only the lines between the tags. */}} | ||
{{ $includedLines := first $snippetLen (after (add $startl 1) $lines) }} | ||
|
||
{{/* Join the lines into the final snippet. */}} | ||
{{ $snippet = delimit $includedLines "\n" }} | ||
{{ $markdown := printf "```%s\n%s\n```" $language $snippet }} | ||
{{ $markdown | markdownify }} | ||
{{ else }} | ||
{{ $markdown := printf "```%s\n%s\n```" $language . }} | ||
{{ $markdown | markdownify }} | ||
{{ end }} | ||
{{ end }} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use vendoring through go mod convention of hugo if we need to use external resources like we do for cli reference docs:
Also git clone without pinning to commit sha is not reproducible and can lead to regressions with unrelated changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this is a PoC, and during the initial discussion with the Docs team we considered having the code snippets living in the docs repo, so the upstream repo (i.e. tc-go) would have to send a PR to the docs syncing the code snippets. As a result, no git clone would be needed, and the embedded short-code would just read from a local path, ideally next to the docs page using the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be something we could somehow integrate in https://github.com/docker/cli-docs-tool, so that the docs repository would just vendor the markdown produced, and the cli-docs-tool being responsible for embedding the code examples?