Skip to content

Commit

Permalink
Merge pull request kivikakk#150 from yujiri8/master
Browse files Browse the repository at this point in the history
Add option to escape HTML instead of clobber (fix kivikakk#149)
  • Loading branch information
kivikakk authored Aug 11, 2020
2 parents aa714d9 + c70d51b commit b46e3a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ impl<'o> HtmlFormatter<'o> {
},
NodeValue::HtmlBlock(ref nhb) => if entering {
self.cr()?;
if !self.options.render.unsafe_ {
if self.options.render.escape {
self.escape(&nhb.literal)?;
} else if !self.options.render.unsafe_ {
self.output.write_all(b"<!-- raw HTML omitted -->")?;
} else if self.options.extension.tagfilter {
tagfilter_block(&nhb.literal, &mut self.output)?;
Expand Down Expand Up @@ -541,7 +543,9 @@ impl<'o> HtmlFormatter<'o> {
self.output.write_all(b"</code>")?;
},
NodeValue::HtmlInline(ref literal) => if entering {
if !self.options.render.unsafe_ {
if self.options.render.escape {
self.escape(&literal)?;
} else if !self.options.render.unsafe_ {
self.output.write_all(b"<!-- raw HTML omitted -->")?;
} else if self.options.extension.tagfilter && tagfilter(literal) {
self.output.write_all(b"&lt;")?;
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.long("unsafe")
.help("Allow raw HTML and dangerous URLs"),
)
.arg(
clap::Arg::with_name("escape")
.long("escape")
.help("Escape raw HTML instead of clobbering it"),
)
.arg(
clap::Arg::with_name("extension")
.short("e")
Expand Down Expand Up @@ -134,6 +139,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.parse()
.unwrap_or(0),
unsafe_: matches.is_present("unsafe"),
escape: matches.is_present("escape"),
},
};

Expand Down
15 changes: 15 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,21 @@ pub struct ComrakRenderOptions {
/// <p><a href=\"http://commonmark.org\">Safe</a>.</p>\n");
/// ```
pub unsafe_: bool,

/// Escape raw HTML instead of clobbering it.
/// ```
/// # use comrak::{markdown_to_html, ComrakOptions};
/// let mut options = ComrakOptions::default();
/// let input = "<i>italic text</i>";
///
/// assert_eq!(markdown_to_html(input, &options),
/// "<p><!-- raw HTML omitted -->italic text<!-- raw HTML omitted --></p>\n");
///
/// options.render.escape = true;
/// assert_eq!(markdown_to_html(input, &options),
/// "<p>&lt;i&gt;italic text&lt;/i&gt;</p>\n");
/// ```
pub escape: bool,
}

#[derive(Clone)]
Expand Down

0 comments on commit b46e3a2

Please sign in to comment.