From 65f97fc7be23cdfa1bcad8dd6bdf31d1ff86c8ff Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 29 Jul 2023 09:16:45 -0700 Subject: [PATCH] pr comments --- d2cli/watch.go | 6 ++++-- d2target/d2target.go | 38 ++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/d2cli/watch.go b/d2cli/watch.go index b370a4d16..866a480b3 100644 --- a/d2cli/watch.go +++ b/d2cli/watch.go @@ -443,8 +443,10 @@ func (w *watcher) handleRoot(hw http.ResponseWriter, r *http.Request) { `, filepath.Base(w.outputPath), w.devMode) // if path is "/x.svg", we just want "x" - split := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), ".") - boardPath := strings.Join(split[:len(split)-1], ".") + boardPath := strings.TrimPrefix(r.URL.Path, "/") + if idx := strings.LastIndexByte(boardPath, '.'); idx != -1 { + boardPath = boardPath[:idx] + } if boardPath != w.boardPath { w.boardPath = boardPath w.requestCompile() diff --git a/d2target/d2target.go b/d2target/d2target.go index 7bbfb63f8..ea8e67b0b 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -81,53 +81,59 @@ func (d *Diagram) GetBoard(boardPath string) *Diagram { return d } - head := path[0] + return d.getBoard(path) +} + +func (d *Diagram) getBoard(boardPath []string) *Diagram { + head := boardPath[0] if head == "index" { return d } switch head { - case "layers", "scenarios", "steps": - if len(path) < 2 { + case "layers": + if len(boardPath) < 2 { return nil } - } - - switch head { - case "layers": for _, b := range d.Layers { - if b.Name == path[1] { - return b.GetBoard(strings.Join(path[2:], string(os.PathSeparator))) + if b.Name == boardPath[1] { + return b.getBoard(boardPath[2:]) } } case "scenarios": + if len(boardPath) < 2 { + return nil + } for _, b := range d.Scenarios { - if b.Name == path[1] { - return b.GetBoard(strings.Join(path[2:], string(os.PathSeparator))) + if b.Name == boardPath[1] { + return b.getBoard(boardPath[2:]) } } case "steps": + if len(boardPath) < 2 { + return nil + } for _, b := range d.Steps { - if b.Name == path[1] { - return b.GetBoard(strings.Join(path[2:], string(os.PathSeparator))) + if b.Name == boardPath[1] { + return b.getBoard(boardPath[2:]) } } } for _, b := range d.Layers { if b.Name == head { - return b.GetBoard(strings.Join(path[1:], string(os.PathSeparator))) + return b.getBoard(boardPath[2:]) } } for _, b := range d.Scenarios { if b.Name == head { - return b.GetBoard(strings.Join(path[1:], string(os.PathSeparator))) + return b.getBoard(boardPath[2:]) } } for _, b := range d.Steps { if b.Name == head { - return b.GetBoard(strings.Join(path[1:], string(os.PathSeparator))) + return b.getBoard(boardPath[2:]) } } return nil