Skip to content

Commit

Permalink
Merge pull request #999 from chrislopresto/escape-curly-braces
Browse files Browse the repository at this point in the history
Update AST transform to replace curly braces
  • Loading branch information
lukemelia authored Sep 27, 2024
2 parents ecb01f8 + ddabbba commit 532984b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ You should include `ember-freestyle` in your `devDependencies` so that apps
using your addon will not include Ember Freestyle CSS and JavaScript in their
production builds.
### In V2 addons
You will need to configure babel to run ember-freestyle's AST Transform in order to capture
source code from Freestyle::Usage example blocks. For example:
```js
// babel.config.mjs
import FreestyleTransform from 'ember-freestyle/lib/ast-transform.js';

export default {
plugins: [
// ...
[
'babel-plugin-ember-template-compilation',
{
targetFormat: 'hbs',
transforms: [FreestyleTransform],
},
],
// ...
],
};

```
## Contributing
See the [Contributing](CONTRIBUTING.md) guide for details.
Expand Down
6 changes: 5 additions & 1 deletion addon/components/freestyle-source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ export default class FreestyleSource extends Component<Signature> {
if (!this.args.source) {
return '';
}
let result = this.args.source.replace(/^(\s*\n)*/, '').replace(/\s*$/, '');
let result = this.args.source
.replace(/^(\s*\n)*/, '')
.replace(/\s*$/, '')
.replace(/&#123;/g, '{')
.replace(/&#125;/g, '}');

if (this.args.isDynamic) {
result = this.dynamafy(result);
Expand Down
6 changes: 5 additions & 1 deletion lib/ast-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function cleanupNamedBlocksPolyfillSyntax(sourceString) {
return sourceString;
}

function escapeCurlyBraces(s) {
return s.replace(/\{/g, '&#123;').replace(/\}/g, '&#125;');
}

function extractSource(nodes, contents) {
// nodes should be a contiguous collection of ast nodes
let startNode = nodes[0];
Expand All @@ -57,7 +61,7 @@ function extractSource(nodes, contents) {
let lines = contents.split('\n').slice(start.line - 1, end.line);
lines[0] = new Array(start.column).join(' ') + lines[0].slice(start.column);
lines[lines.length - 1] = lines[lines.length - 1].slice(0, end.column);
return stripIndent(lines.join('\n'));
return escapeCurlyBraces(stripIndent(lines.join('\n')));
}

module.exports = function ({ contents, syntax }) {
Expand Down

0 comments on commit 532984b

Please sign in to comment.