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

Add escape_square_brackets into comment_formats markdown configuration #379

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

What's changed

* Add `escape_square_brackets` into `comment_formats` markdown configuration. ([#XXX](...) by lquerel).
* Add `enforce_trailing_dots` into the `comment_formats` configuration. ([#XXX](...) by lquerel).
* Add support for `indent_type` in both the comment filter and the `comment_formats` configuration. ([#XXX](...) by lquerel).
* Add `regex_replace` filter to support replacing text using regex. ([#XXX](...) by lquerel).
Expand Down
1 change: 1 addition & 0 deletions crates/weaver_forge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ comment_formats: # optional

# Fields specific to 'markdown' format
escape_backslashes: <bool> # Whether to escape backslashes in markdown (default: false).
escape_square_brackets: <bool> # Whether to escape square brackets in markdown (default: false).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have brackets already in the conventions, what is happening there today? And then, shouldn't this be true by default, as we want to keep the brackets to match the source data? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It’s probably better to have this escaping enabled by default. I will make this change before merging. Thanks

shortcut_reference_links: <bool> # Convert inlined links into shortcut reference links (default: false).
indent_first_level_list_items: <bool> # Indent the first level of list items in markdown (default: false).
default_block_code_language: <string> # Default language for block code snippets (default: "").
Expand Down
115 changes: 111 additions & 4 deletions crates/weaver_forge/src/formats/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
#[serde(rename_all = "snake_case")]
pub struct MarkdownRenderOptions {
/// Whether to escape backslashes in the markdown.
/// Whether to escape backslashes in the Markdown.
/// Default is false.
#[serde(default)]
pub(crate) escape_backslashes: bool,
/// Whether to escape square brackets in the Markdown text. Valid links are not affected.
/// Default is false.
#[serde(default)]
pub(crate) escape_square_brackets: bool,
/// Whether to indent the first level of list items in the markdown.
/// Default is false.
#[serde(default)]
Expand Down Expand Up @@ -196,11 +200,36 @@
}
}
Node::Text(text) => {
fn escape_unescaped_chars(s: &str, chars_to_escape: &[char]) -> String {
let mut result = String::with_capacity(s.len());
let mut backslash_count = 0;

for c in s.chars() {
if c == '\\' {
backslash_count += 1;
result.push(c);
} else {
if chars_to_escape.contains(&c) && backslash_count % 2 == 0 {
// Even number of backslashes means the character is unescaped
result.push('\\');
}
result.push(c);
// Reset the backslash count after a non-backslash character
backslash_count = 0;
}
}

result
}

let mut text = text.value.clone();
if options.escape_backslashes {
ctx.add_text(&text.value.replace('\\', "\\\\"));
} else {
ctx.add_text(&text.value);
text = text.replace('\\', "\\\\");
}
if options.escape_square_brackets {
text = escape_unescaped_chars(&text, &['[', ']']);
}
ctx.add_text(&text);
}
Node::Paragraph(p) => {
ctx.add_cond_blank_line();
Expand Down Expand Up @@ -375,6 +404,7 @@
indent_type: IndentType::Space,
format: RenderFormat::Markdown(MarkdownRenderOptions {
escape_backslashes: false,
escape_square_brackets: false,
indent_first_level_list_items: true,
shortcut_reference_link: true,
default_block_code_language: None,
Expand Down Expand Up @@ -467,6 +497,83 @@
- Use a domain-specific attribute
- Set `error.type` to capture all errors, regardless of whether they are defined within the domain-specific set or not."##
);

let config = WeaverConfig {
comment_formats: Some(
vec![(
"go".to_owned(),
CommentFormat {
Fixed Show fixed Hide fixed
header: None,
prefix: Some("// ".to_owned()),
footer: None,
format: RenderFormat::Markdown(MarkdownRenderOptions {
escape_backslashes: false,
escape_square_brackets: false,
indent_first_level_list_items: true,
shortcut_reference_link: true,
default_block_code_language: None,
}),
trim: true,
remove_trailing_dots: true,
},
)]
.into_iter()
.collect(),
),
default_comment_format: Some("go".to_owned()),
..WeaverConfig::default()
};

let renderer = MarkdownRenderer::try_new(&config)?;
let markdown = r##"In some cases a [URL] may refer to an [IP](http://ip.com) and/or port directly,
The file \\[extension\\] extracted \\[from] the `url.full`, excluding the leading dot."##;
let html = renderer.render(markdown, "go")?;
assert_eq!(
html,
r##"In some cases a [URL] may refer to an [IP] and/or port directly,
The file \[extension\] extracted \[from] the `url.full`, excluding the leading dot.

[IP]: http://ip.com"##
);

let config = WeaverConfig {
comment_formats: Some(
vec![(
"go".to_owned(),
CommentFormat {
Fixed Show fixed Hide fixed
header: None,
prefix: Some("// ".to_owned()),
footer: None,
format: RenderFormat::Markdown(MarkdownRenderOptions {
escape_backslashes: false,
escape_square_brackets: true,
indent_first_level_list_items: true,
shortcut_reference_link: true,
default_block_code_language: None,
}),
trim: true,
remove_trailing_dots: true,
},
)]
.into_iter()
.collect(),
),
default_comment_format: Some("go".to_owned()),
..WeaverConfig::default()
};

let renderer = MarkdownRenderer::try_new(&config)?;
let markdown = r##"In some cases a [URL] may refer to an [IP](http://ip.com) and/or port directly,
The file \[extension\] extracted \[from] the `url.full`, excluding the leading dot."##;
let html = renderer.render(markdown, "go")?;
assert_eq!(
html,
r##"In some cases a \[URL\] may refer to an [IP] and/or port directly,
The file \[extension\] extracted \[from\] the `url.full`, excluding the leading dot.

[IP]: http://ip.com"##
);

Ok(())
}
}
1 change: 1 addition & 0 deletions docs/weaver-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ comment_formats: # optional

# The following fields are enabled only when format is set to 'markdown'
escape_backslashes: <bool> # Whether to escape backslashes in the markdown (default: false).
escape_square_brackets: <bool> # Whether to escape square brackets in markdown (default: false).
shortcut_reference_links: <bool> # Use this to convert inlined links into shortcut reference links, similar to those in Go documentation (default: false).
indent_first_level_list_items: <bool> # Whether to indent the first level of list items in the markdown (default: false).
default_block_code_language: <string> # The default language for block code snippets (default: "").
Expand Down
Loading