website: generate proper titles for markdown files

It appears that markdown generation added class ids at some point.
This means that all pages on perkeep.org have the title "Perkeep"

Also added a test for this case.

This issue was flagged by the Google Webmaster Tools.

Change-Id: I7ddab56e03a506b84aab415d94856b8f1c126250
This commit is contained in:
Paul Lindner 2018-01-04 11:26:06 -08:00
parent 9265c980de
commit 3c9440708e
2 changed files with 50 additions and 1 deletions

View File

@ -68,7 +68,7 @@ const (
prodDomain = "perkeep.org"
)
var h1TitlePattern = regexp.MustCompile(`<h1>([^<]+)</h1>`)
var h1TitlePattern = regexp.MustCompile(`<h1[^>]*>([^<]+)</h1>`)
var (
httpAddr = flag.String("http", defaultAddr, "HTTP address")

View File

@ -17,7 +17,11 @@ limitations under the License.
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
)
@ -83,3 +87,48 @@ func TestIsIssueRequest(t *testing.T) {
}
}
}
func TestDocHandler(t *testing.T) {
// Set up environment
var err error
*root, err = os.Getwd()
if err != nil {
t.Fatalf("Failed to getwd: %v", err)
}
readTemplates()
tests := []struct {
path string
status int
wantSubstr string
}{
// Test that the title tag is constructed from the h1 element
{"/doc/uses", http.StatusOK,
"<title>Use Cases - Perkeep</title>"},
// Test that an html extension redirects to the base path
{"/doc/uses.html", 302, "Found"},
}
for _, tt := range tests {
// Construct a request that maps to the given path
req, err := http.NewRequest("GET", tt.path, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(docHandler)
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != tt.status {
t.Errorf("for path %s, code=%v want %v", tt.path, status, tt.status)
}
// Check that the output contains the specified substring
if !strings.Contains(rr.Body.String(), tt.wantSubstr) {
t.Errorf("for path %s, got %q should contain %q",
tt.path, rr.Body.String(), tt.wantSubstr)
}
}
}