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

Add support for styled-components syntax highlighting #1714

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"javascript": {
"title": "JavaScript",
"require": "clike",
"peerDependencies": "markup",
"peerDependencies": ["markup", "css"],
"alias": "js",
"option": "default"
},
Expand Down
20 changes: 20 additions & 0 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ Prism.languages.insertBefore('javascript', 'string', {
}
});

Prism.languages.insertBefore("javascript", "template-string", {
"styled-components-template-string": {
pattern: /(styled(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))*)*|css|createGlobalStyle|keyframes)`(?:\$\{[^}]+\}|\\\\|\\?[^\\])*?`/,
lookbehind: true,
greedy: true,
inside: {
interpolation: {
pattern: /\$\{[^}]+\}/,
inside: {
"interpolation-punctuation": {
pattern: /^\$\{|\}$/,
alias: "punctuation"
},
rest: Prism.languages.javascript
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
});

if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', {
'script': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,26 @@ Prism.languages.insertBefore('javascript', 'string', {
}
});

Prism.languages.insertBefore("javascript", "template-string", {
"styled-components-template-string": {
pattern: /(styled(\.\w+|\([^\)]*\))(\.\w+(\([^\)]*\))*)*|css|createGlobalStyle|keyframes)`(?:\$\{[^}]+\}|\\\\|\\?[^\\])*?`/,
lookbehind: true,
greedy: true,
inside: {
interpolation: {
pattern: /\$\{[^}]+\}/,
inside: {
"interpolation-punctuation": {
pattern: /^\$\{|\}$/,
alias: "punctuation"
},
rest: Prism.languages.javascript
}
}
}
}
});

if (Prism.languages.markup) {
Prism.languages.insertBefore('markup', 'tag', {
'script': {
Expand Down
69 changes: 69 additions & 0 deletions tests/languages/javascript/styled-components_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
styled.button`
color: blue;
background: ${props => props.background || 'none'};
`
styled(Button)`
background: red;
`
createGlobalStyle`
html {
color: blue;
}
`
css`
color: blue;
`
keyframes`
0% {
color: blue;
}
100% {
color: red;
}
`

----------------------------------------------------

[
"styled",
["punctuation", "."],
"button",
["styled-components-template-string", [
"`\n\tcolor: blue;\n\tbackground: ",
["interpolation", [
["interpolation-punctuation", "${"],
["parameter", ["props"]],
["operator", "=>"],
" props",
["punctuation", "."],
"background ",
["operator", "||"],
["string", "'none'"],
["interpolation-punctuation", "}"]
]],
";\n`"
]],
["function", "styled"],
["punctuation", "("],
"Button",
["punctuation", ")"],
["styled-components-template-string", [
"`\n\tbackground: red;\n`"
]],
"\ncreateGlobalStyle",
["styled-components-template-string", [
"`\n\thtml {\n\t\tcolor: blue;\n\t}\n`"
]],
"\ncss",
["styled-components-template-string", [
"`\n\tcolor: blue;\n`"
]],
"\nkeyframes",
["styled-components-template-string", [
"`\n\t0% {\n\t\tcolor: blue;\n\t}\n\t100% {\n\t\tcolor: red;\n\t}\n`"
]]
]

----------------------------------------------------

Checks for styled-components and its APIs.