-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "feat!: removes custom content help support"
This reverts commit 904b2a4.
- Loading branch information
Showing
8 changed files
with
138 additions
and
0 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
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,18 @@ | ||
<template> | ||
<page-with-links> | ||
<section> | ||
<article class="prose" v-html="data.content" v-if="data"></article> | ||
</section> | ||
</page-with-links> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
const { id } = defineProps<{ id: string }>(); | ||
const { data } = useFetch(() => withBase("/api/content/" + id), { | ||
refetch: true, | ||
}) | ||
.get() | ||
.json<{ title: string; content: string }>(); | ||
</script> | ||
<style lang="postcss" scoped></style> |
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,67 @@ | ||
package content | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/yuin/goldmark" | ||
meta "github.com/yuin/goldmark-meta" | ||
"github.com/yuin/goldmark/extension" | ||
"github.com/yuin/goldmark/parser" | ||
) | ||
|
||
type Page struct { | ||
Id string `json:"id"` | ||
Title string `json:"title"` | ||
Content string `json:"content,omitempty"` | ||
} | ||
|
||
func ReadAll() ([]Page, error) { | ||
var pages []Page | ||
files, err := filepath.Glob("data/content/*.md") | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading /data/content/*.md: %w", err) | ||
} | ||
|
||
for _, file := range files { | ||
id := filepath.Base(file) | ||
id = id[0 : len(id)-3] | ||
page, err := Read(id) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading %s: %w", id, err) | ||
} | ||
pages = append(pages, page) | ||
} | ||
|
||
return pages, nil | ||
} | ||
|
||
func Read(id string) (Page, error) { | ||
data, err := os.ReadFile("data/content/" + id + ".md") | ||
if err != nil { | ||
return Page{}, fmt.Errorf("error reading /data/content/%s.md: %w", id, err) | ||
} | ||
|
||
markdown := goldmark.New( | ||
goldmark.WithExtensions(extension.GFM, meta.New()), | ||
) | ||
context := parser.NewContext() | ||
var buf bytes.Buffer | ||
if err := markdown.Convert(data, &buf, parser.WithContext(context)); err != nil { | ||
return Page{}, fmt.Errorf("error converting markdown: %w", err) | ||
} | ||
|
||
metaData := meta.Get(context) | ||
page := Page{ | ||
Content: buf.String(), | ||
Id: id, | ||
Title: id, | ||
} | ||
if title, ok := metaData["title"]; ok { | ||
page.Title = title.(string) | ||
} | ||
|
||
return page, nil | ||
} |
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,25 @@ | ||
package web | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/amir20/dozzle/internal/content" | ||
"github.com/go-chi/chi/v5" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (h *handler) staticContent(w http.ResponseWriter, r *http.Request) { | ||
id := chi.URLParam(r, "id") | ||
content, err := content.Read(id) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
log.Warnf("error reading content: %v", err) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(content); err != nil { | ||
log.Errorf("json encoding error while streaming %v", err.Error()) | ||
} | ||
} |
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