Self-hosting go documentation on vanity URL
2 December 2022, on Olivier's logGo packages are identified by their import path. This import path usually starts with the URL where the code is hosted, but it can also be another domain which informs the go tool where the actual code can be downloaded (so-called "vanity import paths"). Using a domain you control is great because it allows for the code to be moved between software forges without any disruption for people using the package.
There are already a couple of packages able to automatically generate html with the right meta tags:
4d63.com/vangen (static html, with links to repo and doc)
code.rocketnine.space/tslocum/godoc-static (static godoc)
go.yhsif.com/vanity (redirects to pkg.go.dev)
kkn.fi/vanity (redirects to pkg.go.dev)
However I wasn't delighted with any of those solutions, so I developed my own.
Introducing vanitydoc
The vanitydoc command has the following features:
- static generation of HTML
- inclusion of the `go-import` meta tags (obviously)
- dark and light theme
- static generation of Gemtext (for gemini hosting)
- inclusion of the `VCS Autodiscovery` meta tags
The last point is an RFC which aims at increasing interoperability between software forges and third party tool:
To show an example of interoperability, consider one of my other package:
code.pfad.fr/risefront
- its import path is a vanity URL, which is a static site serving the latest documentation of the package (accessible via https and gemini protocols thanks to sourcehut pages)
- the presence of the meta tags, allows godocs.io (alternative to pkg.go.dev) to generate links to the source code
godocs.io/code.pfad.fr/risefront
- while the actual source code is hosted on codeberg
https://codeberg.org/pfad.fr/risefront
Basic usage
To generate the documentation for a module, simply call `vanitydoc` (it does not rely on `go list` since March 2023):
go run code.pfad.fr/vanitydoc@latest -html=dist/html -gemtext=dist/gmi gitea://codeberg.org/pfad.fr/risefront
Requirements
- The package requires go 1.19 for compilation (it uses the new go/doc package)
Future work
I am currently happy of this package and plan to use it as-is. It could be improved in multiple ways, however I didn't find an elegant solution to the following problems yet:
- Listing the packages currently requires the go toolchain. Figure out a way to compile "go list" inside the package (done in March 2023).
- The generated documentation is for the current build tags. Generate all sets of builds tags under specific URLs.
- Document the version which was used to generate the documentation (golang.org/x/tools/go/vcs package is missing cmd/go/internal/vcs.Cmd.Status)
Feel free to send a patch on codeberg or by email to contribute to this package (licensed under BSD-3-Clause)!