-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTheme): allows to customize CodeSamples langs/generator (#116)
- Loading branch information
1 parent
7ff03ae
commit 4636f94
Showing
27 changed files
with
1,177 additions
and
269 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script setup lang="ts"> | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col"> | ||
<slot name="code" /> | ||
|
||
<details class="rounded border" style="padding: 24px; margin: 16px 0;" open> | ||
<summary>Example</summary> | ||
<slot name="example" /> | ||
</details> | ||
</div> | ||
</template> |
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
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,44 @@ | ||
--- | ||
aside: false | ||
outline: false | ||
--- | ||
|
||
# Code Samples | ||
|
||
Code Samples allow you to showcase API request examples in multiple programming languages. | ||
This feature helps API consumers understand how to integrate with your API using their preferred language. | ||
|
||
You can customize: | ||
|
||
- Which languages to display (`langs`) | ||
- Available languages for selection (`availableLangs`) | ||
- How code is generated for each language (`generator`) | ||
|
||
|
||
<ExampleBlock> | ||
|
||
<template #code> | ||
|
||
## Custom Languages | ||
|
||
For example, you can add [Bru Markup Language](https://docs.usebruno.com/bru-lang/overview) to the list of languages to show and available languages to select from and a generator to convert the request object to Bru code. | ||
|
||
```markdown | ||
--- | ||
aside: false | ||
outline: false | ||
title: vitepress-openapi | ||
--- | ||
|
||
<!--@include: ./parts/code-samples-example.md--> | ||
``` | ||
|
||
</template> | ||
|
||
<template #example> | ||
|
||
<!--@include: ./parts/code-samples-example.md--> | ||
|
||
</template> | ||
|
||
</ExampleBlock> |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<script setup lang="ts"> | ||
import { useData } from 'vitepress' | ||
import { useTheme, generateCodeSample } from 'vitepress-openapi' | ||
|
||
const { isDark } = useData() | ||
|
||
useTheme({ | ||
codeSamples: { | ||
// List of languages to show in Code Samples section. | ||
langs: [ | ||
'bruno', | ||
...useTheme().getCodeSamplesLangs(), | ||
], | ||
// List of available languages to select from. | ||
availableLanguages: [ | ||
{ | ||
lang: 'bruno', | ||
label: 'Bruno', | ||
highlighter: 'plaintext', | ||
}, | ||
...useTheme().getCodeSamplesAvailableLanguages(), | ||
], | ||
defaultLang: 'bruno', | ||
generator: (lang, request) => { | ||
if (lang === 'bruno') { | ||
return generateBruRequest(request) | ||
} | ||
|
||
return generateCodeSample(lang, request) | ||
}, | ||
}, | ||
}) | ||
|
||
function generateBruRequest(request) { | ||
const { url, method, headers, body, query } = request; | ||
|
||
const methodLower = method.toLowerCase(); | ||
|
||
const queryString = query && Object.keys(query).length | ||
? `${url}?${new URLSearchParams(query).toString()}` | ||
: url; | ||
|
||
const headersSection = headers && Object.keys(headers).length | ||
? `headers {\n${Object.entries(headers) | ||
.map(([key, value]) => ` ${key}: ${value}`) | ||
.join('\n')}\n}` | ||
: ''; | ||
|
||
const bodySection = body | ||
? `body {\n ${JSON.stringify(body, null, 2).replace(/\n/g, '\n ')}\n}` | ||
: ''; | ||
|
||
const bruRequest = `${methodLower} { | ||
url: ${queryString} | ||
} | ||
${headersSection} | ||
${bodySection} | ||
`; | ||
|
||
return bruRequest | ||
.trim() | ||
.replace(/\n{2,}/g, '\n\n') // Remove extra newlines | ||
} | ||
</script> | ||
|
||
<OASpec :isDark="isDark" /> |
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,14 @@ | ||
<script setup lang="ts"> | ||
import { useData } from 'vitepress' | ||
import { useTheme } from 'vitepress-openapi' | ||
|
||
const { isDark } = useData() | ||
|
||
useTheme({ | ||
operation: { | ||
badges: ['deprecated', 'operationId'], | ||
}, | ||
}) | ||
</script> | ||
|
||
<OASpec :isDark="isDark" /> |
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
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
Oops, something went wrong.