Skip to content

Commit 97f39a3

Browse files
authored
feat: support advanced substitutions in replacers (#1517)
1 parent 3a7fb5c commit 97f39a3

10 files changed

Lines changed: 948 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,13 @@ replacers:
376376
replace: 'https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-$1-$2'
377377
- search: 'myname'
378378
replace: 'My Name'
379+
- search: '/- ([a-z])/g'
380+
replace: '- \u$1' # Uppercase the first letter of each changelog entry
379381
```
380382

383+
`search` will be parsed to a RegExp, and `replace` supports substitution in the
384+
[same flavour VSCode does](https://code.visualstudio.com/docs/editing/codebasics#_case-changing-in-regex-replace).
385+
381386
## Autolabeler
382387

383388
You can add automatically a label into a pull request, with the `autolabeler`

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/actions/drafter/run.js

Lines changed: 320 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
type NestedTemplate,
3+
renderTemplate,
4+
type Template,
5+
} from './render-template'

src/actions/drafter/lib/build-release-payload/render-template.ts renamed to src/actions/drafter/lib/build-release-payload/render-template/render-template.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
import type { ParsedConfig } from '../../config'
1+
import type { ParsedConfig } from 'src/actions/drafter/config'
2+
import { parseReplaceString } from './util'
3+
4+
type TemplateReplacer = NonNullable<ParsedConfig['replacers']>[number]
5+
6+
const getReplaceMatches = (args: unknown[]): string[] => {
7+
const lastArg = args[args.length - 1]
8+
const hasGroups = typeof lastArg === 'object' && lastArg !== null
9+
const matchCount = args.length - (hasGroups ? 3 : 2)
10+
11+
return args.slice(0, matchCount) as string[]
12+
}
13+
14+
const applyReplacer = (input: string, replacer: TemplateReplacer): string => {
15+
const replacePattern = parseReplaceString(replacer.replace)
16+
17+
return input.replace(replacer.search, (...args) => {
18+
const matches = getReplaceMatches(args)
19+
return replacePattern.buildReplaceString(matches)
20+
})
21+
}
222

323
export type Template = {
424
[key: `$${Uppercase<string>}`]:
@@ -52,8 +72,8 @@ export const renderTemplate = (params: {
5272
})
5373

5474
if (replacers) {
55-
for (const { search, replace } of replacers) {
56-
input = input.replace(search, replace)
75+
for (const replacer of replacers) {
76+
input = applyReplacer(input, replacer)
5777
}
5878
}
5979

0 commit comments

Comments
 (0)