From 575c18f915504cf2d9568865f790714f8a6ca5b3 Mon Sep 17 00:00:00 2001 From: Magnus Hovland Hoff Date: Wed, 3 Oct 2018 18:24:55 +0200 Subject: [PATCH] Support automatically slugged links --- Cargo.lock | 8 ++++---- src/rendering.rs | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2cdcc36..1690174 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -633,8 +633,8 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.1.0" -source = "git+https://github.com/maghoff/pulldown-cmark.git#eec135668e6961c00a20cca4e3e5f6602ec68983" +version = "0.1.2" +source = "git+https://github.com/maghoff/pulldown-cmark.git#e5dea36c4891413e58654fc0b9d9d0eeb3374fa5" dependencies = [ "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -777,7 +777,7 @@ dependencies = [ "matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "pulldown-cmark 0.1.0 (git+https://github.com/maghoff/pulldown-cmark.git)", + "pulldown-cmark 0.1.2 (git+https://github.com/maghoff/pulldown-cmark.git)", "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1402,7 +1402,7 @@ dependencies = [ "checksum proc-macro-hack-impl 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5cb6f960ad471404618e9817c0e5d10b1ae74cfdf01fab89ea0641fe7fb2892" "checksum proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7" "checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6" -"checksum pulldown-cmark 0.1.0 (git+https://github.com/maghoff/pulldown-cmark.git)" = "" +"checksum pulldown-cmark 0.1.2 (git+https://github.com/maghoff/pulldown-cmark.git)" = "" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8" "checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035" diff --git a/src/rendering.rs b/src/rendering.rs index f26d31f..2e39a14 100644 --- a/src/rendering.rs +++ b/src/rendering.rs @@ -1,9 +1,18 @@ +use slug::slugify; use pulldown_cmark::{Parser, Tag, html, OPTION_ENABLE_TABLES, OPTION_DISABLE_HTML}; use pulldown_cmark::Event::{Text, End}; -pub fn render_markdown(src: &str) -> String { +fn slugify_link(text: &str, title: &str) -> Option<(String, String)> { + Some((slugify(text), title.to_owned())) +} + +fn parser(src: &str) -> Parser { let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; - let p = Parser::new_ext(src, opts); + Parser::new_with_broken_link_callback(src, opts, Some(&slugify_link)) +} + +pub fn render_markdown(src: &str) -> String { + let p = parser(src); let mut buf = String::new(); html::push_html(&mut buf, p); buf @@ -14,8 +23,7 @@ fn is_html_special(c: char) -> bool { } pub fn render_markdown_for_fts(src: &str) -> String { - let opts = OPTION_ENABLE_TABLES | OPTION_DISABLE_HTML; - let p = Parser::new_ext(src, opts); + let p = parser(src); let mut buf = String::new(); for event in p {