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

Markdown output needs to escape some characters #84

Closed
alerque opened this issue Apr 23, 2022 · 5 comments
Closed

Markdown output needs to escape some characters #84

alerque opened this issue Apr 23, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@alerque
Copy link
Contributor

alerque commented Apr 23, 2022

I just tried to use git-cliff to update the changelog for a project (luacheck) that happens to have some commits that mention the LUA builtin global _ENV — without enclosing it in backticks. While it might have been better if I'd thought about this sooner and formatted it as code, generating a Markdown changlog is now a problem. I have to hand-edit the changelog to either escape the underscore (\_ENV) or add backticks (`_ENV`).

Given that it is also fair game to include things with the expectation that they will be Markdown I'm not sure how fixable this is. One idea I had is it might be possible to use a parser such as pulldown-cmark to check for unclosed markup and escape anything that causes tags to be left open ended. It might also be possible to just round trip the commit messages though that or another parser and get a variant that is "Markdown safe".

Note for example round tripping each commit message to pandoc would fix this problem:

$ echo "foo _ENV bar" | pandoc -t commonmark
foo \_ENV bar
$ echo "foo _ENV_ bar" | pandoc -t commonmark
foo *ENV* bar
@alerque alerque added the bug Something isn't working label Apr 23, 2022
@kaushalmodi
Copy link
Contributor

kaushalmodi commented Apr 24, 2022

Check out the new commit_preprocessors feature (see the README for examples).

You can use that to escape special Markdown characters (I use git-cliff to generate changelog files in Org mode. So I am glad that Markdown specific treatment is not baked into it!). Here's an untested example:

  commit_preprocessors = [
    { pattern = "[_]", replace = "\\${0}"}
  ]

If you wish, you can add other Markdown markup chars to the pattern like so: "[_*`]".

@orhun
Copy link
Owner

orhun commented Apr 28, 2022

As @kaushalmodi said, using commit_preprocessors is the way to go for this. I'm not planning to add validation checks for different output formats including markdown.

@orhun orhun closed this as completed Apr 28, 2022
@alerque
Copy link
Contributor Author

alerque commented Apr 28, 2022

It seems to be the commit_preprocessors is a bit of a misnomer. Having a regex replacement system available is handy for many uses cases, but it does not cover this use case at all. The naive example shown will blow up in all sorts of circumstances, including the simple MWE shown for the simple reason that in Markdown whether _ has any meaning is highly context sensitive and very difficult to get right with regular expressions alone.

And actual preprocessor in the form of a callback/hook that shipped the text out to some other command and read the results back would fix this. That would also enable much more advanced use cases.

@alerque
Copy link
Contributor Author

alerque commented Apr 28, 2022

By the way, not adding Markdown specific parsing makes some sense (I hadn't even thought of that when I wrote the issue and probably many other will assume Markdown as well especially considering the default changelog template is full on Markdown) but having a hook so that a full on preprocessor could be used would open the door to many more use cases.

@orhun
Copy link
Owner

orhun commented Apr 28, 2022

@alerque do you suggest having something like this?

commit_preprocessors = [
    { pattern = '...', command = "pandoc -t commonmark" },
]

So that given command will be run and the result will be assigned to the commits that are matched with pattern.

If you can give some examples and further clarify your requirements I will be more than happy to implement it. Also, moving this to a separate issue would be nice 🐻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants