1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! The Obsidian tag token (`#tag`).
//!
//! Rules that meet a `#` have to tell a tag from an ATX heading marker or an
//! issue reference, and they have to agree on the answer, so the definition
//! lives here once.
use regex::Regex;
use std::sync::LazyLock;
/// A tag must contain at least one non-numerical character, wherever it sits:
/// `#1984` is not a tag, `#y1984` and `#3d_printing` are. A leading run of
/// digits is therefore allowed as long as a non-numerical tag character follows
/// it. That character may be a letter, a combining mark, an emoji or one of the
/// three punctuation characters Obsidian lists (`_`, `-`, `/`), but not
/// punctuation in general: `#37.` and `#42,` are issue references, not tags.
/// The token runs to the next whitespace.
pub const TAG_PATTERN_STR: &str = r"^#(?:[^\d\s#]|\d+[\p{L}\p{M}\p{So}_/-])[^\s#]*(?:\s|$)";
/// [`TAG_PATTERN_STR`] compiled. The pattern is anchored, so the text handed to
/// it starts at the `#`.
pub static TAG_PATTERN: LazyLock<Regex> =
LazyLock::new(|| Regex::new(TAG_PATTERN_STR).expect("tag pattern is a valid regex"));
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_tag_holds_a_non_numerical_character() {
for text in [
"#tagname",
"#tagname followed by prose",
"#project/active",
"#my-tag_2023",
"#3d_printing",
"#y1984",
"#tag中文",
] {
assert!(TAG_PATTERN.is_match(text), "{text:?} is a tag");
}
// Digits alone, and digits followed by punctuation Obsidian does not
// list, are issue references. A heading marker takes a space, and a
// second `#` ends the token before it can qualify.
for text in ["#1984", "#123", "#37.", "#42,", "# heading", "#", "##sub"] {
assert!(!TAG_PATTERN.is_match(text), "{text:?} is not a tag");
}
}
}