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

Fix string escaping in markdown table #49

Merged
merged 6 commits into from
Jul 16, 2024
Merged

Fix string escaping in markdown table #49

merged 6 commits into from
Jul 16, 2024

Conversation

frenchy64
Copy link
Contributor

@frenchy64 frenchy64 commented Jul 12, 2024

The idea behind this function is to escape newlines and pipes in particular places, but it throws a NPE because str/replace is used incorrectly. There's a way to fix the replacement map by using vector keys but I wasn't comfortable using it and has already proven error-prone.

@frenchy64 frenchy64 changed the base branch from release-1.0.1 to master July 15, 2024 20:17
@frenchy64 frenchy64 self-assigned this Jul 16, 2024
Copy link

@marioaquino marioaquino left a comment

Choose a reason for hiding this comment

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

I always learn something from your PRs. LGTM

@frenchy64 frenchy64 merged commit 427377c into master Jul 16, 2024
7 checks passed
Copy link

@yogsototh yogsototh left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +40 to +42
(-> str
(str/replace \newline \space)
(str/replace "|" "\\\\|")))
Copy link

@yogsototh yogsototh Jul 17, 2024

Choose a reason for hiding this comment

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

by changing this function I believe you are passing twice on the full string instead of taking advantage of making a single pass.

Also, it looks like the logic is changed, not sure why we previously replaced \| by \\| instead of | by \\| but this might break something elsewhere.

Edit: I jumped on this PR without reading your description first. So ok that makes sens, I still wonder if that would be possible to do this in a single pass perhaps by using a function instead of a map to be able to sneak a default value en prevent NPE.

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'm also confused by the logic. Firstly, I think the "\\|" in the previous code was wrong. It doesn't agree with the regex AFAICT.

user=> (re-matches #"(\n|\|)" "|")
["|" "|"]
user=> (re-matches #"(\n|\|)" "\\|")
nil

Secondly, yes, the double escaping is strange. The regex matches |, then the map rewrites \| to \\|. It doesn't seem internally consistent. My hunch is that this PR should be changed to escape | to \|.

Regarding perf, I agree it would be nice to do this in one pass. I think str/escape might be a better tool for the job.

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

Successfully merging this pull request may close these issues.

3 participants