![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
code-excerpt
Advanced tools
The code-excerpt npm package is designed to extract and highlight excerpts from code snippets. It is particularly useful for creating documentation, tutorials, or any content that requires displaying code with specific lines highlighted.
Extracting Code Excerpts
This feature allows you to extract a specific range of lines from a code snippet. The `start` and `end` options specify the range of lines to be extracted.
const codeExcerpt = require('code-excerpt');
const code = `
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
`;
const excerpt = codeExcerpt(code, { start: 2, end: 4 });
console.log(excerpt);
// Output:
// {
// value: ' return a + b;\n}\n\nconsole.log(add(2, 3));\n',
// start: 2,
// end: 4
// }
Highlighting Specific Lines
This feature allows you to highlight specific lines within the extracted code excerpt. The `highlight` option takes an array of line numbers to be highlighted.
const codeExcerpt = require('code-excerpt');
const code = `
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
`;
const excerpt = codeExcerpt(code, { start: 2, end: 4, highlight: [3] });
console.log(excerpt);
// Output:
// {
// value: ' return a + b;\n}\n\nconsole.log(add(2, 3));\n',
// start: 2,
// end: 4,
// highlight: [3]
// }
highlight.js is a popular library for syntax highlighting of code snippets. Unlike code-excerpt, which focuses on extracting and highlighting specific lines, highlight.js is designed to apply syntax highlighting to entire code blocks. It supports a wide range of programming languages and is highly customizable.
Prism is a lightweight, extensible syntax highlighter. It is similar to highlight.js in that it focuses on syntax highlighting for entire code blocks. Prism is known for its performance and ease of use, and it also supports a wide range of languages and plugins for additional functionality.
CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is more than just a syntax highlighter; it provides a full-featured code editor with features like autocompletion, linting, and more. While it can be used to highlight code, its primary use case is as an in-browser code editor.
Extract code excerpts
$ npm install --save code-excerpt
const codeExcerpt = require('code-excerpt');
const source = `
'use strict';
function someFunc() {}
module.exports = () => {
const a = 1;
const b = 2;
const c = 3;
someFunc();
};
`.trim();
const excerpt = codeExcerpt(source, 5);
//=> [
// {line: 2, value: ''},
// {line: 3, value: 'function someFunc() {}'},
// {line: 4, value: ''},
// {line: 5, value: 'module.exports = () => {'},
// {line: 6, value: ' const a = 1;'},
// {line: 7, value: ' const b = 2;'},
// {line: 8, value: ' const c = 3;'}
// ]
Type: string
Source code.
Type: number
Line number to extract excerpt for.
Type: number
Default: 3
Number of surrounding lines to extract.
FAQs
Extract code excerpts
We found that code-excerpt 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.