Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
compose-code-highlighter
Advanced tools
Easily highlight static code snippets using the wonderful CodeMirror. This wraps the CodeMirror run mode feature and adds language aliases, and DOM interaction.
When requiring this module, be sure to also require the CodeMirror language modes for the languages you'll be working with. For example if you are going to use Ruby, C, and CSS, you might add this:
require('codemirror')
require('codemirror/mode/ruby/ruby')
require('codemirror/mode/clike/clike')
require('codemirror/mode/css/css')
See CodeMirror's mode documentation for a complete list of supported languages.
Code snippets can be any element (not just a <pre>
block) but should either have a
data-lang
attribute to set the language or should have a classname matching matching lang-[language]
.
For example:
<pre data-lang='css'>
body {
background: #c0ffee;
}
</pre>
<div class='lang-ruby'>
puts 'hello world'
</div>
Then to highlight all code snippets on the page:
Highlighter.highlight()
If your language doesn't seem to be highlighting properly, you can specify
the mimetype that CodeMirror is using to identify your language. For Scss, you'd use data-lang="text/x-scss"
. That's kind of verbose so this library adds some aliases so you can go on using data-lang="scss"
and it is converted before invoking CodeMirror.
aliases: {
'bash' : 'text/x-sh',
'c' : 'text/x-csrc',
'html' : 'text/html',
'js' : 'text/javascript',
'json' : 'application/json',
'java' : 'text/x-java',
'markup': 'text/html',
'sass' : 'text/x-sass',
'scss' : 'text/x-scss',
'sh' : 'text/x-sh'
},
If you want to add aliases you can do so like this:
var Highlighter = require('compose-code-highlighter')
Highlighter.addAlias({
'less': 'text/x-less'
})
I'm happy to accept pullrequests for adding aliases to the default list.
FAQs
Highlight static code snippets with CodeMirror
The npm package compose-code-highlighter receives a total of 1 weekly downloads. As such, compose-code-highlighter popularity was classified as not popular.
We found that compose-code-highlighter 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.