Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Collective](https://opencollective.com/comrak/all/badge.svg?label=financial+cont
[![crates.io version](https://img.shields.io/crates/v/comrak.svg)](https://crates.io/crates/comrak)
[![docs.rs](https://docs.rs/comrak/badge.svg)](https://docs.rs/comrak)

Rust port of [github's `cmark-gfm`](https://github.com/github/cmark).
Rust port of [github's `cmark-gfm`](https://github.com/github/cmark). *Currently synced with release `0.29.0.gfm.11`*.

- [Installation](#installation)
- [Usage](#usage)
Expand Down
13 changes: 8 additions & 5 deletions fuzz/fuzz_targets/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#![feature(int_roundings)]
#![no_main]
use comrak::{
markdown_to_html, ExtensionOptions, Options, ParseOptions,
markdown_to_html, markdown_to_commonmark, markdown_to_commonmark_xml,
ExtensionOptions, Options, ParseOptions,
RenderOptions, ListStyleType,
};
use libfuzzer_sys::arbitrary::{self, Arbitrary};
Expand Down Expand Up @@ -273,20 +274,22 @@ fn fuzz_one_input(input: &Input, num_bytes: usize) -> (usize, Duration, f64) {
let now = Instant::now();
{
let _ = markdown_to_html(&markdown, &input.options.to_options());
let _ = markdown_to_commonmark(&markdown, &input.options.to_options());
let _ = markdown_to_commonmark_xml(&markdown, &input.options.to_options());
}

let duration = now.elapsed();
let byte_length = markdown.len() * 3;
let duration_per_byte = duration.as_secs_f64() / (byte_length as f64);

if DEBUG {
println!(
"do_one: {} bytes, duration = {:?}",
markdown.len(),
byte_length,
duration
);
}

let byte_length = markdown.len();
let duration_per_byte = duration.as_secs_f64() / (markdown.len() as f64);

(
byte_length,
duration,
Expand Down
43 changes: 29 additions & 14 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,23 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
self.node = node;
let allow_wrap = self.options.render.width > 0 && !self.options.render.hardbreaks;

if !(matches!(
node.data.borrow().value,
NodeValue::Item(..) | NodeValue::TaskItem(..)
) && node.previous_sibling().is_none()
&& entering)
{
self.in_tight_list_item = self.get_in_tight_list_item(node);
let parent_node = node.parent();
if entering {
if parent_node.is_some()
&& matches!(
parent_node.unwrap().data.borrow().value,
NodeValue::Item(..) | NodeValue::TaskItem(..)
)
{
self.in_tight_list_item = self.get_in_tight_list_item(node);
}
} else if matches!(node.data.borrow().value, NodeValue::List(..)) {
self.in_tight_list_item = parent_node.is_some()
&& matches!(
parent_node.unwrap().data.borrow().value,
NodeValue::Item(..) | NodeValue::TaskItem(..)
)
&& self.get_in_tight_list_item(node);
}

match node.data.borrow().value {
Expand Down Expand Up @@ -343,7 +353,13 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
NodeValue::HtmlInline(ref literal) => {
self.format_html_inline(literal.as_bytes(), entering)
}
NodeValue::Strong => self.format_strong(),
NodeValue::Strong => {
if parent_node.is_none()
|| !matches!(parent_node.unwrap().data.borrow().value, NodeValue::Strong)
{
self.format_strong();
}
}
NodeValue::Emph => self.format_emph(node),
NodeValue::TaskItem(symbol) => self.format_task_item(symbol, node, entering),
NodeValue::Strikethrough => self.format_strikethrough(),
Expand Down Expand Up @@ -410,13 +426,12 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
let marker_width = if parent.list_type == ListType::Bullet {
2
} else {
let mut list_number = parent.start;
let list_number = match node.data.borrow().value {
NodeValue::Item(ref ni) => ni.start,
NodeValue::TaskItem(_) => parent.start,
_ => unreachable!(),
};
let list_delim = parent.delimiter;
let mut tmpch = node;
while let Some(tmp) = tmpch.previous_sibling() {
tmpch = tmp;
list_number += 1;
}
write!(
listmarker,
"{}{}{}",
Expand Down
19 changes: 12 additions & 7 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,17 @@ impl<'o> HtmlFormatter<'o> {
}
}
NodeValue::Strong => {
if entering {
self.output.write_all(b"<strong")?;
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
} else {
self.output.write_all(b"</strong>")?;
let parent_node = node.parent();
if parent_node.is_none()
|| !matches!(parent_node.unwrap().data.borrow().value, NodeValue::Strong)
{
if entering {
self.output.write_all(b"<strong")?;
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
} else {
self.output.write_all(b"</strong>")?;
}
}
}
NodeValue::Emph => {
Expand Down Expand Up @@ -977,7 +982,7 @@ impl<'o> HtmlFormatter<'o> {
self.output.write_all(b">")?;
write!(
self.output,
"<input type=\"checkbox\" disabled=\"\" {}/> ",
"<input type=\"checkbox\" {}disabled=\"\" /> ",
if symbol.is_some() {
"checked=\"\" "
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ use self::inlines::RefMap;
const TAB_STOP: usize = 4;
const CODE_INDENT: usize = 4;

// Very deeply nested lists can cause quadratic performance issues.
// This constant is used in open_new_blocks() to limit the nesting
// depth. It is unlikely that a non-contrived markdown document will
// be nested this deeply.
const MAX_LIST_DEPTH: usize = 100;

macro_rules! node_matches {
($node:expr, $( $pat:pat )|+) => {{
matches!(
Expand Down Expand Up @@ -222,7 +228,7 @@ pub struct ExtensionOptions {
/// options.extension.tasklist = true;
/// options.render.unsafe_ = true;
/// assert_eq!(markdown_to_html("* [x] Done\n* [ ] Not done\n", &options),
/// "<ul>\n<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Done</li>\n\
/// "<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
/// <li><input type=\"checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
/// ```
pub tasklist: bool,
Expand Down Expand Up @@ -954,11 +960,13 @@ impl<'a, 'o, 'c> Parser<'a, 'o, 'c> {
let mut nl: NodeList = NodeList::default();
let mut sc: scanners::SetextChar = scanners::SetextChar::Equals;
let mut maybe_lazy = node_matches!(self.current, NodeValue::Paragraph);
let mut depth = 0;

while !node_matches!(
container,
NodeValue::CodeBlock(..) | NodeValue::HtmlBlock(..)
) {
depth += 1;
self.find_first_nonspace(line);
let indented = self.indent >= CODE_INDENT;

Expand Down Expand Up @@ -1112,6 +1120,7 @@ impl<'a, 'o, 'c> Parser<'a, 'o, 'c> {
}
} else if (!indented || node_matches!(container, NodeValue::List(..)))
&& self.indent < 4
&& depth < MAX_LIST_DEPTH
&& unwrap_into_2(
parse_list_marker(
line,
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::panic;

mod api;
mod autolink;
mod commonmark;
mod core;
mod description_lists;
mod footnotes;
Expand Down
11 changes: 11 additions & 0 deletions src/tests/commonmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::*;

#[test]
fn commonmark_removes_redundant_strong() {
let options = ComrakOptions::default();

let input = "This is **something **even** better**";
let output = "This is **something even better**\n";

commonmark(input, output, Some(&options));
}
2 changes: 1 addition & 1 deletion src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn tasklist() {
html_opts!(
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [*]",
"<ul>\n<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> </li>\n</ul>\n",
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> </li>\n</ul>\n",
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/tests/tasklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ fn tasklist() {
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Red</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Green</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Papayawhip</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Papayawhip</li>\n",
"</ul>\n",
"<!-- end list -->\n",
"<ol>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Bird</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> McHale</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Parish</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Parish</li>\n",
"</ol>\n",
"<!-- end list -->\n",
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Red\n",
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Green\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green\n",
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
"</ul>\n",
Expand All @@ -58,7 +58,7 @@ fn tasklist_relaxed_regression() {
"* [!] Red\n",
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Red</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"</ul>\n"
),
);
Expand All @@ -74,7 +74,7 @@ fn tasklist_relaxed_regression() {
"* [!] Red\n",
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> Red</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"</ul>\n"
),
);
Expand All @@ -93,7 +93,7 @@ fn tasklist_32() {
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> List item 1</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> This list item is <strong>bold</strong></li>\n",
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\" /> There is some <code>code</code> here</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> There is some <code>code</code> here</li>\n",
"</ul>\n"
),
);
Expand Down
5 changes: 4 additions & 1 deletion src/xml.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::nodes::{AstNode, ListType, NodeCode, NodeValue};
use crate::parser::{Options, Plugins};
use once_cell::sync::Lazy;
use std::cmp;
use std::io::{self, Write};

use crate::nodes::NodeHtmlBlock;

const MAX_INDENT: u32 = 40;

/// Formats an AST as HTML, modified by the given options.
pub fn format_document<'a>(
root: &'a AstNode<'a>,
Expand Down Expand Up @@ -121,7 +124,7 @@ impl<'o> XmlFormatter<'o> {
}

fn indent(&mut self) -> io::Result<()> {
for _ in 0..self.indent {
for _ in 0..(cmp::min(self.indent, MAX_INDENT)) {
self.output.write_all(b" ")?;
}
Ok(())
Expand Down