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

textrun-javascript

v0.2.0

Published

This package provides [Text-Runner](https://github.com/kevgo/text-runner) actions for JavaScript code snippets inside documentation.

Downloads

19

Readme

Text-Runner JavaScript Actions

This package provides Text-Runner actions for JavaScript code snippets inside documentation.

Installation

To use these actions, add this package as a development dependency by running npm i -D textrun-javascript or yarn i -D textrun-javascript.

Run JavaScript

Assume your documentation instructs the reader to run a line of JavaScript. It could contain something like this:

Let's run our first JavaScript command:

```js
console.log("Hello world!")
```

When you assign the javascript/runnable type to this document part, Text-Runner executes the JavaScript similar to how the user would:

Let's run our first JavaScript command:

<a type="javascript/runnable">

```js
console.log("Hello world")
```

</a>

You can simplify this to:

Let's run our first JavaScript command:

<pre type="javascript/runnable">
console.log("Hello world!")
</pre>

Asynchronous JavaScript

The javascript/runnable action waits for the code block to finish. To wait for asynchronous code, add the placeholder <CALLBACK> where your code would call the callback when its done. Only one placeholder is allowed per code block. Example:

function asyncFoo(done) {
  console.log("some async work")
  done()
}

asyncFoo(<CALLBACK>)

You can also use // ... as the placeholder:

function asyncFoo(done) {
  console.log("some async work")
  done()
}

asyncFoo(function (err) {
  // ...
})

Sharing JavaScript variables

Let's say your documentation contains two regions of JavaScript that share a variable:

const text = "hello"

and

const complete = text + "world"

Each JavaScript region runs in its own isolated environment. The second region would not see the variable text from the first region. They do share the global object, though. To share local variables between different regions of Javascript, this step replaces all occurrences of const⎵, var⎵, let⎵, and this. with global. As an example, const foo = 123 gets turned into global.foo = 123, thereby making foo accessible in all code regions.

Validate JavaScript

The javascript/non-runnable action marks documented JavaScript code that should not be executed. Example:

<pre type="javascript/non-runnable">
const a = 1;
</pre>