diff options
Diffstat (limited to 'brokenlinks.go')
| -rw-r--r-- | brokenlinks.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/brokenlinks.go b/brokenlinks.go new file mode 100644 index 0000000..768216d --- /dev/null +++ b/brokenlinks.go @@ -0,0 +1,68 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-only + +package jarink + +import ( + "fmt" + "slices" + "strings" +) + +const Version = `0.1.0` + +// StatusBadLink status for link that is not parseable by [url.Parse] or not +// reachable during GET or HEAD, either timeout or IP or domain not exist. +const StatusBadLink = 700 + +// BrokenlinksOptions define the options for scanning broken links. +type BrokenlinksOptions struct { + Url string + IsVerbose bool +} + +// Broken store the broken link, HTTP status code, and the error message that +// cause it. +type Broken struct { + Link string + Error string `json:"omitempty"` + Code int +} + +// BrokenlinksResult store the result of scanning for broken links. +type BrokenlinksResult struct { + // PageLinks store the page and its broken links. + PageLinks map[string][]Broken +} + +func newBrokenlinksResult() *BrokenlinksResult { + return &BrokenlinksResult{ + PageLinks: map[string][]Broken{}, + } +} + +func (result *BrokenlinksResult) sort() { + for _, listBroken := range result.PageLinks { + slices.SortFunc(listBroken, func(a, b Broken) int { + return strings.Compare(a.Link, b.Link) + }) + } +} + +// Brokenlinks scan the URL for broken links. +func Brokenlinks(opts BrokenlinksOptions) (result *BrokenlinksResult, err error) { + var logp = `brokenlinks` + var wrk *brokenlinksWorker + + wrk, err = newWorker(opts) + if err != nil { + return nil, fmt.Errorf(`%s: %s`, logp, err) + } + + result, err = wrk.run() + if err != nil { + return nil, fmt.Errorf(`%s: %s`, logp, err) + } + + return result, nil +} |
