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

Source to source formatting issue #520

Closed
luveti opened this issue Jan 18, 2025 · 4 comments
Closed

Source to source formatting issue #520

luveti opened this issue Jan 18, 2025 · 4 comments

Comments

@luveti
Copy link

luveti commented Jan 18, 2025

I'm experiencing a weird escaping issue and a underscore to dash conversion when parsing then formatting a document:

fn main() -> Result<()> {
    let before = r#"## 1. Foo
**Test**: __________________________
__________________________
"#;
    let options = comrak::Options::default();
    let arena = comrak::Arena::new();
    let root = comrak::parse_document(&arena, &before, &options);
    let mut out = Vec::new();
    comrak::format_commonmark(root, &options, &mut out)?;
    let after = std::str::from_utf8(&out)?;
    println!("Before:");
    println!("{before}");
    println!("After:");
    println!("{after}");
    Ok(())
}

Before:

## 1. Foo
**Test**: __________________________
__________________________

After:

## 1\. Foo

**Test**: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

-----

Is there an option that would disable the addition of these backslashes?

This is happening on version 0.33.

@luveti luveti changed the title Backslash escaping issue in format_commonmark Source to source formatting issue Jan 18, 2025
@kivikakk
Copy link
Owner

There isn't, currently; see #509 for a similar issue opened on this topic recently, and #510 for why it becomes difficult.

@luveti
Copy link
Author

luveti commented Jan 19, 2025

Ahh, thanks for clearing that up!

My use case is replacing the content of specific nodes in a parsed document then serializing it back to disk.

I suppose an alternative is to use Ast.sourcepos to replace content in the unparsed source.

@luveti luveti closed this as completed Jan 19, 2025
@kivikakk
Copy link
Owner

I understand! I use it for the same function in a few places, but for my part just accept there'll be some "needlessly" escaped things.

Sourcepos is a viable option, but please be aware there are some bugs, especially with inline elements; see the sourcepos issue tag for more details.

I'm having some ideas brewing for a (kind of) quick and (kind of) easy way to reduce the backslashes without running into the above issues; I'll post back here in a day or two if I get anywhere with it!

@charlottia
Copy link
Collaborator

I've tried putting together an option for this at #523 — if you're still interested, could you give that branch a try and let me know if it works OK?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants