Skip to content

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ WORKDIR /out
ADD https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz .
RUN tar xvf hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz

# git-src clones the OSS projects in order to include
# code snippets into the docs.
FROM base AS git-src-oss
WORKDIR /git-src
RUN git clone https://github.com/testcontainers/testcontainers-go.git
Comment on lines +36 to +40
Copy link
Member

@crazy-max crazy-max Mar 12, 2025

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.

Copy link
Member Author

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.

Copy link
Member

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?


# build-base is the base stage used for building the site
FROM base AS build-base
WORKDIR /project
COPY --from=git-src-oss /git-src /project/git-src
COPY --from=hugo /out/hugo /bin/hugo
COPY --from=npm /out/node_modules node_modules
COPY . .
Expand Down
2 changes: 2 additions & 0 deletions content/manuals/testcontainers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Expand Down
65 changes: 65 additions & 0 deletions layouts/shortcodes/embedded.html
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 }}