gobby-wiki 0.7.0

Gobby wiki CLI shell
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::fmt;
use std::path::{Path, PathBuf};

use gobby_core::indexing::Chunk;
use serde_json::json;

use crate::frontmatter::{FrontmatterError, WikiFrontmatter, parse_frontmatter};
use crate::links::{WikiLink, extract_links};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarkdownHeading {
    pub level: u8,
    pub title: String,
    pub path: Vec<String>,
    pub byte_start: usize,
    pub byte_end: usize,
    pub section_byte_start: usize,
    pub section_byte_end: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MarkdownDomainRecord {
    pub path: PathBuf,
    pub frontmatter: WikiFrontmatter,
    pub body_start: usize,
    pub headings: Vec<MarkdownHeading>,
    pub links: Vec<WikiLink>,
    pub chunks: Vec<Chunk>,
}

#[derive(Debug)]
pub enum MarkdownParseError {
    Frontmatter(FrontmatterError),
    Io(std::io::Error),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MarkdownFence {
    marker: u8,
    len: usize,
}

pub(crate) fn markdown_fence_start(line: &str) -> Option<MarkdownFence> {
    let leading_spaces = line.len() - line.trim_start_matches(' ').len();
    if leading_spaces > 3 {
        return None;
    }
    let trimmed = &line[leading_spaces..];
    let marker = match trimmed.as_bytes().first().copied()? {
        b'`' | b'~' => trimmed.as_bytes()[0],
        _ => return None,
    };
    let len = trimmed.bytes().take_while(|byte| *byte == marker).count();
    (len >= 3).then_some(MarkdownFence { marker, len })
}

pub(crate) fn markdown_fence_closes(line: &str, fence: MarkdownFence) -> bool {
    let leading_spaces = line.len() - line.trim_start_matches(' ').len();
    if leading_spaces > 3 {
        return false;
    }
    let trimmed = &line[leading_spaces..];
    let len = trimmed
        .bytes()
        .take_while(|byte| *byte == fence.marker)
        .count();
    len >= fence.len && trimmed[len..].trim().is_empty()
}

/// Normalize generated Markdown so it satisfies the whitespace family of
/// markdownlint rules without reflowing prose or altering meaningful content.
///
/// Guaranteed rules:
/// - **MD009** strip end-of-line whitespace (outside fenced code)
/// - **MD012** collapse runs of blank lines to a single blank line
/// - **MD022** surround ATX headings with a blank line
/// - **MD031** surround fenced code blocks with a blank line
/// - **MD047** end the file with exactly one trailing newline
///
/// The pass is fence-aware (content inside ` ``` ` / `~~~` fences is preserved
/// byte for byte) and frontmatter-aware (a leading YAML `---` / TOML `+++` block
/// is emitted verbatim). It never edits within a line, so table-cell `|` content
/// and `[[wikilink]]` text are left intact. It is idempotent:
/// `normalize(normalize(x)) == normalize(x)`.
pub(crate) fn normalize(input: &str) -> String {
    gobby_core::markdown::normalize_markdown(input)
}

impl fmt::Display for MarkdownParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Frontmatter(error) => write!(f, "{error}"),
            Self::Io(error) => write!(f, "{error}"),
        }
    }
}

impl std::error::Error for MarkdownParseError {}

impl From<FrontmatterError> for MarkdownParseError {
    fn from(error: FrontmatterError) -> Self {
        Self::Frontmatter(error)
    }
}

impl From<std::io::Error> for MarkdownParseError {
    fn from(error: std::io::Error) -> Self {
        Self::Io(error)
    }
}

pub fn parse_markdown<I, S>(
    path: impl Into<PathBuf>,
    markdown: &str,
    known_targets: I,
) -> Result<MarkdownDomainRecord, MarkdownParseError>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let path = path.into();
    let frontmatter = parse_frontmatter(markdown)?;
    let links = extract_links(markdown, known_targets);
    let headings = extract_headings(markdown, frontmatter.body_start);
    let chunks = build_chunks(&path, markdown, frontmatter.body_start, &headings);

    Ok(MarkdownDomainRecord {
        path,
        frontmatter: frontmatter.metadata,
        body_start: frontmatter.body_start,
        headings,
        links,
        chunks,
    })
}

#[allow(dead_code, reason = "reserved gwiki CLI/API split")]
pub fn parse_index_file<I, S>(
    path: impl AsRef<Path>,
    known_targets: I,
) -> Result<MarkdownDomainRecord, MarkdownParseError>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let path = path.as_ref();
    let markdown = std::fs::read_to_string(path)?;
    parse_markdown(path.to_path_buf(), &markdown, known_targets)
}

fn extract_headings(markdown: &str, body_start: usize) -> Vec<MarkdownHeading> {
    let mut headings = Vec::new();
    let mut heading_path = Vec::new();
    let mut offset = body_start;
    let mut fence: Option<MarkdownFence> = None;

    while offset < markdown.len() {
        let line_end = markdown[offset..]
            .find('\n')
            .map_or(markdown.len(), |relative| offset + relative);
        let line_content_end = markdown[..line_end]
            .strip_suffix('\r')
            .map_or(line_end, |line| line.len());
        let line = &markdown[offset..line_content_end];

        if let Some(active_fence) = fence {
            if markdown_fence_closes(line, active_fence) {
                fence = None;
            }
        } else if let Some(opening_fence) = markdown_fence_start(line) {
            fence = Some(opening_fence);
        } else if let Some((level, title)) = parse_atx_heading(line) {
            let parent_depth = usize::from(level.saturating_sub(1));
            heading_path.truncate(parent_depth.min(heading_path.len()));
            heading_path.push(title.clone());
            headings.push(MarkdownHeading {
                level,
                title,
                path: heading_path.clone(),
                byte_start: offset,
                byte_end: line_content_end,
                section_byte_start: offset,
                section_byte_end: markdown.len(),
            });
        }

        if line_end == markdown.len() {
            break;
        }
        offset = line_end + 1;
    }

    let section_ends: Vec<usize> = headings
        .iter()
        .skip(1)
        .map(|heading| heading.byte_start)
        .chain(std::iter::once(markdown.len()))
        .collect();
    for (heading, section_end) in headings.iter_mut().zip(section_ends) {
        heading.section_byte_end = section_end;
    }

    headings
}

pub(crate) fn parse_atx_heading(line: &str) -> Option<(u8, String)> {
    let leading_spaces = line.len() - line.trim_start_matches(' ').len();
    if leading_spaces > 3 {
        return None;
    }

    let line = &line[leading_spaces..];
    let level = line.bytes().take_while(|byte| *byte == b'#').count();
    if !(1..=6).contains(&level) {
        return None;
    }

    let after_marks = &line[level..];
    if !after_marks.is_empty() && !after_marks.chars().next().is_some_and(char::is_whitespace) {
        return None;
    }

    let title = strip_atx_closing_sequence(after_marks.trim()).to_string();
    Some((level as u8, title))
}

fn strip_atx_closing_sequence(title: &str) -> &str {
    let mut hash_start = title.len();
    let mut saw_hash = false;
    for (index, ch) in title.char_indices().rev() {
        if ch == '#' {
            saw_hash = true;
            hash_start = index;
        } else {
            break;
        }
    }
    if saw_hash
        && hash_start > 0
        && title[..hash_start]
            .chars()
            .next_back()
            .is_some_and(char::is_whitespace)
    {
        title[..hash_start].trim_end()
    } else {
        title
    }
}

fn build_chunks(
    path: &Path,
    markdown: &str,
    body_start: usize,
    headings: &[MarkdownHeading],
) -> Vec<Chunk> {
    let mut chunks = Vec::new();
    let mut next_start = body_start;

    for heading in headings {
        push_chunk(
            &mut chunks,
            path,
            markdown,
            next_start,
            heading.byte_start,
            None,
            Vec::new(),
        );
        push_chunk(
            &mut chunks,
            path,
            markdown,
            heading.section_byte_start,
            heading.section_byte_end,
            Some(heading.title.clone()),
            heading.path.clone(),
        );
        next_start = heading.section_byte_end;
    }

    push_chunk(
        &mut chunks,
        path,
        markdown,
        next_start,
        markdown.len(),
        None,
        Vec::new(),
    );

    chunks
}

fn push_chunk(
    chunks: &mut Vec<Chunk>,
    path: &Path,
    markdown: &str,
    byte_start: usize,
    byte_end: usize,
    heading: Option<String>,
    heading_path: Vec<String>,
) {
    if byte_start >= byte_end || markdown[byte_start..byte_end].trim().is_empty() {
        return;
    }

    chunks.push(Chunk {
        file_path: path.to_path_buf(),
        byte_start,
        byte_end,
        heading,
        metadata: json!({ "heading_path": heading_path }),
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Value;

    #[test]
    fn extracts_heading_ranges() {
        let markdown = concat!(
            "---\n",
            "title: Parser Test\n",
            "---\n",
            "Intro text.\n",
            "\n",
            "# Overview\n",
            "Overview body with [[Known]].\n",
            "\n",
            "# Details\n",
            "Details body.\n",
        );
        let overview_start = markdown.find("# Overview").expect("overview offset");
        let details_start = markdown.find("# Details").expect("details offset");

        let parsed = parse_markdown("knowledge/topics/parser.md", markdown, ["Known"])
            .expect("parse markdown");

        assert_eq!(
            parsed.body_start,
            markdown.find("Intro text.").expect("body offset")
        );
        assert_eq!(parsed.headings.len(), 2);
        assert_eq!(parsed.headings[0].title, "Overview");
        assert_eq!(parsed.headings[0].path, vec!["Overview"]);
        assert_eq!(parsed.headings[0].byte_start, overview_start);
        assert_eq!(parsed.headings[0].section_byte_start, overview_start);
        assert_eq!(parsed.headings[0].section_byte_end, details_start);
        assert_eq!(parsed.headings[1].title, "Details");
        assert_eq!(parsed.headings[1].section_byte_start, details_start);
        assert_eq!(parsed.headings[1].section_byte_end, markdown.len());

        assert_eq!(parsed.chunks.len(), 3);
        assert_eq!(parsed.chunks[0].heading, None);
        assert_eq!(parsed.chunks[0].byte_start, parsed.body_start);
        assert_eq!(parsed.chunks[0].byte_end, overview_start);
        assert_eq!(parsed.chunks[1].heading.as_deref(), Some("Overview"));
        assert_eq!(parsed.chunks[1].byte_start, overview_start);
        assert_eq!(parsed.chunks[1].byte_end, details_start);
        assert_eq!(
            parsed.chunks[1]
                .metadata
                .get("heading_path")
                .and_then(Value::as_array)
                .and_then(|path| path.first())
                .and_then(Value::as_str),
            Some("Overview")
        );
    }

    #[test]
    fn headings_ignore_code_until_matching_fence_length_closes() {
        let markdown = "````md\n# Not Heading\n```\n# Still Not Heading\n````\n# Heading\n";

        let parsed = parse_markdown(
            "knowledge/topics/fences.md",
            markdown,
            std::iter::empty::<&str>(),
        )
        .expect("parse markdown");

        assert_eq!(parsed.headings.len(), 1);
        assert_eq!(parsed.headings[0].title, "Heading");
    }

    #[test]
    fn index_parse_is_read_only() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let page = tmp.path().join("knowledge/topics/Page.md");
        std::fs::create_dir_all(page.parent().expect("parent")).expect("create parent");
        let markdown = "---\ntitle: Page\n---\n# Page\nSee [[Other Page]].\n";
        std::fs::write(&page, markdown).expect("write page");

        let parsed = parse_index_file(&page, ["Other Page"]).expect("parse index file");

        assert_eq!(parsed.path, page);
        assert_eq!(parsed.frontmatter.title.as_deref(), Some("Page"));
        assert_eq!(parsed.links.len(), 1);
        assert_eq!(
            std::fs::read_to_string(&parsed.path).expect("read page"),
            markdown
        );
    }

    #[test]
    fn atx_heading_keeps_hash_without_preceding_space() {
        assert_eq!(
            parse_atx_heading("# C#").map(|(_, title)| title),
            Some("C#".to_string())
        );
        assert_eq!(
            parse_atx_heading("# Title ###").map(|(_, title)| title),
            Some("Title".to_string())
        );
    }

    #[test]
    fn normalize_strips_trailing_whitespace_and_collapses_blank_lines() {
        // MD009 (trailing whitespace) + MD012 (multiple blank lines).
        assert_eq!(normalize("alpha   \n\n\n\nbeta\t\n"), "alpha\n\nbeta\n");
    }

    #[test]
    fn normalize_surrounds_headings_with_blank_lines() {
        // MD022.
        assert_eq!(
            normalize("intro\n# Heading\nbody\n"),
            "intro\n\n# Heading\n\nbody\n"
        );
    }

    #[test]
    fn normalize_does_not_insert_blank_before_leading_heading() {
        // MD022 does not require a blank line above the first line of a file.
        assert_eq!(normalize("# Title\ntext\n"), "# Title\n\ntext\n");
    }

    #[test]
    fn normalize_surrounds_fenced_code_with_blank_lines() {
        // MD031.
        assert_eq!(
            normalize("before\n```rust\nlet x = 1;\n```\nafter\n"),
            "before\n\n```rust\nlet x = 1;\n```\n\nafter\n"
        );
    }

    #[test]
    fn normalize_preserves_fenced_code_content_verbatim() {
        // Trailing whitespace and blank runs inside a fence are not touched.
        let input = "```\ncode  trailing   \n\n\nblank\n```\n";
        assert_eq!(normalize(input), input);
    }

    #[test]
    fn normalize_ends_with_exactly_one_trailing_newline() {
        // MD047.
        assert_eq!(normalize("text"), "text\n");
        assert_eq!(normalize("text\n\n\n"), "text\n");
    }

    #[test]
    fn normalize_preserves_frontmatter_and_separates_first_heading() {
        assert_eq!(
            normalize("---\ntitle: Page\n---\n# Heading\nbody\n"),
            "---\ntitle: Page\n---\n\n# Heading\n\nbody\n"
        );
    }

    #[test]
    fn normalize_keeps_wikilink_and_table_pipes_intact() {
        let normalized =
            normalize("## Refs\n| Name | Link |\n| --- | --- |\n| A | [[Page|Alias]] |\n");
        assert_eq!(
            normalized,
            "## Refs\n\n| Name | Link |\n| --- | --- |\n| A | [[Page|Alias]] |\n"
        );
    }

    #[test]
    fn normalize_is_idempotent() {
        let input = "---\ntitle: T\n---\n\n\n#  Heading  \ntext   \n```\ncode \n```\nmore\n";
        let once = normalize(input);
        assert_eq!(normalize(&once), once);
    }
}