Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnoweb): add metadata to md content #3797

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion examples/gno.land/r/docs/docs.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package docs

func frontMatter() string {
return `---
Title: Welcome to the Gno examples documentation index
Description: Welcome to the Gno examples documentation index. Explore various examples to learn more about Gno functionality and usage.
---`
}

func Render(_ string) string {
return `# Gno Examples Documentation
content := ""
content += frontMatter()
content += `
# Gno Examples Documentation

Welcome to the Gno examples documentation index.
Explore various examples to learn more about Gno functionality and usage.
Expand All @@ -23,4 +33,5 @@ Explore various examples to learn more about Gno functionality and usage.

- [Official documentation](https://github.com/gnolang/gno/tree/master/docs) <!-- should be /docs with gnoweb embedding the docs/ folder. -->
`
return content
}
45 changes: 35 additions & 10 deletions gno.land/pkg/gnoweb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,35 @@

switch {
case gnourl.IsRealm(), gnourl.IsPure():
return h.GetPackageView(gnourl)
return h.GetPackageView(gnourl, indexData)
default:
h.Logger.Debug("invalid path: path is neither a pure package or a realm")
return http.StatusBadRequest, components.StatusErrorComponent("invalid path")
}
}

// GetPackageView handles package pages.
func (h *WebHandler) GetPackageView(gnourl *weburl.GnoURL) (int, *components.View) {
func (h *WebHandler) GetPackageView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View) {
// Handle Help page
if gnourl.WebQuery.Has("help") {
return h.GetHelpView(gnourl)
return h.GetHelpView(gnourl, indexData)
}

// Handle Source page
if gnourl.WebQuery.Has("source") || gnourl.IsFile() {
return h.GetSourceView(gnourl)
return h.GetSourceView(gnourl, indexData)
}

// Handle Source page
if gnourl.IsDir() || gnourl.IsPure() {
return h.GetDirectoryView(gnourl)
return h.GetDirectoryView(gnourl, indexData)
}

// Ultimately get realm view
return h.GetRealmView(gnourl)
return h.GetRealmView(gnourl, indexData)
}

func (h *WebHandler) GetRealmView(gnourl *weburl.GnoURL) (int, *components.View) {
func (h *WebHandler) GetRealmView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View) {
var content bytes.Buffer

meta, err := h.Client.RenderRealm(&content, gnourl.Path, gnourl.EncodeArgs())
Expand All @@ -171,6 +171,19 @@
return GetClientErrorStatusPage(gnourl, err)
}

// HTML Head metadata
if meta.Head.Title != "" {
indexData.Title = meta.Head.Title + " - " + h.Static.Domain

Check warning on line 176 in gno.land/pkg/gnoweb/handler.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/handler.go#L176

Added line #L176 was not covered by tests
} else {
indexData.HeadData.Title = h.Static.Domain + " - " + gnourl.Path
}

if meta.Head.Description != "" {
indexData.Description = meta.Head.Description

Check warning on line 182 in gno.land/pkg/gnoweb/handler.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/handler.go#L182

Added line #L182 was not covered by tests
} else {
indexData.Description = "Render of the Gno Realm " + gnourl.Path + " from " + h.Static.Domain + "."
}

return http.StatusOK, components.RealmView(components.RealmData{
TocItems: &components.RealmTOCData{
Items: meta.Toc.Items,
Expand All @@ -182,13 +195,17 @@
})
}

func (h *WebHandler) GetHelpView(gnourl *weburl.GnoURL) (int, *components.View) {
func (h *WebHandler) GetHelpView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View) {
fsigs, err := h.Client.Functions(gnourl.Path)
if err != nil {
h.Logger.Error("unable to fetch path functions", "error", err)
return GetClientErrorStatusPage(gnourl, err)
}

// HTML Head metadata
indexData.HeadData.Title = h.Static.Domain + " - " + gnourl.Path + " docs and realm interactions"
indexData.Description = "Read the Realm " + gnourl.Path + " functions an interact with them from " + h.Static.Domain + "."

// Get selected function
selArgs := make(map[string]string)
selFn := gnourl.WebQuery.Get("func")
Expand Down Expand Up @@ -220,7 +237,7 @@
})
}

func (h *WebHandler) GetSourceView(gnourl *weburl.GnoURL) (int, *components.View) {
func (h *WebHandler) GetSourceView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View) {
pkgPath := gnourl.Path
files, err := h.Client.Sources(pkgPath)
if err != nil {
Expand Down Expand Up @@ -251,6 +268,10 @@
return GetClientErrorStatusPage(gnourl, err)
}

// HTML Head metadata
indexData.HeadData.Title = h.Static.Domain + " - " + fileName + " source code from " + gnourl.Path
indexData.Description = "Check " + fileName + " source code from " + gnourl.Path + " on " + h.Static.Domain + "."

fileSizeStr := fmt.Sprintf("%.2f Kb", meta.SizeKb)
return http.StatusOK, components.SourceView(components.SourceData{
PkgPath: gnourl.Path,
Expand All @@ -263,7 +284,7 @@
})
}

func (h *WebHandler) GetDirectoryView(gnourl *weburl.GnoURL) (int, *components.View) {
func (h *WebHandler) GetDirectoryView(gnourl *weburl.GnoURL, indexData *components.IndexData) (int, *components.View) {
pkgPath := strings.TrimSuffix(gnourl.Path, "/")
files, err := h.Client.Sources(pkgPath)
if err != nil {
Expand All @@ -276,6 +297,10 @@
return http.StatusOK, components.StatusErrorComponent("no files available")
}

// HTML Head metadata
indexData.HeadData.Title = h.Static.Domain + " - " + gnourl.Path + " files directory"
indexData.Description = "Browse the " + gnourl.Path + " directory on " + h.Static.Domain + "."

return http.StatusOK, components.DirectoryView(components.DirData{
PkgPath: gnourl.Path,
Files: files,
Expand Down
Loading
Loading