You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by sh...@apache.org on 2024/03/14 02:04:52 UTC

(incubator-answer) 03/06: fix: template supports multiple script tags

This is an automated email from the ASF dual-hosted git repository.

shuai pushed a commit to branch feat/1.3.0/ui
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 6c9868dcb31068a2021d2de80468898f8aa20471
Author: shuai <li...@sifou.com>
AuthorDate: Wed Mar 6 18:43:43 2024 +0800

    fix: template supports multiple script tags
---
 internal/controller/template_controller.go | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 6b4c4bbe..b128b91a 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -22,7 +22,6 @@ package controller
 import (
 	"encoding/json"
 	"fmt"
-	"github.com/apache/incubator-answer/internal/entity"
 	"html/template"
 	"net/http"
 	"regexp"
@@ -32,6 +31,7 @@ import (
 	"github.com/apache/incubator-answer/internal/base/constant"
 	"github.com/apache/incubator-answer/internal/base/handler"
 	templaterender "github.com/apache/incubator-answer/internal/controller/template_render"
+	"github.com/apache/incubator-answer/internal/entity"
 	"github.com/apache/incubator-answer/internal/schema"
 	"github.com/apache/incubator-answer/internal/service/siteinfo_common"
 	"github.com/apache/incubator-answer/pkg/checker"
@@ -47,7 +47,7 @@ import (
 var SiteUrl = ""
 
 type TemplateController struct {
-	scriptPath               string
+	scriptPath               []string
 	cssPath                  string
 	templateRenderController *templaterender.TemplateRenderController
 	siteInfoService          siteinfo_common.SiteInfoCommonService
@@ -66,18 +66,21 @@ func NewTemplateController(
 		siteInfoService:          siteInfoService,
 	}
 }
-func GetStyle() (script, css string) {
+func GetStyle() (script []string, css string) {
 	file, err := ui.Build.ReadFile("build/index.html")
 	if err != nil {
 		return
 	}
-	scriptRegexp := regexp.MustCompile(`<script defer="defer" src="(.*)"></script>`)
-	scriptData := scriptRegexp.FindStringSubmatch(string(file))
+	scriptRegexp := regexp.MustCompile(`<script defer="defer" src="([^"]*)"></script>`)
+	scriptData := scriptRegexp.FindAllStringSubmatch(string(file), -1)
+	for _, s := range scriptData {
+		if len(s) == 2 {
+			script = append(script, s[1])
+		}
+	}
+
 	cssRegexp := regexp.MustCompile(`<link href="(.*)" rel="stylesheet">`)
 	cssListData := cssRegexp.FindStringSubmatch(string(file))
-	if len(scriptData) == 2 {
-		script = scriptData[1]
-	}
 	if len(cssListData) == 2 {
 		css = cssListData[1]
 	}