Skip to content

Commit 3a607b1

Browse files
committed
Use configexample to format snippets
1 parent cc7e7fe commit 3a607b1

File tree

4 files changed

+87
-50
lines changed

4 files changed

+87
-50
lines changed

docs/k8s/guides/endpoint-types.mdx

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: Endpoint Types
44

55
import TabItem from "@theme/TabItem";
66
import Tabs from "@theme/Tabs";
7+
import ConfigExample from "/src/components/ConfigExample.tsx";
78

89
The ngrok Kubernetes operator offers `CloudEndpoint` and `AgentEndpoint` as custom resource types that let you create endpoints in ngrok.
910
The [using CRDs guide](/k8s/guides/using-crds) goes over the details and differences about how to configure each one.
@@ -17,39 +18,63 @@ The following examples showcase how you can create endpoints that accept `http:/
1718
<TabItem value="agent-endpoint" label="AgentEndpoint" default>
1819
The example showcases an upstream that accepts `http://` requests, but you can also use `https://` for the upstream.
1920
For `AgentEndpoints`, the `upstream.url` scheme does not need to match the scheme of the public URL.
20-
21-
```yaml
22-
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
23-
kind: AgentEndpoint
24-
metadata:
25-
name: https-agent-endpoint
26-
namespace: default
27-
spec:
28-
url: http://example-http-domain.ngrok.app
29-
upstream:
30-
url: http://example-service.default:8080
31-
bindings:
32-
- public
33-
```
21+
22+
<ConfigExample
23+
title="ngrok-crds"
24+
snippetText={null}
25+
showLineNumbers={true}
26+
hideJSON={true}
27+
config={{
28+
apiVersion: "ngrok.k8s.ngrok.com/v1alpha1",
29+
kind: "AgentEndpoint",
30+
metadata: {
31+
name: "https-agent-endpoint",
32+
namespace: "default",
33+
},
34+
spec: {
35+
url: "http://example-http-domain.ngrok.app",
36+
upstream: {
37+
url: "http://example-service.default:8080",
38+
},
39+
bindings: ["public"],
40+
},
41+
}}
42+
/>
3443
</TabItem>
3544
<TabItem value="cloud-endpoint" label="CloudEndpoint">
36-
```yaml
37-
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
38-
kind: CloudEndpoint
39-
metadata:
40-
name: http-cloud-endpoint
41-
spec:
42-
url: http://example-http-domain.ngrok.app
43-
bindings:
44-
- public
45-
trafficPolicy:
46-
policy:
47-
on_http_request:
48-
- actions:
49-
- type: forward-internal
50-
config:
51-
url: http://example-service.internal:8080
52-
```
45+
<ConfigExample
46+
title="ngrok-crds"
47+
snippetText={null}
48+
showLineNumbers={true}
49+
hideJSON={true}
50+
config={{
51+
apiVersion: "ngrok.k8s.ngrok.com/v1alpha1",
52+
kind: "CloudEndpoint",
53+
metadata: {
54+
name: "http-cloud-endpoint",
55+
},
56+
spec: {
57+
url: "http://example-http-domain.ngrok.app",
58+
bindings: ["public"],
59+
trafficPolicy: {
60+
policy: {
61+
on_http_request: [
62+
{
63+
actions: [
64+
{
65+
type: "forward-internal",
66+
config: {
67+
url: "http://example-service.internal:8080",
68+
},
69+
},
70+
],
71+
},
72+
],
73+
},
74+
},
75+
},
76+
}}
77+
/>
5378
</TabItem>
5479
</Tabs>
5580

src/components/CodeBlockWithInfo/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function CodeBlockWithInfo({
6666
<CodeBlock.Body>
6767
{!meta?.disableCopy && <CodeBlock.CopyButton />}
6868
<CodeBlock.Code
69+
indentation={meta?.indentation}
6970
language={language}
7071
value={fmtCode`${content}`}
7172
/>

src/components/ConfigExample.tsx

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,37 @@ const showExample = (
1313
title,
1414
icon,
1515
snippetText,
16+
hideJSON,
17+
hideYAML,
1618
}: ConfigExampleProps,
1719
yamlConfig: string,
1820
jsonConfig: string,
1921
) => {
2022
const titleToUse = title || defaultTitle;
23+
const yamlBlock = <DocsCodeBlock
24+
language="yaml"
25+
metastring={yamlMetastring}
26+
title={`${titleToUse}.yml`}
27+
icon={icon}
28+
>
29+
{snippetText ? `# ${snippetText}\n${yamlConfig}` : yamlConfig}
30+
</DocsCodeBlock>;
31+
32+
const jsonBlock =
33+
<DocsCodeBlock
34+
language="json"
35+
metastring={jsonMetastring}
36+
title={`${titleToUse}.json`}
37+
icon={icon}
38+
>
39+
{snippetText ? `// ${snippetText}\n${jsonConfig}` : jsonConfig}
40+
</DocsCodeBlock>;
41+
if(hideJSON) return yamlBlock;
42+
if(hideYAML) return jsonBlock;
2143
return (
2244
<LangSwitcher className="mb-3">
23-
<DocsCodeBlock
24-
language="yaml"
25-
metastring={yamlMetastring}
26-
title={`${titleToUse}.yml`}
27-
icon={icon}
28-
>
29-
{snippetText ? `# ${snippetText}\n${yamlConfig}` : yamlConfig}
30-
</DocsCodeBlock>
31-
<DocsCodeBlock
32-
language="json"
33-
metastring={jsonMetastring}
34-
title={`${titleToUse}.json`}
35-
icon={icon}
36-
>
37-
{snippetText ? `// ${snippetText}\n${jsonConfig}` : jsonConfig}
38-
</DocsCodeBlock>
45+
{yamlBlock}
46+
{jsonBlock}
3947
</LangSwitcher>
4048
);
4149
};
@@ -68,6 +76,8 @@ export type ConfigExampleProps = {
6876
showLineNumbers?: boolean;
6977
yamlMetastring?: string;
7078
jsonMetastring?: string;
79+
hideJSON?: boolean;
80+
hideYAML?: boolean;
7181
title?: string;
7282
icon?: ReactNode;
7383
showAgentConfig?: boolean;
@@ -78,6 +88,7 @@ export default function ConfigExample({
7888
// Show the agent config by default
7989
showAgentConfig = false,
8090
showTrafficPolicy = true,
91+
title,
8192
...props
8293
}: ConfigExampleProps) {
8394
const yamlOptions = {
@@ -91,15 +102,15 @@ export default function ConfigExample({
91102
const policyJsonConfig = JSON.stringify(props.config, null, 2);
92103

93104
const policySnippet = showExample(
94-
"traffic-policy",
105+
title || "traffic-policy",
95106
props,
96107
policyYamlConfig,
97108
policyJsonConfig,
98109
);
99110

100111
const agentConfig = getAgentConfig(props.config, yamlOptions);
101112
const agentConfigSnippet = showExample(
102-
"config",
113+
title || "ngrok",
103114
props,
104115
agentConfig.yamlConfig,
105116
agentConfig.jsonConfig,

src/components/code-block.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function DocsCodeBlock({
6161
const langInClassName = langMatchesInClassName
6262
? langMatchesInClassName[0]?.split("-")[1]
6363
: "";
64-
const langToFind = langInClassName || parseLanguage(langInClassName);
64+
const langToFind = langInClassName || parseLanguage(_language || langInClassName);
6565

6666
const language = getLanguageInfo(langToFind) || defaultLanguageInfo;
6767

0 commit comments

Comments
 (0)