Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target_blank to RenderOptions for links #358

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ impl<'o> HtmlFormatter<'o> {
if entering {
self.output.write_all(b"<a")?;
self.render_sourcepos(node)?;
if self.options.render.target_blank {
self.output.write_all(b" target=\"_blank\"")?;
}
self.output.write_all(b" href=\"")?;
let url = nl.url.as_bytes();
if self.options.render.unsafe_ || !dangerous_url(url) {
Expand Down
12 changes: 12 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,18 @@ pub struct RenderOptions {
/// assert!(xml.contains("<emph sourcepos=\"1:7-1:13\">"));
/// ```
pub sourcepos: bool,

/// Make all links in the document open in a new tab by setting `target="_blank"`.
///
/// ```rust
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// options.render.target_blank = true;
/// let input = "Hello [world](https://www.github.com)!";
/// let html = markdown_to_html(input, &options);
/// assert!(html.contains("target=\"_blank\""));
///
pub target_blank: bool,
}

#[non_exhaustive]
Expand Down
1 change: 1 addition & 0 deletions src/tests/propfuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn propfuzz_doesnt_crash(md: String) {
escape: false,
list_style: ListStyleType::Dash,
sourcepos: true,
target_blank: false,
},
};

Expand Down