Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
babel-plugin-transform-syntax-highlight
Advanced tools
Perform syntax highlighting of string and template literals during Babel compilation, rather than at runtime
🚧 🚧 EXPERIMENTAL! 🚧 🚧
Performs syntax highlighting of string and template literals during Babel compilation, rather than at runtime.
Transforms a special function call into one of the following:
In both cases, you can provide delimited placeholders within the code string that can be interpreted at runtime, when you invoke the function or create a React element from the component.
require
or import
'babel-plugin-transform-syntax-highlight/highlight', or whatever you've specified as packageName
in your Babel options.
Now you have an object with react
and html
functions.
Those functions accept two arguments: options
and code
.
(options
is optional.)
The react
function returns a React component, whose props can be used for interpolation.
The html
function returns a function that accepts a props
argument, which can be used for interpolation.
React mode example:
// Input:
const highlight = require('babel-plugin-transform-syntax-highlight/highlight');
// Or import highlight from 'babel-plugin-transform-syntax-highlight/highlight';
const SomeCode = highlight.react({
language: 'javascript',
highlight: 'prism'
}, `
const foo = "bar";
const bar = "{# props.bar #}";
`);
// Output
const SomeCode = function SomeCode(props) {
return (
<pre>
<code className="language-javascript">
<span className="token keyword">const</span> foo{' '}
<span className="token operator">=</span>{' '}
<span className="token string">"bar"</span>
<span className="token punctuation">;</span>{'\n'}
<span className="token keyword">const</span> bar{' '}
<span className="token operator">=</span>{' '}
<span className="token string">"{props.bar}"</span>
<span className="token punctuation">;</span>
</code>
</pre>
);
}
// Usage
<SomeCode bar="something special" />;
HTML mode example:
// Input:
const highlight = require('babel-plugin-transform-syntax-highlight/highlight');
// Or import highlight from 'babel-plugin-transform-syntax-highlight/highlight';
const someCode = highlight.html(`
const foo = 'bar';
const bar = '{# props.bar #}';
`);
// Output
const someCode = function(props) {
return `<pre><code class="hljs"><span class="hljs-keyword">const</span> foo = <span class="hljs-string">'bar'</span>;
<span class="hljs-keyword">const</span> bar = <span class="hljs-string">'${props.bar}'</span>;</code></pre>`;
}
// Usage
myDiv.innerHTML = someCode({ bar: 'something special' });
When you add the plugin to your Babel configuration, you can pass these options:
string
- Default: 'babel-plugin-transform-syntax-highlight/highlight'.
The name of the package that you will require
or import
.'highlightjs' | 'prism'
- Default: 'highlight'
.
Choose the highlighter that you'd like to use, either highlight.js or Prism.
Make sure you include CSS for the highlighter in your page.[string, string]
- Default: ['{#', '#}']
.
Delimiters for marking placeholders in the code that can later be replaced at runtime, either by props (in React mode) or function arguments (in HTML mode).
If you don't use the default, make sure to choose delimiters that will not clash with the language of the code to be highlighted.
And do not use <
and >
, which will be escaped by the syntax highlighter.For example:
// .babelrc
{
"plugins": [
"transform-syntax-highlighting",
{
"packageName": "babel-highlighting",
"highlight": "prism",
"delimiters": ["$$", "$$"]
}
]
}
When you invoke the html
or react
functions, the first argument can be an options object.
That object can include the following:
string
- A language identifier that your highlighter of choice will understand.
If no value is provided, highlight.js will try to guess the language. Prism will return an unhighlighted code.FAQs
Perform syntax highlighting of string and template literals during Babel compilation, rather than at runtime
We found that babel-plugin-transform-syntax-highlight demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.