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 render option to wrap escaped chars in span #367

Merged
merged 5 commits into from
Mar 28, 2024

Conversation

digitalmoksha
Copy link
Collaborator

@digitalmoksha digitalmoksha commented Mar 26, 2024

CommonMark supports escaping characters, which short-circuits normal markdown processing for that character.

But normally this information is lost in the final html. Post-processing filters sometimes need this information in order to keep special references from being recognized, such as @user. Ideally \@user should keep the reference from being recognized, but the final html only has @user.

This PR adds an option --escaped-char-spans that will wrap escaped characters in a <span data-escaped-char>. Post-processing filters can then use this when parsing for special references.

This allows post-processing filters to know if a
character was originally escaped or not. Normally
that information is lost.
@digitalmoksha digitalmoksha self-assigned this Mar 26, 2024
@digitalmoksha digitalmoksha marked this pull request as draft March 26, 2024 16:58
Copy link
Owner

@kivikakk kivikakk left a comment

Choose a reason for hiding this comment

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

Very very good, and super pleased to see things like sourcepos 100% correct. Thanks so much!

@kivikakk
Copy link
Owner

As an aside, I can't believe I didn't think of doing something like this back when I was at GitHub and contending with the exact same problem (then in cmark-gfm) — despite very much wanting to make backslash-escaping of @mentions a possibility, I simply couldn't think of a solution, and so \@kivikakk still renders as @kivikakk today.

@digitalmoksha
Copy link
Collaborator Author

Thanks! Yeah finally figured out a solution a couple years ago for GitLab, using pre-processing and post-processing filters, but it's a hack. So I'm quite stoked to be able to add this support natively!

I just need to add some tests...

Comment on lines +34 to +37
println!("Original Input:");
println!("==============================");
println!("{}", original_input);
println!("==============================");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kivikakk added this so it's easier to see the original input that caused the error, rather than referring to the actual test.

with cargo run --example update-readme
@digitalmoksha digitalmoksha marked this pull request as ready for review March 28, 2024 18:49
@digitalmoksha digitalmoksha merged commit 01c56d9 into kivikakk:main Mar 28, 2024
12 checks passed
@digitalmoksha digitalmoksha deleted the bw-escape-processing branch March 28, 2024 19:01
@gjtorikian
Copy link
Collaborator

This is fantastic!

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

Successfully merging this pull request may close these issues.

3 participants