Skip to content

Commit

Permalink
Add alerts extension option
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Jan 18, 2025
1 parent 8686e9e commit bf459e2
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Options:
[possible values: strikethrough, tagfilter, table, autolink, tasklist, superscript,
footnotes, description-lists, multiline-block-quotes, math-dollars, math-code,
wikilinks-title-after-pipe, wikilinks-title-before-pipe, underline, subscript, spoiler,
greentext]
greentext, alerts]
-t, --to <FORMAT>
Specify output format
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/all_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fuzz_target!(|s: &str| {
extension.underline = true;
extension.spoiler = true;
extension.greentext = true;
extension.alerts = true;

let mut parse = ParseOptions::default();
parse.smart = true;
Expand Down
8 changes: 8 additions & 0 deletions fuzz/fuzz_targets/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ struct FuzzExtensionOptions {
shortcodes: bool,
wikilinks_title_after_pipe: bool,
wikilinks_title_before_pipe: bool,
underline: bool,
spoiler: bool,
greentext: bool,
alerts: bool,
}

impl FuzzExtensionOptions {
Expand All @@ -216,6 +220,10 @@ impl FuzzExtensionOptions {
extension.shortcodes = self.shortcodes;
extension.wikilinks_title_after_pipe = self.wikilinks_title_after_pipe;
extension.wikilinks_title_before_pipe = self.wikilinks_title_before_pipe;
extension.underline = self.underline;
extension.spoiler = self.spoiler;
extension.greentext = self.greentext;
extension.alerts = self.alerts;
extension.front_matter_delimiter = None;
extension.header_ids = None;
extension
Expand Down
2 changes: 1 addition & 1 deletion script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/wikilink
|| failed=1
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/description_lists.md "$PROGRAM_ARG -e description-lists" \
|| failed=1
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/alerts.md "$PROGRAM_ARG -e description-lists" \
python3 spec_tests.py --no-normalize --spec ../../../src/tests/fixtures/alerts.md "$PROGRAM_ARG -e alerts" \
|| failed=1

python3 spec_tests.py --no-normalize --spec regression.txt "$PROGRAM_ARG" \
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ enum Extension {
Subscript,
Spoiler,
Greentext,
Alerts,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
Expand Down Expand Up @@ -271,6 +272,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.subscript(exts.contains(&Extension::Subscript))
.spoiler(exts.contains(&Extension::Spoiler))
.greentext(exts.contains(&Extension::Greentext))
.alerts(exts.contains(&Extension::Alerts))
.maybe_front_matter_delimiter(cli.front_matter_delimiter);

#[cfg(feature = "shortcodes")]
Expand Down
20 changes: 18 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,23 @@ pub struct ExtensionOptions<'c> {
#[cfg_attr(feature = "bon", builder(default))]
pub multiline_block_quotes: bool,

// #[cfg_attr(feature = "bon", builder(default))]
// pub alerts: bool,
/// Enables GitHub style alerts
///
/// ```md
/// > [!note]
/// > Something of note
/// ```
///
/// ```
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// options.extension.alerts = true;
/// assert_eq!(markdown_to_html("> [!note]\n> Something of note", &options),
/// "<div class=\"alert alert-note\">\n<p class=\"alert-title\">Note</p>\n<p>Something of note</p>\n</div>\n");
/// ```
#[cfg_attr(feature = "bon", builder(default))]
pub alerts: bool,

/// Enables math using dollar syntax.
///
/// ``` md
Expand Down Expand Up @@ -1996,6 +2011,7 @@ where

fn detect_alert(&mut self, line: &[u8], indented: bool, alert_type: &mut AlertType) -> bool {
!indented
&& self.options.extension.alerts
&& line[self.first_nonspace] == b'>'
&& unwrap_into(
scanners::alert_start(&line[self.first_nonspace..]),
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::io::{self, Write};
use std::panic;

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

#[test]
fn alerts() {
html_opts!(
[extension.alerts],
concat!("> [!note]\n", "> Pay attention\n",),
concat!(
"<div class=\"alert alert-note\">\n",
"<p class=\"alert-title\">Note</p>\n",
"<p>Pay attention</p>\n",
"</div>\n",
),
);
}

#[test]
fn sourcepos() {
assert_ast_match!(
[extension.alerts],
"> [!note]\n"
"> Pay attention\n",
(document (1:1-2:15) [
(alert (1:1-2:15) [
(paragraph (2:3-2:15) [
(text (2:3-2:15) "Pay attention")
])
])
])
);
}

#[test]
fn sourcepos_in_list() {
assert_ast_match!(
[extension.alerts],
"- item one\n"
"\n"
" > [!note]\n"
" > Pay attention\n",
(document (1:1-4:17) [
(list (1:1-4:17) [
(item (1:1-4:17) [
(paragraph (1:3-1:10) [
(text (1:3-1:10) "item one")
])
(alert (3:3-4:17) [
(paragraph (4:5-4:17) [
(text (4:5-4:17) "Pay attention")
])
])
])
])
])
);
}
3 changes: 2 additions & 1 deletion src/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ fn exercise_full_api() {
.underline(true)
.subscript(true)
.spoiler(true)
.greentext(true);
.greentext(true)
.alerts(true);

let parse = ParseOptions::builder()
.smart(false)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/commonmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn commonmark_relist() {
#[test_case("> [!note] Title\n> A note", "> [!NOTE] Title\n> A note\n")]
fn commonmark_alerts(markdown: &str, cm: &str) {
let mut options = Options::default();
options.extension.wikilinks_title_before_pipe = true;
options.extension.alerts = true;

commonmark(markdown, cm, Some(&options));
}

0 comments on commit bf459e2

Please sign in to comment.