Skip to content

Commit

Permalink
Add support for enabling GFM tasklist checkedness
Browse files Browse the repository at this point in the history
Closes GH-77.
  • Loading branch information
rotmoded committed Aug 16, 2023
1 parent 56cd834 commit dea07bb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,38 @@ pub struct CompileOptions {
/// ```
pub gfm_footnote_clobber_prefix: Option<String>,

/// Whether or not GFM task list html `<input>` items are enabled.
///
/// This determines whether or not the user of the browser is able
/// to click and toggle generated checkbox items. The default is false.
///
/// ## Examples
///
/// ```
/// use markdown::{to_html_with_options, CompileOptions, Options, ParseOptions};
/// # fn main() -> Result<(), String> {
///
/// // With `gfm_task_list_item_checkable`, generated `<input type="checkbox" />`
/// // tags do not contain the attribute `disabled=""` and are thus toggleable by
/// // browser users.
/// assert_eq!(
/// to_html_with_options(
/// "* [x] y.",
/// &Options {
/// parse: ParseOptions::gfm(),
/// compile: CompileOptions {
/// gfm_task_list_item_checkable: true,
/// ..CompileOptions::gfm()
/// }
/// }
/// )?,
/// "<ul>\n<li><input type=\"checkbox\" checked=\"\" /> y.</li>\n</ul>"
/// );
/// # Ok(())
/// # }
/// ```
pub gfm_task_list_item_checkable: bool,

/// Whether to support the GFM tagfilter.
///
/// This option does nothing if `allow_dangerous_html` is not turned on.
Expand Down
5 changes: 4 additions & 1 deletion src/to_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,10 @@ fn on_enter_gfm_table_row(context: &mut CompileContext) {
/// Handle [`Enter`][Kind::Enter]:[`GfmTaskListItemCheck`][Name::GfmTaskListItemCheck].
fn on_enter_gfm_task_list_item_check(context: &mut CompileContext) {
if !context.image_alt_inside {
context.push("<input type=\"checkbox\" disabled=\"\" ");
context.push("<input type=\"checkbox\" ");
if !context.options.gfm_task_list_item_checkable {
context.push("disabled=\"\" ");
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,7 @@ mod tests {
fn longest<'a>(list: &[&'a str]) -> Option<&'a str> {
let mut max = 0;
let mut result = None;
for name in list.iter() {
for name in list {
let len = name.len();
if len > max {
max = len;
Expand Down
17 changes: 16 additions & 1 deletion tests/gfm_task_list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use markdown::{
mdast::{Emphasis, List, ListItem, Node, Paragraph, Root, Text},
to_html, to_html_with_options, to_mdast,
unist::Position,
Options, ParseOptions,
CompileOptions, Options, ParseOptions,
};
use pretty_assertions::assert_eq;

Expand All @@ -26,6 +26,21 @@ fn gfm_task_list_item() -> Result<(), String> {
"should support unchecked task list item checks"
);

assert_eq!(
to_html_with_options(
"* [x] y.",
&Options {
parse: ParseOptions::gfm(),
compile: CompileOptions {
gfm_task_list_item_checkable: true,
..CompileOptions::gfm()
}
}
)?,
"<ul>\n<li><input type=\"checkbox\" checked=\"\" /> y.</li>\n</ul>",
"should support option for enabled (checkable) task list item checks"
);

assert_eq!(
to_html_with_options("*\n [x]", &Options::gfm())?,
"<ul>\n<li>[x]</li>\n</ul>",
Expand Down

0 comments on commit dea07bb

Please sign in to comment.