fix(layouts): use File.Dir for baseURL-independent product detection

Create centralized product-info.html partial that extracts product/version
from .File.Dir instead of .RelPermalink. This ensures product detection
works correctly regardless of baseURL configuration, including PR preview
deployments at subdirectory paths.

Updated shortcodes:
- api-endpoint.html
- influxdb/host.html
- children.html
fix/baseurl-product-detection
Jason Stirnaman 2025-12-22 09:02:45 -06:00
parent 61a88db39b
commit 6799c3c753
4 changed files with 104 additions and 27 deletions

View File

@ -0,0 +1,74 @@
{{- /*
Returns a dict with product path information extracted from the page.
Uses .File.Dir when available (baseURL-independent) with fallback to .RelPermalink.
This ensures product detection works correctly regardless of baseURL configuration,
including PR preview deployments at subdirectory paths.
Usage:
{{ $productInfo := partial "product-info.html" . }}
{{ $productInfo.product }} - e.g., "influxdb3", "influxdb", "flux"
{{ $productInfo.version }} - e.g., "core", "enterprise", "v2", "cloud"
{{ $productInfo.isOSS }} - true if version matches ^v[0-9] (OSS versioned product)
{{ $productInfo.productKey }} - normalized key for product aliases
{{ $productInfo.productRef }} - key for .Site.Data.products lookup
{{ $productInfo.productData }} - product data from .Site.Data.products (may be nil)
*/ -}}
{{- /* Extract path from .File.Dir or fall back to .RelPermalink */ -}}
{{- $path := "" -}}
{{- if .File -}}
{{- /* .File.Dir returns the directory path relative to content/, e.g., "influxdb3/core/get-started/" */ -}}
{{- $path := .File.Dir -}}
{{- $.Scratch.Set "productPath" $path -}}
{{- else -}}
{{- /* Fallback: strip site base path from RelPermalink */ -}}
{{- $.Scratch.Set "productPath" .RelPermalink -}}
{{- end -}}
{{- $path = $.Scratch.Get "productPath" -}}
{{- /* Parse path segments - handles both File.Dir (no leading slash) and RelPermalink (leading slash) */ -}}
{{- $pathSegments := findRE "[^/]+" $path -}}
{{- $product := index $pathSegments 0 | default "" -}}
{{- $version := index $pathSegments 1 | default "" -}}
{{- /* Determine if this is an OSS version (matches v1, v2, etc.) */ -}}
{{- $isOSS := ne (len (findRE `^v[0-9]` $version)) 0 -}}
{{- /* Calculate product key - use "oss" for versioned products, otherwise use the version */ -}}
{{- $productKey := cond $isOSS "oss" $version -}}
{{- /* Product aliases map version/product keys to .Site.Data.products keys */ -}}
{{- $productAliases := dict
"oss" "influxdb"
"cloud" "influxdb_cloud"
"cloud-tsm" "influxdb_cloud"
"core" "influxdb3_core"
"enterprise" "influxdb3_enterprise"
"cloud-serverless" "influxdb3_cloud_serverless"
"serverless" "influxdb3_cloud_serverless"
"cloud-dedicated" "influxdb3_cloud_dedicated"
"dedicated" "influxdb3_cloud_dedicated"
"clustered" "influxdb3_clustered"
"explorer" "influxdb3_explorer"
-}}
{{- /* Look up the product reference */ -}}
{{- $productRef := index $productAliases $productKey -}}
{{- /* Get product data (may be nil if productRef is not found) */ -}}
{{- $productData := dict -}}
{{- if $productRef -}}
{{- $productData = index .Site.Data.products $productRef | default dict -}}
{{- end -}}
{{- /* Return the product info dict */ -}}
{{- return dict
"product" $product
"version" $version
"isOSS" $isOSS
"productKey" $productKey
"productRef" $productRef
"productData" $productData
"pathSegments" $pathSegments
-}}

View File

@ -1,13 +1,17 @@
{{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}}
{{- $currentVersion := index $productPathData 1 -}}
{{- /* Get product info using baseURL-independent path detection */ -}}
{{- $productInfo := partial "product-info.html" .Page -}}
{{- $endpoint := .Get "endpoint" -}}
{{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}}
{{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}}
{{- $productKey := .Get "influxdb_host" | default $parsedProductKey -}}
{{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}}
{{- $productRef := index $productAliases $productKey -}}
{{- $productData := index .Site.Data.products $productRef -}}
{{- $placeholderHost := $productData.placeholder_host }}
{{- /* Allow influxdb_host parameter to override auto-detected product */ -}}
{{- $overrideKey := .Get "influxdb_host" -}}
{{- $productData := $productInfo.productData -}}
{{- if $overrideKey -}}
{{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}}
{{- $overrideRef := index $productAliases $overrideKey -}}
{{- if $overrideRef -}}
{{- $productData = index .Site.Data.products $overrideRef | default dict -}}
{{- end -}}
{{- end -}}
{{- $placeholderHost := $productData.placeholder_host | default "localhost:8086" }}
{{- $method := .Get "method" | upper -}}
{{- $methodStyle := .Get "method" | lower -}}
{{- $apiRef := .Get "api-ref" | default "" -}}
@ -18,4 +22,4 @@
{{- else -}}
<span class="api {{ $methodStyle }}">{{ $method }}</span> {{ $renderedEndpoint }}
{{- end -}}
</pre>
</pre>

View File

@ -42,14 +42,9 @@
{{ end }}
{{ end }}
{{ if .Params.list_code_example }}
{{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}}
{{- $currentVersion := index $productPathData 1 -}}
{{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}}
{{- $productKey := cond $isOSS "oss" $currentVersion -}}
{{- $productAliases := dict "oss" "influxdb" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}}
{{- $productRef := index $productAliases $productKey -}}
{{- $productData := index .Site.Data.products $productRef -}}
{{- $placeholderHost := $productData.placeholder_host }}
{{- /* Get product info using baseURL-independent path detection */ -}}
{{- $productInfo := partial "product-info.html" $.Page -}}
{{- $placeholderHost := $productInfo.productData.placeholder_host | default "localhost:8086" }}
{{ .Params.list_code_example | replaceRE `\{\{[<\%] influxdb/host [>%]\}\}` $placeholderHost | .RenderString }}
{{ end }}

View File

@ -1,9 +1,13 @@
{{- $productPathData := findRE "[^/]+.*?" .Page.RelPermalink -}}
{{- $currentVersion := index $productPathData 1 -}}
{{- $isOSS := ne (len (findRE `^v[0-9]` $currentVersion)) 0 -}}
{{- $parsedProductKey := cond $isOSS "oss" $currentVersion -}}
{{- $productKey := .Get 0 | default $parsedProductKey -}}
{{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}}
{{- $productRef := index $productAliases $productKey -}}
{{- $productData := index .Site.Data.products $productRef -}}
{{ $productData.placeholder_host }}
{{- /* Get product info using baseURL-independent path detection */ -}}
{{- $productInfo := partial "product-info.html" .Page -}}
{{- /* Allow positional parameter to override auto-detected product */ -}}
{{- $overrideKey := .Get 0 -}}
{{- $productData := $productInfo.productData -}}
{{- if $overrideKey -}}
{{- $productAliases := dict "oss" "influxdb" "cloud" "influxdb_cloud" "cloud-tsm" "influxdb_cloud" "core" "influxdb3_core" "enterprise" "influxdb3_enterprise" "cloud-serverless" "influxdb3_cloud_serverless" "serverless" "influxdb3_cloud_serverless" "cloud-dedicated" "influxdb3_cloud_dedicated" "dedicated" "influxdb3_cloud_dedicated" "clustered" "influxdb3_clustered" -}}
{{- $overrideRef := index $productAliases $overrideKey -}}
{{- if $overrideRef -}}
{{- $productData = index .Site.Data.products $overrideRef | default dict -}}
{{- end -}}
{{- end -}}
{{ $productData.placeholder_host | default "localhost:8086" }}