diff --git a/docs/usage/templates.md b/docs/usage/templates.md index 38a963f571..8aa2a282be 100644 --- a/docs/usage/templates.md +++ b/docs/usage/templates.md @@ -49,6 +49,16 @@ In the example above `baseDir` is the string you want to transform into a valid Read the [MDN Web Docs, encodeURIComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) to learn more. +### decodeURIComponent + +If you want to decode a percent-encoded string, use the built-in function `decodeURIComponent` like this: + +`{{{decodeURIComponent depName}}}` + +In the example above `depName` is the string you want to decode. + +Read the [MDN Web Docs, decodeURIComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) to learn more. + ### replace The `replace` helper replaces _all_ found strings with the replacement string. diff --git a/lib/util/template/index.spec.ts b/lib/util/template/index.spec.ts index a6d79c2d80..a658988362 100644 --- a/lib/util/template/index.spec.ts +++ b/lib/util/template/index.spec.ts @@ -158,4 +158,22 @@ describe('util/template/index', () => { expect(template.containsTemplates('{{body}}', ['logJSON'])).toBeFalse(); }); }); + + describe('percent encoding', () => { + it('encodes values', () => { + const output = template.compile( + '{{{encodeURIComponent "@fsouza/prettierd"}}}', + undefined as never + ); + expect(output).toBe('%40fsouza%2Fprettierd'); + }); + + it('decodes values', () => { + const output = template.compile( + '{{{decodeURIComponent "%40fsouza/prettierd"}}}', + undefined as never + ); + expect(output).toBe('@fsouza/prettierd'); + }); + }); }); diff --git a/lib/util/template/index.ts b/lib/util/template/index.ts index 8e9f67ab26..4ff1ae2316 100644 --- a/lib/util/template/index.ts +++ b/lib/util/template/index.ts @@ -4,6 +4,7 @@ import { GlobalConfig } from '../../config/global'; import { logger } from '../../logger'; handlebars.registerHelper('encodeURIComponent', encodeURIComponent); +handlebars.registerHelper('decodeURIComponent', decodeURIComponent); handlebars.registerHelper('stringToPrettyJSON', (input: string): string => JSON.stringify(JSON.parse(input), null, 2)