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

Dollar sign in file contents renders RegExp matches #7

Closed
msimmer opened this issue Apr 9, 2019 · 2 comments · Fixed by #8
Closed

Dollar sign in file contents renders RegExp matches #7

msimmer opened this issue Apr 9, 2019 · 2 comments · Fixed by #8
Labels

Comments

@msimmer
Copy link
Contributor

msimmer commented Apr 9, 2019

Dollar signs in the the file contents seem to require escaping, but seems like unintended behaviour since matches are dropped into the rendered string:

const renderLayouts = require('layouts')

const file = {
  contents: Buffer.from('<div>A $100 layout!!!</div>'),
  layout: 'one',
}

const layoutCollection = {
  one: { contents: Buffer.from('{% body %}') },
}

const res = renderLayouts(file, layoutCollection)
console.log(res.contents.toString())

yields

<div>A body00 layout!!!</div>

Escaping the dollar sign works:

const renderLayouts = require('layouts')

const file = {
  contents: Buffer.from('<div>A $$100 layout!!!</div>'),
  layout: 'one',
}

const layoutCollection = {
  one: { contents: Buffer.from('{% body %}') },
}

const res = renderLayouts(file, layoutCollection)
console.log(res.contents.toString())

Although the issue is compounded when nesting layouts, and requires multiple escape characters:

const renderLayouts = require('layouts')

const file = {
  contents: Buffer.from('<div>Wrap me with $$$$100 a layout!!!</div>'),
  layout: 'one',
}

const layoutCollection = {
  one: { contents: Buffer.from('one before\n{% body %}\none after'), layout: 'two' },
  two: { contents: Buffer.from('two before\n{% body %}\ntwo after') },
}

const res = renderLayouts(file, layoutCollection)
console.log(res.contents.toString())

will yield:

two before
one before
<div>Wrap me with $100 a layout!!!</div>
one after
two after

Is there a recommended way of dealing with this? If it's a bug I might be able to put together a PR


NodeJS v10.15.3
layouts@3.0.1

@doowb
Copy link
Owner

doowb commented Apr 9, 2019

Hi @msimmer, thanks for finding this issue!

If it's a bug I might be able to put together a PR

That would be awesome! I think you can just change the following:

str = Buffer.from(layoutString.replace(regex, fileString));

to

str = Buffer.from(layoutString.replace(regex, () => fileString)); 

Will you also add some unit tests? I like your examples above so tests like those would be helpful.

@doowb doowb added the bug label Apr 9, 2019
@msimmer
Copy link
Contributor Author

msimmer commented Apr 10, 2019

Hey @doowb, sounds good! I'll have something done up in the next day or so.

Will you also add some unit tests? I like your examples above so tests like those would be helpful.

Yep, no problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants