{"id":26600673,"url":"https://github.com/multipliedtwice/backticks-codeblocks","last_synced_at":"2025-10-04T10:50:34.512Z","repository":{"id":205583811,"uuid":"714244931","full_name":"multipliedtwice/backticks-codeblocks","owner":"multipliedtwice","description":"Tiny (~900 bytes) utility with no dependencies. Parses fenced code inside of text with backticks into code blocks and inline code. Parse and render text with highlighted code blocks","archived":false,"fork":false,"pushed_at":"2024-08-31T00:56:45.000Z","size":1221,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-22T07:02:41.569Z","etag":null,"topics":["codeblocks","prismjs","syntax-highlighting"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multipliedtwice.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-11-04T10:46:33.000Z","updated_at":"2024-09-27T18:20:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"d985810d-e5e6-4d5f-b2fb-fd47186c651d","html_url":"https://github.com/multipliedtwice/backticks-codeblocks","commit_stats":null,"previous_names":["multipliedtwice/backticks-codeblocks"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/multipliedtwice/backticks-codeblocks","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multipliedtwice%2Fbackticks-codeblocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multipliedtwice%2Fbackticks-codeblocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multipliedtwice%2Fbackticks-codeblocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multipliedtwice%2Fbackticks-codeblocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multipliedtwice","download_url":"https://codeload.github.com/multipliedtwice/backticks-codeblocks/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multipliedtwice%2Fbackticks-codeblocks/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278302557,"owners_count":25964520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeblocks","prismjs","syntax-highlighting"],"created_at":"2025-03-23T18:33:15.663Z","updated_at":"2025-10-04T10:50:34.479Z","avatar_url":"https://github.com/multipliedtwice.png","language":"TypeScript","readme":"[![npm version](https://badge.fury.io/js/backticks-codeblocks.svg)](https://badge.fury.io/js/backticks-codeblocks)\r\n[![npm](https://img.shields.io/npm/dt/backticks-codeblocks.svg)](https://www.npmjs.com/package/backticks-codeblocks)\r\n[![HitCount](https://hits.dwyl.com/multipliedtwice/backticks-codeblocks.svg?style=flat)](http://hits.dwyl.com/multipliedtwice/backticks-codeblocks)\r\n[![Coverage](https://img.shields.io/codecov/c/github/multipliedtwice/backticks-codeblocks/main.svg)](https://codecov.io/gh/multipliedtwice/backticks-codeblocks)\r\n[![npm](https://img.shields.io/npm/l/backticks-codeblocks.svg)](LICENSE)\r\n\r\n# Backticks-Codeblocks, syntax highlighting parser for fenced code\r\n\r\nBackticks Codeblocks is a **code fence** library for parsing text and generating structures that can be rendered as code blocks or inline code using PrismJS. \r\n\r\n## Features\r\n\r\n- Generates PrismJS-compatible structures\r\n- Works in node/edge/browser\r\n- Easy integration (works as a pure function)\r\n- Customizable (ignore edge cases by regex)\r\n\r\n![backticks-codeblocks](./static/backticks-codeblocks.png)\r\n\r\nPackage 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. \n\nIt's particularly useful for processing markdown text or any text that follows a similar convention for denoting code segments.\r\n\r\n## Installation\r\n\r\nTo install the package, use the following command:\r\n\r\n```bash\r\nnpm install backticks-codeblocks\r\n```\r\n\r\nor if you use yarn\r\n\r\n```bash\r\nyarn add backticks-codeblocks\r\n```\r\n\r\n## Usage\r\n\r\nImport the `processText` function from the package and use it to parse a string with backticks:\r\n\r\n````javascript\r\nimport { processText } from 'backticks-codeblocks';\r\n\r\nconst result = processText('Some `inline code` and ```block code```');\r\nconsole.log(result);\r\n````\r\n\r\n## Ignore patterns\r\n\r\nIn some cases you can prevent backticks to be parsed as a block of code using second parameter.\r\n\r\n````ts\r\nconst ignorePatterns: [RegExp, string][] = [\r\n  [\r\n    new RegExp('\"```\\\\n\"'.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$\u0026'), 'g'),\r\n    'unique_string_as_a_name_identifier',\r\n  ],\r\n];\r\nprocessText('Some `inline code` and ```block code```', ignorePatterns);\r\n````\r\n\r\n## Input/Output Examples\r\n\r\nThe following table shows some example inputs and the corresponding output from the `processText` function:\r\n\r\n### Convert Block Code to Preformatted Text\r\n\r\n- Input:\r\n\r\n  \\`\\`\\`requiredDependency.mockImplementationOnce(() =\u003e {\\\r\n   throw new Error('Test error');\\\r\n  });\\`\\`\\`\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [{\r\n    code: \"requiredDependency.mockImplementationOnce(() =\u003e {\r\n      throw new Error('Test error');\r\n    });\",\r\n    isBlock: true\r\n  }]\r\n  ```\r\n\r\n### Convert Inline and Block Code Correctly\r\n\r\n- Input:\r\n\r\n  Look at \\`this\\` code:\r\n  \\`\\`\\`requiredDependency.mockImplementationOnce(() =\u003e {\r\n  throw new Error('Test error');\r\n  });\\`\\`\\`\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'Look at ',\r\n    { code: 'this', isBlock: false },\r\n    ' code:\\n    ',\r\n    {\r\n      code: `requiredDependency.mockImplementationOnce(() =\u003e {\r\n        throw new Error('Test error');\r\n      });`,\r\n      isBlock: true,\r\n    },\r\n  ];\r\n  ```\r\n\r\n### Handles Consecutive Code Blocks\r\n\r\n- Input:\r\n\r\n  Check these snippets: \\`\\`\\`code1\\`\\`\\` then \\`\\`\\`code2\\`\\`\\`\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'Check these snippets: ',\r\n    { code: 'code1', isBlock: true },\r\n    ' then ',\r\n    { code: 'code2', isBlock: true },\r\n  ];\r\n  ```\r\n\r\n### Handles Nested Backticks\r\n\r\n- Input:\r\n\r\n  Here is an example \\`code with \\`nested\\` backticks\\` end\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'Here is an example ',\r\n    {\r\n      code: 'code with ',\r\n      isBlock: false,\r\n    },\r\n    'nested',\r\n    {\r\n      code: ' backticks',\r\n      isBlock: false,\r\n    },\r\n    ' end',\r\n  ];\r\n  ```\r\n\r\n### Handles Interrupted Code Blocks\r\n\r\n- Input:\r\n\r\n  Start ```incomplete code block\r\n\r\n- Output:\r\n  ````js\r\n  ['Start ```incomplete code block'];\r\n  ````\r\n\r\n### Handles Mixed Backticks\r\n\r\n- Input:\r\n\r\n  Inline \\`code\\` and \\`\\`\\`block code``` mixed\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'Inline ',\r\n    { code: 'code', isBlock: false },\r\n    ' and ',\r\n    { code: 'block code', isBlock: true },\r\n    ' mixed',\r\n  ];\r\n  ```\r\n\r\n### Handles Escaped Backticks\r\n\r\n- Input:\r\n\r\n  This is not \\`code but \\\\\\`escaped backticks\\\\\\``\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'This is not ',\r\n    { code: 'code but \\`escaped backticks\\`', isBlock: false }\r\n  ]`\r\n  ```\r\n\r\n### Handles Code Block at the Start\r\n\r\n- Input:\r\n\r\n  \\`\\`\\`code at start``` followed by text\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [{ code: 'code at start', isBlock: true }, ' followed by text'];\r\n  ```\r\n\r\n### Handles Code Block at the End\r\n\r\n- Input:\r\n\r\n  Text followed by `code at end`\r\n\r\n- Output:\r\n\r\n  ```js\r\n  ['Text followed by ', { code: 'code at end', isBlock: true }];\r\n  ```\r\n\r\n### Handles Empty Code Blocks\r\n\r\n- Input:\r\n\r\n  Empty code blocks \\`\\`\\` \\`\\`\\` are weird\r\n\r\n- Output:\r\n\r\n  ```js\r\n  ['Empty code blocks are weird'];\r\n  ```\r\n\r\n### Handles Adjacent Code Blocks\r\n\r\n- Input:\r\n\r\n  No space between \\`\\`\\`code1\\`\\`\\`\\`\\`\\`code2``` blocks\r\n\r\n- Output:\r\n\r\n  ```js\r\n  [\r\n    'No space between',\r\n    { code: 'code1', isBlock: true },\r\n    { code: 'code2', isBlock: true },\r\n    'blocks',\r\n  ];\r\n  ```\r\n\r\n### Handles Unmatched Backticks Inside Code Blocks\r\n\r\n- Input:\r\n  ``code with ` inside``\r\n\r\n- Output:\r\n  ```js\r\n  [{ code: 'code with ` inside', isBlock: true }];\r\n  ```\r\n\r\n### Handles Code Blocks With New Lines\r\n\r\n- Input:\r\n  `Multi-line ```\\ncode block\\n````\r\n\r\n- Output:\r\n  ```js\r\n  ['Multi-line ', { code: '\\ncode block\\n', isBlock: true }];\r\n  ```\r\n\r\n## Performance\r\n\r\nThe `processText` function works fairly fast and is capable of handling large texts within reasonable time frames.\r\n\r\n## License\r\n\r\nThis project is open-sourced software licensed under the MIT license.\r\n\r\n#### Credits:\r\n- Super Kick Gym - [Brazilian Jiu Jitsu in Bangkok](https://en.bjj-bangkok.com)\r\n\r\n- Rememo - [Free Task Management and Corporate Chat](https://rememo.io)\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultipliedtwice%2Fbackticks-codeblocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultipliedtwice%2Fbackticks-codeblocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultipliedtwice%2Fbackticks-codeblocks/lists"}