mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-04-22 14:59:16 +00:00
feat: prepend /exchange/:sub to relative anchor hrefs in question bodies
This commit is contained in:
parent
f2ccff9f1a
commit
5b01a5c232
1 changed files with 11 additions and 3 deletions
|
@ -80,7 +80,7 @@ func ViewQuestion(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newFilteredQuestion, err := extractQuestionData(doc, domain)
|
newFilteredQuestion, err := extractQuestionData(doc, domain, params.Sub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(500, "home.html", gin.H{
|
c.HTML(500, "home.html", gin.H{
|
||||||
"errorMessage": "Failed to extract question data",
|
"errorMessage": "Failed to extract question data",
|
||||||
|
@ -167,7 +167,7 @@ func fetchQuestionData(soLink string) (resp *resty.Response, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractQuestionData parses the HTML document and extracts question data.
|
// extractQuestionData parses the HTML document and extracts question data.
|
||||||
func extractQuestionData(doc *goquery.Document, domain string) (question types.FilteredQuestion, err error) {
|
func extractQuestionData(doc *goquery.Document, domain, exchangeSub string) (question types.FilteredQuestion, err error) {
|
||||||
// Extract the question title.
|
// Extract the question title.
|
||||||
questionTextParent := doc.Find("h1.fs-headline1").First()
|
questionTextParent := doc.Find("h1.fs-headline1").First()
|
||||||
question.Title = strings.TrimSpace(questionTextParent.Children().First().Text())
|
question.Title = strings.TrimSpace(questionTextParent.Children().First().Text())
|
||||||
|
@ -182,7 +182,15 @@ func extractQuestionData(doc *goquery.Document, domain string) (question types.F
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return question, err
|
return question, err
|
||||||
}
|
}
|
||||||
question.Body = template.HTML(utils.ProcessHTMLBody(questionBodyParentHTML))
|
|
||||||
|
questionBodyParentHTML = utils.ProcessHTMLBody(questionBodyParentHTML)
|
||||||
|
|
||||||
|
// If there's an exchange sub, prepend the /exchange/:sub prefix to prevent unintended platform switching.
|
||||||
|
if exchangeSub != "" {
|
||||||
|
questionBodyParentHTML = utils.ConvertRelativeAnchorURLsToAbsolute(questionBodyParentHTML, fmt.Sprintf("/exchange/%s", exchangeSub))
|
||||||
|
}
|
||||||
|
|
||||||
|
question.Body = template.HTML(questionBodyParentHTML)
|
||||||
|
|
||||||
// Extract the shortened body description.
|
// Extract the shortened body description.
|
||||||
shortenedBody := strings.TrimSpace(questionBodyParent.Text())
|
shortenedBody := strings.TrimSpace(questionBodyParent.Text())
|
||||||
|
|
Loading…
Add table
Reference in a new issue