-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add link-preview handler #929
base: devel
Are you sure you want to change the base?
Conversation
Also please see comments in the code. Thank you. |
This comment was marked as resolved.
This comment was marked as resolved.
data := strings.ToLower(token.Data) | ||
if data == "meta" { | ||
var name, property, content string | ||
for _, attr := range token.Attr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use tokenizer.TagAttr()
instead of iterating attributes directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please resolve my previous comment on this.
…ard cache-control header
data := strings.ToLower(token.Data) | ||
if data == "meta" { | ||
var name, property, content string | ||
for _, attr := range token.Attr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please resolve my previous comment on this.
preview.Title = strings.TrimSpace(tokenizer.Token().Data) | ||
} | ||
case html.EndTagToken: | ||
if tokenizer.Token().DataAtom == atom.Title { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to test it. Just assign false
without testing. Consider this example:
<body>
<head>
<title><!-- no closing title -->
</head>
<body>This is not a title</body>
</html>
} | ||
|
||
case html.TextToken: | ||
if preview.Title == "" && inTitleTag { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if inTitleTag {
if preview.Title == "" {
preview.Title = strings.TrimSpace(tokenizer.Token().Data)
}
inTitleTag = false
}
} | ||
} | ||
|
||
if strings.HasPrefix(property, "og:") && content != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if content != "" {
if strings.HasPrefix(property, "og:") {
...
} else if name == "description" && preview.Description == "" {
...
}
}
or
if content == "" {
continue
}
|
||
case html.TextToken: | ||
if preview.Title == "" && inTitleTag { | ||
preview.Title = strings.TrimSpace(tokenizer.Token().Data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving strings.TrimSpace
to sanitizePreview
and applying it to all fields.
w.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this up, before checking for MethodHead
. Response to HEAD
should have "Content-Type"
header.
add link-preview handler