Skip to content

Commit

Permalink
Merge pull request #48 from dfreeman/exclude-file-extension
Browse files Browse the repository at this point in the history
Allow for excluding the file extension from the snippet name
  • Loading branch information
ef4 authored Feb 16, 2018
2 parents 74918d7 + 002cff0 commit a9665e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ var app = new EmberApp({
});
```

By default, the file extension from the containing file will automatically be included in the snippet name. For instance, the example above has `BEGIN-SNIPPET my-nice-example` in the JS source, and is subsequently referenced as `"my-nice-example.js"`. To disable this behavior, use the `includeFileExtensionInSnippetNames` option:

```js
var app = new EmberApp({
includeFileExtensionInSnippetNames: false
});
```

# Syntax Highlighting Language Support

We depend on [highlight.js](http://highlightjs.org/) for syntax highlighting. It supports 176 languages. But you probably don't want to build all of those into your app.
Expand All @@ -127,7 +135,7 @@ Out of the box, we only enable:
- markdown
- handlebars
- htmlbars

If you want a different set, you can:

1. Tell ember-code-snippet not to include highlight.js automatically for you:
Expand All @@ -146,7 +154,7 @@ If you want a different set, you can:
4. Import it directly from your ember-cli-build.js file:

```js
app.import('vendor/highlight.pack.js', {
app.import('vendor/highlight.pack.js', {
using: [ { transformation: 'amd', as: 'highlight.js' } ]
});
```
Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module.exports = {
}].concat(app.options.snippetRegexes || []);
},

includeExtensions: function() {
var app = findHost(this);
return app.options.includeFileExtensionInSnippetNames !== false;
},

includeHighlightJS: function() {
var app = findHost(this);
if (typeof app.options.includeHighlightJS === 'boolean') {
Expand All @@ -51,9 +56,13 @@ module.exports = {
return fs.existsSync(path);
}));

var snippetRegexes = this.snippetRegexes();
var snippetOptions = {
snippetRegexes: this.snippetRegexes(),
includeExtensions: this.includeExtensions()
};

snippets = mergeTrees(this.snippetSearchPaths().map(function(path){
return snippetFinder(path, snippetRegexes);
return snippetFinder(path, snippetOptions);
}).concat(snippets));

snippets = flatiron(snippets, {
Expand Down
16 changes: 10 additions & 6 deletions snippet-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,30 @@ function extractSnippets(fileContent, regexes) {
}


function SnippetFinder(inputTree, snippetRegexes) {
function SnippetFinder(inputTree, options) {
if (!(this instanceof SnippetFinder)) {
return new SnippetFinder(inputTree, snippetRegexes);
return new SnippetFinder(inputTree, options);
}
this.inputTree = inputTree;
this.snippetRegexes = snippetRegexes;
this.options = options;
}

SnippetFinder.prototype = Object.create(Writer.prototype);
SnippetFinder.prototype.constructor = SnippetFinder;

SnippetFinder.prototype.write = function (readTree, destDir) {
var regexes = this.snippetRegexes;
var regexes = this.options.snippetRegexes;
var includeExtensions = this.options.includeExtensions;

return readTree(this.inputTree).then(findFiles).then(function(files){
files.forEach(function(filename){
var snippets = extractSnippets(fs.readFileSync(filename, 'utf-8'), regexes);
for (var name in snippets){
fs.writeFileSync(path.join(destDir, name)+path.extname(filename),
snippets[name]);
var destFile = path.join(destDir, name);
if (includeExtensions) {
destFile += path.extname(filename);
}
fs.writeFileSync(destFile, snippets[name]);
}
});
});
Expand Down

0 comments on commit a9665e9

Please sign in to comment.