
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@loopmode/codeblock
Advanced tools
A react component for code highlighting - based on prismjs and prepared for code-splitting.
import() statements (e.g. create-react-app/webpack/parcel)yarn add @loopmode/codeblock
# or
npm install --save @loopmode/codeblock
The default prism theme is okaidia, the default language is javascript.
Use the theme and language props to override.
Currently, you have to decide on a single theme because the global prism stylesheets interfere with each other. See local themed component for how to specify a theme.
You can use the component much like you would use the <pre> tag. Pass the contents as regular react children.
import React from 'react';
import Codeblock from '@loopmode/codeblock'
export default function InlineContent() {
return (
<Codeblock>{`class Foo { /* Yay! Code! */ }`}</Codeblock>
);
}
If you specify src, the content will be loaded from that URL and the children prop is ignored.
import React from 'react';
import Codeblock from '@loopmode/codeblock'
export default function ExternalContent() {
return (
<Codeblock
children="Nope. Ignored."
src="https://raw.githubusercontent.com/loopmode/codeblock/master/packages/codeblock/src/Codeblock.js"
/>
);
}
Currently, each prism theme stylesheet is loaded globally. When you load a second theme, the first one is still loaded and the styles interfere. For this reason, you should decide for one theme and stick with it.
While you could just set the theme prop manually over and over, you should create a local component that sets the theme prop once and import that one across your codebase.
Example components/Codeblock:
import React from 'react';
import Codeblock from '@loopmode/codeblock'
export default function ThemedCodeblock(props) {
return (
<Codeblock {...props} theme="twilight" />
);
}
All default prismjs themes and languages are supported and loaded with dynamic import() statements that use /* webpackChunkName */ comments.
When you build your app for production, your final bundle will contain an additional codeblock folder with the theme and language files (codeblock/theme.*.js and codeblock/language.*.js).
However, only the required ones will be loaded at runtime.
Auto-loading content via src uses window.fetch() by default.
If you need to support older browsers, you can either provide a fetch polyfill or a custom loader function.
A custom loader function is pretty easy to create:
const loader = (url, callback) => {
axios.get(url)
.then(response => callback(response.data));
}
(url: String, callback: Function): FunctionThe loader may return a function to cancel pending requests when the requesting component gets unmounted. For example, using axios, it would be something like this:
const loader = (url, callback) => {
const {token: cancelToken, cancel} = axios.CancelToken.source();
axios.get(url, {cancelToken})
.then(response => callback(response.data));
return cancel
}
FAQs
React component to highlight code using prismjs
We found that @loopmode/codeblock 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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.