How to tell JetBrains IDE that template string is JS code - node.js

I have an ES6 template string, which represents JS code. If I put <script> tags around it, JetBrains IntelliJ will do syntax highlighting and checking, but not if there are no script tags.
Is there a way to tell JetBrains IDE how to interpret the template string?
It's definitely the presence/absence of the html tag.

IDEA auto-injects HTML in JavaScript string literals/template string if there are HTML tags in it; to inject other languages, you need adding language injection manually - for example, the language can be temporary injected via Inject language or reference intention available on Alt+Enter:
You can also annotate your injection with comments to make it permanent:
//language=JS
const baz = `console.log("test")`
See https://www.jetbrains.com/help/idea/using-language-injections.html for more information

Related

JetBrains IDE how configure multi language file

There are files in the project that contain three languages: Python, HTML, JS.
Languages ​​are between special tags
<mako>
from pyasm.search import Search
</mako>
<html>
<div style="padding-left: 10px;">Hi</div>
</html>
<behavior>
var el = header_el.getElement(".spt_header_account_top");
</behavior>
How to associate these tags with required syntax in PyCharm IDE?
You can try creating the corresponding language injections: create new XML Tag injection in Settings | Editor | Language Injections, specify your tag name (behavior, for example) as Local name:, choose the desired language (JavaScript, for example) as ID:.
You can also inject languages in your tags using Inject language or reference intention available on Alt+Enter:

Inject meta tag dynamically to HTML with Express

Summary:
I'm currently migrating a website on Apache + PHP stack over to Node + Express, and would like to know what is the best way/best practice (if there is one) for dynamically injecting meta tags under the new stack.
Details:
Under the existing stack, meta tags are injected dynamically by adding PHP codes into the HTML file directly. As rendering is done on server side, the tags are properly interpreted by Facebook/Google+/whatever web crawlers.
Under the new stack, after doing some research, I've come across two options:
Use template engine like Pug (Jade) to render the HTML with locals. (It seems to be an overkill to rewrite the existing HTML with Pug's syntax though? Can Pug deal with HTML, or I've to consider other template engine like EJS? What template engine do you advise me to explore?)
Use DOM manipulation plugin like Cheerio to inject the meta tags first, before rendering begins.
Between these two options, which one will have a better performance or there is no material difference? Are there any other ways that you'd otherwise recommend? Thanks!
EJS would probably be the simplest one for that and very similar to PHP.
You can also take a look at Mustache and Handlebars for other options with minimal changes to your existing HTML.
with EJS: <html><head><%= yourMetaTags %> ...
with Mustache: <html><head>{{ yourMetaTags }} ...
with Handlebars: <html><head>{{ yourMetaTags }} ...
Also doT.js is very fast.
See:
http://www.embeddedjs.com/
https://mustache.github.io/
http://handlebarsjs.com/
http://olado.github.io/doT/
Parsing the HTML and manipulating it with a DOM API just to insert meta tags would be an overkill in my opinion.
On the other hand if all you need is to insert meta tags then you could make a simple regex substitution, using something like yourHTML.replace('<head>', '<head>'+yourMetaTags); but it could potentially get more complex over time when you need more functionality. After all, everyone has made a templating engine at some point in life.

can you set is_safe for a global in a twig extension

Twigs documentation for extensions show that it is possible to use "is_safe" with both simple_filters and and simple_functions, to prevent escaping of html tags in returned values, but I can see any examples of using is_safe with globals. Is there a way to do this?
If your global is pure HTML that needs to be rendered like HTML you could mark it as safe by using
$twig->addGlobal('my_html', new Twig_Markup($html, 'UTF-8'));
If the global is an object and returns the HTML you wrap your return value with a new Twig_Markup

How to detect page language/locale in a Chrome extension content script?

I would like my Chrome extension content script to detect the language or locale of the page's content (not the browser language/locale). I assume there is a method for this in the Chrome extension API, but should I be using standard Javascript libraries instead?
This is the Chrome extension method: chrome.tabs.detectLanguage(...). From the description:
Detects the primary language of the content in a tab.
You could use standard javascript DOM functions to look for a lang attribute on the root html element (or possibly the body element). But keep in mind that a page might not be entirely in one language, so different elements of the page may be marked up with different lang attributes.
Also, if you want to support xhtml, I'd suggest looking the xml:lang attribute as well.

Inner tags not parsing with Expression Engine 2 custom plugin

I've written a simple plugin that basically loops through a bunch of entries. The plugin is used to only display entries that contain a feature image, amongst some other minor logical conditions. The use of my tag looks something like this:
{exp:myentries:withimages channel="mychannel"}
<!-- This works fine -->
<h1>{title}</h1>
<!-- But nested exp:... tags don't seem to parse? -->
<p>{exp:ce_img:single src="feature_image"}</p>
{/exp:myentries:withimages}
I am calling
return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $data);
from my custom EE plugin, the data is there, but only the nested {exp:... tags don't want to parse.
I've followed the online User Guide for creating plugins as close as I possibly could, but I need some help getting the other tags to parse? If I simply output {feature_image}, the field renders the src value for the image as expected.
Can anyone possibly shed some light on what I'm doing wrong?
You want to put parse="inward" parameter in your {exp:myentries:withimages tag, otherwise the template will try to parse the exp:ce_img call before {feature_image} is set.
{exp:myentries:withimages parse="inward" channel="mychannel"}
The parse="inward" will tell EE to run this tag first before parsing any other tags within the tag pair.
There are two important typos in your code.
{exp:ce_img:singe src="feature_image"}
Should read:
{exp:ce_img:single src="{feature_image}"}
If those differences are actually present in your template, then that would be it I believe.

Resources