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
Composable helpers #62
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8b1c59
Remove highlight.js integration
simonihmig 3b5ec22
Create helpers and utility functions
simonihmig 739d699
Add dynamic helper
simonihmig 0374bcb
Removed code-snippet component
simonihmig 9ff24ea
Add helper test
simonihmig c8a9c0e
Update Readme, adding helper and removing component docs
simonihmig 1f73426
Add getCodeSnippet function for JS usage
simonihmig d52e51c
Replace `let` helper with `with` helper, to fix Ember 2.18 tests
simonihmig 0c3905d
Cleanup dynamic-code-snippet
simonihmig a23086e
Add component example to Readme
simonihmig 7694a5d
Adapt returned language to prism.js supported languages
simonihmig File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
extends: 'recommended' | ||
extends: 'recommended', | ||
rules: { | ||
'block-indentation': false, | ||
'no-html-comments': false | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default function getExtension(name) { | ||
let m = /\.(\w+)$/i.exec(name); | ||
return m ? m[1].toLowerCase() : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import snippets from 'ember-code-snippet/snippets'; | ||
import getLanguage from './language'; | ||
import getExtension from './extension'; | ||
import unindentSource from '../-private/unindent'; | ||
import { assert } from '@ember/debug'; | ||
|
||
export default function getSnippet(name, unindent = true) { | ||
let source = name | ||
.split('/') | ||
.reduce((dir, name) => dir && dir[name], snippets); | ||
assert(`Code snippet with name "${name}" not found.`, source); | ||
|
||
source = source | ||
.replace(/^(\s*\n)*/, '') | ||
.replace(/\s*$/, ''); | ||
|
||
if (unindent) { | ||
source = unindentSource(source); | ||
} | ||
|
||
let language = getLanguage(name); | ||
let extension = getExtension(name); | ||
|
||
return { | ||
source, | ||
language, | ||
extension | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import getExtension from './extension'; | ||
|
||
export default function getLanguage(name) { | ||
let ext = getExtension(name); | ||
if (ext) { | ||
switch (ext) { | ||
case 'js': | ||
return 'javascript'; | ||
case 'coffee': | ||
return 'coffeescript'; | ||
case 'hbs': | ||
return 'htmlbars'; | ||
case 'css': | ||
return 'css'; | ||
case 'scss': | ||
return 'scss'; | ||
case 'less': | ||
return 'less'; | ||
case 'emblem': | ||
return 'emblem'; | ||
case 'ts': | ||
return 'typescript'; | ||
default: | ||
return ext; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function unindent(src) { | ||
let match, min, lines = src.split("\n").filter(l => l !== ''); | ||
for (let i = 0; i < lines.length; i++) { | ||
match = /^[ \t]*/.exec(lines[i]); | ||
if (match && (typeof min === 'undefined' || min > match[0].length)) { | ||
min = match[0].length; | ||
} | ||
} | ||
if (typeof min !== 'undefined' && min > 0) { | ||
src = src.replace(new RegExp("^[ \t]{" + min + "}", 'gm'), ""); | ||
} | ||
return src; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import { getCodeSnippet } from 'ember-code-snippet'; | ||
|
||
export default helper(function([name], { unindent = true }) { | ||
return getCodeSnippet(name, unindent); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import getCodeSnippet from './-private/get-snippet'; | ||
|
||
export { | ||
getCodeSnippet | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-code-snippet/helpers/get-code-snippet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use an example like this?
It would show why we decided to return a POJO with the language too. This snippet can more easily generalize to a reusable component that accepts the snippet name as an argument:
Which you would use like:
Maybe we should even suggest that pattern in the README. I think most apps that will have many snippets should make a component like this that puts together their chosen syntax highlighter and get-code-snippet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and also we should explain this is the upgrade path for people who were using the current version of the addon. If they just want to keep the behavior they had, they should make their own
<CodeSnippet />
component to replace the one we removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, updated according to your suggestion!
There is one potential problem though: that example would actually not work properly with prism.js and hbs templates, as our helper returns
'htmlbars'
as the language, while prism expects'handlebars'
. But as that the language value is not standardized across highlighters, there is not so much we can do about it!?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If prism is going to be our recommended syntax highlighter, I'm fine with changing our helper to return
handlebars
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized I committed my recent Readme updates to the wrong branch. Fixed! 🤦♂️
Ok, agree. Updated that, including a test + docs.