2023-06-24 20:39:55 +08:00
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
// 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 model
|
|
|
|
|
|
|
|
import (
|
2025-09-13 11:58:52 +08:00
|
|
|
"bytes"
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/88250/lute/ast"
|
2025-09-13 11:58:52 +08:00
|
|
|
"github.com/88250/lute/editor"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/88250/lute/render"
|
2022-07-17 12:22:32 +08:00
|
|
|
"github.com/siyuan-note/logging"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AutoSpace(rootID string) (err error) {
|
2024-03-10 23:27:13 +08:00
|
|
|
tree, err := LoadTreeByBlockID(rootID)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 11:23:08 +08:00
|
|
|
logging.LogInfof("formatting tree [%s]...", rootID)
|
2023-05-14 10:44:39 +08:00
|
|
|
util.PushProtyleLoading(rootID, Conf.Language(116))
|
2025-04-14 22:03:31 +08:00
|
|
|
defer ReloadProtyle(rootID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2024-10-22 19:20:44 +08:00
|
|
|
FlushTxQueue()
|
2022-09-17 21:35:42 +08:00
|
|
|
|
2024-04-02 17:35:01 +08:00
|
|
|
generateOpTypeHistory(tree, HistoryOpFormat)
|
2022-06-28 18:56:16 +08:00
|
|
|
luteEngine := NewLute()
|
2022-05-26 15:18:53 +08:00
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
2025-09-13 11:58:52 +08:00
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n.Type {
|
|
|
|
case ast.NodeTextMark:
|
|
|
|
luteEngine.MergeSameTextMark(n) // 合并相邻的同类行级节点
|
|
|
|
case ast.NodeCodeBlockCode:
|
|
|
|
// 代码块中包含 ``` 时 `优化排版` 异常 `Optimize typography` exception when code block contains ``` https://github.com/siyuan-note/siyuan/issues/15843
|
|
|
|
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte(editor.Zwj+"```"), []byte("```"))
|
|
|
|
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("```"), []byte(editor.Zwj+"```"))
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2022-06-28 18:56:16 +08:00
|
|
|
return ast.WalkContinue
|
|
|
|
})
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2022-06-28 18:56:16 +08:00
|
|
|
rootIAL := tree.Root.KramdownIAL
|
|
|
|
addBlockIALNodes(tree, false)
|
|
|
|
|
2022-07-27 00:58:53 +08:00
|
|
|
// 第一次格式化为了合并相邻的文本节点
|
2022-05-26 15:18:53 +08:00
|
|
|
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
|
|
|
md := formatRenderer.Render()
|
|
|
|
newTree := parseKTree(md)
|
2022-10-16 16:16:12 +08:00
|
|
|
newTree.Root.Spec = "1"
|
2022-07-27 00:58:53 +08:00
|
|
|
// 第二次格式化启用自动空格
|
|
|
|
luteEngine.SetAutoSpace(true)
|
|
|
|
formatRenderer = render.NewFormatRenderer(newTree, luteEngine.RenderOptions)
|
|
|
|
md = formatRenderer.Render()
|
|
|
|
newTree = parseKTree(md)
|
2022-10-16 16:16:12 +08:00
|
|
|
newTree.Root.Spec = "1"
|
2022-05-26 15:18:53 +08:00
|
|
|
newTree.Root.ID = tree.ID
|
|
|
|
newTree.Root.KramdownIAL = rootIAL
|
|
|
|
newTree.ID = tree.ID
|
|
|
|
newTree.Path = tree.Path
|
|
|
|
newTree.HPath = tree.HPath
|
|
|
|
newTree.Box = tree.Box
|
2024-04-11 21:54:34 +08:00
|
|
|
err = writeTreeUpsertQueue(newTree)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
2024-02-12 11:23:08 +08:00
|
|
|
logging.LogInfof("formatted tree [%s]", rootID)
|
2022-09-17 21:35:42 +08:00
|
|
|
util.RandomSleep(500, 700)
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|