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

@pimd/html-injector-plugin

v0.1.2

Published

Helper for injecting HTML into syntax-highlighted examples

Downloads

9

Readme

PIMD HTML injector plugin

A base for other plugins to insert functionality into code examples.

Basic idea

It is easy to find out in a source code where to insert HTML – for example highlight a word, add a tooltip, etc. After the source code got HTML-escaped and syntax highlighted, it is hard to find the right offsets.

This plugin translates offsets from before to after syntax highlighting.

Manual example without this plugin

Imagine you want to format the x in the following example in italics:

<p>Example</p>

When you want to display HTML in HTML, < and > need to be escaped with &lt; and &gt; first and put into a <pre> and <code> block:

<pre>
  <code>
    &lt;p&gt;Example&lt;/p&gt;
  </code>
</pre>

Afterwards you can insert the <i> and </i>:

<pre>
  <code>
    &lt;p&gt;E<i>x</i>ample&lt;/p&gt;
  </code>
</pre>

With syntax highlighting (for example with PrismJS) enabled, this simple right aboce example already gets quite complex:

<pre>
  <code class="language-html">
    <span class="token tag">
      <span class="token tag">
        <span class="token punctuation">&lt;</span>
        p</span>
      <span class="token punctuation">&gt;</span>
    </span>
    E<i>x</i>ample
    <span class="token tag">
      <span class="token tag">
        <span class="token punctuation">&lt;/</span>
        p
      </span>
      <span class="token punctuation">&gt;</span>
    </span>
  </code>
</pre>

For readability issues, all examples above got formatted and indented. In real life, this would be one line without whitespace.

Usage

With this plugin enabled, you only need to know the offset and what to insert:

// Offsets before highlighting:
// <p>Example</p>
// 01234567890123
example.insertAt(4, "<i>") // Before the `x`
example.insertAt(4 + 1, "</i>") // After the `x`

This is best put into a example:beforeRender hook:

const myPlugin = function(config) {
  config.hooks.add("example:beforeRender", "my-plugin", function(example) {
    example.insertAt(4, "<i>") // Before the `x`
    example.insertAt(4 + 1, "</i>") // After the `x`
  })
}

This of course would format every 4th character of every code example in italic. A real live example would make use of more dynamic code:

const myPlugin = function(config) {
  config.hooks.add("example:beforeRender", "my-plugin", function(example) {
    const offset = example.content.indexOf("x")
    example.insertAt(offset, "<i>") // Before the `x`
    example.insertAt(offset + 1, "</i>") // After the `x`
  })
}

To add this to specific examples only, use a InfoStringParser:

const myPlugin = function(config) {
  config.addInfoStringParser(/italicx/, function(match, rules) {
    this.hooks.add("example:beforeRender", "my-plugin", function(example) {
      const offset = example.content.indexOf("x")
      example.insertAt(offset, "<i>") // Before the `x`
      example.insertAt(offset + 1, "</i>") // After the `x`
    })
  })
}

Back to Markdown, the plugin can be used as simple as:

```html italicx
<p>Example</p>
```

Setup

To render the Markdown example right above, install this plugin:

npm i @pimd/html-injector-plugin

This requires the HTML injector plugin to be loaded first:

const { Document } = require("pimd")
const Config = require("pimd/lib/config")
const htmlInjectorPlugin = require("@pimd/html-injector-plugin")

const config = new Config()
config.use(htmlInjectorPlugin)
config.use(myPlugin) // `myPlugin` as defined further up this page

const markdown = `
\`\`\`html italicx
<p>Example</p>
\`\`\`
`
const doc = new Document(markdown, config)
const html = doc.render()
console.log(html)

Copyright

Copyright 2018++ Nico Hagenburger. See MIT-LICENSE for details. Get in touch with @hagenburger on Twitter or open an issue.