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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! # TurboVault Parser
//!
//! Obsidian Flavored Markdown (OFM) parser built on `pulldown-cmark`.
//!
//! This crate provides:
//! - Fast markdown parsing via `pulldown-cmark` (CommonMark foundation)
//! - Frontmatter extraction (YAML via pulldown-cmark metadata blocks)
//! - Obsidian-specific syntax: wikilinks, embeds, callouts, tags
//! - **Code block awareness**: patterns inside code blocks/inline code are excluded
//! - Link extraction and resolution
//! - **Standalone parsing without vault context** (for tools like treemd)
//!
//! ## Architecture
//!
//! The parser uses a hybrid two-phase approach via unified `ParseEngine`:
//!
//! ### Phase 1: pulldown-cmark pass
//! - Extracts CommonMark elements: headings, markdown links, tasks, frontmatter
//! - Builds excluded ranges (code blocks, inline code, HTML) for Phase 2
//!
//! ### Phase 2: Regex pass (OFM extensions)
//! - Parses Obsidian-specific syntax: wikilinks `[[]]`, embeds `![[]]`, tags `#tag`, callouts
//! - **Skips excluded ranges** to avoid matching inside code blocks
//!
//! ### Performance optimizations
//! - Builds a `LineIndex` once for O(log n) position lookups
//! - Uses fast pre-filters to skip regex when patterns aren't present
//!
//! ## Quick Start
//!
//! ### With Vault Context
//!
//! ```
//! use turbovault_parser::Parser;
//! use std::path::PathBuf;
//!
//! let content = r#"---
//! title: My Note
//! tags: [important, review]
//! ---
//!
//! # Heading
//!
//! [[WikiLink]] and [[Other Note#Heading]].
//!
//! - [x] Completed task
//! - [ ] Pending task
//! "#;
//!
//! let vault_path = PathBuf::from("/vault");
//! let parser = Parser::new(vault_path);
//!
//! let path = PathBuf::from("my-note.md");
//! if let Ok(result) = parser.parse_file(&path, content) {
//! // Access parsed components
//! if let Some(frontmatter) = &result.frontmatter {
//! println!("Frontmatter data: {:?}", frontmatter.data);
//! }
//! println!("Links: {}", result.links.len());
//! println!("Tasks: {}", result.tasks.len());
//! }
//! ```
//!
//! ### Standalone Parsing (No Vault Required)
//!
//! ```
//! use turbovault_parser::{ParsedContent, ParseOptions};
//!
//! let content = "# Title\n\n[[WikiLink]] and [markdown](url) with #tag";
//!
//! // Parse everything
//! let parsed = ParsedContent::parse(content);
//! assert_eq!(parsed.wikilinks.len(), 1);
//! assert_eq!(parsed.markdown_links.len(), 1);
//! assert_eq!(parsed.tags.len(), 1);
//!
//! // Or parse selectively for better performance
//! let parsed = ParsedContent::parse_with_options(content, ParseOptions::links_only());
//! ```
//!
//! ### Individual Parsers (Granular Control)
//!
//! ```
//! use turbovault_parser::{parse_wikilinks, parse_tags, parse_callouts};
//!
//! let content = "[[Link]] with #tag and > [!NOTE] callout";
//!
//! let wikilinks = parse_wikilinks(content);
//! let tags = parse_tags(content);
//! let callouts = parse_callouts(content);
//! ```
//!
//! ## Supported OFM Features
//!
//! ### Links
//! - Wikilinks: `[[Note]]`
//! - Aliases: `[[Note|Alias]]`
//! - Block references: `[[Note#^blockid]]`
//! - Heading references: `[[Note#Heading]]`
//! - Embeds: `![[Note]]`
//! - Markdown links: `[text](url)`
//!
//! ### Frontmatter
//! YAML frontmatter between `---` delimiters is extracted and parsed.
//!
//! ### Elements
//! - **Headings**: H1-H6 with level tracking
//! - **Tasks**: Markdown checkboxes with completion status
//! - **Tags**: Inline tags like `#important`
//! - **Callouts**: Obsidian callout syntax `> [!TYPE]` with multi-line content
//!
//! ## Performance
//!
//! The parser uses:
//! - `pulldown-cmark` for CommonMark parsing + code block detection (O(n) linear time)
//! - `std::sync::LazyLock` for compiled regex patterns (Rust 1.80+)
//! - `LineIndex` for O(log n) position lookups via binary search
//! - Fast pre-filters to skip regex when patterns aren't present
//! - Excluded range tracking to avoid parsing inside code blocks
// Core modules
// Main exports
pub use TaskStatus;
pub use Parser;
pub use ;
// Re-export frontmatter extraction (deprecated but kept for backwards compatibility)
pub use extract_frontmatter;
// Block-level parsing (for treemd integration)
pub use ;
// Re-export core types for consumers (no need to depend on turbovault-core separately)
pub use ;
// ============================================================================
// Simplified Public API - Individual Parser Functions
// ============================================================================
//
// These functions provide granular parsing when you only need specific elements.
// They all use the unified engine internally with LineIndex for efficient position tracking.
/// Parse wikilinks from content.
///
/// Returns links with empty `source_file`. Use `Parser::parse_file()` for vault-aware parsing.
///
/// # Example
/// ```
/// use turbovault_parser::parse_wikilinks;
///
/// let links = parse_wikilinks("See [[Note]] and [[Other|alias]]");
/// assert_eq!(links.len(), 2);
/// assert_eq!(links[0].target, "Note");
/// ```
/// Parse embeds from content.
///
/// # Example
/// ```
/// use turbovault_parser::parse_embeds;
///
/// let embeds = parse_embeds("![[image.png]] and ![[Note]]");
/// assert_eq!(embeds.len(), 2);
/// ```
/// Parse markdown links from content.
///
/// # Example
/// ```
/// use turbovault_parser::parse_markdown_links;
///
/// let links = parse_markdown_links("[text](url) and [other](http://example.com)");
/// assert_eq!(links.len(), 2);
/// ```
/// Parse tags from content.
///
/// # Example
/// ```
/// use turbovault_parser::parse_tags;
///
/// let tags = parse_tags("Has #tag and #nested/tag");
/// assert_eq!(tags.len(), 2);
/// assert!(tags[1].is_nested);
/// ```
/// Parse headings from content.
///
/// # Example
/// ```
/// use turbovault_parser::parse_headings;
///
/// let headings = parse_headings("# H1\n## H2\n### H3");
/// assert_eq!(headings.len(), 3);
/// assert_eq!(headings[0].level, 1);
/// ```
/// Parse tasks from content.
///
/// # Example
/// ```
/// use turbovault_parser::parse_tasks;
///
/// let tasks = parse_tasks("- [ ] Todo\n- [x] Done");
/// assert_eq!(tasks.len(), 2);
/// assert!(!tasks[0].is_completed);
/// assert!(tasks[1].is_completed);
/// ```
/// Parse callouts from content (header only, no multi-line content).
///
/// # Example
/// ```
/// use turbovault_parser::parse_callouts;
///
/// let callouts = parse_callouts("> [!NOTE] Title\n> Content");
/// assert_eq!(callouts.len(), 1);
/// ```
/// Parse callouts with full multi-line content extraction.
///
/// # Example
/// ```
/// use turbovault_parser::parse_callouts_full;
///
/// let callouts = parse_callouts_full("> [!NOTE] Title\n> Line 1\n> Line 2");
/// assert_eq!(callouts[0].content, "Line 1\nLine 2");
/// ```
/// Convenient prelude for common imports.
///
/// Includes core types, the main parser, standalone parsing API, and all parser functions.