use std::{ io::{BufWriter, Write}, path::Path, }; use pulldown_cmark::{CowStr, Options, Parser}; pub fn compile_markdown( src: impl AsRef, html: impl AsRef, gmi: impl AsRef, ) -> std::io::Result<()> { let src_text = std::fs::read_to_string(src)?; let mut html = create_file(html)?; let mut gmi = create_file(gmi)?; let mut state = State::Start; let mut ol = false; // ordered list let mut counter: u64 = 0; let mut links: Vec> = vec![]; for event in Parser::new_ext(&src_text, Options::all()) { use pulldown_cmark::Event::*; dbg!(&event); match event { Start(tag) => { use pulldown_cmark::{CodeBlockKind, HeadingLevel, Tag::*}; match tag { Paragraph => { html.write_all(b"

")?; write_paragraph_start(&mut gmi, state)?; } Heading { level, id, .. } => { if let Some(id) = id { html.write_fmt(format_args!("<{} id=\"{}\">", level, id))?; } else { html.write_fmt(format_args!("<{}>", level))?; } write_paragraph_start(&mut gmi, state)?; let hashes = match level { HeadingLevel::H1 => "# ", HeadingLevel::H2 => "## ", _ => "### ", }; gmi.write_all(hashes.as_bytes())?; state = State::Paragraph; } BlockQuote(_kind) => { html.write_all(b"

")?; write_paragraph_start(&mut gmi, state)?; gmi.write_all(b"> ")?; state = State::Quote; } CodeBlock(CodeBlockKind::Fenced(lang)) => { // TODO: highlighting with syntect html.write_all(b"
")?;

                        write_paragraph_start(&mut gmi, state)?;
                        gmi.write_fmt(format_args!("```{}\r\n", lang))?;
                    }

                    CodeBlock(CodeBlockKind::Indented) => {
                        html.write_all(b"
")?;

                        write_paragraph_start(&mut gmi, state)?;
                        gmi.write_all(b"```")?;
                    }

                    List(None) => {
                        html.write_all(b"