Backticks Codeblocks is a code fence library for parsing text and generating structures that can be rendered as code blocks or inline code using PrismJS.
Package provides a simple and efficient way to parse strings containing backticks. It differentiates between inline code, marked by single backticks, and code blocks, surrounded by triple backticks, and returns them as structured data.
It's particularly useful for processing markdown text or any text that follows a similar convention for denoting code segments.
To install the package, use the following command:
npm install backticks-codeblocks
or if you use yarn
yarn add backticks-codeblocks
Import the processText
function from the package and use it to parse a string with backticks:
import { processText } from 'backticks-codeblocks';
const result = processText('Some `inline code` and ```block code```');
console.log(result);
In some cases you can prevent backticks to be parsed as a block of code using second parameter.
const ignorePatterns: [RegExp, string][] = [
[
new RegExp('"```\\n"'.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'),
'unique_string_as_a_name_identifier',
],
];
processText('Some `inline code` and ```block code```', ignorePatterns);
The following table shows some example inputs and the corresponding output from the processText
function:
Input:
```requiredDependency.mockImplementationOnce(() => {\ throw new Error('Test error');\ });```
Output:
[{
code: "requiredDependency.mockImplementationOnce(() => {
throw new Error('Test error');
});",
isBlock: true
}]
Input:
Look at `this` code: ```requiredDependency.mockImplementationOnce(() => { throw new Error('Test error'); });```
Output:
[
'Look at ',
{ code: 'this', isBlock: false },
' code:\n ',
{
code: `requiredDependency.mockImplementationOnce(() => {
throw new Error('Test error');
});`,
isBlock: true,
},
];
Input:
Check these snippets: ```code1``` then ```code2```
Output:
[
'Check these snippets: ',
{ code: 'code1', isBlock: true },
' then ',
{ code: 'code2', isBlock: true },
];
Input:
Here is an example `code with `nested` backticks` end
Output:
[
'Here is an example ',
{
code: 'code with ',
isBlock: false,
},
'nested',
{
code: ' backticks',
isBlock: false,
},
' end',
];
Input:
Start ```incomplete code block
Output:
['Start ```incomplete code block'];
Input:
Inline `code` and ```block code``` mixed
Output:
[
'Inline ',
{ code: 'code', isBlock: false },
' and ',
{ code: 'block code', isBlock: true },
' mixed',
];
Input:
This is not `code but \`escaped backticks\``
Output:
[
'This is not ',
{ code: 'code but \`escaped backticks\`', isBlock: false }
]`
Input:
```code at start``` followed by text
Output:
[{ code: 'code at start', isBlock: true }, ' followed by text'];
Input:
Text followed by code at end
Output:
['Text followed by ', { code: 'code at end', isBlock: true }];
Input:
Empty code blocks ``` ``` are weird
Output:
['Empty code blocks are weird'];
Input:
No space between ```code1``````code2``` blocks
Output:
[
'No space between',
{ code: 'code1', isBlock: true },
{ code: 'code2', isBlock: true },
'blocks',
];
Input:
code with ` inside
Output:
[{ code: 'code with ` inside', isBlock: true }];
Input: `Multi-line ```\ncode block\n````
Output:
['Multi-line ', { code: '\ncode block\n', isBlock: true }];
The processText
function works fairly fast and is capable of handling large texts within reasonable time frames.
This project is open-sourced software licensed under the MIT license.
Super Kick Gym - Brazilian Jiu Jitsu in Bangkok