npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backticks-codeblocks

v1.0.7

Published

A tiny (700b gzipped) utility to parse text with backticks into code blocks and inline code

Downloads

208

Readme

backticks-codeblocks

backticks-codeblocks

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.

Installation

To install the package, use the following command:

npm install backticks-codeblocks

or if you use yarn

yarn add backticks-codeblocks

Usage

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);

Ignore patterns

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);

Input/Output Examples

The following table shows some example inputs and the corresponding output from the processText function:

Convert Block Code to Preformatted Text

  • Input:

    ```requiredDependency.mockImplementationOnce(() => {
    throw new Error('Test error');
    });```

  • Output:

    [{
      code: "requiredDependency.mockImplementationOnce(() => {
        throw new Error('Test error');
      });",
      isBlock: true
    }]

Convert Inline and Block Code Correctly

  • 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,
      },
    ];

Handles Consecutive Code Blocks

  • Input:

    Check these snippets: ```code1``` then ```code2```

  • Output:

    [
      'Check these snippets: ',
      { code: 'code1', isBlock: true },
      ' then ',
      { code: 'code2', isBlock: true },
    ];

Handles Nested Backticks

  • 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',
    ];

Handles Interrupted Code Blocks

  • Input:

    Start ```incomplete code block

  • Output:

    ['Start ```incomplete code block'];

Handles Mixed Backticks

  • Input:

    Inline `code` and ```block code``` mixed

  • Output:

    [
      'Inline ',
      { code: 'code', isBlock: false },
      ' and ',
      { code: 'block code', isBlock: true },
      ' mixed',
    ];

Handles Escaped Backticks

  • Input:

    This is not `code but \`escaped backticks\``

  • Output:

    [
      'This is not ',
      { code: 'code but \`escaped backticks\`', isBlock: false }
    ]`

Handles Code Block at the Start

  • Input:

    ```code at start``` followed by text

  • Output:

    [{ code: 'code at start', isBlock: true }, ' followed by text'];

Handles Code Block at the End

  • Input:

    Text followed by code at end

  • Output:

    ['Text followed by ', { code: 'code at end', isBlock: true }];

Handles Empty Code Blocks

  • Input:

    Empty code blocks ``` ``` are weird

  • Output:

    ['Empty code blocks are weird'];

Handles Adjacent Code Blocks

  • Input:

    No space between ```code1``````code2``` blocks

  • Output:

    [
      'No space between',
      { code: 'code1', isBlock: true },
      { code: 'code2', isBlock: true },
      'blocks',
    ];

Handles Unmatched Backticks Inside Code Blocks

  • Input: code with ` inside

  • Output:

    [{ code: 'code with ` inside', isBlock: true }];

Handles Code Blocks With New Lines

  • Input: `Multi-line ```\ncode block\n````

  • Output:

    ['Multi-line ', { code: '\ncode block\n', isBlock: true }];

Performance

The processText function works fairly fast and is capable of handling large texts within reasonable time frames.

License

This project is open-sourced software licensed under the MIT license.

psst, want some free Kanban app with chat for team of any size? https://rememo.io