Skip to main content

markdown_btree_core/
lib.rs

1pub mod engine;
2
3use engine::MarkdownEngine;
4use wasm_bindgen::prelude::*;
5
6/// The primary interface for the Markdown VFS exposed to WebAssembly.
7/// It wraps a high-performance B-Tree index of headings and content.
8#[wasm_bindgen]
9pub struct MarkdownDB {
10    inner: MarkdownEngine,
11}
12
13#[wasm_bindgen]
14impl MarkdownDB {
15    /// Creates a new instance by parsing and indexing the provided Markdown string.
16    #[wasm_bindgen(constructor)]
17    pub fn new(content: &str) -> MarkdownDB {
18        MarkdownDB {
19            inner: MarkdownEngine::new(content),
20        }
21    }
22
23    /// Lists child sections for a given path with pagination.
24    /// Returns a `JsValue` containing the paginated results.
25    pub fn ls(&self, path: &str, page: usize, page_size: usize) -> JsValue {
26        let result = self.inner.ls(path, page, page_size);
27        serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
28    }
29
30    /// Returns the original title of a section at the specified path.
31    pub fn get_title(&self, path: &str) -> Option<String> {
32        self.inner.get_title(path)
33    }
34
35    /// Retrieves the content of a section (excluding nested sub-sections).
36    pub fn read(&self, path: &str) -> Option<String> {
37        self.inner.read(path)
38    }
39
40    /// Retrieves the content of a section INCLUDING all nested sub-sections.
41    pub fn read_full(&self, path: &str) -> Option<String> {
42        self.inner.read_full(path)
43    }
44
45    /// Resolves a reference ID from the Markdown document.
46    pub fn get_reference(&self, ref_id: &str) -> Option<String> {
47        self.inner.get_reference(ref_id)
48    }
49
50    /// Searches for matching section titles and returns their paths.
51    pub fn search(&self, query: &str) -> JsValue {
52        let result = self.inner.search(query);
53        serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
54    }
55}