From a8c54c1a32764d38727fb7c9f02ed9bc298e3174 Mon Sep 17 00:00:00 2001 From: mbays Date: Wed, 25 Aug 2021 12:08:56 +0200 Subject: Serve robots.txt disallowing all robots This overrides any robots.txt file in the proxied gemini capsule, on the basis that this is intended for gemini robots (which can be expected to follow the robots.txt companion spec) rather than web robots. The main purpose though for disallowing web robots is to prevent them from crawling the proxied cross-site geminispace under /x/, since web robots won't know even to read the robots.txt files for other capsules proxied this way. --- main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index c600985..2b227a0 100644 --- a/main.go +++ b/main.go @@ -583,6 +583,12 @@ func main() { return } + if r.URL.Path == "/robots.txt" { + w.WriteHeader(http.StatusOK) + w.Write([]byte("User-agent: *\nDisallow: /\n")) + return + } + req := gemini.Request{} req.URL = &url.URL{} req.URL.Scheme = root.Scheme -- cgit v1.2.3 From 857f8c97ebc5724f4c34931ba497425e7653894e Mon Sep 17 00:00:00 2001 From: nytpu Date: Thu, 4 Nov 2021 11:04:50 -0600 Subject: Generate anchor links for headings This will generate `id` attributes for all heading levels. The labels are generated using steps 1-4 of the gitlab flavored markdown algorithm which seems to be pretty standard for generating anchors: https://docs.gitlab.com/ee/user/markdown.html#header-ids-and-links A unique ID isn't appended to avoid having to store a list of previous headers to compare against. --- main.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2b227a0..c96b8a1 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "os" "strings" "time" + "unicode" "git.sr.ht/~adnano/go-gemini" "git.sr.ht/~sircmpwn/getopt" @@ -24,11 +25,11 @@ var gemtextPage = template.Must(template. "heading": func(line gemini.Line) *GemtextHeading { switch l := line.(type) { case gemini.LineHeading1: - return &GemtextHeading{1, string(l)} + return &GemtextHeading{1, string(l), createAnchor(string(l))} case gemini.LineHeading2: - return &GemtextHeading{2, string(l)} + return &GemtextHeading{2, string(l), createAnchor(string(l))} case gemini.LineHeading3: - return &GemtextHeading{3, string(l)} + return &GemtextHeading{3, string(l), createAnchor(string(l))} default: return nil } @@ -147,7 +148,7 @@ var gemtextPage = template.Must(template. {{- with . | heading }} {{- $isList = false -}} - {{.Text}} + {{.Text}} {{- end -}} {{- with . | link }} @@ -388,8 +389,24 @@ type InputContext struct { } type GemtextHeading struct { - Level int - Text string + Level int + Text string + Anchor string +} + +func createAnchor(heading string) string { + var anchor strings.Builder + prev := '-' + for _, c := range heading { + if unicode.IsLetter(c) || unicode.IsDigit(c) { + anchor.WriteRune(unicode.ToLower(c)) + prev = c + } else if (unicode.IsSpace(c) || c == '-') && prev != '-' { + anchor.WriteRune('-') + prev = '-' + } + } + return strings.ToLower(anchor.String()) } func proxyGemini(req gemini.Request, external bool, root *url.URL, -- cgit v1.2.3