-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Highlight JS (tagged) template literals #1931
Conversation
This reverts commit d4736f0.
821ad68
to
43329a9
Compare
Will be awesome to have this in Prism. Nice work! Could the
Quite a few libs have this tag separately from the |
Thanks @RunDevelopment! I already put your code to use, https://sinuous.netlify.com/examples/clock/ There seems to be one issue with the html/svg tags that have an expression in them, they have other colors. |
Found a fix I think. The issue was that the markup pattern regex was too strict w/ the included interpolations. The following is a snippet of the working code. If it's const lang = language === 'markup' ? 'template-string-markup' : language;
patternObj.inside[tokenName] = !language ? /[\s\S]+/ : {
pattern: /[\s\S]+/,
alias: 'language-' + language,
inside: Prism.languages[lang]
};
return patternObj;
}
});
const templateMarkup = Prism.languages['template-string-markup'] = Prism.languages.extend('markup', {});
templateMarkup.tag.pattern = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>])|$)|(?=[\s/>])))+)?\s*\/?/i;
Prism.languages.javascript['template-string'].push(Prism.languages.javascript['template-string'].createTemplate('string')); |
The solution is really not optimal. Instead of relaxing the HTML tag pattern we should do something similar to Markup templating (MT). I also want to do the same for all other embedded languages, so I could either adjust MT to support more than just Markup (which kind of invalidates the name) or I could write my own MT without the Markup focus. As to why relaxing the pattern isn't the best solution: A relaxed pattern will also match syntactically invalid code and are likely to break in more complex situations. Also, you relaxed just one pattern when you actually had to relax ALL patterns to make the string interpolation for other tokens as well. This is a whole lot of work and had to be done for every embedded language. |
Thanks for the reply, that makes a lot of sense. I'll have a look at MT. |
Don't worry. I'll do it. |
Looking over the PRs, one thing that occurs to me is we've got a few implementations of "highlighting one language in another." I'm going to take another pass over them individually, but I wanted to ask: is there any logic that could be shared between them, a la |
Have we? I only know all the languages which use Markup templating and the ones which embed languages literally like HTML embeds CSS & JS. Even though I'll admit that I'm guilty of copying a few lines from MT, I don't think that there is a whole lot to share. At least, not now. |
We have markdown, js template strings, & diff, which do highlighting of one language in another. They don't have to use |
What if we made the dependencies peerDependencies so you wouldn't need to include |
Sorry, that's not what I wanted to say. Diff, JS Templating, and Markup Templating don't share a lot of logic because they have different solutions for the same problem. I absolutely think that we could make a more general templating engine which would be able to make the above three obsolete. I actually tried just that in the process of making this but MT is very different from the solution I employed here. I would most likely have to adjust every language using MT, so I didn't want to include this here. |
Yeah, that sounds good to me. |
I want to cry a little: The test suite can't handle peer dependencies. |
Don't we have a syntax to load languages in the tests? We've got these folders like |
Yup, that's what I meant with "modifying the path". |
Missing changes? walkTokens is still duped.
|
Yeah, you renamed the second function, which I missed when I was looking over the code. My b. |
This PR adds support for arbitrary syntax highlighting within JS template literals via a new JS Templates language/extension.
Currently implemented are most ideas from #1930:
html`<p></p>`
div.innerHTML = `<p></p>`
css`a { color: red; }`
styled.foo`color: red;`
Component.extend`color: red;`
Since this is deprecated, I don't think we have to support it.<style jsx>{`div{color:red}`}</style>
Implementing this will likely be a bit harder because of the way JSX matches text. But this implementation won't be part of JS Templates, so it's not included in this PR.
md`# title`
gql`{ foo }`
/* HTML */`...`
Not included because I don't want to introduce Prsim-specific syntax for the purpose of highlighting.Example
This is based on #1714.