Fix link parsing

This commit is contained in:
Trevor Slocum 2020-11-25 08:52:51 -08:00
parent 208beca154
commit 8c9f7852bb
3 changed files with 33 additions and 9 deletions

View file

@ -1,6 +1,7 @@
1.0.1:
- Display navigation bar in pages
- Add option to allow local file access
- Display navigation bar in pages
- Support dark themed pages via prefers-color-scheme
1.0.0:
- Initial release

View file

@ -399,15 +399,25 @@ template {
color: white;
background-color: black;
}
h1, h2, h3, h4, h5, h6 {
color: white;
}
a {
color: #8d94ff;
color: rgb(26, 168, 245);
text-decoration: none;
}
a:hover {
color: white;
a:hover,
a:focus,
a:active {
color: rgb(24, 151, 219);
text-decoration: underline;
}
a:visited {
color: rgb(200, 118, 255);
}
input {
background-color: black;
color: white;

View file

@ -38,7 +38,11 @@ func rewriteURL(u string, loc *url.URL) string {
} else if loc != nil && len(u) > 0 && !strings.HasPrefix(u, "//") {
newPath := u
if u[0] != '/' {
newPath = path.Join(loc.Path, u)
if loc.Path[len(loc.Path)-1] == '/' {
newPath = path.Join("/", loc.Path, u)
} else {
newPath = path.Join("/", path.Dir(loc.Path), u)
}
}
return "http://" + daemonAddress + "/" + scheme + "/" + loc.Host + newPath
}
@ -84,17 +88,26 @@ func Convert(page []byte, u string) []byte {
if line[splitStart] == ' ' || line[splitStart] == '\t' {
splitStart++
}
split := bytes.SplitN(line[splitStart:], []byte(" "), 2)
if len(split) != 2 {
var split [][]byte
firstSpace := bytes.IndexRune(line[splitStart:], ' ')
firstTab := bytes.IndexRune(line[splitStart:], '\t')
if firstSpace != -1 && (firstTab == -1 || firstSpace < firstTab) {
split = bytes.SplitN(line[splitStart:], []byte(" "), 2)
} else if firstTab != -1 {
split = bytes.SplitN(line[splitStart:], []byte("\t"), 2)
}
linkURL := line[splitStart:]
linkLabel := line[splitStart:]
var linkURL []byte
var linkLabel []byte
if len(split) == 2 {
linkURL = split[0]
linkLabel = split[1]
} else {
linkURL = line[splitStart:]
linkLabel = line[splitStart:]
}
link := append([]byte(`<a href="`), html.EscapeString(rewriteURL(string(linkURL), parsedURL))...)
link = append(link, []byte(`">`)...)
link = append(link, html.EscapeString(string(linkLabel))...)