函式在[[資料庫介紹|資料庫]]中用於操作[[屬性]]中的資料,可應用於[[檢視#過濾設定|過濾設定]]和[[公式]]中。請參閱[[資料庫語法|資料庫語法]]參考文件以深入了解如何使用函式。 資料庫函式遵循 JavaScript 行為。完整的參考文件請參閱 [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)。 除了[[函式#全域|全域]]函式之外,大多數函式取決於你要修改的值的類型: - [[函式#Any 類型|Any]] - [[函式#Date 類型|Date]] - [[函式#String 類型|String]] - [[函式#Number 類型|Number]] - [[函式#List 類型|List]] - [[函式#Link 類型|Link]] - [[函式#File 類型|File]] - [[函式#Object 類型|Object]] - [[函式#正則表達式類型|正則表達式]] ## 全域 全域函式不需要指定類型即可使用。 ### `escapeHTML()` `escapeHTML(html: string): string` - 將字串中的特殊字元進行跳脫處理,使其能安全地包含在 HTML 中。 ### `date()` `date(date: string): date` - `date(string): date` 解析提供的字串並回傳日期物件。 - `date` 字串的格式應為 `YYYY-MM-DD HH:mm:ss`。 ### `duration()` `duration(value: string): duration` - 將字串解析為持續時間。有關 `value` 字串格式,請參閱[[資料庫語法#日期算術|日期算術章節]]。 - 在進行日期算術時不需要明確解析(例如 `now() + '1d'`),但在對持續時間進行算術運算時則需要(例如 `now() + (duration('1d') * 2)`)。 - 當持續時間與純量進行算術運算時,持續時間必須在左側。例如 `duration('5h') * 2`,而非 `2 * duration('5h')`。 ### `file()` `file(path: string | file | url): file` - 回傳給定檔案或路徑的檔案物件。 - 範例:`file(link("[[filename]]"))` 或 `file("path to file")`。 ### `html()` `html(html: string): html` - 將字串轉換為以 HTML 方式呈現的程式碼片段。 ### `if()` `if(condition: any, trueResult: any, falseResult?: any): any` - `condition` 是要評估的運算式。 - `trueResult` 是 `condition` 為真時的輸出。 - `falseResult` 是 `condition` 為假時的可選輸出。若省略,則預設為 `null`。 - 當 `condition` 為真或為真值時回傳 `trueResult`,否則回傳 `falseResult`。 - 範例:`if(isModified, "Modified", "Unmodified")` ### `image()` `image(path: string | file | url): image` - 回傳一個在檢視中呈現圖片的圖片物件。 - 範例:`image(image-property)` 或 `image("https://obsidian.md/images/obsidian-logo-gradient.svg")`。 ### `icon()` `icon(name: string): icon` - 回傳一個在檢視中呈現為圖示的值。圖示名稱必須與支援的 Lucide 圖示相符。 - 範例:`icon("arrow-right")`。 ### `link()` `link(path: string | file, display?: value): Link` - 解析字串 `path` 並回傳一個 Link 物件,該物件呈現為指向給定路徑的連結。 - 可選擇提供 `display` 參數來設定連結顯示的文字。 ### `list()` `list(element: any): List` - 若提供的元素已經是清單,則原樣回傳。 - 否則,將提供的 `element` 包裝在清單中,建立一個只有單一元素的清單。 - 當屬性在保管庫中混合包含字串或清單時,可使用此函式。 - 範例:`list("value")` 回傳 `["value"]`。 ### `max()` `max(value1: number, value2: number...): number` - 回傳所有提供數字中的最大值。 ### `min()` `min(value1: number, value2: number...): number` - 回傳所有提供數字中的最小值。 ### `now()` `now(): date` - 回傳代表當前時刻的日期物件。 ### `number()` `number(input: any): number` - 嘗試將提供的值作為數字回傳。 - 將日期物件回傳為自 Unix 紀元以來的毫秒數。 - 將布林值回傳為 `1` 或 `0`。 - 將字串解析為數字,若字串不是有效的數字則回傳錯誤。 - 範例:`number("3.4")` 回傳 `3.4`。 ### `today()` `today(): date` - 回傳代表當天日期的日期物件。時間部分設為午夜。 ### `random()` `random(): number` - 回傳 0 到 1 之間的隨機數。 - 每次載入檢視時會重新產生數字。在檢視之間切換會改變隨機數。 ## Any 類型 可用於任何值的函式。包括字串(例如 `"hello"`)、數字(例如 `42`)、清單(例如 `[1,2,3]`)、物件等。 ### `isTruthy()` `any.isTruthy(): boolean` - 回傳將值強制轉換為布林值的結果。 - 範例:`1.isTruthy()` 回傳 `true`。 ### `isType()` `any.isType(type: string): boolean` - 若值為指定的類型則回傳 true。 - 範例:`"example".isType("string")` 和 `true.isType("boolean")` 都回傳 true。 ### `toString()` `any.toString(): string` - 回傳任何值的字串表示。 - 範例:`123.toString()` 回傳 `"123"`。 ## Date 類型 可用於日期和時間的函式,例如 `date("2025-05-27")`。日期比較可使用[[資料庫語法#日期算術|日期算術]]來完成。 ### 欄位 日期可使用以下欄位: | 欄位 | 類型 | 說明 | | ----------------- | -------- | --------------------- | | `date.year` | `number` | 日期的年份 | | `date.month` | `number` | 日期的月份(1–12) | | `date.day` | `number` | 日期的日 | | `date.hour` | `number` | 小時(0–23) | | `date.minute` | `number` | 分鐘(0–59) | | `date.second` | `number` | 秒(0–59) | | `date.millisecond` | `number` | 毫秒(0–999) | ### `date()` `date.date(): date` - 回傳移除時間部分的日期物件。 - 範例:`now().date().format("YYYY-MM-DD HH:mm:ss")` 回傳類似 "2025-12-31 00:00:00" 的字串。 ### `format()` `date.format(format: string): string` - `format` 是格式字串(例如 `"YYYY-MM-DD"`)。 - 根據 Moment.js 格式字串的指定格式回傳日期。 - 範例:`date.format("YYYY-MM-DD")` 回傳 `"2025-05-27"`。 ### `time()` `date.time(): string` - 回傳時間部分的字串。 - 範例:`now().time()` 回傳類似 "23:59:59" 的字串。 ### `relative()` `date.relative(): string` - 回傳日期與當前日期時間的可讀比較。 - 範例:`file.mtime.relative()` 回傳類似 `3 days ago` 的值。 ### `isEmpty()` `date.isEmpty(): boolean` - 回傳 false。 ## String 類型 可用於字元序列的函式,例如 `"hello"`。 ### 欄位 | 欄位 | 類型 | 說明 | | -------------- | -------- | ------------------- | | `string.length` | `number` | 字串中的字元數 | ### `contains()` `string.contains(value: string): boolean` - `value` 是要搜尋的子字串。 - 若字串包含 `value` 則回傳 true。 - 範例:`"hello".contains("ell")` 回傳 `true`。 ### `containsAll()` `string.containsAll(...values: string): boolean` - `values` 是一個或多個要搜尋的子字串。 - 若字串包含所有 `values` 則回傳 true。 - 範例:`"hello".containsAll("h", "e")` 回傳 `true`。 ### `containsAny()` `string.containsAny(...values: string): boolean` - `values` 是一個或多個要搜尋的子字串。 - 若字串包含至少一個 `values` 則回傳 true。 - 範例:`"hello".containsAny("x", "y", "e")` 回傳 `true`。 ### `endsWith()` `string.endsWith(query: string): boolean` - `query` 是要檢查是否在結尾的字串。 - 若此字串以 `query` 結尾則回傳 true。 - 範例:`"hello".endsWith("lo")` 回傳 `true`。 ### `isEmpty()` `string.isEmpty(): boolean` - 若字串沒有字元或不存在則回傳 true。 - 範例:`"Hello world".isEmpty()` 回傳 `false`。 - 範例:`"".isEmpty()` 回傳 `true`。 ### `lower()` `string.lower(): string` - 回傳轉換為小寫的字串。 ### `replace()` `string.replace(pattern: string | Regexp, replacement: string): string` - `pattern` 是要在目標字串中搜尋的值。 - `replacement` 是用來取代找到的模式的值。當 `pattern` 是正則表達式時,可在 `replacement` 中使用 `$1`、`$2` 等來引用擷取群組。 - 若 `pattern` 是字串,所有出現的模式都會被取代。 - 若 `pattern` 是正則表達式,`g` 旗標決定只取代第一個還是所有出現的項目。 - 範例:`"a:b:c:d".replace(/:/, "-")` 回傳 `"a-b:c:d"`,而 `"a:b:c:d".replace(/:/g, "-")` 回傳 `"a-b-c-d"`。 - 擷取群組範例:`"John Smith".replace(/(\w+) (\w+)/, "$2, $1")` 回傳 `"Smith, John"`。 ### `repeat()` `string.repeat(count: number): string` - `count` 是要重複字串的次數。 - 範例:`"123".repeat(2)` 回傳 `"123123"`。 ### `reverse()` `string.reverse(): string` - 反轉字串。 - 範例:`"hello".reverse()` 回傳 `"olleh"`。 ### `slice()` `string.slice(start: number, end?: number): string` - `start` 是包含的起始索引。 - `end` 是可選的不包含結束索引。 - 回傳從 `start`(包含)到 `end`(不包含)的子字串。 - 範例:`"hello".slice(1, 4)` 回傳 `"ell"`。 - 若省略 `end`,則截取到字串末尾。 ### `split()` `string.split(separator: string | Regexp, n?: number): list` - `separator` 是用於分割字串的分隔符。 - `n` 是可選的數字。若提供,結果將包含前 `n` 個元素。 - 回傳子字串的清單。 - 範例:`"a,b,c,d".split(",", 3)` 或 `"a,b,c,d".split(/,/, 3)` 回傳 `["a", "b", "c"]`。 ### `startsWith()` `string.startsWith(query: string): boolean` - `query` 是要檢查是否在開頭的字串。 - 若此字串以 `query` 開頭則回傳 true。 - 範例:`"hello".startsWith("he")` 回傳 `true`。 ### `title()` `string.title(): string` - 將字串轉換為標題大小寫(每個單字的首字母大寫)。 - 範例:`"hello world".title()` 回傳 `"Hello World"`。 ### `trim()` `string.trim(): string` - 移除字串兩端的空白字元。 - 範例:`" hi ".trim()` 回傳 `"hi"`。 ## Number 類型 可用於數值的函式,例如 `42`、`3.14`。 ### `abs()` `number.abs(): number` - 回傳數字的絕對值。 - 範例:`(-5).abs()` 回傳 `5`。 ### `ceil()` `number.ceil(): number` - 將數字向上捨入到最接近的整數。 - 範例:`(2.1).ceil()` 回傳 `3`。 ### `floor()` `number.floor(): number` - 將數字向下捨入到最接近的整數。 - 範例:`(2.9).floor()` 回傳 `2`。 ### `isEmpty()` `number.isEmpty(): boolean` - 若數字不存在則回傳 true。 - 範例:`5.isEmpty()` 回傳 `false`。 ### `round()` `number.round(digits: number): number` - 將數字四捨五入到最接近的整數。 - 可選擇提供 `digits` 參數以捨入到指定的小數位數。 - 範例:`(2.5).round()` 回傳 `3`,而 `(2.3333).round(2)` 回傳 `2.33`。 ### `toFixed()` `number.toFixed(precision: number): string` - `precision` 是小數位數。 - 回傳以定點表示法表示的數字字串。 - 範例:`(3.14159).toFixed(2)` 回傳 `"3.14"`。 ## List 類型 可用於有序元素清單的函式,例如 `[1, 2, 3]`。 ### 欄位 | 欄位 | 類型 | 說明 | | ------------ | -------- | ------------------- | | `list.length` | `number` | 清單中的元素數量 | ### `contains()` `list.contains(value: any): boolean` - `value` 是要搜尋的元素。 - 若清單包含 `value` 則回傳 true。 - 範例:`[1,2,3].contains(2)` 回傳 `true`。 ### `containsAll()` `list.containsAll(...values: any): boolean` - `values` 是一個或多個要搜尋的元素。 - 若清單包含所有 `values` 則回傳 true。 - 範例:`[1,2,3].containsAll(2,3)` 回傳 `true`。 ### `containsAny()` `list.containsAny(...values: any): boolean` - `values` 是一個或多個要搜尋的元素。 - 若清單包含至少一個 `values` 則回傳 true。 - 範例:`[1,2,3].containsAny(3,4)` 回傳 `true`。 ### `filter()` `list.filter(value: Boolean): list` - 篩選清單,僅保留運算式為 true 的元素。 - `value` 是清單中項目的值。 - `index` 是當前值的索引。 - 範例:`[1,2,3,4].filter(value > 2)` 回傳 `[3,4]`。 ### `flat()` `list.flat(): list` - 將巢狀清單扁平化為單一清單。 - 範例:`[1,[2,3]].flat()` 回傳 `[1,2,3]`。 ### `isEmpty()` `list.isEmpty(): boolean` - 若清單沒有元素則回傳 true。 - 範例:`[1,2,3].isEmpty()` 回傳 `false`。 ### `join()` `list.join(separator: string): string` - `separator` 是要在元素之間插入的字串。 - 將所有清單元素合併為單一字串。 - 範例:`[1,2,3].join(",")` 回傳 `"1,2,3"`。 ### `map()` `list.map(value: Any): list` - 使用運算式轉換清單中的每個元素。 - `value` 是清單中項目的值。 - `index` 是當前值的索引。 - 範例:`[1,2,3,4].map(value + 1)` 回傳 `[2,3,4,5]`。 ### `reduce()` `list.reduce(expression: Any, acc: Any): Any` - 透過對每個元素執行運算式,將清單歸約為單一值。運算式必須回傳 `acc` 的下一個值。使用 `value` 表示當前元素,`index` 表示其位置,`acc` 表示目前為止的累積結果。 - `expression` 對清單中的每個元素進行評估。 - `value` 是清單中當前項目的值。 - `index` 是當前項目的索引。 - `acc` 是目前為止的累積值。 - 範例(求和):`[1,2,3].reduce(acc + value, 0)` 回傳 `6`。 - 範例(求最大值):`values.filter(value.isType("number")).reduce(if(acc == null || value > acc, value, acc), null)` 回傳最大的數字,若無則回傳 `null`。 ### `reverse()` `list.reverse(): list` - 就地反轉清單。 - 範例:`[1,2,3].reverse()` 回傳 `[3,2,1]`。 ### `slice()` `list.slice(start: number, end?: number): list` - `start` 是包含的起始索引。 - `end` 是可選的不包含結束索引。 - 回傳清單中從 `start`(包含)到 `end`(不包含)部分的淺拷貝。 - 範例:`[1,2,3,4].slice(1,3)` 回傳 `[2,3]`。 - 若省略 `end`,則截取到清單末尾。 ### `sort()` `list.sort(): list` - 將清單元素從小到大排序。 - 範例:`[3, 1, 2].sort()` 回傳 `[1, 2, 3]`。 - 範例:`["c", "a", "b"].sort()` 回傳 `["a", "b", "c"]`。 ### `unique()` `list.unique(): list` - 移除重複元素。 - 範例:`[1,2,2,3].unique()` 回傳 `[1,2,3]`。 ## Link 類型 可用於連結的函式。連結可從檔案(`file.asLink()`)或路徑(`link("path")`)建立。 ### `asFile()` `link.asFile(): file` - 若連結指向有效的本機檔案,則回傳檔案物件。 - 範例:`link("[[filename]]").asFile()`。 ### `linksTo()` `link.linksTo(file): boolean` - 若 `link` 代表的檔案包含指向 `file` 的連結,則回傳 `true`。 ## File 類型 可用於保管庫中檔案的函式。 ### 欄位 檔案可使用以下欄位: | 欄位 | 類型 | 說明 | | ---------------- | -------- | ------------------------------------------- | | `file.name` | `string` | 此檔案的名稱。 | | `file.basename` | `string` | 不含副檔名的檔案名稱。 | | `file.path` | `string` | 此檔案的完整路徑,相對於保管庫根目錄。 | | `file.folder` | `string` | 父資料夾的完整路徑。 | | `file.ext` | `string` | 此檔案的副檔名。 | | `file.size` | `number` | 此檔案的大小,以位元組為單位。 | | `file.properties` | `object` | 此檔案的筆記屬性。 | | `file.tags` | `list` | 此檔案的標籤。包含行內標籤。 | | `file.links` | `list` | 此檔案中的內部連結。 | | `file.ctime` | `date` | 此檔案建立的時間戳記。 | | `file.mtime` | `date` | 此檔案上次修改的時間戳記。 | ### `asLink()` `file.asLink(display?: string): Link` - `display` 是連結的可選顯示文字。 - 回傳一個呈現為可運作連結的 Link 物件。 - 範例:`file.asLink()` ### `hasLink()` `file.hasLink(otherFile: file | string): boolean` - `otherFile` 是另一個檔案物件或要檢查的字串路徑。 - 若 `file` 連結到 `otherFile` 則回傳 true。 - 範例:若 `file` 中有指向 `otherFile` 的連結,`file.hasLink(otherFile)` 回傳 `true`。 ### `hasProperty()` `file.hasProperty(name: string): boolean` - 若檔案具有指定的屬性則回傳 `true`。 ### `hasTag()` `file.hasTag(...values: string): boolean` - `values` 是一個或多個標籤名稱。 - 若檔案具有 `values` 中的任何標籤則回傳 true。 - 範例:若檔案具有標籤 `#tag1` 或 `#tag2`,`file.hasTag("tag1", "tag2")` 回傳 `true`。這也包含任何[[標籤#巢狀標籤|巢狀標籤]],例如 `#tag1/a` 或 `#tag2/b`。 ### `inFolder()` `file.inFolder(folder: string): boolean` - `folder` 是要檢查的資料夾名稱。 - 若檔案在指定資料夾或其子資料夾中則回傳 true。 - 範例:`file.inFolder("notes")` 回傳 `true`。 ## Object 類型 可用於鍵值對集合的函式,例如 `{"a": 1, "b": 2}`。 ### `isEmpty()` `object.isEmpty(): boolean` - 若物件沒有自有屬性則回傳 true。 - 範例:`{}.isEmpty()` 回傳 `true`。 ### `keys()` `object.keys(): list` - 回傳包含物件鍵的清單。 ### `values()` `object.values(): list` - 回傳包含物件值的清單。 ## 正則表達式類型 可用於正則表達式模式的函式。範例:`/abc/`。 ### `matches()` `regexp.matches(value: string): boolean` - `value` 是要測試的字串。 - 若正則表達式與 `value` 匹配則回傳 true。 - 範例:`/abc/.matches("abcde")` 回傳 `true`。