Skip to content

Commit

Permalink
Revert "feat!: removes custom content help support"
Browse files Browse the repository at this point in the history
This reverts commit 904b2a4.
  • Loading branch information
amir20 committed Dec 28, 2023
1 parent 904b2a4 commit 6a4c384
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
12 changes: 12 additions & 0 deletions assets/components/Links.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<template>
<div class="flex items-center justify-end gap-4">
<template v-if="config.pages">
<router-link
:to="{ name: 'content-id', params: { id: page.id } }"
:title="page.title"
v-for="page in config.pages"
:key="page.id"
class="link-primary"
>
{{ page.title }}
</router-link>
</template>

<dropdown class="dropdown-end" @closed="latestTag = latest?.tag ?? config.version">
<template #trigger>
<mdi:announcement class="size-6 -rotate-12" />
Expand Down
18 changes: 18 additions & 0 deletions assets/pages/content/[id].vue
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>
1 change: 1 addition & 0 deletions assets/stores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Config {
name: string;
};
profile?: Profile;
pages?: { id: string; title: string }[];
}

export interface Profile {
Expand Down
67 changes: 67 additions & 0 deletions internal/content/disk.go
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
}
25 changes: 25 additions & 0 deletions internal/web/content.go
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())
}
}
3 changes: 3 additions & 0 deletions internal/web/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/amir20/dozzle/internal/analytics"
"github.com/amir20/dozzle/internal/content"
"github.com/amir20/dozzle/internal/docker"

log "github.com/sirupsen/logrus"
Expand All @@ -32,6 +33,7 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
events := make(chan docker.ContainerEvent)
stats := make(chan docker.ContainerStat)

pages, _ := content.ReadAll()
b := analytics.BeaconEvent{
Name: "events",
Version: h.config.Version,
Expand All @@ -41,6 +43,7 @@ func (h *handler) streamEvents(w http.ResponseWriter, r *http.Request) {
HasCustomBase: h.config.Base != "/",
HasCustomAddress: h.config.Addr != ":8080",
Clients: len(h.clients),
HasDocumentation: len(pages) > 0,
HasActions: h.config.EnableActions,
}

Expand Down
11 changes: 11 additions & 0 deletions internal/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"

"github.com/amir20/dozzle/internal/auth"
"github.com/amir20/dozzle/internal/content"
"github.com/amir20/dozzle/internal/docker"
"github.com/amir20/dozzle/internal/profile"

Expand Down Expand Up @@ -53,6 +54,16 @@ func (h *handler) executeTemplate(w http.ResponseWriter, req *http.Request) {
"enableActions": h.config.EnableActions,
}

pages, err := content.ReadAll()
if err != nil {
log.Errorf("error reading content: %v", err)
} else if len(pages) > 0 {
for i, _ := range pages {
pages[i].Content = ""
}
config["pages"] = pages
}

user := auth.UserFromContext(req.Context())
if user != nil {
if profile, err := profile.Load(*user); err == nil {
Expand Down
1 change: 1 addition & 0 deletions internal/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func createRouter(h *handler) *chi.Mux {
r.Get("/api/releases", h.releases)
r.Get("/api/profile/avatar", h.avatar)
r.Patch("/api/profile", h.updateProfile)
r.Get("/api/content/{id}", h.staticContent)
r.Get("/logout", h.clearSession) // TODO remove this
r.Get("/version", h.version)
})
Expand Down

0 comments on commit 6a4c384

Please sign in to comment.