From 221693d93796c382d09fd72d8c2ef9142653761c Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 26 Oct 2022 20:44:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:art:=20=E6=94=AF=E6=8C=81=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=95=8C=E9=9D=A2=E8=AE=BE=E7=BD=AE=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=89=87=E6=AE=B5=20https://github.com/siyuan-note/siyuan/issu?= =?UTF-8?q?es/6357?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/router.go | 1 + kernel/api/snippet.go | 21 +++++++++++ kernel/model/conf.go | 25 ------------- kernel/model/snippet.go | 82 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 kernel/model/snippet.go diff --git a/kernel/api/router.go b/kernel/api/router.go index 6507ebd59..ba33d4ac1 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -283,5 +283,6 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/notification/pushErrMsg", model.CheckAuth, pushErrMsg) ginServer.Handle("POST", "/api/snippet/getSnippet", model.CheckAuth, getSnippet) + ginServer.Handle("POST", "/api/snippet/setSnippet", model.CheckAuth, setSnippet) ginServer.Handle("GET", "/snippets/*filepath", serveSnippets) } diff --git a/kernel/api/snippet.go b/kernel/api/snippet.go index 1b4853944..12fe7a230 100644 --- a/kernel/api/snippet.go +++ b/kernel/api/snippet.go @@ -88,3 +88,24 @@ func getSnippet(c *gin.Context) { "snippets": snippets, } } + +func setSnippet(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + name := arg["name"].(string) + typ := arg["type"].(string) + content := arg["content"].(string) + enabled := arg["enabled"].(bool) + err := model.SetSnippet(name, typ, content, enabled) + if nil != err { + ret.Code = -1 + ret.Msg = "set snippet failed: " + err.Error() + return + } +} diff --git a/kernel/model/conf.go b/kernel/model/conf.go index f652a94ec..0ea11d94f 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -696,28 +696,3 @@ func clearWorkspaceTemp() { logging.LogInfof("cleared workspace temp") } - -var loadSnippetsLock = sync.Mutex{} - -func LoadSnippets() (ret []*conf.Snippet, err error) { - loadSnippetsLock.Lock() - defer loadSnippetsLock.Unlock() - - ret = []*conf.Snippet{} - confPath := filepath.Join(util.DataDir, "snippets/conf.json") - if !gulu.File.IsExist(confPath) { - return - } - - data, err := filelock.ReadFile(confPath) - if nil != err { - logging.LogErrorf("load js snippets failed: %s", err) - return - } - - if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err { - logging.LogErrorf("unmarshal js snippets failed: %s", err) - return - } - return -} diff --git a/kernel/model/snippet.go b/kernel/model/snippet.go new file mode 100644 index 000000000..719ebc8c1 --- /dev/null +++ b/kernel/model/snippet.go @@ -0,0 +1,82 @@ +// SiYuan - Build Your Eternal Digital Garden +// 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 . + +package model + +import ( + "path/filepath" + "sync" + + "github.com/88250/gulu" + "github.com/siyuan-note/filelock" + "github.com/siyuan-note/logging" + "github.com/siyuan-note/siyuan/kernel/conf" + "github.com/siyuan-note/siyuan/kernel/util" +) + +var snippetsLock = sync.Mutex{} + +func SetSnippet(name, typ, content string, enabled bool) (err error) { + snippetsLock.Lock() + defer snippetsLock.Unlock() + + snippets, err := loadSnippets() + if nil != err { + return + } + snippet := &conf.Snippet{Name: name, Type: typ, Content: content, Enabled: enabled} + snippets = append(snippets, snippet) + err = writeSnippetsConf(snippets) + return +} + +func LoadSnippets() (ret []*conf.Snippet, err error) { + snippetsLock.Lock() + defer snippetsLock.Unlock() + return loadSnippets() +} + +func loadSnippets() (ret []*conf.Snippet, err error) { + ret = []*conf.Snippet{} + confPath := filepath.Join(util.DataDir, "snippets/conf.json") + if !gulu.File.IsExist(confPath) { + return + } + + data, err := filelock.ReadFile(confPath) + if nil != err { + logging.LogErrorf("load js snippets failed: %s", err) + return + } + + if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err { + logging.LogErrorf("unmarshal js snippets failed: %s", err) + return + } + return +} + +func writeSnippetsConf(snippets []*conf.Snippet) (err error) { + data, err := gulu.JSON.MarshalIndentJSON(snippets, "", " ") + if nil != err { + logging.LogErrorf("marshal snippets failed: %s", err) + return + } + + confPath := filepath.Join(util.DataDir, "snippets/conf.json") + err = filelock.WriteFile(confPath, data) + return +} From ed7e85ce6d7841526156751e15403a113e396b47 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 26 Oct 2022 22:23:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=E5=8F=8D?= =?UTF-8?q?=E9=93=BE=E6=8F=90=E5=8F=8A=E6=96=87=E6=A1=A3=20https://ld246.c?= =?UTF-8?q?om/article/1666764286802?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/appearance/langs/en_US.json | 2 +- app/appearance/langs/es_ES.json | 2 +- .../20200924101200-gss5vee.sy | 63 ++++++++++++++++-- .../20200813131152-0wk5akh.sy | 65 +++++++++++++++++-- .../20211226120349-rbkmozu.sy | 60 ++++++++++++++--- 5 files changed, 171 insertions(+), 21 deletions(-) diff --git a/app/appearance/langs/en_US.json b/app/appearance/langs/en_US.json index e255de83d..b568c6f11 100644 --- a/app/appearance/langs/en_US.json +++ b/app/appearance/langs/en_US.json @@ -115,7 +115,7 @@ "type": "Type", "searchType": "Type (searches in the enabled types below, the filter option in the global search overrides this setting)", "searchAttr": "Attribute (search not only in the content, but also in the following enabled attributes)", - "searchBackmention": "Backlink Mentions (backlink mentions search keywords are obtained from)", + "searchBackmention": "Backlink mentions (backlink mentions search keywords are obtained from)", "searchVirtualRef": "Virtual reference (virtual reference search keywords are obtained from)", "netImg2LocalAsset": "Convert network images to local images", "releaseDate": "Release Date", diff --git a/app/appearance/langs/es_ES.json b/app/appearance/langs/es_ES.json index 62c9cacce..275a00789 100644 --- a/app/appearance/langs/es_ES.json +++ b/app/appearance/langs/es_ES.json @@ -115,7 +115,7 @@ "type": "Tipo", "searchType": "Tipos (busque en los tipos habilitados a continuación, la opción de filtro en la búsqueda global anula esta configuración)", "searchAttr": "Atributo (busca no sólo en el contenido, sino también en los siguientes atributos habilitados)", - "searchBackmention": "Menciones de Backlinks (las menciones de backlinks de las que se obtienen las palabras clave de la búsqueda)", + "searchBackmention": "Menciones de backlinks (las menciones de backlinks de las que se obtienen las palabras clave de la búsqueda)", "searchVirtualRef": "Referencia virtual (las palabras clave de búsqueda de referencias virtuales se obtienen a partir de ellas)", "netImg2LocalAsset": "Convertir imágenes de red en imágenes locales", "releaseDate": "Fecha de lanzamiento", diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101200-gss5vee.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101200-gss5vee.sy index f474efad0..4e8b6411d 100644 --- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101200-gss5vee.sy +++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-6yi0dv5/20200924101200-gss5vee.sy @@ -6,7 +6,7 @@ "id": "20200924101200-gss5vee", "title": "Navigate in the Content Block", "type": "doc", - "updated": "20220312233028" + "updated": "20221026222138" }, "Children": [ { @@ -430,17 +430,70 @@ "ID": "20210106204954-rpwfh3i", "Type": "NodeParagraph", "Properties": { - "id": "20210106204954-rpwfh3i" + "id": "20210106204954-rpwfh3i", + "updated": "20221026221426" }, "Children": [ { "Type": "NodeText", - "Data": "It is mentioned that the name and aliases of the content block in the current document is used as a keyword to search, and the search result is a content block containing these keywords. You can use the link button to convert the mention into a link: the mention will be replaced with " + "Data": "Mentions are searched using the content block name, alias, reference anchor text and document name in the current document as keywords (the search scope can be set in " }, { "Type": "NodeTextMark", - "TextMarkType": "code", - "TextMarkTextContent": "((id \"content block name\"))" + "TextMarkType": "kbd", + "TextMarkTextContent": "Settings" + }, + { + "Type": "NodeText", + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "Search" + }, + { + "Type": "NodeText", + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "Backlink mentions" + }, + { + "Type": "NodeText", + "Data": "​), the search results are blocks of content that contain these keywords." + } + ] + }, + { + "ID": "20221026222024-1ug5zq4", + "Type": "NodeParagraph", + "Properties": { + "id": "20221026222024-1ug5zq4", + "updated": "20221026222138" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "tag", + "TextMarkTextContent": "Note" + }, + { + "Type": "NodeText", + "Data": "​: Mention search will only search in normal text, not in line-level elements such as bold and italics, and block-level elements such as code blocks and charts. The rule are the same as the " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "block-ref", + "TextMarkBlockRefID": "20211010212319-oy401ib", + "TextMarkBlockRefSubtype": "s", + "TextMarkTextContent": "detection rules of virtual reference" }, { "Type": "NodeText", diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200813131152-0wk5akh.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200813131152-0wk5akh.sy index bc19067c8..468ac6586 100644 --- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200813131152-0wk5akh.sy +++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-fqgskfj/20200813131152-0wk5akh.sy @@ -6,7 +6,7 @@ "id": "20200813131152-0wk5akh", "title": "在内容块中遨游", "type": "doc", - "updated": "20220312233009" + "updated": "20221026222004" }, "Children": [ { @@ -433,21 +433,74 @@ "ID": "20210106201743-z1iz0ce", "Type": "NodeParagraph", "Properties": { - "id": "20210106201743-z1iz0ce" + "id": "20210106201743-z1iz0ce", + "updated": "20221026221118" }, "Children": [ { "Type": "NodeText", - "Data": "提及使用当前文档中内容块名称和别名作为关键字进行搜索,搜索结果为包含这些关键字的内容块。可以通过链接按钮将提及转换为链接:提及处会使用 " + "Data": "提及使用当前文档中内容块命名、别名、引用锚文本和文档名作为关键字进行搜索(可在 " }, { "Type": "NodeTextMark", - "TextMarkType": "code", - "TextMarkTextContent": "((id \"内容块名称\"))" + "TextMarkType": "kbd", + "TextMarkTextContent": "设置" }, { "Type": "NodeText", - "Data": " 进行替换。" + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "搜索" + }, + { + "Type": "NodeText", + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "反链提及" + }, + { + "Type": "NodeText", + "Data": "​ 中设置搜索范围),搜索结果为包含这些关键字的内容块。" + } + ] + }, + { + "ID": "20221026221105-dyj8l8n", + "Type": "NodeParagraph", + "Properties": { + "id": "20221026221105-dyj8l8n", + "updated": "20221026222004" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "tag", + "TextMarkTextContent": "注意" + }, + { + "Type": "NodeText", + "Data": "​:提及搜索仅会在普通文本中搜索,不会在粗体、斜体等行级元素和代码块、图表等块级元素中搜索,这一点和" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "block-ref", + "TextMarkBlockRefID": "20211010211708-0qn8cpl", + "TextMarkBlockRefSubtype": "s", + "TextMarkTextContent": "虚拟引用探测规则" + }, + { + "Type": "NodeText", + "Data": "是相同的。" } ] } diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120349-rbkmozu.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120349-rbkmozu.sy index 76a8ccb8a..891918b5d 100644 --- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120349-rbkmozu.sy +++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226115825-mhcslw2/20211226120349-rbkmozu.sy @@ -5,7 +5,7 @@ "Properties": { "id": "20211226120349-rbkmozu", "title": "在内容塊中遨遊", - "updated": "20220312233003" + "updated": "20221026222236" }, "Children": [ { @@ -380,21 +380,39 @@ "Type": "NodeParagraph", "Properties": { "id": "20211226120415-k8yvb62", - "updated": "20211226152312" + "updated": "20221026221359" }, "Children": [ { "Type": "NodeText", - "Data": "提及使用當前文檔中內容塊名稱和別名作為關鍵字進行搜索,搜索結果為包含這些關鍵字的內容塊。可以通過連結按鈕將提及轉換為連結:提及處會使用 " + "Data": "提及使用當前文檔中內容塊命名、別名、引用錨文本和文檔名作為關鍵字進行搜索(可在 " }, { "Type": "NodeTextMark", - "TextMarkType": "code", - "TextMarkTextContent": "((id \"內容塊名稱\"))" + "TextMarkType": "kbd", + "TextMarkTextContent": "設置" }, { "Type": "NodeText", - "Data": " 進行替換。" + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "搜索" + }, + { + "Type": "NodeText", + "Data": "​ - " + }, + { + "Type": "NodeTextMark", + "TextMarkType": "kbd", + "TextMarkTextContent": "反鏈提及" + }, + { + "Type": "NodeText", + "Data": "​ 中設置搜索範圍),搜索結果為包含這些關鍵字的內容塊。" } ] }, @@ -403,8 +421,34 @@ "Type": "NodeParagraph", "Properties": { "id": "20211228134547-inyam43", - "updated": "20211228134547" - } + "updated": "20221026222236" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "tag", + "TextMarkTextContent": "注意" + }, + { + "Type": "NodeText", + "Data": "​:提及搜索僅會在普通文本中搜索,不會在粗體、斜體等行級元素和代碼塊、圖表等塊級元素中搜索,這一點和" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "block-ref", + "TextMarkBlockRefID": "20211226122825-w6wxq0c", + "TextMarkBlockRefSubtype": "s", + "TextMarkTextContent": "虛擬引用探測規則" + }, + { + "Type": "NodeText", + "Data": "是相同的。" + } + ] } ] } \ No newline at end of file