Refactor SDK snippets and add title
This commit is contained in:
parent
2db7468cd5
commit
a4c2e87473
|
@ -39,7 +39,7 @@ type DefaultString struct {
|
|||
}
|
||||
|
||||
type Language struct {
|
||||
FileExtension string
|
||||
Handle string
|
||||
Name string
|
||||
Icon string
|
||||
SdkUrl string
|
||||
|
@ -63,67 +63,67 @@ var Consts = &ConstTemplateData{
|
|||
DoNotEdit: "This is auto-generated by Datastar. DO NOT EDIT.",
|
||||
SDKLanguages: []Language{
|
||||
{
|
||||
FileExtension: "clojure",
|
||||
Handle: "clojure",
|
||||
Name: "Clojure",
|
||||
Icon: "vscode-icons:file-type-clojure",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/clojure",
|
||||
},
|
||||
{
|
||||
FileExtension: "dotnet",
|
||||
Name: "Dotnet",
|
||||
Handle: "csharp",
|
||||
Name: "C#",
|
||||
Icon: "vscode-icons:file-type-csharp2",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/dotnet",
|
||||
},
|
||||
{
|
||||
FileExtension: "go",
|
||||
Handle: "go",
|
||||
Name: "Go",
|
||||
Icon: "vscode-icons:file-type-go-gopher",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/go",
|
||||
},
|
||||
{
|
||||
FileExtension: "haskell",
|
||||
Handle: "haskell",
|
||||
Name: "Haskell",
|
||||
Icon: "vscode-icons:file-type-haskell",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/haskell",
|
||||
},
|
||||
{
|
||||
FileExtension: "java",
|
||||
Handle: "java",
|
||||
Name: "Java",
|
||||
Icon: "vscode-icons:file-type-java",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/java",
|
||||
},
|
||||
{
|
||||
FileExtension: "php",
|
||||
Handle: "php",
|
||||
Name: "PHP",
|
||||
Icon: "vscode-icons:file-type-php",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/php",
|
||||
},
|
||||
{
|
||||
FileExtension: "python",
|
||||
Handle: "python",
|
||||
Name: "Python",
|
||||
Icon: "vscode-icons:file-type-python",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/python",
|
||||
},
|
||||
{
|
||||
FileExtension: "rust",
|
||||
Handle: "rust",
|
||||
Name: "Rust",
|
||||
Icon: "vscode-icons:file-type-rust",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/rust",
|
||||
},
|
||||
{
|
||||
FileExtension: "ruby",
|
||||
Handle: "ruby",
|
||||
Name: "Ruby",
|
||||
Icon: "vscode-icons:file-type-ruby",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/ruby",
|
||||
},
|
||||
{
|
||||
FileExtension: "ts",
|
||||
Handle: "typescript",
|
||||
Name: "TypeScript",
|
||||
Icon: "vscode-icons:file-type-typescript-official",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/typescript",
|
||||
},
|
||||
{
|
||||
FileExtension: "zig",
|
||||
Handle: "zig",
|
||||
Name: "Zig",
|
||||
Icon: "vscode-icons:file-type-zig",
|
||||
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/zig",
|
||||
|
|
|
@ -352,12 +352,13 @@ templ codeSnippetFragment(block CodeSnippetBlock) {
|
|||
<div class="join">
|
||||
for _, snippet := range block.Snippets {
|
||||
<button
|
||||
class="btn px-4 join-item btn-lg flex items-center gap-2"
|
||||
data-on-click={ fmt.Sprintf("$language = '%s'", snippet.Handle) }
|
||||
data-class={ fmt.Sprintf(
|
||||
"{'btn-primary btn-outline z-0': $language == '%s'}",
|
||||
snippet.Extension,
|
||||
snippet.Handle,
|
||||
) }
|
||||
data-on-click={ fmt.Sprintf("$language = '%s'", snippet.Extension) }
|
||||
class="btn px-4 join-item btn-lg flex items-center gap-2"
|
||||
title={ snippet.Name }
|
||||
>
|
||||
<iconify-icon icon={ snippet.Icon } class="text-4xl" noobserver></iconify-icon>
|
||||
</button>
|
||||
|
@ -365,7 +366,7 @@ templ codeSnippetFragment(block CodeSnippetBlock) {
|
|||
</div>
|
||||
</div>
|
||||
for _, snippet := range block.Snippets {
|
||||
<div data-show={ fmt.Sprintf("$language == '%s'", snippet.Extension) }>
|
||||
<div data-show={ fmt.Sprintf("$language == '%s'", snippet.Handle) }>
|
||||
{ snippet.Path.Snake }
|
||||
@templ.Raw(snippet.ContentHighlighted)
|
||||
</div>
|
||||
|
|
|
@ -33,7 +33,8 @@ var (
|
|||
|
||||
type CodeSnippet struct {
|
||||
Path toolbelt.CasedString
|
||||
Extension string
|
||||
Handle string
|
||||
Name string
|
||||
Icon string
|
||||
Content string
|
||||
ContentHighlighted string
|
||||
|
@ -186,7 +187,7 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
|
|||
continue
|
||||
}
|
||||
|
||||
ext := strings.TrimSuffix(filepath.Ext(name), "snippet")[1:] // remove the dot
|
||||
handle := strings.TrimSuffix(filepath.Ext(name), "snippet")[1:] // remove the dot
|
||||
|
||||
codeSnippetRaw, err := staticFS.ReadFile(fileFullPath)
|
||||
if err != nil {
|
||||
|
@ -197,13 +198,15 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
|
|||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
||||
if err := htmlHighlight(buf, codeSnippet, ext, ""); err != nil {
|
||||
if err := htmlHighlight(buf, codeSnippet, handle, ""); err != nil {
|
||||
return nil, fmt.Errorf("error highlighting code snippet %s: %w", fileFullPath, err)
|
||||
}
|
||||
|
||||
name = ""
|
||||
icon := ""
|
||||
for _, lang := range build.Consts.SDKLanguages {
|
||||
if lang.FileExtension == ext {
|
||||
if lang.Handle == handle {
|
||||
name = lang.Name
|
||||
icon = lang.Icon
|
||||
break
|
||||
}
|
||||
|
@ -213,7 +216,8 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
|
|||
}
|
||||
|
||||
snippet := CodeSnippet{
|
||||
Extension: ext,
|
||||
Handle: handle,
|
||||
Name: name,
|
||||
Icon: icon,
|
||||
Content: codeSnippet,
|
||||
ContentHighlighted: buf.String(),
|
||||
|
@ -221,7 +225,7 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
|
|||
snippetBlock.Snippets = append(snippetBlock.Snippets, snippet)
|
||||
}
|
||||
slices.SortFunc(snippetBlock.Snippets, func(a, b CodeSnippet) int {
|
||||
return strings.Compare(a.Extension, b.Extension)
|
||||
return strings.Compare(a.Handle, b.Handle)
|
||||
})
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
|
Loading…
Reference in New Issue