Преглед на файлове

Merge remote-tracking branch 'origin/dev' into dev

Vanessa преди 1 година
родител
ревизия
a54ddd5c8e
променени са 5 файла, в които са добавени 100 реда и са изтрити 18 реда
  1. 22 17
      kernel/filesys/json_parser.go
  2. 36 0
      kernel/filesys/json_unmarshal.go
  3. 34 0
      kernel/filesys/json_unmarshal_windows_amd64.go
  4. 1 1
      kernel/go.mod
  5. 7 0
      kernel/model/import.go

+ 22 - 17
kernel/filesys/json_parser.go

@@ -1,12 +1,18 @@
-// Lute - 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript
-// Copyright (c) 2019-present, b3log.org
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
 //
-// Lute is licensed under Mulan PSL v2.
-// You can use this software according to the terms and conditions of the Mulan PSL v2.
-// You may obtain a copy of Mulan PSL v2 at:
-//         http://license.coscl.org.cn/MulanPSL2
-// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-// See the Mulan PSL v2 for more details.
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 package filesys
 
@@ -14,17 +20,16 @@ import (
 	"bytes"
 	"strings"
 
+	"github.com/88250/gulu"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/editor"
 	"github.com/88250/lute/parse"
-	"github.com/88250/lute/util"
-	"github.com/goccy/go-json"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
 )
 
 func ParseJSONWithoutFix(jsonData []byte, options *parse.Options) (ret *parse.Tree, err error) {
 	root := &ast.Node{}
-	err = json.Unmarshal(jsonData, root)
+	err = unmarshalJSON(jsonData, root)
 	if nil != err {
 		return
 	}
@@ -45,7 +50,7 @@ func ParseJSONWithoutFix(jsonData []byte, options *parse.Options) (ret *parse.Tr
 
 func ParseJSON(jsonData []byte, options *parse.Options) (ret *parse.Tree, needFix bool, err error) {
 	root := &ast.Node{}
-	err = json.Unmarshal(jsonData, root)
+	err = unmarshalJSON(jsonData, root)
 	if nil != err {
 		return
 	}
@@ -91,7 +96,7 @@ func ParseJSON(jsonData []byte, options *parse.Options) (ret *parse.Tree, needFi
 }
 
 func genTreeByJSON(node *ast.Node, tree *parse.Tree, idMap *map[string]bool, needFix, needMigrate2Spec1 *bool, ignoreFix bool) {
-	node.Tokens, node.Type = util.StrToBytes(node.Data), ast.Str2NodeType(node.TypeStr)
+	node.Tokens, node.Type = gulu.Str.ToBytes(node.Data), ast.Str2NodeType(node.TypeStr)
 	node.Data, node.TypeStr = "", ""
 	node.KramdownIAL = parse.Map2IAL(node.Properties)
 	node.Properties = nil
@@ -178,14 +183,14 @@ func fixLegacyData(tip, node *ast.Node, idMap *map[string]bool, needFix, needMig
 
 	switch node.Type {
 	case ast.NodeIFrame:
-		if bytes.Contains(node.Tokens, util.StrToBytes("iframe-content")) {
-			start := bytes.Index(node.Tokens, util.StrToBytes("<iframe"))
-			end := bytes.Index(node.Tokens, util.StrToBytes("</iframe>"))
+		if bytes.Contains(node.Tokens, gulu.Str.ToBytes("iframe-content")) {
+			start := bytes.Index(node.Tokens, gulu.Str.ToBytes("<iframe"))
+			end := bytes.Index(node.Tokens, gulu.Str.ToBytes("</iframe>"))
 			node.Tokens = node.Tokens[start : end+9]
 			*needFix = true
 		}
 	case ast.NodeWidget:
-		if bytes.Contains(node.Tokens, util.StrToBytes("http://127.0.0.1:6806")) {
+		if bytes.Contains(node.Tokens, gulu.Str.ToBytes("http://127.0.0.1:6806")) {
 			node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("http://127.0.0.1:6806"), nil)
 			*needFix = true
 		}

+ 36 - 0
kernel/filesys/json_unmarshal.go

@@ -0,0 +1,36 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+//go:build !(windows && amd64)
+
+package filesys
+
+import (
+	"time"
+
+	"github.com/goccy/go-json"
+	"github.com/siyuan-note/logging"
+)
+
+func unmarshalJSON(data []byte, v interface{}) error {
+	now := time.Now()
+	defer func() {
+		elapsed := time.Since(now)
+		logging.LogInfof("[go-json] unmarshalJSON took %s", elapsed)
+	}()
+
+	return json.Unmarshal(data, v)
+}

+ 34 - 0
kernel/filesys/json_unmarshal_windows_amd64.go

@@ -0,0 +1,34 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package filesys
+
+import (
+	"time"
+
+	"github.com/bytedance/sonic"
+	"github.com/siyuan-note/logging"
+)
+
+func unmarshalJSON(data []byte, v interface{}) error {
+	now := time.Now()
+	defer func() {
+		elapsed := time.Since(now)
+		logging.LogInfof("[sonic] unmarshalJSON took %s", elapsed)
+	}()
+
+	return sonic.Unmarshal(data, v)
+}

+ 1 - 1
kernel/go.mod

@@ -17,6 +17,7 @@ require (
 	github.com/PuerkitoBio/goquery v1.8.1
 	github.com/Xuanwo/go-locale v1.1.0
 	github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
+	github.com/bytedance/sonic v1.10.0
 	github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
 	github.com/denisbrodbeck/machineid v1.0.1
 	github.com/dgraph-io/ristretto v0.1.1
@@ -77,7 +78,6 @@ require (
 	github.com/andybalholm/cascadia v1.3.2 // indirect
 	github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect
 	github.com/aws/aws-sdk-go v1.44.332 // indirect
-	github.com/bytedance/sonic v1.10.0 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
 	github.com/chenzhuoyu/iasm v0.9.0 // indirect

+ 7 - 0
kernel/model/import.go

@@ -170,6 +170,13 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
 				if "" != newDefID {
 					n.TextMarkBlockRefID = newDefID
 				}
+			} else if ast.NodeTextMark == n.Type && n.IsTextMarkType("a") && strings.HasPrefix(n.TextMarkAHref, "siyuan://blocks/") {
+				// Block hyperlinks do not point to regenerated block IDs when importing .sy.zip https://github.com/siyuan-note/siyuan/issues/9083
+				defID := strings.TrimPrefix(n.TextMarkAHref, "siyuan://blocks/")
+				newDefID := blockIDs[defID]
+				if "" != newDefID {
+					n.TextMarkAHref = "siyuan://blocks/" + newDefID
+				}
 			} else if ast.NodeBlockQueryEmbedScript == n.Type {
 				for oldID, newID := range blockIDs {
 					// 导入 `.sy.zip` 后查询嵌入块失效 https://github.com/siyuan-note/siyuan/issues/5316