aboutsummaryrefslogtreecommitdiff
path: root/pkg/doc.go
blob: 9f85b3a32613a0b4bde2c0b6bc682c3ee33c99fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package pkg

import (
	"bytes"
	"io/fs"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/ast"
	"github.com/yuin/goldmark/extension"
	"github.com/yuin/goldmark/parser"
	"github.com/yuin/goldmark/text"
)

var md = goldmark.New(
	goldmark.WithExtensions(extension.TaskList),
)

type Doc struct {
	path      string
	content   []byte
	links     Set[string]
	Backlinks Set[string]
}

func (doc *Doc) UpdateLinks(source, target string) {
	dirname := filepath.Dir(doc.path)
	relpath, err := filepath.Rel(dirname, target)
	if err != nil {
		return
	}

	for link := range doc.links {
		if filepath.Join(dirname, link) != source {
			continue
		}

		doc.content = bytes.Replace(doc.content, []byte(link), []byte(relpath), -1)
		// TODO: Update doc.links
	}

	doc.Overwrite()
}

func (doc *Doc) Overwrite() {
	os.WriteFile(doc.path, doc.content, 0644)
}

func NewDoc(filename string) *Doc {
	// NOTE: so far, only markdown is supported
	if !strings.HasSuffix(filename, ".md") {
		return nil
	}

	content, err := os.ReadFile(filename)
	if err != nil {
		return nil
	}

	root := md.Parser().Parse(
		text.NewReader(content),
		parser.WithContext(parser.NewContext()),
	)

	links := Set[string]{}
	err = ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
		if entering {
			switch link := n.(type) {
			case *ast.Link:
				href, err := url.PathUnescape(string(link.Destination))
				if err != nil {
					return ast.WalkStop, err
				}

				links.Insert(href)
			}
		}
		return ast.WalkContinue, nil
	})

	if err != nil {
		return nil
	}

	return &Doc{
		path:      filename,
		content:   content,
		links:     links,
		Backlinks: Set[string]{},
	}
}

type Docs Map[string, *Doc]

func (docs Docs) GetOrNew(filename string) *Doc {
	if doc, ok := docs[filename]; ok {
		return doc
	}

	doc := NewDoc(filename)
	docs[filename] = doc
	return doc
}

func NewDocs(dirname string) Docs {
	docs := Docs{}

	dirname, err := filepath.Abs(dirname)
	if err != nil {
		return nil
	}

	if err := filepath.Walk(dirname, func(path string, info fs.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// TODO: Add skip dir config
		if info.IsDir() && (info.Name() == ".git" || info.Name() == "vendor") {
			return filepath.SkipDir
		}

		if doc := NewDoc(path); doc != nil {
			docs[path] = doc
		}

		return nil
	}); err != nil {
		return nil
	}

	for filename, doc := range docs {
		for link := range doc.links {
			abslink := filepath.Join(filepath.Dir(filename), link)
			if _, ok := docs[abslink]; !ok {
				continue
			}

			docs[abslink].Backlinks.Insert(filename)
		}
	}

	return docs
}

func (docs Docs) Contain(filename string) bool {
	_, ok := docs[filename]
	return ok
}

var DocCollection Docs