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

@codeblock/core

v2.0.6

Published

core dependencies and functionality for @codeblock packages

Downloads

24

Readme

@codeblock/core

Core dependencies and functionality for @codeblock.

The @codeblock/core package is the primary connection between @codeblock and prismjs.

It is the only @codeblock packages that directly depends on prismjs, while all other @codeblock packages depend on it.

It provides the main interfaces and typescript definitions, the main functions for highlighting an element and some utility functions.

You probably want to use @codeblock-react instead - you shouldn't ever need to use the core functionality directly or manually.

Prism instance

Any @codeblock modules that require access to the global Prism object should import it from @codeblock/core:

import { Prism } from '@codeblock/core';
// do something with Prism

utilities

applyPrism

Applies prismjs highlighting with codeblock mechanisms to an element or its children.

import { applyPrism } from '@codeblock/core';
import languages from '@codeblock/languages/lib/empty';
import themes from '@codeblock/themes/lib/empty';

applyPrism(document.getElementById('content'), {
  providers: { languages, themes }
}).then(console.log('done'));

getLanguageClassName

Creates the classname for a prism language.

import { getLanguageClassName } from '@codeblock/core';
getLanguageClassName('jsx'); // 'language-jsx'
getLanguageClassName('foo'); // '
getLanguageClassName(null); // '

getThemeClassName

Creates the classname for a codeblock theme given a prism theme name.

import { getThemeClassName } from '@codeblock/core';
const className = getThemeClassName('okaidia'); // 'codeblock-theme-okaidia'
const className = getThemeClassName('foo'); // ''
const className = getThemeClassName(null); // ''

getLanguageMap

Detects all languages in the CSS class names of one or more given DOM elements and returns a LanguageElementMap ({ [lang]: elements[...] }), or an empty object if no languages are found.

If the given element

  • does not have a language class, its children will be queried and used
  • does have a language class itself, its children will be ignored
import { getLanguageMap } from '@codeblock/core';
const map = getLanguageMap(document.body);
console.log(map);

autoload

In order to reliably support all languages, you have to use the autoloader plugin of prismjs.

The way to do this in @codeblock is to set the autoload path to a location where prismjs is available:

import { setAutoload } from '@codeblock/core/lib/http';
setAutoload('https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/');
// unset to disable:
setAutoload(null);

CDN_AUTOLOAD_PATH

This constant holds the autoload path for cdn-based @codeblock modules.

  • defaults to "https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/"
  • you can override it at compile-time using a PRISM_CDN_AUTOLOAD_PATH env variable

This is done under the hood by cdn-based components like @codeblock/react/cdn.

import { setAutoload, CDN_AUTOLOAD_PATH } from '@codeblock/core/lib/http';
setAutoload(CDN_AUTOLOAD_PATH);

available themes and languages

The list of available themes and languages is provided by core as well. It is generated based on the installed node_modules/prismjs/components and node_modules/prismjs/themes.

Run update-available.sh in order to update those files. Note that this will override the current files in src/available!

@codeblock

Core dependencies and functionality for @codeblock