forked from Gitlink/gitlink-cli
fix: update _Sidebar.md on wiki page create/delete
CI / test (push) Failing after 2m6s
Details
CI / test (push) Failing after 2m6s
Details
When creating a wiki page, add [[PageName]] to _Sidebar.md. When deleting a wiki page, remove [[PageName]] from _Sidebar.md. This ensures the web UI properly reflects page changes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
87277cbf4f
commit
3ee71a1ffc
|
|
@ -297,6 +297,46 @@ func runView(ctx *common.RuntimeContext) error {
|
|||
return ctx.Output(output.SuccessEnvelope(data, nil))
|
||||
}
|
||||
|
||||
// addSidebarLink adds a page reference to _Sidebar.md (if it exists).
|
||||
func addSidebarLink(tmpDir, pageName string) error {
|
||||
sidebarPath := filepath.Join(tmpDir, "_Sidebar.md")
|
||||
entry := fmt.Sprintf("[[%s]]", pageName)
|
||||
content, err := os.ReadFile(sidebarPath)
|
||||
if err != nil {
|
||||
// If _Sidebar.md doesn't exist, create it
|
||||
return os.WriteFile(sidebarPath, []byte(entry+"\n"), 0644)
|
||||
}
|
||||
if strings.Contains(string(content), entry) {
|
||||
return nil // already present
|
||||
}
|
||||
// Append entry
|
||||
f, err := os.OpenFile(sidebarPath, os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.WriteString(entry + "\n")
|
||||
return err
|
||||
}
|
||||
|
||||
// removeSidebarLink removes a page reference from _Sidebar.md (if it exists).
|
||||
func removeSidebarLink(tmpDir, pageName string) error {
|
||||
sidebarPath := filepath.Join(tmpDir, "_Sidebar.md")
|
||||
content, err := os.ReadFile(sidebarPath)
|
||||
if err != nil {
|
||||
return nil // no sidebar to update
|
||||
}
|
||||
entry := fmt.Sprintf("[[%s]]", pageName)
|
||||
lines := strings.Split(string(content), "\n")
|
||||
var newLines []string
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line) != entry {
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
}
|
||||
return os.WriteFile(sidebarPath, []byte(strings.Join(newLines, "\n")+"\n"), 0644)
|
||||
}
|
||||
|
||||
func runCreate(ctx *common.RuntimeContext) error {
|
||||
if err := ctx.ResolveOwnerRepo(); err != nil {
|
||||
return err
|
||||
|
|
@ -411,6 +451,10 @@ func runUpdate(ctx *common.RuntimeContext) error {
|
|||
return fmt.Errorf("write page: %w", err)
|
||||
}
|
||||
} else if title := ctx.Arg("title"); title != "" {
|
||||
// Update sidebar
|
||||
if err := addSidebarLink(tmpDir, pageName); err != nil {
|
||||
return fmt.Errorf("update sidebar: %w", err)
|
||||
}
|
||||
// Only updating title: read existing content and replace/re-add the heading
|
||||
existing, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
|
|
@ -488,6 +532,10 @@ func runDelete(ctx *common.RuntimeContext) error {
|
|||
return fmt.Errorf("page %q not found", pageName)
|
||||
}
|
||||
|
||||
// Remove from sidebar
|
||||
if err := removeSidebarLink(tmpDir, pageName); err != nil {
|
||||
return fmt.Errorf("update sidebar: %w", err)
|
||||
}
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
return fmt.Errorf("delete page: %w", err)
|
||||
}
|
||||
|
|
@ -497,7 +545,7 @@ func runDelete(ctx *common.RuntimeContext) error {
|
|||
msg = fmt.Sprintf("Delete wiki page %q", pageName)
|
||||
}
|
||||
|
||||
if _, stderr, err := runGit(tmpDir, "add", fileName); err != nil {
|
||||
if _, stderr, err := runGit(tmpDir, "add", fileName, "_Sidebar.md"); err != nil {
|
||||
return fmt.Errorf("git add: %s%s", stderr, err)
|
||||
}
|
||||
if _, stderr, err := runGit(tmpDir, "commit", "-m", msg); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue