├── .gitignore ├── .travis.yml ├── .appveyor.yml ├── README.md ├── src ├── utils.rs └── lib.rs ├── Cargo.toml └── LICENSE_MIT /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /pkg 3 | /bin 4 | wasm-pack.log 5 | **/*.rs.bk 6 | Cargo.lock 7 | .crates.toml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | sudo: false 3 | 4 | matrix: 5 | include: 6 | 7 | # tests pass 8 | - rust: nightly 9 | script: 10 | - cargo test --locked 11 | - rustup component add rustfmt-preview 12 | - cargo fmt --all -- --check 13 | env: RUST_BACKTRACE=1 14 | -------------------------------------------------------------------------------- /.appveyor.yml: -------------------------------------------------------------------------------- 1 | install: 2 | - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe 3 | - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly 4 | - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin 5 | - rustc -V 6 | - cargo -V 7 | 8 | build: false 9 | 10 | test_script: 11 | - cargo test --locked 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pulldown-cmark-wasm 2 | 3 | A simple WebAssembly wrapper for the excellent [pulldown-cmark](https://crates.io/crates/pulldown-cmark) parser for the standardized version of Markdown, CommonMark. 4 | 5 | When packaged with [wasm-pack](https://rustwasm.github.io/wasm-pack/), it creates a JS module exporting a single function, `format`. Pass in a string containing Markdown formatted text, get out CommonMark-compliantly generated html—that simple. 6 | -------------------------------------------------------------------------------- /src/utils.rs: -------------------------------------------------------------------------------- 1 | use cfg_if::cfg_if; 2 | 3 | cfg_if! { 4 | // When the `console_error_panic_hook` feature is enabled, we can call the 5 | // `set_panic_hook` function to get better error messages if we ever panic. 6 | if #[cfg(feature = "console_error_panic_hook")] { 7 | extern crate console_error_panic_hook; 8 | pub use self::console_error_panic_hook::set_once as set_panic_hook; 9 | } else { 10 | #[inline] 11 | pub fn set_panic_hook() {} 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pulldown-cmark-wasm" 3 | version = "0.1.0" 4 | authors = ["Till Schneidereit "] 5 | description = "A WebAssembly wrapper for pulldown-cmark, a pull parser for CommonMark (i.e. Markdown)" 6 | repository = "https://github.com/tschneidereit/pulldown-cmark-wasm" 7 | license = "MIT" 8 | 9 | [lib] 10 | crate-type = ["cdylib"] 11 | 12 | [features] 13 | default = [] 14 | 15 | [profile.release] 16 | lto = true 17 | opt-level = "s" 18 | 19 | [dependencies] 20 | cfg-if = "0.1.2" 21 | wasm-bindgen = "0.2" 22 | console_error_panic_hook = { version = "0.1.1", optional = true } 23 | wee_alloc = { version = "0.4.1", optional = true } 24 | 25 | pulldown-cmark = "0.1.2" 26 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate cfg_if; 2 | extern crate pulldown_cmark; 3 | extern crate wasm_bindgen; 4 | 5 | mod utils; 6 | 7 | use cfg_if::cfg_if; 8 | use pulldown_cmark::{html, Parser}; 9 | use wasm_bindgen::prelude::*; 10 | 11 | cfg_if! { 12 | // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global 13 | // allocator. 14 | if #[cfg(feature = "wee_alloc")] { 15 | extern crate wee_alloc; 16 | #[global_allocator] 17 | static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; 18 | } 19 | } 20 | 21 | #[wasm_bindgen] 22 | pub fn format(markdown: &str) -> String { 23 | utils::set_panic_hook(); 24 | let mut html_buf = String::new(); 25 | let parser = Parser::new(markdown); 26 | html::push_html(&mut html_buf, parser); 27 | html_buf 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE_MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 Till Schneidereit 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | --------------------------------------------------------------------------------