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

Custom function to collapse all whitespaces (incl newlines) into a single space #77

Closed
kaushalmodi opened this issue Apr 5, 2022 · 4 comments
Assignees
Labels
feature/request New feature or request

Comments

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Apr 5, 2022

Is your feature request related to a problem? Please describe.

I write details git commit messages. So they often have newlines and indentations. But when git-cliff is parsing those, I would like to see a 1-liner commit message truncated to 90 characters. I would like to do something like s/\s+/ /g as one would do in sed (i.e. collapse multiple spaces into a single space). But I cannot find a way to do regex based replacement using Tera.

Describe the solution you'd like

Custom git-cliff function called ws_collapse or something that reduces consecutive newlines, spaces, tabs into a single space.

Describe alternatives you've considered

So I am doing this:

commit.message
             | trim_end
             | replace(from="\n", to=" ")
             | replace(from="  ", to=" ")  | replace(from="    ", to=" ")  | replace(from="      ", to=" ")
             | replace(from="   ", to=" ") | replace(from="     ", to=" ") | replace(from="       ", to=" ")

https://github.com/kaushalmodi/ox-hugo/blob/7af7e72e139655132cfa3ca5b9152690b8eaf1fe/doc/cliff.toml#L31-L35

@kaushalmodi kaushalmodi added the feature/request New feature or request label Apr 5, 2022
@kaushalmodi
Copy link
Contributor Author

kaushalmodi commented Apr 5, 2022

If a generated regex based replacement function can be added then I would like to replace this:

replace(from="https://github.com/kaushalmodi/ox-hugo/issues/", to="Issue ")

https://github.com/kaushalmodi/ox-hugo/blob/7af7e72e139655132cfa3ca5b9152690b8eaf1fe/doc/cliff.toml#L36

with a Tera or custom git-cliff function that does something like this in sed:

s|https://github.com/.*/issues/([0-9]+)|[Issue # \1](\0)|g

@orhun
Copy link
Owner

orhun commented Apr 6, 2022

Hello!

I recently pushed 64317f2 which makes it possible to manipulate commit messages before actually parsing/templating. Maybe it can also be utilized for your use-case like this:

[git]
commit_preprocessors = [
	{ pattern = "  +", replace = " "}, # remove multiple whitespaces
    { pattern = "https://github.com/.*/issues/([0-9]+)", replace = "[Issue #${1}]"} # turn issue links into numbers
 ]

Can you try this out?

See commit_preprocessors for more information.

@kaushalmodi
Copy link
Contributor Author

Thank you! 🙇 . You can see me using this new feature in kaushalmodi/ox-hugo@cb2fad1 .

@orhun
Copy link
Owner

orhun commented Apr 6, 2022

Thank you! bow . You can see me using this new feature in kaushalmodi/ox-hugo@cb2fad1 .

Oh nice! You seem to use it very extensively, feel free to add new examples to README.md 😄

kaushalmodi added a commit to kaushalmodi/git-cliff that referenced this issue Apr 6, 2022
kaushalmodi added a commit to kaushalmodi/git-cliff that referenced this issue Apr 6, 2022
kaushalmodi added a commit to kaushalmodi/git-cliff that referenced this issue Apr 6, 2022
kaushalmodi added a commit to kaushalmodi/git-cliff that referenced this issue Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants