Skip to content

Support code block highlighting template syntax and export code block templates as paragraphs - #15345

Merged
Vanessa219 merged 12 commits into
siyuan-note:devfrom
Achuan-2:patch-4
Jul 29, 2025
Merged

Support code block highlighting template syntax and export code block templates as paragraphs#15345
Vanessa219 merged 12 commits into
siyuan-note:devfrom
Achuan-2:patch-4

Conversation

@Achuan-2

@Achuan-2 Achuan-2 commented Jul 21, 2025

Copy link
Copy Markdown
Member

✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法

Feature or bug? 特性或者缺陷?

关联:#15344

痛点

思源笔记原生没有模板编辑器,模板语法直接粘贴到笔记里,容易渲染成数学公式等样式,无法原样导出到模板,即无法在思源笔记里完成包含模板语法的模板制作
PixPin_2025-07-21_22-27-38

而使用外部编辑器,当创作带有背景颜色、自定义属性以及含有大量列表的模板时,会非常不便,过去经常要先在思源笔记弄好样式,然后导出为模板,再在外部编辑器添加模板语法,替换占位文字
PixPin_2025-07-21_22-37-01

改进
支持使用代码块来存储模板语法:
代码块语言设置为template时,认为是存放模板语法的地方,导出为模板,代码块里的模板内容变为纯文本段落,这样就可以直接导出带有模板语法的模板,就能直接调用,创建模板变得所见即所得

例子,在思源笔记创建如下内容的笔记
PixPin_2025-07-21_22-25-08

导出为模板
PixPin_2025-07-21_22-25-59

调用
PixPin_2025-07-21_22-26-16

Multilingual or copywriting? 多语言或者文案?

Please submit directly, we will evaluate.
请直接提交,我们会进行评估。

Dev branch!

Any changes should be submitted to the dev branch.
任何改动,请提交到 dev 分支。

@emptylight370

This comment was marked as outdated.

@TCOTC

TCOTC commented Jul 21, 2025

Copy link
Copy Markdown
Contributor

我感觉可以给代码块增加一个内置的 siyuan-template 语言(命名参考 php-template)

@Achuan-2

Achuan-2 commented Jul 21, 2025

Copy link
Copy Markdown
Member Author

我感觉可以给代码块增加一个内置的 siyuan-template 语言(命名参考 php-template)

注册高亮,就会添加语言选项了,至于叫siyuan-template,还是template,D大决定吧(个人觉siyuan-template太长了,不方便打),在思源本体没必要加siyuan,外部软件和网站高亮思源模板才需要加
PixPin_2025-07-22_02-39-17

- 渲染.action 块和{{}}块规则
- 渲染Markdown块
- 思源块属性设置语法
@Achuan-2

Achuan-2 commented Jul 21, 2025

Copy link
Copy Markdown
Member Author

模板

.action{/*<!-- 计算本周每一天的日期 -->*/}
.action{$weekday := ternary 7 (int (now | Weekday)) (eq (int (now | Weekday)) 0)}
.action{$monday := now.AddDate 0 0 (int (sub 1 $weekday))}
.action{$tuesday := $monday.AddDate 0 0 1}
.action{$wednesday := $monday.AddDate 0 0 2}
.action{$thursday := $monday.AddDate 0 0 3}
.action{$friday := $monday.AddDate 0 0 4}
.action{$saturday := $monday.AddDate 0 0 5}
.action{$sunday := $monday.AddDate 0 0 6}

.action{/*<!-- 获取今天的日期用于标记 -->*/}
.action{$today := (now | date "20060102")}

## .action{$monday| date "20060102 Mon"} - .action{$sunday| date "20060102 Sun"}

#### .action{$monday| date "20060102 Mon"}
{: id="20250721222017-67h99yt" style="background-color: var(--b3-font-background1); --b3-parent-background: var(--b3-font-background1);"}

#### .action{$tuesday| date "20060102 Tue"}
{: style="background-color: var(--b3-font-background2); --b3-parent-background: var(--b3-font-background2);" id="20250721222017-9v6mkzm"}

#### .action{$wednesday| date "20060102 Wed"}
{: id="20250721222017-1l2x8v4" style="background-color: var(--b3-font-background3); --b3-parent-background: var(--b3-font-background3);"}

#### .action{$thursday| date "20060102 Thu"}
{: id="20250721222017-q0u8i0i" style="background-color: var(--b3-font-background4); --b3-parent-background: var(--b3-font-background4);"}

#### .action{$friday| date "20060102 Fri"}
{: id="20250721222017-836ne5r" style="background-color: var(--b3-font-background5); --b3-parent-background: var(--b3-font-background5);"}

#### .action{$saturday| date "20060102 Sat"}
{: id="20250721222017-he3fp7f" style="background-color: var(--b3-font-background6); --b3-parent-background: var(--b3-font-background6);"}

#### .action{$sunday| date "20060102 Sun"}
{: id="20250721222017-tca2w82" style="background-color: var(--b3-font-background7); --b3-parent-background: var(--b3-font-background7);"}

{: id="20250721221357-c0e8mkd" title="20250721 Mon 22:13:57" type="doc"}

渲染效果

  • 渲染.action 块和{{}}块规则:变量、注释、函数、关键字、字符串、数字
  • 渲染Markdown块
  • 渲染思源块属性设置语法为注释,对属性名进行高亮
PixPin_2025-07-22_10-52-43

@Achuan-2

Achuan-2 commented Jul 22, 2025

Copy link
Copy Markdown
Member Author

模板语法高亮预览
PixPin_2025-07-22_10-53-54

<!-- 注释 -->
这个是当前插入文档块的标题:.action{.title}
这个是当前插入文档块的ID:.action{.id}
这个是当前插入文档块的命名:.action{.name}
这个是当前插入文档块的别名:.action{.alias}


## 逻辑判断

.action{if or (eq $weekday "Sat") (eq $weekday "Sun") }

## 使用 ternary 函数计算内联条件

.action{ ternary "Show 1" "Show 2" (eq 1 2) }

## range循环语句

.action{ range $v := <List> }
  {{ $v }}
.action{ end }

## 字典

.action{ $weekday := now | date "Mon" }

.action{$docIconDict := dict "Mon" "day_Today_1_Monday.png" "Tue" "day_Today_2_Tuesday.png" "Wed"
"day_Today_3_Wednesday.png" "Thu" "day_Today_4_Thursday.png" "Fri" "day_Today_5_Friday.png" "Sat"
"day_Today_6_Saturday.png" "Sun" "day_Today_7_Sunday.png"}
.action{$docIconUrl := get $docIconDict $weekday}


{: icon="time/.action{$docIconUrl}" type="doc"}

## 常用数值计算函数
- add
  - {{ add 1 2 3 4 }} -> 10
- sub
  - {{ sub 4 1 }} -> 3
- mul
  - {{ mul 1 2 3 4 }} -> 24
- div
  - {{ div 5 2 }} -> 2.5
- mod
  - {{ mod 5 2 }} -> 1
- min/max
  - {{ min 5 1 }} -> 1
  - {{ max 5 1 }} -> 5
- pow
  - {{ pow 5 2 }} -> 25
- powf
  - {{ powf 2.5 2 }} -> 6.25
- log
  - {{ log 5 2 }} -> 2
- logf
  - {{ logf 5 2 }} -> 约 2.32
- FormatFloat
  - {{ FormatFloat "#,###.##" 2345.6789 }} -> 2,345.68


## 常用的时间函数
- now
  - {{ now }}
- date
  - {{ date "2006-01-02 15:04:05" now }}
- toDate
  - {{ toDate "2006-01-02" "2020-01-01" }}
- duration
  - 1800 second: {{ duration 1800 }}
- ISOWeek
  - 第 {{ now | ISOWeek }} 周
- Weekday
  - 今天是星期:
  - {{ now | Weekday }} {{ now | WeekdayCN }} {{ now | WeekdayCN2 }}
- parseTime
  - {{ parseTime "2020-01-01 12:00:00" }}
- Time 对象
  - {{ $t := parseTime "2020-01-01 12:00:00" }}
  - {{ $t.Year }}/{{ $t.Month }}/{{ $t.Day }} {{ $t.Hour }}:{{ $t.Minute }}:{{ $t.Second }}
  - {{ now.Sub $t }}
  - {{ $t.Compare now }}
  - {{ $t.AddDate 0 0 7}}
- Duration 对象
  - {{ $du := now.Sub $t }}
  - {{ $du }}
  - {{ $du.Hours }}; {{ $du.Minutes }}; {{ $du.Seconds }}
  - {{ $du.String }}
## 常用字符串操作函数
- trim
  - {{ trim "   aa   " }} -> "aa"
- repeat
  - {{ repeat 5 "12" }} -> "1212121212"
- substr
  - {{ substr 1 3 "abcedfg" }} -> "bc"
- trunc
  - {{ trunc 3 "abcedfg" }} -> "abc"
  - {{ trunc -3 "abcedfg" }} -> "efg"
- abbrev
  - {{ abbrev 5 "hello world" }} -> "he..."
- contains
  - {{ contains "bb" "aabb" }} -> true
- cat
  - {{ cat "1" "2" "3" }} -> "1 2 3"
- replace
  - {{ replace "aa" "bb" "11aaccaa" }} -> "11bbccbb"
- join
  - {{ list "hello" "siyuan" | join "?" }} -> "hello?siyuan"
- splitList
  - {{ splitList "$" "foo$bar$baz" }} -> ["foo", "bar", "baz"]

## 列表操作函数
- list
  - {{ list 1 2 3 4 }} -> [1 2 3 4]
  - {{ list "a" "b" "c" }} -> ["a" "b" "c"]
- first
  - {{ first (list 1 2 3) }} -> 1
  - {{ first (list "a" "b" "c") }} -> "a"
- last
  - {{ last (list 1 2 3) }} -> 3
  - {{ last (list "a" "b" "c") }} -> "c"
- append
  - {{ append (list 1 2 3) 4 }} -> [1 2 3 4]
  - {{ append (list "a" "b" "c") "d" }} -> ["a" "b" "c" "d"]
- prepend
  - {{ prepend (list 1 2 3) 0 }} -> [0 1 2 3]
  - {{ prepend (list "a" "b" "c") "z" }} -> ["z" "a" "b" "c"]
- concat
  - {{ concat (list 1 2) (list 3 4) }} -> [1 2 3 4]
  - {{ concat (list "a" "b") (list "c" "d") }} -> ["a" "b" "c" "d"]
- reverse
  - {{ reverse (list 1 2 3) }} -> [3 2 1]
  - {{ reverse (list "a" "b" "c") }} -> ["c" "b" "a"]
- has
  - {{ has 2 (list 1 2 3) }} -> true
  - {{ has "d" (list "a" "b" "c") }} -> false
- index
  - {{ index (list 1 2 3 4) 2 }} -> 3
  - {{ index (list "a" "b" "c" "d") 0}} -> "a"
- slice
  - {{ slice (list 1 2 3 4 5) 1 3 }} -> [2 3]
  - {{ slice (list "a" "b" "c" "d") 2 }} -> ["c" "d"]
- len
  - {{ list 1 2 3 4 | len }} -> 4

## 类型转换函数
- {{atoi "11"}} -> 11
- {{float64 1}} -> 1.0
- {{int "12"}} -> 12
- {{int64 1}} -> 1
    - 注意:有些函数的参数只接受 int,而有些只接受 int64,所以某些情况下不得不需要对 int 进行类型转换
- {{toDecimal "20"}} -> 16
- 八进制的 "20" 就是十进制的 16
- {{toString 120}} -> "120"
- {{toStrings (list 1 2 3 4)}} -> [1 2 3 4]

@emptylight370

emptylight370 commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

relevance的范围好像是0-10,我去找找相关文档

https://highlightjs.readthedocs.io/en/latest/language-guide.html#relevance

@Achuan-2

Copy link
Copy Markdown
Member Author

relevance的范围好像是0-10,我去找找相关文档

https://highlightjs.readthedocs.io/en/latest/language-guide.html#relevance

改了,设置更高其实应该没事

@Achuan-2

Copy link
Copy Markdown
Member Author

支持渲染queryBlocks的sql语句和嵌入块sql语句

PixPin_2025-07-22_14-57-23

补充变量高亮:getHPathByID|getBlock|statBlock|runeCount|wordCount|toPrettyJson|
@Achuan-2 Achuan-2 closed this Jul 23, 2025
@Achuan-2
Achuan-2 deleted the patch-4 branch July 23, 2025 06:23
@Achuan-2
Achuan-2 restored the patch-4 branch July 23, 2025 06:23
@Achuan-2 Achuan-2 reopened this Jul 23, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances template editing experience in SiYuan by allowing users to store template syntax within code blocks, improving the in-app template creation workflow.

  • Adds support for template syntax highlighting in code blocks with language set to "template" or "siyuan-template"
  • Implements conversion logic to transform template code blocks into plain text when exporting as templates
  • Enables WYSIWYG template creation directly within SiYuan without external editors

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
kernel/model/template.go Adds regex-based processing to convert template code blocks to plain text during template export
app/stage/protyle/js/highlight.js/third-languages.js Implements comprehensive syntax highlighting for SiYuan template language with SQL and Go template support
app/src/constants.ts Adds "siyuan-template" to the list of recognized code language aliases

Comment thread kernel/model/template.go Outdated
Comment thread kernel/model/template.go Outdated
Comment thread app/stage/protyle/js/highlight.js/third-languages.js
@emptylight370

Copy link
Copy Markdown
Contributor

虽然明显偏题,但是我找到highlightjs-solidity 2.0.5->2.0.6,highlightjs-sap-abap 0.2.0->0.3.0,如果D大和V大觉得有必要的话在合并之后看看吧

@88250 88250 changed the title ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 Support code block highlighting template syntax Jul 29, 2025
@88250 88250 added this to the 3.3.0 milestone Jul 29, 2025
@88250 88250 changed the title Support code block highlighting template syntax Support code block highlighting template syntax and export code block templates as paragraphs Jul 29, 2025

@88250 88250 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

导出模板的 go 代码已经从正则替换改为修改语法树实现了。

@88250

88250 commented Jul 29, 2025

Copy link
Copy Markdown
Member

虽然明显偏题,但是我找到highlightjs-solidity 2.0.5->2.0.6,highlightjs-sap-abap 0.2.0->0.3.0,如果D大和V大觉得有必要的话在合并之后看看吧

麻烦提 pr

@Achuan-2

Copy link
Copy Markdown
Member Author

导出模板的 go 代码已经从正则替换改为修改语法树实现了。

辛苦啦,我一开始也是想修改语法树的,但是不熟悉语法树结构,就曲线救国用正则啦

@Vanessa219
Vanessa219 merged commit 009a68a into siyuan-note:dev Jul 29, 2025
Vanessa219 added a commit that referenced this pull request Jul 29, 2025
@Achuan-2

Achuan-2 commented Jul 30, 2025

Copy link
Copy Markdown
Member Author

@Vanessa219 我看是直接把siyuan-template别名都删掉了吗,我觉得highlight.js的aliases可以保留下,代码块可以不支持直接选择siyuan-template,但需要siyuan-template的高亮,

后面可以给highlight.js提交pr,支持siyuan-template的高亮

这样后面如果其他外部网站介绍思源模板语法,可以用siyuan-template为代码块语言(用template为代码语言适合思源自己内部用),用思源笔记剪藏文章下来也能直接高亮

Vanessa219 added a commit that referenced this pull request Jul 30, 2025
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 30, 2025
… templates as paragraphs (siyuan-note#15345)

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法

* ✨ feat: 支持对template代码块进行高亮

- 渲染.action 块和{{}}块规则
- 渲染Markdown块
- 思源块属性设置语法

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js:块属性支持高亮.action{}语法,调整relevance

* 支持渲染queryBlocks的sql语句和嵌入块sql语句

* Update third-languages.js

补充变量高亮:getHPathByID|getBlock|statBlock|runeCount|wordCount|toPrettyJson|

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

导出时,增加识别`siyuan-template`代码块为模板

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

代码块语言新增`siyuan-template`

* Update template.go

---------

Co-authored-by: D <845765@qq.com>
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 30, 2025
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 30, 2025
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 31, 2025
… templates as paragraphs (siyuan-note#15345)

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法

* ✨ feat: 支持对template代码块进行高亮

- 渲染.action 块和{{}}块规则
- 渲染Markdown块
- 思源块属性设置语法

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js:块属性支持高亮.action{}语法,调整relevance

* 支持渲染queryBlocks的sql语句和嵌入块sql语句

* Update third-languages.js

补充变量高亮:getHPathByID|getBlock|statBlock|runeCount|wordCount|toPrettyJson|

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

导出时,增加识别`siyuan-template`代码块为模板

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

代码块语言新增`siyuan-template`

* Update template.go

---------

Co-authored-by: D <845765@qq.com>
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 31, 2025
boyiwan pushed a commit to boyiwan/siyuan that referenced this pull request Jul 31, 2025
TCOTC added a commit to TCOTC/siyuan that referenced this pull request Aug 1, 2025
@Achuan-2
Achuan-2 deleted the patch-4 branch September 26, 2025 08:40
leolee9086 pushed a commit to leolee9086/siyuan that referenced this pull request Oct 25, 2025
… templates as paragraphs (siyuan-note#15345)

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法

* ✨ feat: 支持对template代码块进行高亮

- 渲染.action 块和{{}}块规则
- 渲染Markdown块
- 思源块属性设置语法

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js

* Update third-languages.js:块属性支持高亮.action{}语法,调整relevance

* 支持渲染queryBlocks的sql语句和嵌入块sql语句

* Update third-languages.js

补充变量高亮:getHPathByID|getBlock|statBlock|runeCount|wordCount|toPrettyJson|

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

导出时,增加识别`siyuan-template`代码块为模板

* ✨ feat: 优化模板编辑体验,支持使用代码块来存储模板语法 siyuan-note#15345

代码块语言新增`siyuan-template`

* Update template.go

---------

Co-authored-by: D <845765@qq.com>
leolee9086 pushed a commit to leolee9086/siyuan that referenced this pull request Oct 25, 2025
leolee9086 pushed a commit to leolee9086/siyuan that referenced this pull request Oct 25, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants