Skip to content

Commit

Permalink
Merge pull request #1316 from Jordinateur/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Jul 30, 2024
2 parents b62c762 + 5d7acea commit 239e4c9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/transformers/replaceStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { parser as parse } from 'posthtml-parser'
import posthtmlConfig from '../posthtml/defaultConfig.js'
import defaultPostHTMLConfig from '../posthtml/defaultConfig.js'

const captureRegex = /\$\d/gi

const posthtmlPlugin = (replacements = {}) => tree => {
if (!isEmpty(replacements)) {
const regexes = Object.entries(replacements).map(([k, v]) => [new RegExp(k, 'gi'), v])
Expand All @@ -17,13 +15,11 @@ const posthtmlPlugin = (replacements = {}) => tree => {
render(tree).replace(patterns, matched => {
for (const [regex, replacement] of regexes) {
if (regex.test(matched)) {
if (captureRegex.test(replacement)) {
return matched.replace(regex, replacement)
}

return replacement
return matched.replace(regex, replacement)
}
}

return matched
}),
defaultPostHTMLConfig
)
Expand Down
1 change: 1 addition & 0 deletions test/transformers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ describe.concurrent('Transformers', () => {
expect(await replaceStrings('initial text', { '/not/': 'found' })).toBe('initial text')
expect(await replaceStrings('initial text', { 'initial': 'updated' })).toBe('updated text')
expect(await replaceStrings('initial [text]', { '(initial) \\[(text)\\]': '($2) updated' })).toBe('(text) updated')
expect(await replaceStrings('«initial» «text»', { '«(.*?)»' : '« $1 »' })).toBe('« initial » « text »')

expect(
await useTransformers('initial text', { replaceStrings: { 'initial': 'updated' } }).then(({ html }) => html)
Expand Down

0 comments on commit 239e4c9

Please sign in to comment.