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

Discord-flavored Markdown #421

Merged
merged 7 commits into from
Jun 4, 2024
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
99 changes: 27 additions & 72 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions fuzz/fuzz_targets/all_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ fuzz_target!(|s: &str| {
extension.shortcodes = true;
extension.wikilinks_title_after_pipe = true;
extension.wikilinks_title_before_pipe = true;
extension.underline = true;
extension.spoiler = true;
extension.greentext = true;

let mut parse = ParseOptions::default();
parse.smart = true;
Expand All @@ -42,6 +45,8 @@ fuzz_target!(|s: &str| {
render.list_style = ListStyleType::Star;
render.sourcepos = true;
render.escaped_char_spans = true;
render.ignore_setext = true;
render.ignore_empty_links = true;

markdown_to_html(
s,
Expand Down
17 changes: 17 additions & 0 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
}
NodeValue::Math(ref math) => self.format_math(math, allow_wrap, entering),
NodeValue::WikiLink(ref nl) => return self.format_wikilink(nl, entering),
NodeValue::Underline => self.format_underline(),
NodeValue::SpoileredText => self.format_spoiler(),
NodeValue::EscapedTag(ref net) => self.format_escaped_tag(net),
};
true
}
Expand Down Expand Up @@ -595,6 +598,8 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
&& !self.options.render.hardbreaks
{
self.cr();
} else if self.options.render.hardbreaks {
self.output(&[b'\n'], allow_wrap, Escaping::Literal);
} else {
self.output(&[b' '], allow_wrap, Escaping::Literal);
}
Expand Down Expand Up @@ -668,6 +673,18 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
write!(self, "^").unwrap();
}

fn format_underline(&mut self) {
write!(self, "__").unwrap();
}

fn format_spoiler(&mut self) {
write!(self, "||").unwrap();
}

fn format_escaped_tag(&mut self, net: &String) {
self.output(net.as_bytes(), false, Escaping::Literal);
}

fn format_link(&mut self, node: &'a AstNode<'a>, nl: &NodeLink, entering: bool) -> bool {
if is_autolink(node, nl) {
if entering {
Expand Down
17 changes: 17 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,23 @@ impl<'o> HtmlFormatter<'o> {
self.output.write_all(b"</a>")?;
}
}
NodeValue::Underline => {
if entering {
self.output.write_all(b"<u>")?;
} else {
self.output.write_all(b"</u>")?;
}
}
NodeValue::SpoileredText => {
if entering {
self.output.write_all(b"<span class=\"spoiler\">")?;
} else {
self.output.write_all(b"</span>")?;
}
}
NodeValue::EscapedTag(ref net) => {
self.output.write_all(net.as_bytes())?;
}
}
Ok(false)
}
Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ struct Cli {
/// Include source position attribute in HTML and XML output
#[arg(long)]
sourcepos: bool,

/// Ignore setext headers
#[arg(long)]
ignore_setext: bool,

/// Ignore empty links
#[arg(long)]
ignore_empty_links: bool,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
Expand Down Expand Up @@ -161,6 +169,9 @@ enum Extension {
MathCode,
WikilinksTitleAfterPipe,
WikilinksTitleBeforePipe,
Underline,
Spoiler,
Greentext,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
Expand Down Expand Up @@ -242,6 +253,9 @@ fn main() -> Result<(), Box<dyn Error>> {
.math_code(exts.contains(&Extension::MathCode))
.wikilinks_title_after_pipe(exts.contains(&Extension::WikilinksTitleAfterPipe))
.wikilinks_title_before_pipe(exts.contains(&Extension::WikilinksTitleBeforePipe))
.underline(exts.contains(&Extension::Underline))
.spoiler(exts.contains(&Extension::Spoiler))
.greentext(exts.contains(&Extension::Greentext))
.front_matter_delimiter(cli.front_matter_delimiter);

#[cfg(feature = "shortcodes")]
Expand All @@ -268,6 +282,8 @@ fn main() -> Result<(), Box<dyn Error>> {
.list_style(cli.list_style.into())
.sourcepos(cli.sourcepos)
.escaped_char_spans(cli.escaped_char_spans)
.ignore_setext(cli.ignore_setext)
.ignore_empty_links(cli.ignore_empty_links)
.build()?;

let options = Options {
Expand Down
Loading