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

@t-ski/html-sourcecode-element

v1.1.0

Published

Rich HTML code element with a native API.

Downloads

6

Readme

HTMLSourceCodeElement

NPM Version GitHub License HTMLSourceCodeElement

Rich HTML code element with a native API.

1. Integration
2. Attributes
2.1 copy
2.2 edit
2.3 type
2.4 language
2.5 scroll
3. Themes
3.1 min
3.2 common
3.3 outline
4. Syntax Highlighting
4.1 glitch
4.2 matrix

⬇️ Integration

via CDN

recommended

<script src="https://unpkg.com/@t-ski/html-sourcecode-element/dist/HTMLSourceCodeElement.<theme>[.<highlighting>].js"></script>

via NPM

npm i @t-ski/html-sourcecode-element
default function(theme: string = "min", highlighting?: string);
import HTMLSourceCodeElement from "@t-ski/html-sourcecode-element";

HTMLSourceCodeElement("common", "glitch");

⚠️ Integration via NPM serves ECMAScript module type bundlers.

Usage

<source-code edit type language="py">
  print('Hello, world!')
</source-code>`

ℹ️ Anything slotted within the <source-code> is interpreted as code contents. HTML code does in particular not have to be escaped – except for script tags.

☑️ Attributes

copy

<source-code copy>

Make element copyable by hover activated button.

edit

<source-code edit>

Make element editable like a script editor.

scroll

<source-code type>

Make element scrollable at horizontal overflow, instead of wrap.

type

<source-code type>

Make element as if a human would type the code.

language

<source-code language="php">

Specify language to help with highlighting (if necessary).

maxheight

<source-code maxheight="php">

Specify maximum amount of lines after which to enable vertical scroll.

ℹ️ A minimum of 5 lines are shown when used with type.


💻 Attributes API

The DOM class HTMLSourceCodeElement is associated with the <source-code> tag. The DOM class provides a static configuration function to override certain attributes globally.

HTMLSourceCodeElement
.globalAttrs(attrs: { [name: string]: boolean; });
HTMLSourceCodeElement
.globalAttrs({
  copy: true,
  edit: false
});

ℹ️ A global configuration does not invert, but override individual attributes.

🖼️ Themes

min

<script src="…/HTMLSourceCodeElement.min[.<syntax>].js">

Minimal editor theme.

common

<script src="…/HTMLSourceCodeElement.common[.<syntax>].js">

outline

<script src="…/HTMLSourceCodeElement.outline[.<syntax>].js">

opaque

<script src="…/HTMLSourceCodeElement.opaque[.<syntax>].js">

💻 Theme API

Using the addStylesheet() method, custom styles can be injected into the <source-code> shadow DOM. The method exists both statically on HTMLSourceCodeElement, as well as on each individual instance. The method must be passed a URL to a stylesheet. Alternatively, an existing <link> or <style> element can be reused through a reference.

(HTMLSourceCodeElement|instanceof HTMLSourceCodeElement)
.addStylesheet(stylesheet: HTMLStyleElement|HTMLLinkElement|string);

In a stylesheet, the :host selector refers to the encompassing <source-code>. The internals of the shadow DOM base on the following markup:

<div class="edit"></div>
<code class="display">
  <table>
    <tr class="line" *>
        <td class="line-number">
            <span>
              <!-- Individual line number -->
            </span>
        </td>
        <td class="line-code">
            <pre mirror>
              <!-- Individual line code -->
            </pre>
        </td>
    </tr>
  </table>
</code>
<button type="button" class="copy">Copy</span>

Color Scheme

Themes adopt the colour scheme preferred by the user. Manually adjustable color schemes, however, can use the static setColorScheme() method. This way, a specific color scheme can be enforced globally.

HTMLSourceCodeElement.setColorScheme("light" | "dark" | "auto");

🎨 Syntax Highlighting

Syntax highlighting is an optional addition to the basic API. In fact, it requires highlight.js to work:

<head>
  <script src="https://unpkg.com/@highlightjs/cdn-assets/highlight.min.js"></script> <!-- dependency -->
  <script src="unpkg.com/@t-ski/html-sourcecode-element/dist/HTMLSourceCodeElement.common.glitch.js"></script>
  <script>
    document.addEventListener("DOMContentLoaded", () => {
      HTMLSourceCodeElement.on("highlight", (code, language) => {
        return language
             ? hljs.highlight(code, { language }).value
             : hljs.highlightAuto(code).value);
      });
    });
  </script>
</head>

glitch

<script src="…/HTMLSourceCodeElement.<theme>.glitch.js">

matrix

<script src="…/HTMLSourceCodeElement.<theme>.matrix.js">

💻 Config API

The HTMLSourceCodeElement provides reasonable commons for abstract visual or behavioural aspects. For instance, the tab width is two spaces by common. However, such aspects can be manipulated in a fashion similar to defining global attributes.

HTMLSourceCodeElement
.config(overrides: { [name: string]: unknown; });
HTMLSourceCodeElement
.config({
  tabWidth: 4
});

ℹ️ A global configuration does not invert, but override individual attributes.

💻 Events API

The HTMLSourceCodeElement DOM class provides a static API to handle events in a custom fashion.

HTMLSourceCodeElement
.on(event: string, cb: (...args: unknown[]) => unknown)

on copy

HTMLSourceCodeElement.on("copy", (dom: {
  host: HTMLSourceCodeElement;
  edit: HTMLDivElement;
  display: HTMLSourceCodeElement;
  table: HTMLTableElement;
  copy: HTMLButtonElement;
}) => void)

Callback fires whenever code is copied. The callback is passed the respective element's shadow DOM key elements. The DOM might be used to to reflect that the code was in fact copied.

on highlight

HTMLSourceCodeElement.on("highlight", cb: (code: string, language?: string) => string)

Callback fires whenever code is rendered. The callback is passed the respective raw code to highlight. If the respective element has an assigned language attribute that value is also passed.

© Thassilo Martin Schiepanski