Skip to main content

markdown_org_extract/
parser.rs

1//! Markdown to tasks: the extraction step itself.
2//!
3//! A document is parsed with comrak, and every heading carrying an org-mode
4//! keyword (`TODO`, `DONE`, …) becomes a [`Task`], together with the timestamp,
5//! priority and clock entries found in its body.
6//!
7//! [`Task`]: crate::types::Task
8
9use comrak::nodes::{AstNode, NodeValue};
10use comrak::{parse_document, Arena, Options};
11use regex::Regex;
12use std::collections::BTreeMap;
13use std::ops::Range;
14use std::path::Path;
15use std::sync::LazyLock;
16
17use crate::clock::{calculate_total_minutes, extract_clocks, format_duration};
18use crate::regex_limits::compile_bounded;
19use crate::timestamp::{
20    extract_created_normalized, extract_repeater_normalized, extract_timestamp_normalized,
21    normalize_weekdays, parse_timestamp_fields_normalized,
22};
23use crate::types::{Priority, Task, TaskType, MAX_DIAGNOSTIC_ITEMS};
24
25// Per-call cap on invalid-timestamp warnings reuses `MAX_DIAGNOSTIC_ITEMS` so
26// both diagnostic surfaces (failed-path list and parse-warning stream) stay
27// aligned: "20 entries is already noisy". The counter is owned by the caller
28// -- typically `ProcessingStats::ts_warnings_emitted` for a CLI run -- so
29// long-running library use cases and parallel scans do not pollute each
30// other's budget. The previous process-global `AtomicUsize` was replaced as
31// part of the 0.5.0 review (M1).
32fn warn_invalid_timestamp(counter: &mut usize, path: &Path, line: u32, ts: &str) {
33    let n = *counter;
34    *counter = counter.saturating_add(1);
35    if n < MAX_DIAGNOSTIC_ITEMS {
36        tracing::warn!(
37            file = %path.display(),
38            line,
39            timestamp = ts.trim(),
40            "cannot parse timestamp"
41        );
42    } else if n == MAX_DIAGNOSTIC_ITEMS {
43        tracing::warn!(
44            limit = MAX_DIAGNOSTIC_ITEMS,
45            "more invalid timestamps suppressed (showed first {MAX_DIAGNOSTIC_ITEMS})"
46        );
47    }
48}
49
50// Mirror of `warn_invalid_timestamp` for malformed `org-properties` lines
51// (a line that has no `:`). The counter is owned by the caller -- typically
52// `ProcessingStats::prop_warnings_emitted` for a CLI run -- so the
53// per-`MAX_DIAGNOSTIC_ITEMS` cap spans the whole scan and parallel/library
54// uses do not pollute each other's budget. See ADR-0020.
55fn warn_invalid_property_line(counter: &mut usize, path: &Path, line: u32, raw: &str) {
56    let n = *counter;
57    *counter = counter.saturating_add(1);
58    if n < MAX_DIAGNOSTIC_ITEMS {
59        tracing::warn!(
60            file = %path.display(),
61            line,
62            content = raw.trim(),
63            "org-properties line has no ':'; skipping"
64        );
65    } else if n == MAX_DIAGNOSTIC_ITEMS {
66        tracing::warn!(
67            limit = MAX_DIAGNOSTIC_ITEMS,
68            "more malformed org-properties lines suppressed (showed first {MAX_DIAGNOSTIC_ITEMS})"
69        );
70    }
71}
72
73/// Optional TODO/DONE/CANCELLED/CANCELED keyword anchored to the start of a
74/// heading.
75///
76/// Matches `TODO`, `DONE`, `CANCELLED` (double-L) or `CANCELED` (single-L,
77/// the upstream Emacs Org-mode spelling) followed by at least one whitespace
78/// character. The double-L `CANCELLED` is listed before the single-L
79/// `CANCELED` so the alternation prefers the longer spelling. Used as the
80/// first step of heading parsing — see `parse_heading`.
81static HEADING_TODO_RE: LazyLock<Regex> =
82    LazyLock::new(|| compile_bounded(r"^(TODO|DONE|CANCELLED|CANCELED)\s+"));
83
84/// Priority cookie `[#X]` with an optional trailing space, matching anywhere
85/// in the heading text.
86///
87/// Mirrors emacs org-mode's `org-priority-regexp` semantics: the priority
88/// cookie may appear at any position in the (remaining) heading title. The
89/// value is either an uppercase ASCII letter or a one- or two-digit integer;
90/// the integer range is validated by `Priority::parse` (only `0..=64` is
91/// accepted).
92///
93/// Where the cookie sits decides only whether it is taken out of the title —
94/// see `parse_heading`. It never decides whether the priority is read.
95///
96/// Two-digit alternatives are listed before single-digit `[0-9]` so the
97/// matcher prefers the longest valid run (e.g. matches `15`, not just `1`).
98static HEADING_PRIORITY_RE: LazyLock<Regex> =
99    LazyLock::new(|| compile_bounded(r"\[#([A-Z]|6[0-4]|[1-5][0-9]|[0-9])\] ?"));
100
101/// Extract tasks from markdown content with a caller-owned warning counter.
102///
103/// Production callers (see `main.rs::scan_files`) pass
104/// `&mut ProcessingStats::ts_warnings_emitted` so the per-`MAX_DIAGNOSTIC_ITEMS`
105/// cap on invalid-timestamp warnings spans every file in the run. Library
106/// callers can pass their own counter to scope the budget per scan.
107///
108/// # Arguments
109/// * `path` - Path to the markdown file. Stored verbatim in `Task.file` for output.
110/// * `content` - File content (UTF-8).
111/// * `mappings` - Weekday name mappings for localization.
112/// * `max_tasks` - Per-file cap. Parsing stops as soon as this many tasks accumulate.
113/// * `ts_warning_counter` - Mutable counter used to gate invalid-timestamp warnings.
114///
115/// # Returns
116/// Vector of extracted tasks, capped at `max_tasks`.
117pub fn extract_tasks_with_counter(
118    path: &Path,
119    content: &str,
120    mappings: &[(&str, &str)],
121    max_tasks: usize,
122    ts_warning_counter: &mut usize,
123    prop_warning_counter: &mut usize,
124) -> Vec<Task> {
125    let arena = Arena::new();
126    let root = parse_document(&arena, content, &safe_comrak_options());
127
128    let mut tasks = Vec::new();
129    let mut current_heading: Option<HeadingInfo> = None;
130
131    for node in root.children() {
132        process_node(
133            node,
134            path,
135            &mut tasks,
136            &mut current_heading,
137            mappings,
138            ts_warning_counter,
139            prop_warning_counter,
140        );
141
142        if tasks.len() >= max_tasks {
143            tracing::warn!(
144                file = %path.display(),
145                limit = max_tasks,
146                "reached per-file task limit"
147            );
148            break;
149        }
150    }
151
152    // Flush remaining heading
153    if let Some(info) = current_heading.take() {
154        if let Some(task) = finalize_task(path, info, ts_warning_counter) {
155            tasks.push(task);
156        }
157    }
158
159    tracing::debug!(
160        file = %path.display(),
161        bytes = content.len(),
162        tasks = tasks.len(),
163        "parsed file"
164    );
165
166    tasks
167}
168
169/// Extract tasks from markdown content with a per-call warning budget.
170///
171/// Convenience wrapper around [`extract_tasks_with_counter`] that owns the
172/// counter for the duration of one call. Used by the unit-test suite and
173/// available to library callers that scope the invalid-timestamp warning
174/// cap per file. The production CLI (`main.rs::scan_files`) uses
175/// `extract_tasks_with_counter` directly so the cap spans the whole run.
176#[cfg_attr(not(test), allow(dead_code))]
177pub fn extract_tasks(
178    path: &Path,
179    content: &str,
180    mappings: &[(&str, &str)],
181    max_tasks: usize,
182) -> Vec<Task> {
183    let mut counter = 0_usize;
184    let mut prop_counter = 0_usize;
185    extract_tasks_with_counter(
186        path,
187        content,
188        mappings,
189        max_tasks,
190        &mut counter,
191        &mut prop_counter,
192    )
193}
194
195/// Comrak parsing options.
196///
197/// **Security note**: this is `Options::default()` deliberately. Defaults:
198/// - `render.unsafe_ = false` — raw HTML in markdown is escaped, not passed through.
199/// - `extension.tagfilter = false` (filter not applied, since unsafe HTML is already escaped).
200/// - No extensions that interpret embedded HTML or scripts are enabled.
201///
202/// **Do not enable `render.unsafe_` or `extension.tagfilter` here without a
203/// security review** — the HTML output goes through `html_escape`, but enabling
204/// raw HTML would let untrusted markdown inject arbitrary tags into the rendered
205/// page bypassing that escape.
206fn safe_comrak_options() -> Options<'static> {
207    Options::default()
208}
209
210/// Information extracted from a heading
211struct HeadingInfo {
212    heading: String,
213    task_type: Option<TaskType>,
214    priority: Option<Priority>,
215    line: u32,
216    content: String,
217    created: Option<String>,
218    timestamp: Option<String>,
219    clocks: Vec<crate::types::ClockEntry>,
220    properties: BTreeMap<String, String>,
221}
222
223/// Process a single markdown node
224fn process_node<'a>(
225    node: &'a AstNode<'a>,
226    path: &Path,
227    tasks: &mut Vec<Task>,
228    current_heading: &mut Option<HeadingInfo>,
229    mappings: &[(&str, &str)],
230    ts_warning_counter: &mut usize,
231    prop_warning_counter: &mut usize,
232) {
233    // Snapshot the borrow once — clone the value (cheap for Heading/Paragraph) and
234    // read the sourcepos line in the same scope; drop before any code that
235    // recurses into children (which take their own borrows).
236    let (value_clone, line) = {
237        let data = node.data.borrow();
238        (data.value.clone(), data.sourcepos.start.line as u32)
239    };
240    match value_clone {
241        NodeValue::Heading(_) => {
242            // Finalize previous heading first
243            if let Some(info) = current_heading.take() {
244                if let Some(task) = finalize_task(path, info, ts_warning_counter) {
245                    tasks.push(task);
246                }
247            }
248
249            let text = extract_text(node);
250            let (task_type, priority, heading) = parse_heading(&text);
251            *current_heading = Some(HeadingInfo {
252                heading,
253                task_type,
254                priority,
255                line,
256                content: String::new(),
257                created: None,
258                timestamp: None,
259                clocks: Vec::new(),
260                properties: BTreeMap::new(),
261            });
262        }
263        NodeValue::Paragraph => {
264            if let Some(ref mut info) = current_heading {
265                let (created, timestamp) = extract_timestamps_from_node(node, mappings);
266                let content = extract_paragraph_text(node);
267
268                for child in node.children() {
269                    if let NodeValue::Code(code) = &child.data.borrow().value {
270                        info.clocks.extend(extract_clocks(&code.literal));
271                    }
272                }
273
274                if created.is_some() {
275                    info.created = created;
276                }
277                if timestamp.is_some() {
278                    info.timestamp = timestamp;
279                }
280                if !content.is_empty() {
281                    if info.content.is_empty() {
282                        info.content = content;
283                    } else {
284                        info.content.push_str("\n\n");
285                        info.content.push_str(&content);
286                    }
287                }
288            }
289        }
290        NodeValue::CodeBlock(code) => {
291            if let Some(ref mut info) = current_heading {
292                // Performance: check the property-block info string first and
293                // return early on a match, so an org-properties block skips the
294                // backtick-strip / weekday-normalise / clock-extract work below.
295                // For every other code block the only added cost is this one
296                // `&str` comparison. The grep pre-filter (main.rs) is NOT widened
297                // for `org-properties`, so the set of scanned files is unchanged.
298                if code.info.trim() == "org-properties" {
299                    parse_org_properties(
300                        &code.literal,
301                        &mut info.properties,
302                        path,
303                        line,
304                        prop_warning_counter,
305                    );
306                } else {
307                    let raw = code.literal.trim();
308                    // An indented code block (4-space indent) reaches us with
309                    // the planning line still wrapped in inline-code backticks
310                    // (`    \`DEADLINE: <...>\``). Comrak strips the indent but
311                    // leaves the wrapping backticks in `code.literal`, which
312                    // would otherwise prevent the DEADLINE/SCHEDULED/CREATED
313                    // regex from anchoring on the keyword. Drop a matched
314                    // backtick pair before regex matching.
315                    let literal = strip_wrapping_backticks(raw);
316                    let normalized = normalize_weekdays(literal, mappings);
317                    let created = extract_created_normalized(&normalized);
318                    let timestamp = extract_timestamp_normalized(&normalized);
319
320                    info.clocks.extend(extract_clocks(literal));
321
322                    if created.is_some() {
323                        info.created = created;
324                    }
325                    if timestamp.is_some() {
326                        info.timestamp = timestamp;
327                    }
328                }
329            }
330        }
331        _ => {}
332    }
333}
334
335fn finalize_task(path: &Path, info: HeadingInfo, ts_warning_counter: &mut usize) -> Option<Task> {
336    if info.task_type.is_none() && info.created.is_none() && info.timestamp.is_none() {
337        return None;
338    }
339
340    let line = info.line;
341    let (ts_type, ts_date, ts_time, ts_end_time, ts_active, ts_repeater) =
342        if let Some(ref ts) = info.timestamp {
343            // `info.timestamp` is assembled from `extract_timestamp_normalized`
344            // regex captures over an already-`normalize_weekdays`d string in
345            // both `process_node` branches, so a second normalisation here
346            // would be redundant work on every task.
347            let parsed = parse_timestamp_fields_normalized(ts);
348            if parsed.1.is_none() {
349                warn_invalid_timestamp(ts_warning_counter, path, line, ts);
350            }
351            // The repeater is extracted via a second, fuller pass
352            // (`parse_org_timestamp`) rather than the light regex path above:
353            // the repeater grammar (prefix/value/unit, `wd`) lives in that
354            // parser and is not worth duplicating as another regex helper.
355            // The extra parse is timestamp-string-local and runs once per task.
356            let repeater = extract_repeater_normalized(ts);
357            (parsed.0, parsed.1, parsed.2, parsed.3, parsed.4, repeater)
358        } else {
359            (None, None, None, None, None, None)
360        };
361
362    let (clocks_opt, total_time) = if !info.clocks.is_empty() {
363        let total = calculate_total_minutes(&info.clocks).map(format_duration);
364        (Some(info.clocks), total)
365    } else {
366        (None, None)
367    };
368
369    let properties = if info.properties.is_empty() {
370        None
371    } else {
372        Some(info.properties)
373    };
374
375    Some(Task {
376        file: path.display().to_string(),
377        // Filled in by the scan when it walked several roots: the parser is
378        // given one file and has no notion of which collection it belongs to.
379        root: None,
380        line,
381        heading: info.heading,
382        content: info.content,
383        task_type: info.task_type,
384        priority: info.priority,
385        created: info.created,
386        timestamp: info.timestamp,
387        timestamp_type: ts_type,
388        timestamp_active: ts_active,
389        timestamp_date: ts_date,
390        timestamp_time: ts_time,
391        timestamp_end_time: ts_end_time,
392        timestamp_repeater: ts_repeater,
393        // Populated later by `annotate_next_occurrences` (needs the agenda
394        // reference date, which the parser does not have).
395        timestamp_next: None,
396        timestamp_next_after: None,
397        clocks: clocks_opt,
398        total_clock_time: total_time,
399        properties,
400    })
401}
402
403/// Parse heading text to extract task type, priority, and title.
404///
405/// Follows the emacs org-mode parser
406/// (`org-element--headline-parse-title` / `org-priority-regexp`):
407///
408/// 1. Strip an optional `TODO` / `DONE` keyword anchored at the start.
409/// 2. Search the remaining text for the first `[#X]` cookie at any position,
410///    where `X` is `A-Z` or an integer `0..=64`. If found, that becomes the
411///    priority, wherever it sits — emacs reads it the same way, through the
412///    `.*?` prefix of `org-priority-regexp`.
413/// 3. The cookie is taken **out of** the title only when it is in its canonical
414///    place: at the start of what is left, i.e. directly after the keyword, or
415///    opening the heading when there is no keyword. A cookie written anywhere
416///    else stays in the title, together with the text before it.
417/// 4. Whatever remains is trimmed and returned as the heading.
418///
419/// Step 3 is where this parser parts company with `org-element`, whose
420/// `:raw-value` drops everything up to the cookie (`goto-char (match-end 0)`).
421/// That value is not what a reader sees: `org-agenda` builds its line from
422/// `org-get-heading`, which keeps the title whole — `* TODO Buy [#A] filter`
423/// is shown as written and still sorts as an `A`. Dropping the prefix here
424/// left a trailing cookie with an empty heading and a blank agenda row. See
425/// [ADR-0002](../docs/adr/0002-supported-org-mode-subset.md).
426///
427/// A heading without TODO/DONE and without a priority cookie is returned
428/// verbatim (trimmed).
429fn parse_heading(text: &str) -> (Option<TaskType>, Option<Priority>, String) {
430    // Step 1: optional TODO/DONE prefix.
431    let (task_type, rest) = if let Some(caps) = HEADING_TODO_RE.captures(text) {
432        let kw = caps.get(1).map(|m| m.as_str()).unwrap_or("");
433        let m = caps
434            .get(0)
435            .expect("Captures::get(0) is Some when captures() succeeds");
436        (TaskType::from_keyword(kw), &text[m.end()..])
437    } else {
438        (None, text)
439    };
440    let title = rest.trim();
441
442    // Step 2: optional priority cookie anywhere in the remainder.
443    if let Some(caps) = HEADING_PRIORITY_RE.captures(title) {
444        let value = caps.get(1).map(|m| m.as_str()).unwrap_or("");
445        if let Some(priority) = Priority::parse(value) {
446            let whole = caps
447                .get(0)
448                .expect("Captures::get(0) is Some when captures() succeeds");
449            // Step 3: only a cookie opening the title is consumed by it.
450            let heading = if whole.start() == 0 {
451                title[whole.end()..].trim()
452            } else {
453                title
454            };
455            return (task_type, Some(priority), heading.to_string());
456        }
457    }
458
459    (task_type, None, title.to_string())
460}
461
462/// Leading `#` run of an ATX heading and the gap after it.
463///
464/// Capped at six hashes, which is where markdown stops treating the run as a
465/// heading, and the gap is mandatory for the same reason: `#no gap` is a
466/// paragraph. Used by `parse_heading_line`, which works on the raw file line —
467/// unlike `parse_heading`, which is handed the text comrak already stripped
468/// the hashes from.
469static HEADING_HASHES_RE: LazyLock<Regex> = LazyLock::new(|| compile_bounded(r"^(#{1,6})[ \t]+"));
470
471/// A token found on a heading line, with the byte range it occupies.
472///
473/// The range covers the token as written, framing included: a priority cookie
474/// reports `[#A]`, not `A`. Callers replacing one token slice the line around
475/// this range and keep everything else byte-for-byte.
476#[derive(Debug, Clone, PartialEq)]
477pub struct HeadingToken<T> {
478    /// Byte range within the line the token was parsed from.
479    pub range: Range<usize>,
480    /// What the token parsed to.
481    pub value: T,
482}
483
484/// A heading line as it sits in a file, located token by token.
485///
486/// This is the read half of an editing operation and the reason it lives here
487/// rather than in the editor: the keyword and cookie grammars are the ones the
488/// extractor itself applies, and a second copy of them would drift. Writing —
489/// assembling a line back from parts — is deliberately not part of this crate.
490///
491/// Unlike [`Task`], which carries the heading the agenda displays, nothing is
492/// summarised here. A heading may hold text between the keyword and the cookie
493/// (`# TODO leftover [#B] Title`): the agenda keeps that text and shows the
494/// cookie inside it, and [`title_start`] points at the text rather than past
495/// the cookie, so an editor rewriting the title neither swallows the cookie
496/// nor moves it. Replacing the cookie itself is what [`priority`] is for — its
497/// range addresses the cookie where the user wrote it.
498///
499/// [`title_start`]: HeadingLine::title_start
500/// [`priority`]: HeadingLine::priority
501/// [`Task`]: crate::types::Task
502#[derive(Debug, Clone, PartialEq)]
503pub struct HeadingLine {
504    /// Heading level, i.e. the number of leading `#` characters (1 to 6).
505    pub level: usize,
506    /// The `TODO` / `DONE` / `CANCELLED` / `CANCELED` keyword, when present.
507    pub status: Option<HeadingToken<TaskType>>,
508    /// The `[#A]` priority cookie, when present and within the accepted range.
509    pub priority: Option<HeadingToken<Priority>>,
510    /// Byte offset the title starts at, past the tokens above and the
511    /// whitespace after them. Also where a caller inserts a token the heading
512    /// does not carry yet. A cookie away from its canonical place counts as
513    /// part of the title, so the offset stops before it.
514    pub title_start: usize,
515}
516
517/// Locate the parts of a heading line, or return `None` when `line` is not a
518/// heading.
519///
520/// Applies the same keyword and priority grammars as the extraction path, so a
521/// heading the extractor reads one way cannot be rewritten another way. See
522/// [`HeadingLine`] for what the result addresses.
523///
524/// ```
525/// # use markdown_org_extract::parse_heading_line;
526/// let line = "## TODO [#A] Write the report";
527/// let heading = parse_heading_line(line).expect("a heading");
528/// assert_eq!(&line[heading.priority.expect("a cookie").range], "[#A]");
529/// ```
530pub fn parse_heading_line(line: &str) -> Option<HeadingLine> {
531    let hashes = HEADING_HASHES_RE.captures(line)?;
532    let level = hashes
533        .get(1)
534        .expect("group 1 is Some when captures() succeeds")
535        .len();
536    let after_hashes = hashes
537        .get(0)
538        .expect("Captures::get(0) is Some when captures() succeeds")
539        .end();
540
541    let (status, after_status) = match HEADING_TODO_RE.captures(&line[after_hashes..]) {
542        Some(caps) => {
543            let keyword = caps
544                .get(1)
545                .expect("group 1 is Some when captures() succeeds");
546            let whole = caps
547                .get(0)
548                .expect("Captures::get(0) is Some when captures() succeeds");
549            let token = TaskType::from_keyword(keyword.as_str()).map(|value| HeadingToken {
550                range: after_hashes + keyword.start()..after_hashes + keyword.end(),
551                value,
552            });
553            (token, after_hashes + whole.end())
554        }
555        None => (None, after_hashes),
556    };
557
558    // The cookie is searched for in the remainder, at any position, the way
559    // `parse_heading` does it — org-mode allows text before it.
560    let priority = HEADING_PRIORITY_RE
561        .captures(&line[after_status..])
562        .and_then(|caps| {
563            let value = caps
564                .get(1)
565                .expect("group 1 is Some when captures() succeeds");
566            // The cookie is `[#` + value + `]`; the regex match may also cover
567            // a trailing space, which is not part of the token.
568            let parsed = Priority::parse(value.as_str())?;
569            Some(HeadingToken {
570                range: after_status + value.start() - "[#".len()
571                    ..after_status + value.end() + "]".len(),
572                value: parsed,
573            })
574        });
575
576    // A cookie opening the remainder is a token of its own and the title
577    // starts past it; one written further along belongs to the title, which
578    // therefore starts where the keyword left off. Same rule as `parse_heading`
579    // applies to the heading it hands the agenda.
580    let after_tokens = priority
581        .as_ref()
582        .filter(|cookie| line[after_status..cookie.range.start].trim().is_empty())
583        .map_or(after_status, |cookie| cookie.range.end);
584    let title_start = after_tokens
585        + line[after_tokens..]
586            .find(|c: char| !c.is_whitespace())
587            .unwrap_or(line.len() - after_tokens);
588
589    Some(HeadingLine {
590        level,
591        status,
592        priority,
593        title_start,
594    })
595}
596
597/// Strip a matched pair of inline-code backtick fences from the trimmed
598/// content of an indented code block.
599///
600/// Markdown's indented code blocks preserve the literal source minus the
601/// leading 4-space indent, so a line like `    \`DEADLINE: <...>\`` arrives
602/// here with the wrapping backticks intact. Those wrappers are not part of
603/// the planning-line keyword grammar — they're inline-code framing that the
604/// user added to keep the line visually attached to the heading in their
605/// editor — so we peel one balanced run of backticks before regex matching.
606///
607/// Returns `s` unchanged when the wrapping is asymmetric or absent.
608fn strip_wrapping_backticks(s: &str) -> &str {
609    let bytes = s.as_bytes();
610    let n_leading = bytes.iter().take_while(|&&b| b == b'`').count();
611    if n_leading == 0 {
612        return s;
613    }
614    let n_trailing = bytes.iter().rev().take_while(|&&b| b == b'`').count();
615    // Require equal-length fences with at least one non-fence byte between
616    // them; otherwise the input is just a run of backticks and stripping
617    // would over-consume.
618    if n_trailing != n_leading || bytes.len() < 2 * n_leading + 1 {
619        return s;
620    }
621    s[n_leading..bytes.len() - n_leading].trim()
622}
623
624/// Parse the literal of an `org-properties` fenced code block into `props`,
625/// merging into any existing entries with last-wins on duplicate keys.
626///
627/// Each non-blank line is split on its first `:`: the key is the text
628/// before it (trimmed, case preserved), the value is the remainder
629/// (trimmed). An empty key or a line with no `:` is skipped and reported
630/// via `warn_invalid_property_line`, gated by the caller-owned counter so
631/// the `MAX_DIAGNOSTIC_ITEMS` budget spans the whole run. `block_start_line`
632/// is the source line of the opening fence; the per-line offset is added so
633/// warnings point near the offending line. See ADR-0020.
634fn parse_org_properties(
635    literal: &str,
636    props: &mut BTreeMap<String, String>,
637    path: &Path,
638    block_start_line: u32,
639    prop_warning_counter: &mut usize,
640) {
641    for (offset, line) in literal.lines().enumerate() {
642        if line.trim().is_empty() {
643            continue;
644        }
645        // Source line of this content line: opening fence + 1 + offset.
646        let src_line = block_start_line
647            .saturating_add(1)
648            .saturating_add(offset as u32);
649        match line.split_once(':') {
650            Some((key, value)) => {
651                let key = key.trim();
652                if key.is_empty() {
653                    warn_invalid_property_line(prop_warning_counter, path, src_line, line);
654                    continue;
655                }
656                props.insert(key.to_string(), value.trim().to_string());
657            }
658            None => {
659                warn_invalid_property_line(prop_warning_counter, path, src_line, line);
660            }
661        }
662    }
663}
664
665/// Extract timestamps (CREATED and others) from paragraph node
666fn extract_timestamps_from_node<'a>(
667    node: &'a AstNode<'a>,
668    mappings: &[(&str, &str)],
669) -> (Option<String>, Option<String>) {
670    let mut created = None;
671    let mut timestamp = None;
672
673    if let NodeValue::Paragraph = &node.data.borrow().value {
674        for child in node.children() {
675            if let NodeValue::Code(code) = &child.data.borrow().value {
676                // Normalize the literal once per inline-code node; both extractors
677                // would otherwise scan the same string in lockstep.
678                let normalized = normalize_weekdays(&code.literal, mappings);
679                if created.is_none() {
680                    created = extract_created_normalized(&normalized);
681                }
682                if timestamp.is_none() {
683                    timestamp = extract_timestamp_normalized(&normalized);
684                }
685            }
686        }
687    }
688    (created, timestamp)
689}
690
691/// Extract plain text from paragraph, including text inside Emph/Strong/Link nodes
692///
693/// Inline code is left out here, unlike in a heading: in a body paragraph it
694/// carries the planning lines and the property markers, which are read by
695/// their own extractors and would otherwise appear twice — once as data and
696/// once as prose.
697fn extract_paragraph_text<'a>(node: &'a AstNode<'a>) -> String {
698    let mut text = String::new();
699    collect_text_recursive(node, &mut text, InlineCode::Drop);
700    text.trim().to_string()
701}
702
703/// Extract all text from a heading node, including text inside Emph/Strong
704/// and the literal of an inline code span.
705fn extract_text<'a>(node: &'a AstNode<'a>) -> String {
706    let mut text = String::new();
707    collect_text_recursive(node, &mut text, InlineCode::Keep);
708    text
709}
710
711/// What [`collect_text_recursive`] does with an inline code span.
712#[derive(Clone, Copy, PartialEq, Eq)]
713enum InlineCode {
714    /// Write its literal out, so a heading keeps every word it was written
715    /// with.
716    Keep,
717    /// Skip it.
718    Drop,
719}
720
721fn collect_text_recursive<'a>(node: &'a AstNode<'a>, out: &mut String, code: InlineCode) {
722    for child in node.children() {
723        let value = child.data.borrow().value.clone();
724        match value {
725            NodeValue::Text(t) => out.push_str(&t),
726            NodeValue::Code(inline) if code == InlineCode::Keep => out.push_str(&inline.literal),
727            NodeValue::Emph | NodeValue::Strong | NodeValue::Link(_) | NodeValue::Strikethrough => {
728                collect_text_recursive(child, out, code)
729            }
730            _ => {}
731        }
732    }
733}
734
735/// The text of a markdown fragment as the agenda shows it, with the inline
736/// markup taken off.
737///
738/// This is how a heading reaches [`Task::heading`](crate::Task::heading):
739/// emphasis, strong text, links and strikethrough contribute their text, and
740/// an inline code span contributes its literal. An editor holding the raw
741/// line can therefore compare what the file says against what it was handed
742/// — `parse_heading_line` says where the title starts, and this says what it
743/// looks like once extracted.
744///
745/// ```
746/// # use markdown_org_extract::{display_text, parse_heading_line};
747/// let line = "# TODO **Отчёт** за июль";
748/// let heading = parse_heading_line(line).expect("a heading");
749/// assert_eq!(display_text(&line[heading.title_start..]), "Отчёт за июль");
750/// ```
751pub fn display_text(markdown: &str) -> String {
752    let arena = Arena::new();
753    let root = parse_document(&arena, markdown, &safe_comrak_options());
754
755    let mut text = String::new();
756    collect_block_text(root, &mut text);
757    text.trim().to_string()
758}
759
760/// Walk down to the inline content of every block and collect its text.
761///
762/// A fragment handed to [`display_text`] is parsed as a document, so its
763/// inline nodes sit under a paragraph (or a heading, when the caller passes a
764/// whole line); the leaves are the same either way.
765fn collect_block_text<'a>(node: &'a AstNode<'a>, out: &mut String) {
766    for child in node.children() {
767        let value = child.data.borrow().value.clone();
768        match value {
769            NodeValue::Paragraph | NodeValue::Heading(_) => {
770                collect_text_recursive(child, out, InlineCode::Keep)
771            }
772            _ => collect_block_text(child, out),
773        }
774    }
775}
776
777// Need clone for NodeValue match — comrak nodes are RefCell-borrowed
778// Re-import for clone derive if not present.
779
780#[cfg(test)]
781mod tests {
782    use super::*;
783    use crate::types::{CancelledSpelling, DEFAULT_MAX_TASKS};
784
785    #[test]
786    fn warn_invalid_timestamp_advances_per_call_counter() {
787        // The 0.5.0 review (M1) replaced a process-global
788        // `TS_WARNINGS_EMITTED: AtomicUsize` with a counter owned by the
789        // caller (typically `ProcessingStats::ts_warnings_emitted`).
790        // This test pins the per-call advance: each call bumps the
791        // counter by exactly one.
792        let mut counter = 0_usize;
793        let path = Path::new("t.md");
794        for i in 1..=25 {
795            warn_invalid_timestamp(&mut counter, path, i, "<bad>");
796        }
797        assert_eq!(counter, 25);
798    }
799
800    #[test]
801    fn warn_invalid_property_line_advances_per_call_counter() {
802        // Same per-call advance contract as warn_invalid_timestamp: each
803        // call bumps the caller-owned counter by exactly one, so the
804        // MAX_DIAGNOSTIC_ITEMS cap spans the whole run (ADR-0020).
805        let mut counter = 0_usize;
806        let path = Path::new("t.md");
807        for i in 1..=25 {
808            warn_invalid_property_line(&mut counter, path, i, "no-colon-here");
809        }
810        assert_eq!(counter, 25);
811    }
812
813    #[test]
814    fn warn_invalid_timestamp_counters_are_independent() {
815        // Independent counters do not pollute each other: e.g. a library
816        // consumer running two separate scans, or unit tests in the same
817        // binary, each see a fresh budget. With the previous global
818        // static this assertion would not hold across runs in one
819        // process.
820        let mut counter_a = 0_usize;
821        let mut counter_b = 0_usize;
822        let path = Path::new("t.md");
823        for _ in 0..MAX_DIAGNOSTIC_ITEMS {
824            warn_invalid_timestamp(&mut counter_a, path, 1, "<bad>");
825        }
826        warn_invalid_timestamp(&mut counter_b, path, 1, "<bad>");
827        assert_eq!(counter_a, MAX_DIAGNOSTIC_ITEMS);
828        assert_eq!(counter_b, 1);
829    }
830
831    #[test]
832    fn test_parse_heading_with_priority() {
833        let (task_type, priority, heading) = parse_heading("TODO [#A] Important task");
834        assert_eq!(task_type, Some(TaskType::Todo));
835        assert_eq!(priority, Some(Priority::A));
836        assert_eq!(heading, "Important task");
837    }
838
839    #[test]
840    fn test_parse_heading_without_priority() {
841        let (task_type, priority, heading) = parse_heading("DONE Simple task");
842        assert_eq!(task_type, Some(TaskType::Done));
843        assert_eq!(priority, None);
844        assert_eq!(heading, "Simple task");
845    }
846
847    #[test]
848    fn test_parse_heading_no_task() {
849        let (task_type, priority, heading) = parse_heading("Regular heading");
850        assert_eq!(task_type, None);
851        assert_eq!(priority, None);
852        assert_eq!(heading, "Regular heading");
853    }
854
855    // The next batch mirrors the test matrix from the bug report
856    // "markdown-org-extract: приоритет `[#X]` без TODO/DONE не распознаётся".
857    // We follow emacs org-mode semantics (`org-priority-regexp`) wherever the
858    // bug report diverged from it — concretely case 8 below.
859
860    #[test]
861    fn parse_heading_priority_without_todo() {
862        // Case 1: `### [#A] Заголовок`.
863        let (tt, p, h) = parse_heading("[#A] Заголовок");
864        assert_eq!(tt, None);
865        assert_eq!(p, Some(Priority::A));
866        assert_eq!(h, "Заголовок");
867    }
868
869    #[test]
870    fn parse_heading_todo_with_priority() {
871        // Case 2: `### TODO [#A] Заголовок`.
872        let (tt, p, h) = parse_heading("TODO [#A] Заголовок");
873        assert_eq!(tt, Some(TaskType::Todo));
874        assert_eq!(p, Some(Priority::A));
875        assert_eq!(h, "Заголовок");
876    }
877
878    #[test]
879    fn parse_heading_done_with_priority_b() {
880        // Case 3: `### DONE [#B] Заголовок`.
881        let (tt, p, h) = parse_heading("DONE [#B] Заголовок");
882        assert_eq!(tt, Some(TaskType::Done));
883        assert_eq!(p, Some(Priority::B));
884        assert_eq!(h, "Заголовок");
885    }
886
887    #[test]
888    fn parse_heading_plain_text_no_markers() {
889        // Case 4: `### Заголовок`.
890        let (tt, p, h) = parse_heading("Заголовок");
891        assert_eq!(tt, None);
892        assert_eq!(p, None);
893        assert_eq!(h, "Заголовок");
894    }
895
896    #[test]
897    fn parse_heading_todo_no_priority() {
898        // Case 5: `### TODO Заголовок`.
899        let (tt, p, h) = parse_heading("TODO Заголовок");
900        assert_eq!(tt, Some(TaskType::Todo));
901        assert_eq!(p, None);
902        assert_eq!(h, "Заголовок");
903    }
904
905    #[test]
906    fn parse_heading_numeric_priority() {
907        // Case 6: `### [#1] Заголовок`.
908        let (tt, p, h) = parse_heading("[#1] Заголовок");
909        assert_eq!(tt, None);
910        assert_eq!(p, Some(Priority::Numeric(1)));
911        assert_eq!(h, "Заголовок");
912    }
913
914    #[test]
915    fn parse_heading_extra_whitespace_around_priority() {
916        // Case 7: `###     [#A]     Заголовок` — comrak normalises the leading
917        // whitespace after the `###` marker, so the heading text reaching us
918        // starts at `[#A]`. Trailing extra spaces around the heading are
919        // trimmed.
920        let (tt, p, h) = parse_heading("[#A]     Заголовок");
921        assert_eq!(tt, None);
922        assert_eq!(p, Some(Priority::A));
923        assert_eq!(h, "Заголовок");
924    }
925
926    #[test]
927    fn parse_heading_priority_in_the_middle_org_semantics() {
928        // Case 8: `### Без приоритета и [#A] внутри`.
929        // The cookie counts wherever it sits — `org-get-priority` finds it
930        // through the `.*?` prefix of `org-priority-regexp` — but the title is
931        // left as written: that is the line emacs puts in the agenda.
932        let (tt, p, h) = parse_heading("Без приоритета и [#A] внутри");
933        assert_eq!(tt, None);
934        assert_eq!(p, Some(Priority::A));
935        assert_eq!(h, "Без приоритета и [#A] внутри");
936    }
937
938    #[test]
939    fn parse_heading_trailing_cookie_leaves_a_title_behind() {
940        // The case the two clients answered differently: a cookie written last
941        // used to take the whole title with it and leave the agenda showing an
942        // empty row.
943        let (tt, p, h) = parse_heading("TODO Заголовок с cookie в конце [#A]");
944        assert_eq!(tt, Some(TaskType::Todo));
945        assert_eq!(p, Some(Priority::A));
946        assert_eq!(h, "Заголовок с cookie в конце [#A]");
947    }
948
949    #[test]
950    fn parse_heading_two_digit_numeric_priority() {
951        let (tt, p, h) = parse_heading("[#15] Mid range");
952        assert_eq!(tt, None);
953        assert_eq!(p, Some(Priority::Numeric(15)));
954        assert_eq!(h, "Mid range");
955
956        let (tt, p, h) = parse_heading("[#64] At upper bound");
957        assert_eq!(tt, None);
958        assert_eq!(p, Some(Priority::Numeric(64)));
959        assert_eq!(h, "At upper bound");
960    }
961
962    #[test]
963    fn parse_heading_rejects_numeric_out_of_range() {
964        // `[#65]` and higher are not a valid org-mode priority. The cookie
965        // stays inside the heading text verbatim.
966        let (tt, p, h) = parse_heading("[#65] Above range");
967        assert_eq!(tt, None);
968        assert_eq!(p, None);
969        assert_eq!(h, "[#65] Above range");
970    }
971
972    #[test]
973    fn parse_heading_rejects_lowercase_priority() {
974        let (tt, p, h) = parse_heading("[#a] Lowercase");
975        assert_eq!(tt, None);
976        assert_eq!(p, None);
977        assert_eq!(h, "[#a] Lowercase");
978    }
979
980    #[test]
981    fn parse_heading_todo_then_priority_with_intervening_text() {
982        // The cookie is out of its canonical place, so it stays in the title
983        // and only the priority is taken from it. Emacs shows the same line in
984        // the agenda, cookie included.
985        let (tt, p, h) = parse_heading("TODO Купить [#A] фильтр");
986        assert_eq!(tt, Some(TaskType::Todo));
987        assert_eq!(p, Some(Priority::A));
988        assert_eq!(h, "Купить [#A] фильтр");
989    }
990
991    #[test]
992    fn parse_heading_priority_without_trailing_space() {
993        // `\] ?` makes the post-cookie space optional.
994        let (tt, p, h) = parse_heading("[#A]NoSpace");
995        assert_eq!(tt, None);
996        assert_eq!(p, Some(Priority::A));
997        assert_eq!(h, "NoSpace");
998    }
999
1000    #[test]
1001    fn parse_heading_cancelled_simple() {
1002        let (tt, p, h) = parse_heading("CANCELLED Foo");
1003        assert_eq!(tt, Some(TaskType::Cancelled(CancelledSpelling::DoubleL)));
1004        assert_eq!(p, None);
1005        assert_eq!(h, "Foo");
1006    }
1007
1008    #[test]
1009    fn parse_heading_cancelled_with_priority() {
1010        let (tt, p, h) = parse_heading("CANCELLED [#A] Foo");
1011        assert_eq!(tt, Some(TaskType::Cancelled(CancelledSpelling::DoubleL)));
1012        assert_eq!(p, Some(Priority::A));
1013        assert_eq!(h, "Foo");
1014    }
1015
1016    #[test]
1017    fn parse_heading_cancelled_without_whitespace() {
1018        // No whitespace after the keyword: not recognised, stays in title.
1019        let (tt, p, h) = parse_heading("CANCELLEDFoo");
1020        assert_eq!(tt, None);
1021        assert_eq!(p, None);
1022        assert_eq!(h, "CANCELLEDFoo");
1023    }
1024
1025    #[test]
1026    fn parse_heading_cancelled_lowercase_not_recognised() {
1027        // Case-sensitive, like TODO/DONE.
1028        let (tt, p, h) = parse_heading("cancelled Foo");
1029        assert_eq!(tt, None);
1030        assert_eq!(p, None);
1031        assert_eq!(h, "cancelled Foo");
1032    }
1033
1034    #[test]
1035    fn parse_heading_todo_cancelled_first_keyword_wins() {
1036        // First keyword wins; the rest goes into the title (existing rule).
1037        let (tt, p, h) = parse_heading("TODO CANCELLED Foo");
1038        assert_eq!(tt, Some(TaskType::Todo));
1039        assert_eq!(p, None);
1040        assert_eq!(h, "CANCELLED Foo");
1041    }
1042
1043    #[test]
1044    fn parse_heading_canceled_single_l() {
1045        // Upstream Emacs Org-mode spells the keyword with a single L. See
1046        // ADR-0021; recognised alongside the double-L `CANCELLED`.
1047        let (tt, p, h) = parse_heading("CANCELED Foo");
1048        assert_eq!(tt, Some(TaskType::Cancelled(CancelledSpelling::SingleL)));
1049        assert_eq!(p, None);
1050        assert_eq!(h, "Foo");
1051    }
1052
1053    #[test]
1054    fn parse_heading_canceled_with_priority() {
1055        let (tt, p, h) = parse_heading("CANCELED [#A] Foo");
1056        assert_eq!(tt, Some(TaskType::Cancelled(CancelledSpelling::SingleL)));
1057        assert_eq!(p, Some(Priority::A));
1058        assert_eq!(h, "Foo");
1059    }
1060
1061    #[test]
1062    fn parse_heading_canceled_lowercase_not_recognised() {
1063        // Case-sensitive, like TODO/DONE/CANCELLED.
1064        let (tt, p, h) = parse_heading("canceled Foo");
1065        assert_eq!(tt, None);
1066        assert_eq!(p, None);
1067        assert_eq!(h, "canceled Foo");
1068    }
1069
1070    #[test]
1071    fn parse_heading_canceled_without_whitespace_not_recognised() {
1072        // No whitespace after the keyword: not recognised, stays in title.
1073        let (tt, p, h) = parse_heading("CANCELEDfoo");
1074        assert_eq!(tt, None);
1075        assert_eq!(p, None);
1076        assert_eq!(h, "CANCELEDfoo");
1077    }
1078
1079    #[test]
1080    fn extract_tasks_marks_scheduled_angle_bracket_as_active() {
1081        // End-to-end: a SCHEDULED line with `<...>` must surface
1082        // `timestamp_active = Some(true)` in the resulting Task, so
1083        // downstream consumers can branch on bracket form without
1084        // re-parsing the timestamp string. See ADR-0014.
1085        let content = "### TODO Pin me\n`SCHEDULED: <2026-05-21 Thu>`\n";
1086        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1087        assert_eq!(tasks.len(), 1);
1088        assert_eq!(tasks[0].timestamp_active, Some(true));
1089    }
1090
1091    #[test]
1092    fn extract_tasks_marks_missing_timestamp_active_as_none() {
1093        // Heading without a timestamp must keep `timestamp_active = None`,
1094        // matching the rule that absent optional fields skip JSON
1095        // serialisation (ADR-0015).
1096        let content = "### Project kickoff\n\n`CREATED: [2025-09-01 Mon]`\n";
1097        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1098        assert_eq!(tasks.len(), 1);
1099        assert_eq!(tasks[0].timestamp_active, None);
1100    }
1101
1102    #[test]
1103    fn extract_tasks_basic_todo_with_deadline() {
1104        let content = "\
1105### TODO [#A] Write docs\n\
1106`DEADLINE: <2025-12-10 Wed>`\n";
1107        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1108        assert_eq!(tasks.len(), 1);
1109        let t = &tasks[0];
1110        assert_eq!(t.task_type, Some(TaskType::Todo));
1111        assert_eq!(t.priority, Some(Priority::A));
1112        assert_eq!(t.heading, "Write docs");
1113        assert_eq!(t.timestamp_type, Some("DEADLINE".to_string()));
1114        assert_eq!(t.timestamp_date, Some("2025-12-10".to_string()));
1115    }
1116
1117    #[test]
1118    fn extract_tasks_extracts_emph_text_in_heading() {
1119        // Regression: previously emphasised text inside heading was dropped.
1120        let content = "### TODO **Important** task\n`DEADLINE: <2025-12-10 Wed>`\n";
1121        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1122        assert_eq!(tasks.len(), 1);
1123        assert_eq!(tasks[0].heading, "Important task");
1124    }
1125
1126    #[test]
1127    fn extract_tasks_ignores_non_task_headings_without_timestamps() {
1128        let content = "### Just a heading\n\nSome text.\n";
1129        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1130        assert!(tasks.is_empty());
1131    }
1132
1133    #[test]
1134    fn extract_tasks_keeps_created_without_todo() {
1135        // Heading without TODO/DONE keyword but with a CREATED line is still a task.
1136        let content = "### Project kickoff\n\n`CREATED: [2025-09-01 Mon]`\n";
1137        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1138        assert_eq!(tasks.len(), 1);
1139        assert_eq!(tasks[0].task_type, None);
1140        assert_eq!(tasks[0].created, Some("CREATED: [2025-09-01 Mon]".into()));
1141    }
1142
1143    #[test]
1144    fn extract_tasks_concatenates_multiple_paragraphs() {
1145        // Regression: previously only the first paragraph was kept as content.
1146        let content = "\
1147### TODO Multi-line task\n\
1148First paragraph.\n\
1149\n\
1150Second paragraph.\n\
1151";
1152        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1153        assert_eq!(tasks.len(), 1);
1154        assert!(tasks[0].content.contains("First paragraph"));
1155        assert!(tasks[0].content.contains("Second paragraph"));
1156    }
1157
1158    #[test]
1159    fn extract_tasks_extracts_clock_from_inline_code() {
1160        let content = "\
1161### TODO Track time\n\
1162`CLOCK: [2025-09-01 Mon 10:00]--[2025-09-01 Mon 11:30] => 1:30`\n\
1163";
1164        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1165        assert_eq!(tasks.len(), 1);
1166        let t = &tasks[0];
1167        assert!(t.clocks.is_some());
1168        assert_eq!(t.total_clock_time.as_deref(), Some("1:30"));
1169    }
1170
1171    #[test]
1172    fn extract_tasks_handles_done_priority() {
1173        let content = "### DONE [#B] Wrap up\n";
1174        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1175        assert_eq!(tasks.len(), 1);
1176        assert_eq!(tasks[0].task_type, Some(TaskType::Done));
1177        assert_eq!(tasks[0].priority, Some(Priority::B));
1178    }
1179
1180    #[test]
1181    fn extract_tasks_priority_without_todo_with_scheduled() {
1182        // Bug report case: priority cookie before SCHEDULED heading, no TODO.
1183        // After the fix the heading must surface as a task with priority=A and
1184        // task_type=None, since the SCHEDULED line is what makes it agenda-eligible.
1185        let content = "\
1186### [#A] Поменять резину до 16.05.2026\n\
1187`SCHEDULED: <2026-05-09 Sat>`\n";
1188        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1189        assert_eq!(tasks.len(), 1);
1190        let t = &tasks[0];
1191        assert_eq!(t.task_type, None);
1192        assert_eq!(t.priority, Some(Priority::A));
1193        assert_eq!(t.heading, "Поменять резину до 16.05.2026");
1194        assert_eq!(t.timestamp_type, Some("SCHEDULED".to_string()));
1195        assert_eq!(t.timestamp_date, Some("2026-05-09".to_string()));
1196    }
1197
1198    #[test]
1199    fn extract_tasks_numeric_priority_with_deadline() {
1200        // Numeric priority `[#1]` without TODO, with a DEADLINE line.
1201        let content = "\
1202### [#1] Numeric priority task\n\
1203`DEADLINE: <2026-05-09 Sat>`\n";
1204        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1205        assert_eq!(tasks.len(), 1);
1206        let t = &tasks[0];
1207        assert_eq!(t.task_type, None);
1208        assert_eq!(t.priority, Some(Priority::Numeric(1)));
1209        assert_eq!(t.heading, "Numeric priority task");
1210    }
1211
1212    #[test]
1213    fn extract_tasks_priority_in_middle_keeps_the_prefix() {
1214        // The cookie is read wherever it sits, and the heading reaches the
1215        // agenda as the file has it — nothing before the cookie is dropped.
1216        let content = "\
1217### Без приоритета и [#A] внутри\n\
1218`SCHEDULED: <2026-05-09 Sat>`\n";
1219        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1220        assert_eq!(tasks.len(), 1);
1221        let t = &tasks[0];
1222        assert_eq!(t.task_type, None);
1223        assert_eq!(t.priority, Some(Priority::A));
1224        assert_eq!(t.heading, "Без приоритета и [#A] внутри");
1225    }
1226
1227    #[test]
1228    fn extract_tasks_bug_report_minimal_reproduction() {
1229        // Three-heading reproduction from the bug report: priority without
1230        // TODO, TODO + priority, plain heading. SCHEDULED is wrapped in
1231        // backticks so the existing inline-code parser picks it up.
1232        let content = "\
1233### [#A] Поменять резину до 16.05.2026\n\
1234`SCHEDULED: <2026-05-09 Sat>`\n\
1235\n\
1236### TODO [#A] Поменять масло\n\
1237`SCHEDULED: <2026-05-09 Sat>`\n\
1238\n\
1239### Купить фильтр\n\
1240`SCHEDULED: <2026-05-09 Sat>`\n";
1241        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1242        assert_eq!(tasks.len(), 3);
1243
1244        assert_eq!(tasks[0].task_type, None);
1245        assert_eq!(tasks[0].priority, Some(Priority::A));
1246        assert_eq!(tasks[0].heading, "Поменять резину до 16.05.2026");
1247
1248        assert_eq!(tasks[1].task_type, Some(TaskType::Todo));
1249        assert_eq!(tasks[1].priority, Some(Priority::A));
1250        assert_eq!(tasks[1].heading, "Поменять масло");
1251
1252        assert_eq!(tasks[2].task_type, None);
1253        assert_eq!(tasks[2].priority, None);
1254        assert_eq!(tasks[2].heading, "Купить фильтр");
1255    }
1256
1257    // Regression suite for the "indented planning line" cases. A heading
1258    // followed by a 4-space-indented DEADLINE/SCHEDULED/CREATED line is
1259    // parsed by comrak as an indented code block; the timestamp must still
1260    // be recovered, whether or not the planning line is wrapped in inline
1261    // backticks. Matches what `emacs` org-agenda surfaces.
1262    // The literals below use real newlines (no `\\\n` Rust string
1263    // continuation): the continuation form would silently swallow the
1264    // four leading spaces and reduce the case to "no indent at all".
1265    #[test]
1266    fn extract_tasks_indented_inline_code_deadline() {
1267        let content = "#### Birthday\n    `DEADLINE: <2026-05-07 Thu +1y>`\n";
1268        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1269        assert_eq!(tasks.len(), 1, "task should not be dropped");
1270        let t = &tasks[0];
1271        assert_eq!(t.timestamp_type.as_deref(), Some("DEADLINE"));
1272        assert_eq!(t.timestamp_date.as_deref(), Some("2026-05-07"));
1273    }
1274
1275    #[test]
1276    fn extract_tasks_todo_indented_inline_code_deadline() {
1277        let content = "#### TODO Birthday\n    `DEADLINE: <2026-05-07 Thu +1y>`\n";
1278        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1279        assert_eq!(tasks.len(), 1);
1280        let t = &tasks[0];
1281        assert_eq!(t.task_type, Some(TaskType::Todo));
1282        assert_eq!(t.timestamp_date.as_deref(), Some("2026-05-07"));
1283    }
1284
1285    #[test]
1286    fn extract_tasks_indented_inline_code_blank_lines_between() {
1287        // Whitespace between heading and planning line (blank lines, tabs,
1288        // mixed indentation) must not block timestamp recovery.
1289        let content = "#### Birthday\n\n  \t  \n    `DEADLINE: <2026-05-07 Thu +1y>`\n";
1290        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1291        assert_eq!(tasks.len(), 1);
1292        let t = &tasks[0];
1293        assert_eq!(t.timestamp_date.as_deref(), Some("2026-05-07"));
1294    }
1295
1296    #[test]
1297    fn extract_tasks_with_ru_mappings_reproduces_cli_pipeline() {
1298        // Reproduce what `main.rs` feeds to `extract_tasks` when the default
1299        // `--locale ru,en` is in effect. Pulling the table from `cli` keeps
1300        // this test in sync with whatever `get_weekday_mappings("ru")` would
1301        // produce in production.
1302        let content = "#### TODO Birthday\n    `DEADLINE: <2026-05-07 Thu +1y>`\n";
1303        let tasks = extract_tasks(
1304            Path::new("t.md"),
1305            content,
1306            crate::locale::RU_WEEKDAY_MAPPINGS,
1307            DEFAULT_MAX_TASKS,
1308        );
1309        assert_eq!(tasks.len(), 1);
1310        let t = &tasks[0];
1311        assert_eq!(t.task_type, Some(TaskType::Todo));
1312        assert_eq!(t.timestamp_date.as_deref(), Some("2026-05-07"));
1313    }
1314
1315    #[test]
1316    fn extract_tasks_inline_code_scheduled_no_indent() {
1317        let content = "#### Followup\n`SCHEDULED: <2026-05-07 Thu>`\n";
1318        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1319        assert_eq!(tasks.len(), 1);
1320        let t = &tasks[0];
1321        assert_eq!(t.timestamp_type.as_deref(), Some("SCHEDULED"));
1322        assert_eq!(t.timestamp_date.as_deref(), Some("2026-05-07"));
1323    }
1324
1325    #[test]
1326    fn extract_tasks_indented_inline_code_created() {
1327        let content = "#### Project kickoff\n    `CREATED: [2025-09-01 Mon]`\n";
1328        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1329        assert_eq!(tasks.len(), 1);
1330        let t = &tasks[0];
1331        assert_eq!(t.created.as_deref(), Some("CREATED: [2025-09-01 Mon]"));
1332    }
1333
1334    #[test]
1335    fn extract_tasks_parses_single_property() {
1336        let content = "### TODO Ship release\n`SCHEDULED: <2026-06-01 Mon 10:00>`\n```org-properties\nGCAL_EVENT_ID: abc123/primary\n```\n";
1337        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1338        assert_eq!(tasks.len(), 1);
1339        let props = tasks[0].properties.as_ref().expect("properties present");
1340        assert_eq!(
1341            props.get("GCAL_EVENT_ID").map(String::as_str),
1342            Some("abc123/primary")
1343        );
1344        // The block must not leak into the task body content.
1345        assert!(!tasks[0].content.contains("GCAL_EVENT_ID"));
1346        assert!(!tasks[0].content.contains("org-properties"));
1347    }
1348
1349    #[test]
1350    fn extract_tasks_parses_multiple_properties() {
1351        let content =
1352            "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\nA: 1\nB: 2\n```\n";
1353        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1354        let props = tasks[0].properties.as_ref().unwrap();
1355        assert_eq!(props.get("A").map(String::as_str), Some("1"));
1356        assert_eq!(props.get("B").map(String::as_str), Some("2"));
1357    }
1358
1359    #[test]
1360    fn extract_tasks_property_duplicate_keys_last_wins() {
1361        let content = "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\nK: first\nK: second\n```\n";
1362        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1363        assert_eq!(
1364            tasks[0]
1365                .properties
1366                .as_ref()
1367                .unwrap()
1368                .get("K")
1369                .map(String::as_str),
1370            Some("second")
1371        );
1372    }
1373
1374    #[test]
1375    fn extract_tasks_property_empty_value_allowed() {
1376        let content = "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\nK:\n```\n";
1377        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1378        assert_eq!(
1379            tasks[0]
1380                .properties
1381                .as_ref()
1382                .unwrap()
1383                .get("K")
1384                .map(String::as_str),
1385            Some("")
1386        );
1387    }
1388
1389    #[test]
1390    fn extract_tasks_property_malformed_line_skipped() {
1391        let content = "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\nGOOD: x\nno colon here\n```\n";
1392        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1393        let props = tasks[0].properties.as_ref().unwrap();
1394        assert_eq!(props.get("GOOD").map(String::as_str), Some("x"));
1395        assert_eq!(props.len(), 1, "malformed line must be skipped");
1396    }
1397
1398    #[test]
1399    fn extract_tasks_empty_property_block_yields_none() {
1400        let content = "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\n\n```\n";
1401        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1402        assert_eq!(tasks[0].properties, None);
1403    }
1404
1405    #[test]
1406    fn extract_tasks_property_info_with_extra_attrs_not_recognised() {
1407        // Info string must be exactly "org-properties"; extra attributes
1408        // mean it is a plain code block, not a property block.
1409        let content =
1410            "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties extra\nK: v\n```\n";
1411        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1412        assert_eq!(tasks[0].properties, None);
1413    }
1414
1415    #[test]
1416    fn extract_tasks_clock_code_block_unaffected_by_properties() {
1417        // A CLOCK-bearing code block on the same task is still parsed for
1418        // clocks; the org-properties block is parsed for properties.
1419        let content = "### TODO T\n```org-properties\nK: v\n```\n`CLOCK: [2025-09-01 Mon 10:00]--[2025-09-01 Mon 11:30] => 1:30`\n";
1420        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1421        assert_eq!(
1422            tasks[0]
1423                .properties
1424                .as_ref()
1425                .unwrap()
1426                .get("K")
1427                .map(String::as_str),
1428            Some("v")
1429        );
1430        assert_eq!(tasks[0].total_clock_time.as_deref(), Some("1:30"));
1431    }
1432
1433    #[test]
1434    fn extract_tasks_merges_multiple_property_blocks_last_wins() {
1435        let content = "### TODO T\n`SCHEDULED: <2026-06-01 Mon>`\n```org-properties\nK: one\n```\n```org-properties\nK: two\nL: three\n```\n";
1436        let tasks = extract_tasks(Path::new("t.md"), content, &[], DEFAULT_MAX_TASKS);
1437        let props = tasks[0].properties.as_ref().unwrap();
1438        assert_eq!(props.get("K").map(String::as_str), Some("two"));
1439        assert_eq!(props.get("L").map(String::as_str), Some("three"));
1440    }
1441}