Skip to content

Commit

Permalink
Replace markdown with pulldown-cmark
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Apr 8, 2024
1 parent 1d4fa1a commit d7d058f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 42 deletions.
55 changes: 38 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ indicatif = "0.16"
itertools = "0.10"
lsp = { package = "lsp-types", version = "0.94" }
memchr = "2.5"
markdown = "1.0.0-alpha.16"
num_cpus = "1.13"
once_cell = "1.7"
percent-encoding = "2.2.0"
parking_lot = "0.12"
percent-encoding = "2.2.0"
pulldown-cmark = "0.10.2"
rayon = "1.5"
regex = "1"
rgb2ansi256 = "0.1.1"
Expand Down
1 change: 0 additions & 1 deletion crates/maple_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ grep-matcher = { workspace = true }
ignore = { workspace = true }
itertools = { workspace = true }
tokio = { workspace = true, features = ["fs", "rt", "process", "macros", "rt-multi-thread", "sync", "time"] }
markdown = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
rayon = { workspace = true }
Expand Down
10 changes: 5 additions & 5 deletions crates/maple_core/src/stdio_server/plugin/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ impl ClapPlugin for Markdown {
// TODO: incremental update?
let lines = self.vim.getbufline(bufnr, 1, "$").await?;
let markdown_content = lines.join("\n");
let html =
markdown::to_html_with_options(&markdown_content, &markdown::Options::gfm())
.map_err(PluginError::Other)?;
let html = maple_markdown::to_html(&markdown_content)?;
if let Some(msg_tx) = self.bufs.get(&bufnr) {
msg_tx.send_replace(Message::UpdateContent(html));
}
Expand Down Expand Up @@ -135,8 +133,10 @@ impl ClapPlugin for Markdown {
let bufnr = self.vim.bufnr("").await?;

tokio::spawn(async move {
if let Err(err) = maple_markdown::open_preview(listener, msg_rx).await {
tracing::error!(?err, "Failed to open markdown preview in browser");
if let Err(err) =
maple_markdown::open_preview_in_browser(listener, msg_rx).await
{
tracing::error!(?err, "Failed to open markdown preview");
}
tracing::debug!(bufnr, "markdown preview exited");
});
Expand Down
2 changes: 1 addition & 1 deletion crates/maple_markdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ publish.workspace = true
[dependencies]
axum = { version = "0.7.5", features = ["ws"] }
axum-extra = { version = "0.9.3", features = ["typed-header"] }
markdown = { workspace = true }
once_cell = { workspace = true }
percent-encoding = { workspace = true }
pulldown-cmark = { workspace = true }
regex = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }
Expand Down
31 changes: 15 additions & 16 deletions crates/maple_markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ async fn handle_websocket(mut socket: WebSocket, mut msg_rx: Receiver<Message>)
let _ = socket.send(WsMessage::Close(None)).await;
}

pub fn to_html(markdown_content: &str) -> Result<String, Error> {
let parser = pulldown_cmark::Parser::new(markdown_content);

let mut html_output = String::new();
pulldown_cmark::html::push_html(&mut html_output, parser);

Ok(html_output)
}

fn process_message(msg: Message) -> Result<serde_json::Value, Error> {
let res = match msg {
Message::FileChanged(path) => {
let markdown_content = std::fs::read_to_string(path)?;
let html = markdown::to_html_with_options(
&markdown_content,
&markdown::Options {
parse: markdown::ParseOptions::gfm(),
compile: markdown::CompileOptions {
gfm_task_list_item_checkable: true,
..markdown::CompileOptions::gfm()
},
},
)?;
let html = to_html(&markdown_content)?;
serde_json::json!({
"type": "update_content",
"data": html,
Expand All @@ -83,7 +83,7 @@ fn process_message(msg: Message) -> Result<serde_json::Value, Error> {
Ok(res)
}

// Message type between the server and ws clients.
// Worker message that the websocket server deals with.
#[derive(Debug, Clone)]
pub enum Message {
/// Markdown file was modified.
Expand All @@ -94,7 +94,7 @@ pub enum Message {
Scroll(usize),
}

pub async fn open_preview(
pub async fn open_preview_in_browser(
listener: tokio::net::TcpListener,
msg_rx: Receiver<Message>,
) -> Result<(), Error> {
Expand All @@ -104,11 +104,10 @@ pub async fn open_preview(

let port = listener.local_addr()?.port();

if let Err(err) = webbrowser::open(&format!("http://127.0.0.1:{port}")) {
tracing::error!("Error serving connection: {:?}", err);
}
webbrowser::open(&format!("http://127.0.0.1:{port}"))?;

tracing::debug!("Listening on {listener:?}");

axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
Expand Down Expand Up @@ -140,7 +139,7 @@ mod tests {
.await
.unwrap();

open_preview(listener, msg_rx)
open_preview_in_browser(listener, msg_rx)
.await
.expect("Failed to open markdown preview");
}
Expand Down

0 comments on commit d7d058f

Please sign in to comment.