mikepenz / release-changelog-builder-action

A GitHub action that builds your release notes / changelog fast, easy and exactly the way you want.
https://blog.mikepenz.dev
Apache License 2.0
689 stars 99 forks source link

[Question]: How to properly extract text from BODY? #1285

Closed Yakutoc closed 7 months ago

Yakutoc commented 7 months ago

Hello, could you please help me to understand how to do it right?

There is this body in my pr

## Release Notes

### Dropdown

some text

### What/why Changed

some text

some text

And I would like to take only part that is between ## Release Notes and ### What/why Changed

In the documentation I've found information about custom_placeholders

This is part of my configuration

{  
    "pr_template": "#{{DESC}}\n\n#{{TITLE}} (#{{URL}})\n\n",  
    "custom_placeholders": [  
      {  
        "name": "DESC",  
        "source": "BODY",  
        "transformer": {  
            "pattern": "(?:## Release Notes)(.*)(?:### What\/why Changed)",  
            "flags": "s",  
            "target": "$1"
        }  
      }  
    ]  
}

I don’t understand what I’m doing wrong

## Components

  ### Dropdown

  - some text
  - some text
  - some text

some text
  <!-- GITHUB_RELEASE PR BODY: canary-version -->
  <details>
    <summary>📦 Published PR as canary version: <code>Canary Versions</code></summary>
    <br />

    :sparkles: Test out this PR locally via:

  </details>
  <!-- GITHUB_RELEASE PR BODY: canary-version -->

  feat: add Dropdown (url)

I expect to see this result

## Uncategorized
### Dropdown

some text

pr_title (#222)

I would be grateful for your help!

mikepenz commented 7 months ago

The custom_placeholders use javascripts replace method.

As far as I can observe, I suppose you would expect it to use match instead? (debugger preview with match in use, which would do what you expect)

Screenshot 2023-11-30 at 15 09 11
Yakutoc commented 7 months ago

you would expect it to use match instead

Yep =)

I expected that 1$ is the result of a regular expression and that it will replace its BODY.

Is it even possible to do this?

mikepenz commented 7 months ago

Unfortunately the replace function of JavaScript works differently

You can best test its behavior here for example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

const paragraph = "## Release Notes\n\n### Dropdown\n\nsome text\n\n### What/why Changed\n\nsome text\n\nsome text";

const regex = /(?:## Release Notes)(.*)(?:### What\/why Changed)/s;

// this is the behaviour within the action
console.log("FIRST EXAMPLE");
console.log(paragraph.replace(regex, '$1'));

// this would be the result of match (which returns an array, and has no target)
console.log("SECOND EXAMPLE");
console.log(paragraph.match(regex));

// the index 1 result of the match array
console.log("THRID EXAMPLE");
console.log(paragraph.match(regex)[1]);
mikepenz commented 7 months ago

I would love to find a better solution with javascript which would work like match, and has the support for the "target" - as that would most of the times be the most convenient way of doing things.

Yakutoc commented 7 months ago

Thanks for your answers and help.

I decided to do it differently.

I'll just cut out everything that comes after "### What/why сhanged"


"custom_placeholders": [  
  {  
    "name": "DESC",  
    "source": "BODY",  
    "transformer": {  
        "pattern": "(?:\n### What\/why сhanged)(.*)",  
        "flags": "s",  
        "target": " "  
    }  
  }  
]