diff --git a/Cargo.lock b/Cargo.lock index 8a4c893..17aa891 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.101" @@ -234,6 +284,21 @@ dependencies = [ "serde", ] +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + [[package]] name = "bitflags" version = "1.3.2" @@ -280,6 +345,31 @@ dependencies = [ "piper", ] +[[package]] +name = "bon" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d13a61f2963b88eef9c1be03df65d42f6996dfeac1054870d950fcf66686f83" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d314cc62af2b6b0c65780555abb4d02a03dd3b799cd42419044f0c38d99738c0" +dependencies = [ + "darling", + "ident_case", + "prettyplease", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.116", +] + [[package]] name = "brotli" version = "8.0.2" @@ -395,6 +485,15 @@ dependencies = [ "toml 0.9.12+spec-1.1.0", ] +[[package]] +name = "caseless" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6fd507454086c8edfd769ca6ada439193cdb209c7681712ef6275cccbfe5d8" +dependencies = [ + "unicode-normalization", +] + [[package]] name = "cc" version = "1.2.56" @@ -452,6 +551,53 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "clap" +version = "4.5.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63be97961acde393029492ce0be7a1af7e323e6bae9511ebfac33751be5e6806" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f13174bda5dfd69d7e947827e5af4b0f2f94a4a3ee92912fba07a66150f21e2" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.116", +] + +[[package]] +name = "clap_lex" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "combine" version = "4.6.7" @@ -462,6 +608,30 @@ dependencies = [ "memchr", ] +[[package]] +name = "comrak" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "321d20bf105b6871a49da44c5fbb93e90a7cd6178ea5a9fe6cbc1e6d4504bc5e" +dependencies = [ + "bon", + "caseless", + "clap", + "emojis", + "entities", + "fmt2io", + "jetscii", + "phf 0.13.1", + "phf_codegen 0.13.1", + "rustc-hash", + "shell-words", + "smallvec", + "syntect", + "typed-arena", + "unicode_categories", + "xdg", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -804,12 +974,27 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7" +[[package]] +name = "emojis" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c1c1870b766fc398e5f0526498d09c94b6de15be5fd769a28bbc804fb1b05d" +dependencies = [ + "phf 0.13.1", +] + [[package]] name = "endi" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" +[[package]] +name = "entities" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5320ae4c3782150d900b79807611a59a99fc9a1d61d686faafc24b93fc8d7ca" + [[package]] name = "enumflags2" version = "0.7.12" @@ -891,6 +1076,17 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" +[[package]] +name = "fancy-regex" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "998b056554fbe42e03ae0e152895cd1a7e1002aec800fdc6635d20270260c46f" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax", +] + [[package]] name = "fastrand" version = "2.3.0" @@ -932,6 +1128,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fmt2io" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b6129284da9f7e5296cc22183a63f24300e945e297705dcc0672f7df01d62c8" + [[package]] name = "fnv" version = "1.0.7" @@ -1760,6 +1962,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.17" @@ -1789,6 +1997,12 @@ dependencies = [ "system-deps", ] +[[package]] +name = "jetscii" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47f142fe24a9c9944451e8349de0a56af5f3e7226dc46f3ed4d4ecc0b85af75e" + [[package]] name = "jni" version = "0.21.1" @@ -1939,6 +2153,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.11.0" @@ -2363,6 +2583,34 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "onig" +version = "6.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "336b9c63443aceef14bea841b899035ae3abe89b7c486aaf4c5bd8aafedac3f0" +dependencies = [ + "bitflags 2.11.0", + "libc", + "once_cell", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f86c6eef3d6df15f23bcfb6af487cbd2fed4e5581d58d5bf1f5f8b7f6727dc" +dependencies = [ + "cc", + "pkg-config", +] + [[package]] name = "open" version = "5.3.3" @@ -2487,6 +2735,16 @@ dependencies = [ "phf_shared 0.11.3", ] +[[package]] +name = "phf" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +dependencies = [ + "phf_shared 0.13.1", + "serde", +] + [[package]] name = "phf_codegen" version = "0.8.0" @@ -2507,6 +2765,16 @@ dependencies = [ "phf_shared 0.11.3", ] +[[package]] +name = "phf_codegen" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49aa7f9d80421bca176ca8dbfebe668cc7a2684708594ec9f3c0db0805d5d6e1" +dependencies = [ + "phf_generator 0.13.1", + "phf_shared 0.13.1", +] + [[package]] name = "phf_generator" version = "0.8.0" @@ -2537,6 +2805,16 @@ dependencies = [ "rand 0.8.5", ] +[[package]] +name = "phf_generator" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +dependencies = [ + "fastrand", + "phf_shared 0.13.1", +] + [[package]] name = "phf_macros" version = "0.10.0" @@ -2591,6 +2869,15 @@ dependencies = [ "siphasher 1.0.2", ] +[[package]] +name = "phf_shared" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +dependencies = [ + "siphasher 1.0.2", +] + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -3033,6 +3320,12 @@ dependencies = [ "sqlite-wasm-rs", ] +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + [[package]] name = "rustc_version" version = "0.4.1" @@ -3324,6 +3617,12 @@ dependencies = [ "digest", ] +[[package]] +name = "shell-words" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6fe69c597f9c37bfeeeeeb33da3530379845f10be461a66d16d03eca2ded77" + [[package]] name = "shlex" version = "1.3.0" @@ -3530,6 +3829,28 @@ dependencies = [ "syn 2.0.116", ] +[[package]] +name = "syntect" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "656b45c05d95a5704399aeef6bd0ddec7b2b3531b7c9e900abbf7c4d2190c925" +dependencies = [ + "bincode", + "fancy-regex", + "flate2", + "fnv", + "once_cell", + "onig", + "plist", + "regex-syntax", + "serde", + "serde_derive", + "serde_json", + "thiserror 2.0.18", + "walkdir", + "yaml-rust", +] + [[package]] name = "system-deps" version = "6.2.2" @@ -3950,6 +4271,16 @@ dependencies = [ "utf-8", ] +[[package]] +name = "terminal_size" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b8cb979cb11c32ce1603f8137b22262a9d131aaa5c37b5678025f22b8becd0" +dependencies = [ + "rustix", + "windows-sys 0.60.2", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -4031,6 +4362,21 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.49.0" @@ -4270,6 +4616,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + [[package]] name = "typeid" version = "1.0.3" @@ -4340,6 +4692,15 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e" +[[package]] +name = "unicode-normalization" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-segmentation" version = "1.12.0" @@ -4352,6 +4713,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + [[package]] name = "url" version = "2.5.8" @@ -4389,6 +4756,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.21.0" @@ -5261,6 +5634,13 @@ dependencies = [ [[package]] name = "writer-md" version = "0.1.0" +dependencies = [ + "comrak", + "serde", + "serde_json", + "thiserror 2.0.18", + "writer-core", +] [[package]] name = "writer-store" @@ -5343,6 +5723,21 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "xdg" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fb433233f2df9344722454bc7e96465c9d03bff9d77c248f9e7523fe79585b5" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "yoke" version = "0.8.1" diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index aaa6f16..a7f4746 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -51,8 +51,7 @@ pub struct DocMeta { } /// File encoding detection and preservation -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] -#[derive(Default)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)] pub enum Encoding { #[default] Utf8, @@ -61,10 +60,8 @@ pub enum Encoding { Utf16Be, } - /// Line ending style preservation -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] -#[derive(Default)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)] pub enum LineEnding { #[default] Lf, @@ -72,7 +69,6 @@ pub enum LineEnding { Auto, } - /// Document content with metadata for opening #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct DocContent { @@ -99,18 +95,15 @@ pub enum DocSortField { } /// Sort order -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] -#[derive(Default)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)] pub enum SortOrder { Ascending, #[default] Descending, } - /// Save policy options -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)] -#[derive(Default)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)] pub enum SavePolicy { /// Atomic save: write to temp, fsync, rename #[default] @@ -119,7 +112,6 @@ pub enum SavePolicy { InPlace, } - /// Result of a save operation #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct SaveResult { diff --git a/crates/markdown/Cargo.toml b/crates/markdown/Cargo.toml index bca7555..02b0ba3 100644 --- a/crates/markdown/Cargo.toml +++ b/crates/markdown/Cargo.toml @@ -4,3 +4,10 @@ version = "0.1.0" edition = "2024" [dependencies] +writer-core = { path = "../core" } +comrak = "0.50" +serde = { version = "1", features = ["derive"] } +thiserror = "2" + +[dev-dependencies] +serde_json = "1" diff --git a/crates/markdown/src/lib.rs b/crates/markdown/src/lib.rs index b93cf3f..d371860 100644 --- a/crates/markdown/src/lib.rs +++ b/crates/markdown/src/lib.rs @@ -1,14 +1,444 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +use comrak::{Arena, Options, parse_document}; +use serde::{Deserialize, Serialize}; + +/// Markdown rendering profiles defining feature sets and safety levels +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum MarkdownProfile { + /// Strict CommonMark with no extensions + StrictCommonMark, + /// GitHub Flavored Markdown with safety features + /// Enables: tables, task lists, strikethrough, autolinks + /// Disables: raw HTML (treated as untrusted) + #[default] + GfmSafe, +} + +impl MarkdownProfile { + /// Converts the profile to Comrak options + pub fn to_options(&self) -> Options<'_> { + match self { + MarkdownProfile::StrictCommonMark => Options { + extension: comrak::options::Extension { ..Default::default() }, + parse: comrak::options::Parse::default(), + render: comrak::options::Render { r#unsafe: false, sourcepos: true, ..Default::default() }, + }, + MarkdownProfile::GfmSafe => Options { + extension: comrak::options::Extension { + strikethrough: true, + tagfilter: true, + table: true, + autolink: true, + tasklist: true, + superscript: false, + header_ids: None, + footnotes: false, + description_lists: false, + front_matter_delimiter: None, + multiline_block_quotes: false, + math_dollars: false, + math_code: false, + wikilinks_title_before_pipe: false, + wikilinks_title_after_pipe: false, + underline: false, + subscript: false, + spoiler: false, + greentext: false, + ..Default::default() + }, + parse: comrak::options::Parse::default(), + render: comrak::options::Render { r#unsafe: false, sourcepos: true, ..Default::default() }, + }, + } + } +} + +/// A heading in the document outline +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Heading { + pub level: u8, + pub text: String, + pub anchor: Option, +} + +/// Extracted document metadata from Markdown parsing +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct DocumentMetadata { + /// Title extracted from first H1 heading + pub title: Option, + /// Document outline (all headings H1-H6) + pub outline: Vec, + /// All link references found in the document + pub links: Vec, + /// Number of task list items (checked and unchecked) + pub task_items: TaskStats, + /// Estimated word count + pub word_count: usize, +} + +/// Statistics about task list items +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +pub struct TaskStats { + pub total: usize, + pub completed: usize, +} + +/// A link reference found in the document +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct LinkRef { + pub url: String, + pub title: Option, +} + +/// Result of rendering Markdown to HTML with metadata +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct RenderResult { + /// The rendered HTML output + pub html: String, + /// Document metadata extracted during rendering + pub metadata: DocumentMetadata, +} + +/// Errors that can occur during Markdown processing +#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)] +pub enum MarkdownError { + #[error("Failed to parse markdown: {0}")] + ParseError(String), +} + +/// The main Markdown engine for parsing and rendering +pub struct MarkdownEngine; + +impl MarkdownEngine { + /// Creates a new Markdown engine + pub fn new() -> Self { + Self + } + + /// Renders Markdown text to HTML using the specified profile + /// + /// # Arguments + /// * `text` - The Markdown text to render + /// * `profile` - The rendering profile to use + /// + /// # Returns + /// A `RenderResult` containing the HTML and extracted metadata + pub fn render(&self, text: &str, profile: MarkdownProfile) -> Result { + let arena = Arena::new(); + let options = profile.to_options(); + + let root = parse_document(&arena, text, &options); + + let mut metadata = DocumentMetadata { + title: None, + outline: Vec::new(), + links: Vec::new(), + task_items: TaskStats::default(), + word_count: 0, + }; + + Self::extract_metadata_from_node(&root, &mut metadata, &mut true); + + metadata.word_count = Self::estimate_word_count(text); + + let mut html_output = String::new(); + comrak::format_html(root, &options, &mut html_output).map_err(|e| MarkdownError::ParseError(e.to_string()))?; + + Ok(RenderResult { html: html_output, metadata }) + } + + /// Renders Markdown using the default GfmSafe profile + pub fn render_default(&self, text: &str) -> Result { + self.render(text, MarkdownProfile::default()) + } + + /// Extracts metadata by traversing the AST + fn extract_metadata_from_node(node: &comrak::nodes::Node, metadata: &mut DocumentMetadata, first_h1: &mut bool) { + use comrak::nodes::NodeValue; + + match &node.data.borrow().value { + NodeValue::Heading(heading) => { + let level = heading.level; + let text = Self::extract_text_from_node(node); + + if level == 1 && *first_h1 { + metadata.title = Some(text.clone()); + *first_h1 = false; + } + + metadata.outline.push(Heading { level, text, anchor: None }); + } + NodeValue::Link(link) => { + metadata.links.push(LinkRef { + url: link.url.clone(), + title: if link.title.is_empty() { None } else { Some(link.title.clone()) }, + }); + } + NodeValue::TaskItem(task_item) => { + metadata.task_items.total += 1; + if let Some(symbol) = task_item.symbol + && (symbol == 'x' || symbol == 'X') + { + metadata.task_items.completed += 1; + } + } + _ => {} + } + + for child in node.children() { + Self::extract_metadata_from_node(&child, metadata, first_h1); + } + } + + /// Extracts plain text from a node and its children + fn extract_text_from_node(node: &comrak::nodes::Node) -> String { + use comrak::nodes::NodeValue; + + let mut text = String::new(); + + match &node.data.borrow().value { + NodeValue::Text(t) => { + text.push_str(t); + } + NodeValue::Code(code) => { + text.push_str(&code.literal); + } + _ => { + for child in node.children() { + text.push_str(&Self::extract_text_from_node(&child)); + } + } + } + + text + } + + /// Estimates word count from Markdown text + /// + /// This is a simple estimation that counts whitespace-separated tokens + fn estimate_word_count(text: &str) -> usize { + text.split_whitespace().filter(|s| !s.is_empty()).count() + } + + /// Validates that HTML output contains source position attributes + /// + /// This is useful for testing that sourcepos is enabled + pub fn has_sourcepos(html: &str) -> bool { + html.contains("data-sourcepos") + } +} + +impl Default for MarkdownEngine { + fn default() -> Self { + Self::new() + } } #[cfg(test)] mod tests { use super::*; + use std::fs; + use std::path::PathBuf; + + #[test] + fn test_gfm_safe_blocks_raw_html() { + let engine = MarkdownEngine::new(); + let markdown = "\n\nHello world"; + let result = engine.render(markdown, MarkdownProfile::GfmSafe).unwrap(); + + assert!(!result.html.contains(" + + + +Click me + +