From 5b01a5c23239cde4d19cc6b9de3127ccf3eb5820 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Thu, 13 Jun 2024 02:03:15 -0400 Subject: [PATCH] feat: prepend /exchange/:sub to relative anchor hrefs in question bodies --- src/routes/question.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/routes/question.go b/src/routes/question.go index b4c230c..4aba159 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -80,7 +80,7 @@ func ViewQuestion(c *gin.Context) { return } - newFilteredQuestion, err := extractQuestionData(doc, domain) + newFilteredQuestion, err := extractQuestionData(doc, domain, params.Sub) if err != nil { c.HTML(500, "home.html", gin.H{ "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. -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. questionTextParent := doc.Find("h1.fs-headline1").First() 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 { 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. shortenedBody := strings.TrimSpace(questionBodyParent.Text())