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

prism-functional

v0.0.7

Published

An unstable side project where I'm trying to understand PrismJS. I might even try to modify it so it fits more closely with functional programming paradigms.

Downloads

16

Readme

Overview

This is currently just an unstable side project where I'm trying to use PrismJs, but as a pure function.

Usage

First, install prism-functional from npm:

npm install prism-functional

Import the module, import the language you need, and run the function on the code you need to highlight.

import prism from "prism-functional"
import langJavascript from "prism-functional/languages/javascript

const codeSnippet = 'console.log("Hello world!")'
const highlightedHtml = prism(codeSnippet, langJavascript)
console.log(highlightedHtml)
// console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello world!"</span><span class="token punctuation">)</span>

Available Languages

I've copy-pasta'd the following languages from Prism's source. The original language files have to be modified slightly to declare and export the grammar rather than append it to the global Prism object. In addition, many languages use properties of the global Prism object, such as Prism.languages.extend. These had to be replaced with more pure-function-like versions.

The following languages can be imported immediately:

// bash
import lang_bash from "prism-functional/languages/bash";
// clike
import lang_clike from "prism-functional/languages/clike";
// ebnf
import lang_ebnf from "prism-functional/languages/ebnf";
// go (extends "clike")
import lang_go from "prism-functional/languages/go";
// hcl (hashicorp configuration language)
import lang_hcl from "prism-functional/languages/hcl";
//  javascript (extends "clike")
import lang_javascript from "prism-functional/languages/javascript";
//  sentinel (extends "clike")
import lang_sentinel from "prism-functional/languages/sentinel";
//  shell-session (uses "bash")
import lang_shell_session from "prism-functional/languages/shell-session";

So far, moving languages over has been a relatively straightforward and quick task. Ideally, I'd find a way to use the Prism source directly somehow rather than copying-and-pasting it, but frankly I don't know enough about javascript to even know whether that would be possible 😬

Plugins and hooks

The proper version of PrismJS seems to have a very robust plugin ecosystem. I have not attempted to recreate it in this "functional" version. I think it's probably possible, but I have much less experience with this kind of implementation, so I'm honestly not sure. I also haven't yet felt an acute need to use plugins.

Background

I was curious how PrismJS could be used in as simple of a way as possible, in a portable way. For example, I wanted to be able to use Prism to highlight code when processing markdown, and also wanted to use it in a React component. I found it challenging to do these things - it seems like typical usage is targeted at running Prism in the browser.

However, I didn't want to give up on Prism - I really like how lightweight it is, and I really like the number of languages available, and the tools for defining new language grammars.

I feel more familiar with functional programming paradigms, and the more I thought about, the more I landed on wanting to use Prism like this:

const highlightedHtml = prism(code, language);

This led me towards trying to lazily adapt PrismJS to meet this need. I've basically just tried to recreate Prism.highlight in a more pure-function-ish way.