Refactor SDK snippets and add title

This commit is contained in:
Ben Croker 2025-02-23 17:48:01 -06:00
parent 2db7468cd5
commit a4c2e87473
No known key found for this signature in database
GPG Key ID: 09D799816F1CF332
5 changed files with 28 additions and 23 deletions

View File

@ -39,7 +39,7 @@ type DefaultString struct {
} }
type Language struct { type Language struct {
FileExtension string Handle string
Name string Name string
Icon string Icon string
SdkUrl string SdkUrl string
@ -63,67 +63,67 @@ var Consts = &ConstTemplateData{
DoNotEdit: "This is auto-generated by Datastar. DO NOT EDIT.", DoNotEdit: "This is auto-generated by Datastar. DO NOT EDIT.",
SDKLanguages: []Language{ SDKLanguages: []Language{
{ {
FileExtension: "clojure", Handle: "clojure",
Name: "Clojure", Name: "Clojure",
Icon: "vscode-icons:file-type-clojure", Icon: "vscode-icons:file-type-clojure",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/clojure", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/clojure",
}, },
{ {
FileExtension: "dotnet", Handle: "csharp",
Name: "Dotnet", Name: "C#",
Icon: "vscode-icons:file-type-csharp2", Icon: "vscode-icons:file-type-csharp2",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/dotnet", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/dotnet",
}, },
{ {
FileExtension: "go", Handle: "go",
Name: "Go", Name: "Go",
Icon: "vscode-icons:file-type-go-gopher", Icon: "vscode-icons:file-type-go-gopher",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/go", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/go",
}, },
{ {
FileExtension: "haskell", Handle: "haskell",
Name: "Haskell", Name: "Haskell",
Icon: "vscode-icons:file-type-haskell", Icon: "vscode-icons:file-type-haskell",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/haskell", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/haskell",
}, },
{ {
FileExtension: "java", Handle: "java",
Name: "Java", Name: "Java",
Icon: "vscode-icons:file-type-java", Icon: "vscode-icons:file-type-java",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/java", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/java",
}, },
{ {
FileExtension: "php", Handle: "php",
Name: "PHP", Name: "PHP",
Icon: "vscode-icons:file-type-php", Icon: "vscode-icons:file-type-php",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/php", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/php",
}, },
{ {
FileExtension: "python", Handle: "python",
Name: "Python", Name: "Python",
Icon: "vscode-icons:file-type-python", Icon: "vscode-icons:file-type-python",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/python", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/python",
}, },
{ {
FileExtension: "rust", Handle: "rust",
Name: "Rust", Name: "Rust",
Icon: "vscode-icons:file-type-rust", Icon: "vscode-icons:file-type-rust",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/rust", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/rust",
}, },
{ {
FileExtension: "ruby", Handle: "ruby",
Name: "Ruby", Name: "Ruby",
Icon: "vscode-icons:file-type-ruby", Icon: "vscode-icons:file-type-ruby",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/ruby", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/ruby",
}, },
{ {
FileExtension: "ts", Handle: "typescript",
Name: "TypeScript", Name: "TypeScript",
Icon: "vscode-icons:file-type-typescript-official", Icon: "vscode-icons:file-type-typescript-official",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/typescript", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/typescript",
}, },
{ {
FileExtension: "zig", Handle: "zig",
Name: "Zig", Name: "Zig",
Icon: "vscode-icons:file-type-zig", Icon: "vscode-icons:file-type-zig",
SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/zig", SdkUrl: "https://github.com/starfederation/datastar/tree/main/sdk/zig",

View File

@ -352,12 +352,13 @@ templ codeSnippetFragment(block CodeSnippetBlock) {
<div class="join"> <div class="join">
for _, snippet := range block.Snippets { for _, snippet := range block.Snippets {
<button <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( data-class={ fmt.Sprintf(
"{'btn-primary btn-outline z-0': $language == '%s'}", "{'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> <iconify-icon icon={ snippet.Icon } class="text-4xl" noobserver></iconify-icon>
</button> </button>
@ -365,7 +366,7 @@ templ codeSnippetFragment(block CodeSnippetBlock) {
</div> </div>
</div> </div>
for _, snippet := range block.Snippets { 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 } { snippet.Path.Snake }
@templ.Raw(snippet.ContentHighlighted) @templ.Raw(snippet.ContentHighlighted)
</div> </div>

View File

@ -33,7 +33,8 @@ var (
type CodeSnippet struct { type CodeSnippet struct {
Path toolbelt.CasedString Path toolbelt.CasedString
Extension string Handle string
Name string
Icon string Icon string
Content string Content string
ContentHighlighted string ContentHighlighted string
@ -186,7 +187,7 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
continue 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) codeSnippetRaw, err := staticFS.ReadFile(fileFullPath)
if err != nil { if err != nil {
@ -197,13 +198,15 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
buf := bytebufferpool.Get() buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf) 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) return nil, fmt.Errorf("error highlighting code snippet %s: %w", fileFullPath, err)
} }
name = ""
icon := "" icon := ""
for _, lang := range build.Consts.SDKLanguages { for _, lang := range build.Consts.SDKLanguages {
if lang.FileExtension == ext { if lang.Handle == handle {
name = lang.Name
icon = lang.Icon icon = lang.Icon
break break
} }
@ -213,7 +216,8 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
} }
snippet := CodeSnippet{ snippet := CodeSnippet{
Extension: ext, Handle: handle,
Name: name,
Icon: icon, Icon: icon,
Content: codeSnippet, Content: codeSnippet,
ContentHighlighted: buf.String(), ContentHighlighted: buf.String(),
@ -221,7 +225,7 @@ func markdownRenders(ctx context.Context, staticMdPath string) (MarkdownDataset,
snippetBlock.Snippets = append(snippetBlock.Snippets, snippet) snippetBlock.Snippets = append(snippetBlock.Snippets, snippet)
} }
slices.SortFunc(snippetBlock.Snippets, func(a, b CodeSnippet) int { 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() buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf) defer bytebufferpool.Put(buf)