{{- /* Extract code examples from the page content */ -}} {{- /* This partial finds all code example anchors and builds a JSON structure */ -}} {{- /* Input: page object with .Content */ -}} {{- /* Output: JSON array of code examples with codetabs container IDs and per-language identifiers */ -}} {{- $content := .Content -}} {{- /* Extract all anchor IDs, descriptions, and difficulties */ -}} {{- $examples := slice -}} {{- /* Use regex to find all anchors with optional data-description, data-difficulty, and data-codetabs-id */ -}} {{- $matches := findRE `]*>` $content -}} {{- range $match := $matches -}} {{- /* Extract the ID from the match */ -}} {{- $id := replaceRE `]*id="([^"]*)"[^>]*data-lang="([^"]*)"[^>]*data-codetabs-id="%s"` $codetabsId -}} {{- $panelMatches := findRE $panelPattern $content -}} {{- range $panelMatch := $panelMatches -}} {{- $panelId := replaceRE $panelPattern `$1` $panelMatch -}} {{- $lang := replaceRE $panelPattern `$2` $panelMatch -}} {{- /* Look up client config to get langId, clientId, and clientName */ -}} {{- $clientConfig := index $.Site.Params.clientsConfig $lang -}} {{- $langId := "" -}} {{- $clientId := "" -}} {{- $clientName := "" -}} {{- if $clientConfig -}} {{- $langId = index $clientConfig "langId" -}} {{- $clientId = index $clientConfig "clientId" -}} {{- $clientName = index $clientConfig "clientName" -}} {{- end -}} {{- $langEntry := dict "id" $lang "panelId" $panelId -}} {{- if $langId -}} {{- $langEntry = merge $langEntry (dict "langId" $langId) -}} {{- end -}} {{- if $clientId -}} {{- $langEntry = merge $langEntry (dict "clientId" $clientId) -}} {{- end -}} {{- if $clientName -}} {{- $langEntry = merge $langEntry (dict "clientName" $clientName) -}} {{- end -}} {{- $languages = $languages | append $langEntry -}} {{- end -}} {{- end -}} {{- /* Create example object with the ID and optional description/difficulty/codetabsId/languages/commands */ -}} {{- $example := dict "id" $id -}} {{- if $description -}} {{- $example = merge $example (dict "description" $description) -}} {{- end -}} {{- if $difficulty -}} {{- $example = merge $example (dict "difficulty" $difficulty) -}} {{- end -}} {{- if gt (len $buildsUpon) 0 -}} {{- $example = merge $example (dict "buildsUpon" $buildsUpon) -}} {{- end -}} {{- if $codetabsId -}} {{- $example = merge $example (dict "codetabsId" $codetabsId) -}} {{- end -}} {{- if gt (len $languages) 0 -}} {{- $example = merge $example (dict "languages" $languages) -}} {{- end -}} {{- if gt (len $commandsWithMetadata) 0 -}} {{- $example = merge $example (dict "commands" $commandsWithMetadata) -}} {{- end -}} {{- $examples = $examples | append $example -}} {{- end -}} {{- /* Return as JSON array */ -}} {{- if gt (len $examples) 0 -}} {{- $examples | jsonify -}} {{- else -}} {{- "[]" -}} {{- end -}}