From d701c4ebc62d5c52ae1f4abeb3137b68cc6410ff Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Mon, 10 Aug 2020 17:59:59 +0200 Subject: [PATCH] hackdoc: do not render links to pages that wouldn't serve anything This gets rid of annoying clickable 404 links. Change-Id: Ibf767875af29f4571e7f935d494b44dde002fac6 --- devtools/hackdoc/markdown.go | 29 ++++++++++++++++++++++++++++- devtools/hackdoc/tpl/default.html | 9 ++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/devtools/hackdoc/markdown.go b/devtools/hackdoc/markdown.go index 14ffe45e..eaed905c 100644 --- a/devtools/hackdoc/markdown.go +++ b/devtools/hackdoc/markdown.go @@ -50,6 +50,29 @@ type pathPart struct { Path string } +func (r *request) renderable(dirpath string) bool { + cfg, err := config.ForPath(r.ctx, r.source, dirpath) + if err != nil { + glog.Errorf("could not get config for path %q: %v", dirpath, err) + return false + } + + for _, f := range cfg.DefaultIndex { + fpath := dirpath + "/" + f + file, err := r.source.IsFile(r.ctx, fpath) + if err != nil { + glog.Errorf("IsFile(%q): %v", fpath, err) + return false + } + + if file { + return true + } + } + + return false +} + func (r *request) handleFile(path string, cfg *config.Config) { data, err := r.source.ReadFile(r.ctx, path) if err != nil { @@ -85,7 +108,11 @@ func (r *request) handleFile(path string, cfg *config.Config) { label = label + "/" } fullPath += "/" + p - pathParts = append(pathParts, pathPart{Label: label, Path: fullPath}) + target := fullPath + if i != len(parts)-1 && !r.renderable("/"+fullPath) { + target = "" + } + pathParts = append(pathParts, pathPart{Label: label, Path: target}) } vars := map[string]interface{}{ diff --git a/devtools/hackdoc/tpl/default.html b/devtools/hackdoc/tpl/default.html index edb4c332..e6280dba 100644 --- a/devtools/hackdoc/tpl/default.html +++ b/devtools/hackdoc/tpl/default.html @@ -72,6 +72,7 @@ body { font-family: Consolas, monospace; margin-top: 1rem; padding: 0.5em 0 0.5em 0; + display: inline-flex; } .header a { @@ -231,7 +232,13 @@ h1,h2,h3,h4 {
hackdoc: - {{ range .PathParts }}{{ .Label }}{{ end }} + {{ range .PathParts }} + {{ if ne .Path "" }} + {{ .Label }} + {{ else }} + {{ .Label }} + {{ end }} + {{ end }} shortcuts: root, cluster docs, codelabs
{{ .Rendered }}