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

Building a HB template with $CLASS$ text fails #19

Closed
jonschlinkert opened this issue May 13, 2015 · 12 comments
Closed

Building a HB template with $CLASS$ text fails #19

jonschlinkert opened this issue May 13, 2015 · 12 comments

Comments

@jonschlinkert
Copy link
Member

moved from assemble/assemble#580 (comment)

@knight-dj-et
Copy link

https://github.com/assemble/grunt-assemble/blob/master/tasks/assemble.js#L700

1 line change

change layout.replace(assemble.engine.bodyRegex, body); to layout.replace(assemble.engine.bodyRegex, body.replace("$", "$$$$"));

@doowb
Copy link
Member

doowb commented May 13, 2015

@jonschlinkert's solution with .split().join() is probably a better way to go. The other way is to use function () { return body; } instead of body, but I think Jon's benchmarked the split/join and it's usually faster, right?

@knight-dj-et if you'd like to do a PR, that'd be awesome!

@jonschlinkert
Copy link
Member Author

I think Jon's benchmarked the split/join and it's usually faster, right?

well, it's not about faster here. it's only useful if we want to replace multiple occurrences of body, which I 'm almost positive we were doing - at least at one point. I remember the pr that added that. If not, then we can't do split.

@jonschlinkert
Copy link
Member Author

but yes, it's also considerably faster

@jonschlinkert
Copy link
Member Author

wait, why would we do layout.replace(assemble.engine.bodyRegex, body.replace("$", "$$$$"));. that will arbitrarily replace the first $ character anywhere in the body string with $$$$.

@knight-dj-et
Copy link

Oh. I suppose I did it too broadly. It should replace any $ followed by a $, ', `, & or number with "$$" (which has to be passed as "$$$$" since this is using replace which will still treat the $ as special characters)

@jonschlinkert
Copy link
Member Author

we would need to do something like this:

var injectBody = function(layout, body) {
  body = body.split('$').join('___ESC_DOLLAR__');
  var res = layout.replace(assemble.engine.bodyRegex, body);
  return res.split('___ESC_DOLLAR__').join('$');
};

edited...

@jonschlinkert
Copy link
Member Author

the other alternative is to just do split/join on the actual body tag. IMO that's a better way to go. since : 1) a user would expect any tag to yield the same result if it was used more than once in a string, 2) it would only do the split/join operation once, whereas the escaping approach ends up doing it three times (including .replace())

@doowb
Copy link
Member

doowb commented May 13, 2015

All we need to do is layout.replace(assemble.engine.bodyRegex, function () { return body; });

@jonschlinkert
Copy link
Member Author

^ what he said lol

@jonschlinkert
Copy link
Member Author

@doowb you said that earlier and I missed the function part of the replacement...

@doowb
Copy link
Member

doowb commented May 13, 2015

fixed in grunt-assemble@0.3.1

@doowb doowb closed this as completed May 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants