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

@thewebforge/astro-code-blocks

v0.2.0

Published

Beautiful code blocks for your astro site

Downloads

41

Readme

astro-code-blocks

Beautiful code blocks for your Astro site This is a custom version of the integration from Astro Docs

Quick Install

The astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren’t sure which package manager you’re using, run the first command.) Then, follow the prompts, and type “y” in the terminal (meaning “yes”) for each one.

# Using NPM
npx astro add @thewebforge/astro-code-blocks
# Using Yarn
yarn astro add @thewebforge/astro-code-blocks
# Using PNPM
pnpm astro add @thewebforge/astro-code-blocks

If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

Manual Install

First, install the @thewebforge/astro-code-blocks package using your package manager. If you’re using npm or aren’t sure, run this in the terminal:

npm install @thewebforge/astro-code-blocks

If you’re using Yarn or PNPM, run this instead:

# Using Yarn
yarn add @thewebforge/astro-code-blocks
# Using PNPM
pnpm add @thewebforge/astro-code-blocks

Next, open your project’s astro.config.mjs file and add the following to the plugins array:

astro.config.mjs

import codeblocks from "@thewebforge/astro-code-blocks";

export default defineConfig({
  integrations: [
    codeblocks(),
  ],
});

Warning If you alreday installed Astro MDX integration. Or another integration that uses MDX, you need to make sure that the codeblocks integration comes before MDX in the integrations array. Otherwise, it will not work.

Config

You can configure the code blocks integration by passing an object to the codeblocks function.

astro.config.mjs

integrations: [
    codeblocks({
        // Copy Button Options
        copyButtonTitle: 'Copy',
        copyButtonTooltip: 'Copied to clipboard',
    }),
  ],

Usage

With the integration installed, the component will automatically be imported and applied ot the code blocks you create in your .mdx files.

Add a title to your code block

You can add a title to your code block by adding a title prop to the code block

```js title="myscript.js"
  console.log('Hello World')
```

Note The title prop is optional. If you don't add it, the code block will not have a title.

Highlight lines

You can highlight lines in your code block by adding a prop to the code blocks as a list of comma separated numbers in curly brackets. For example:

  • {1} will highlight line 1
  • {1,3} will highlight lines 1 and 3
  • {2-5, 7} will highlight lines 1 to 5(not included) and 7
```js {1,3,5}
  console.log('Hello World')
  console.log('Hello World')
  console.log('Hello World')
  console.log('Hello World')
  console.log('Hello World')
```

Highlight strings

You can highlight strings in your code block by adding a prop to the code blocks as a regular expression. The following example will highlight all occurences of "astro":

  • /astro/ will highlight all occurences of "astro"
  • /\w*$/ will highlight the last word of each line
```sh /astro/
  # Using NPM
  npx astro add @thewebforge/astro-code-blocks
  # Using Yarn
  yarn astro add @thewebforge/astro-code-blocks
  # Using PNPM
  pnpm astro add @thewebforge/astro-code-blocks
```

Insertions and Deletions

You can highlight insertions and deletions in your code block by adding ins and/or del props to the code blocks as a list of lines in curly brackets. For example:

```sh ins={3,4} del={5,6}
  # Using NPM
  npx astro add @thewebforge/astro-code-blocks
  # Using Yarn
  yarn astro add @thewebforge/astro-code-blocks
  # Using PNPM
  pnpm astro add @thewebforge/astro-code-blocks
```

Custom CSS

Override the default Astro styles

crazy-code-props.css

  :root {
    --astro-code-color-text: white;
    --astro-code-color-background: black;
    --astro-code-token-constant: plum;
    --astro-code-token-string: purple;
    --astro-code-token-comment: tomato;
    --astro-code-token-keyword: darkslategrey;
    --astro-code-token-parameter: coral;
    --astro-code-token-function: green;
    --astro-code-token-string-expression: chartreuse;
    --astro-code-token-punctuation: gray;
    --astro-code-token-link: firebrick;
  }