permalink: web-clipper/filters
Filters allow you to modify variables in Web Clipper templates. Filters are applied to variables using the syntax {{variable|filter}}
.
prompt
, meta
, selector
, and schema
variables. {{variable|filter1|filter2}}
, and are applied in the order they are added.
转换和修改日期。
date
Converts a date to the specified format, see reference.
{{date|date:"YYYY-MM-DD"}}
converts the current date to "YYYY-MM-DD". date:("outputFormat", "inputFormat")
to specify the input format, e.g. "12/01/2024"|date:("YYYY-MM-DD", "MM/DD/YYYY")
parses "12/01/2024" and returns "2024-12-01"
. date_modify
Modifies a date by adding or subtracting a specified amount of time, see reference.
"2024-12-01"|date_modify:"+1 year"
returns "2025-12-01"
"2024-12-01"|date_modify:"- 2 months"
returns "2024-10-01"
duration
Converts ISO 8601 duration strings or seconds into formatted time strings. Uses tokens: HH
(padded hours), H
(hours), mm
(padded minutes), m
(minutes), ss
(padded seconds), s
(seconds).
"PT1H30M"|duration:"HH:mm:ss"
returns "01:30:00"
. "3665"|duration:"H:mm:ss"
returns "1:01:05"
. duration
without any parameters uses HH:mm:ss
over 1 hour, mm:ss
under 1 hour. PT6702S
,PT1H30M
)和单纯的秒数。
将文本字符串从一种格式转换为另一种格式。
camel
Converts text to camelCase
.
capitalize
Capitalizes the first character of the value and converts the rest to lowercase, e.g. "hELLO wORLD"|capitalize
returns "Hello world"
.
kebab
Converts text to kebab-case
.
lower
Converts text to lowercase
.
pascal
将文本转换为 PascalCase
。
replace
替换指定文本的所有出现:
"hello!"|replace:",":""
移除所有逗号。"hello world"|replace:("e":"a","o":"0")
返回 "hall0 w0rld"
。""
。: | { } ( ) ' "
should be escaped with a backslash when used in the search term, e.g. \:
to search for a literal colon.
支持使用 JavaScript 正则表达式语法:
"hello world"|replace:"/[aeiou]/g":"*"
→ "h*ll* w*rld".
"HELLO world"|replace:"/hello/i":"hi"
→ "hi world".
"hello world"|replace:("/[aeiou]/g":"*","/\s+/":"-")
→ "h*ll*-w*rld"
. g
(global), i
(case-insensitive), m
(multiline), s
(dotAll), u
(unicode), y
(sticky). safe_name
将文本转换为安全的文件名。
safe_name
safe_name:os
where os
can be windows
, mac
, or linux
to only apply the rules for that operating system. snake
将文本转换为 snake_case
。
title
将文本转换为标题格式
,例如 "hello world"|title
返回 "Hello World"
。
trim
从字符串两端移除空白字符。
uncamel
将驼峰命名法(camelCase)或帕斯卡命名法(PascalCase)转换为用空格分隔的单词,你可以使用其他过滤器(如 `title` 或 `capitalize`)进一步格式化。
"PascalCase"|uncamel
返回 .upper
将一个值转换为大写,例如 "hello world"|upper
返回 "HELLO WORLD"
。
应用基本格式化语法和高级格式化语法对文本进行处理。
blockquote
为输入的每一行添加 Markdown 引用前缀(>
)。
callout
创建一个可选参数的提示框: {{variable|callout:("type", "title", foldState)}}
type
是提示框类型,默认为 "info"title
是提示框标题,默认为空foldState
是一个布尔值,用于设置折叠状态(true 表示折叠,false 表示展开,null 表示不可折叠)footnote
将数组或对象转换为 Markdown 脚注列表。
["first item","second item"]|footnote
返回:[^1]: 第一项
等等。{"First Note": "Content 1", "Second Note": "Content 2"}|footnote
返回:[^first-note]: Content 1
等等。fragment_link
将字符串和数组转换为文本片段链接。默认情况下,链接文本为“link”。
highlights|fragment_link
` 返回 ` Highlight content [link](text-fragment-url)
`highlights|fragment_link:"custom title"
` 返回 ` Highlight content [custom title](text-fragment-url)
`image
将字符串、数组或对象转换为 Markdown 图片语法。
"image.jpg"|image:"alt text"
` 返回 `
`。["image1.jpg","image2.jpg"]|image:"alt text"
` 返回一个包含相同替代文本的 Markdown 图像字符串数组。{"image1.jpg": "Alt 1", "image2.jpg": "Alt 2"}|image
返回带有对象键的替代文本的 Markdown 图像字符串。link
将字符串、数组或对象转换为 Markdown 链接语法(请注意,这与 不同)。
"url"|link:"author"
返回 [author](url)
。["url1","url2"]|link:"author"
返回一个包含相同文本的 Markdown 链接数组。{"url1": "Author 1", "url2": "Author 2"}|link
返回 Markdown 链接,链接文本为对象键匹配的内容。list
将数组转换为 Markdown 列表。
list
用于转换为项目符号列表。list:task
用于转换为任务列表。list:numbered
用于转换为编号列表。list:numbered-task
用于转换为带编号的任务列表。table
将数组或数组对象转换为 Markdown 表格:[Tables](https://help.obsidian.md/advanced-syntax#Tables):
table:("Column 1", "Column 2", "Column 3")
。当与简单数组一起使用时,它会根据指定的列数自动将数据拆分为行。wikilink
将字符串、数组或对象转换为 Obsidian `wikilink` 语法。
"page"|wikilink
` 返回 `[[page]]
`。"page"|wikilink:"alias"
` 返回 `[[page|alias]]
`。["page1","page2"]|wikilink
` 返回一个不带别名的 wikilink 数组。["page1","page2"]|wikilink:"alias"
返回一个包含所有链接相同别名的 wikilink 数组。{"page1": "alias1", "page2": "alias2"}|wikilink
返回以键作为页面名、值作为别名的 wikilinks。calc
执行基本的算术运算。
+
、-
、*
、/
、**
(或 ^
)用于指数运算。5|calc:"+10"
返回 15
。2|calc:"**3"
返回 8
(2 的立方)。length
返回字符串、数组的长度或对象中的键的数量。
"hello"|length
` 返回 `5
`。["a","b","c"]|length
` 返回 `3
`。{"a":1,"b":2}|length
` 返回 `2
`。round
将数字四舍五入到最接近的整数或指定的小数位数。
3.7|round
` 返回 `4
`。3.14159|round:2
` 返回 `3.14
`。
处理 HTML 内容并将其转换为 Markdown。请注意,您的输入 `variable` 必须包含 HTML 内容,例如使用 `{{fullHtml}}
`、`{{contentHtml}}
` 或 `{{selectorHtml:}}
` 变量。
markdown
将字符串转换为 Obsidian 格式的 Markdown 字符串。
{{contentHtml}}
`、`{{fullHtml}}
`)结合使用时,或与选择器变量(如 `{{selectorHtml:cssSelector}}
`)结合使用时非常有用。remove_attr
仅移除指定的 HTML 属性。
"<div class="test" id="example">Content</div>"|remove_attr:"class"
返回 <div id="example">Content</div>
{{fullHtml|remove_attr:("class,style,id")}}
remove_html
从字符串中移除指定的 HTML 元素及其内容。
{{fullHtml|remove_html:("img,.class-name,#element-id")}}
remove_tags
仅移除指定的 HTML 标签。保留标签内的内容。
"<p>Hello <b>world</b>!</p>"|remove_tags:"b"
返回 "Hello world!
"
。{{fullHtml|remove_tags:("a,em,strong")}}
replace_tags
替换 HTML 标签,同时保留标签的内容和属性。
{{fullHtml|replace_tags:"strong":"h2"}}
将所有 <strong>
标签替换为 <h2>
。strip_attr
移除字符串中的所有 HTML 属性。
strip_attr:("class, id")
保留特定属性。"<div class="test" id="example">Content</div>"|strip_attr:("class")
返回 <div id="example">Content</div>
。strip_md
移除所有 Markdown 格式,返回一个纯文本字符串,例如将 **text**
转换为 text
。
strip_tags
从字符串中移除所有 HTML 标签。标签内的内容将保留。
strip_tags:("p,strong,em")
可以保留特定的标签。"<p>Hello <b>world</b>!</p>"|strip_tags:("b")
返回 Hello <b>world</b>!
。
处理数组和对象。
first
将数组的第一个元素作为字符串返回。
["a","b","c"]|first
返回 "a"
。join
将数组中的元素合并成一个字符串。
["a","b","c"]|join
返回 "a,b,c"
。["a","b","c"]|join:" "
返回 "a b c"
。使用 join:"\n"
可以用换行符分隔元素。split
或 slice
之后可能会很有用: "a,b,c,d"|split:","|slice:1,3|join:" "
返回 "b c"
。last
返回数组的最后一个元素作为字符串。
["a","b","c"]|last
返回 "c"
。map
使用语法 map:item => item.property
或 map:item => item.nested.property
对数组中的每个元素应用转换,适用于嵌套属性。
[{gem: "obsidian", color: "black"}, {gem: "amethyst", color: "purple"}]|map:item => item.gem
返回 ["obsidian", "amethyst"]
。map:item => ({key: value})
,例如: [{gem: "obsidian", color: "black"}, {gem: "amethyst", color: "purple"}]|map:item => ({name: item.gem, color: item.color})
返回 [{name: "obsidian", color: "black"}, {name: "amethyst", color: "purple"}]
。
字符串字面量受支持,并会自动包装在一个具有 `str` 属性的对象中。`str` 属性用于存储字符串字面量转换的结果,例如 ` ["rock", "pop"]|map:item => "
` 返回 ` [{str: "
`。
将 `map` 与 `template` 过滤器结合使用,例如 ` map:item => ({name: ${item.gem}, color: item.color})|template:"- ${name} is ${color}\n"
`。
merge
向数组中添加新值。
["a","b"]|merge:("c","d")
` 返回 `["a","b","c","d"]
`。["a","b"]|merge:"c"
` 返回 `["a","b","c"]
`。"a"|merge:("b","c")
` 返回 `["a","b","c"]
`。["a"]|merge:('b,"c,d",e')
` 返回 `["a","b","c,d","e"]
`。nth
使用 CSS 风格的 nth-child 语法和分组模式保留数组中的第 n 项。所有位置都是 1 为基础(第一个元素是位置 1)。
重复结构的分组模式语法:
array|nth:1,2,3:5
从每组 5 个项目中保留位置 1、2、3。示例: [1,2,3,4,5,6,7,8,9,10]|nth:1,2,3:5
返回 [1,2,3,6,7,8]
。object
Manipulates object data:
object:array
converts an object to an array of key-value pairs.object:keys
returns an array of the object's keys.object:values
returns an array of the object's values.{"a":1,"b":2}|object:array
returns [["a",1],["b",2]]
.slice
Extracts a portion of a string or array.
"hello"|slice:1,4
returns "ell"
.["a","b","c","d"]|slice:1,3
returns ["b","c"]
."hello"|slice:2
returns "llo"
."hello"|slice:-3
returns "llo"
."hello"|slice:1,4
includes characters at indices 1, 2, and 3."hello"|slice:0,-2
returns "hel"
.split
Divides a string into an array of substrings.
"a,b,c"|split:","
returns ["a","b","c"]
."hello world"|split:" "
returns ["hello","world"]
."hello"|split
returns ["h","e","l","l","o"]
."a1b2c3"|split:[0-9]
returns ["a","b","c"]
.template
object|template:"Template with ${variable}"
.
{"gem":{"name":"Obsidian"}}|template:"${gem.name}"
returns "Obsidian"
.{"gem":"obsidian","hardness":5}|template:"${gem} has a hardness of ${hardness}"
returns "obsidian has a hardness of 5"
.[{"gem":"obsidian","hardness":5},{"gem":"amethyst","hardness":7}]|template:"- ${gem} has a hardness of ${hardness}\n"
returns a formatted list.Works with string literals from map
by accessing the str
property:
["rock", "pop"]|map:item => "genres /${item}"|template:"${str}"
returns "genres /rock\ngenres/pop"
.str
property is automatically used when applying template
to objects created by map
with string literals.unique
Removes duplicate values from arrays and objects.
[1,2,2,3,3]|unique
returns [1,2,3]
.[{"a":1},{"b":2},{"a":1}]|unique
returns [{"a":1},{"b":2}]
.