diff options
author | cel 🌸 <cel@blos.sm> | 2023-08-15 15:59:52 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-08-15 15:59:52 +0100 |
commit | 94fca8874c720ebdd5d18494094cf3048d575394 (patch) | |
tree | 7bce6dd06290aab97aa52dfb1e6b95becf8d7642 | |
parent | 56cd834cf88a58d3429b4b75489f161d57b28eaa (diff) | |
download | markdown-rs-94fca8874c720ebdd5d18494094cf3048d575394.tar.gz markdown-rs-94fca8874c720ebdd5d18494094cf3048d575394.tar.bz2 markdown-rs-94fca8874c720ebdd5d18494094cf3048d575394.zip |
Add ability to enable or disable html checkbox checkability
Diffstat (limited to '')
-rw-r--r-- | src/configuration.rs | 32 | ||||
-rw-r--r-- | src/to_html.rs | 5 | ||||
-rw-r--r-- | tests/gfm_task_list_item.rs | 17 |
3 files changed, 52 insertions, 2 deletions
diff --git a/src/configuration.rs b/src/configuration.rs index fe5698b..e995c09 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -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. diff --git a/src/to_html.rs b/src/to_html.rs index 1892ba1..edd397d 100644 --- a/src/to_html.rs +++ b/src/to_html.rs @@ -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=\"\" "); + } } } diff --git a/tests/gfm_task_list_item.rs b/tests/gfm_task_list_item.rs index 8b3f066..b65681d 100644 --- a/tests/gfm_task_list_item.rs +++ b/tests/gfm_task_list_item.rs @@ -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; @@ -27,6 +27,21 @@ fn gfm_task_list_item() -> Result<(), String> { ); 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>", "should not support laziness (1)" |