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

@heppokofrontend/html-code-block-element

v2.0.5

Published

Code block custom element with syntax highlighting and copy button.

Downloads

429

Readme

<code-block>

MIT License Published on NPM Published on webcomponents.org Maintainability Test Coverage Known Vulnerabilities @heppokofrontend/html-code-block-element

Code block custom element with syntax highlighting and copy button.

It has highlight.js for syntax highlighting.

Usage

DEMO: https://heppokofrontend.github.io/html-code-block-element/

<code-block language="html" label="example.html" controls>
  &lt;script&gt;console.log(true);&lt;/script&gt;
</code-block>

In browser

It can be used by loading html-code-block-element.common.min.js and one CSS theme.

The highlight.js style is available on CDN. You can also download the JS and CSS from the respective repositories and load them into your page.

<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.1.0/styles/vs.min.css"
/>
<script
  src="https://cdn.jsdelivr.net/npm/@heppokofrontend/html-code-block-element/lib/html-code-block-element.common.min.js"
  defer
></script>

There are three versions of this library available.

  • html-code-block-element.common.min.js - One that supports only the popular languages.
  • html-code-block-element.all.min.js - One that enables all languages supported by highligh.js.
  • html-code-block-element.core.min.js - For developers who want to do their own hljs.registerLanguage().

Example

Note: The textarea element cannot be included in the content of the textarea element. If you want to include it, please use HTML Entity for the tag.

<code-block language="html" label="example.html" controls>
  <textarea><script>console.log(true);</script></textarea>
</code-block>

or

<code-block language="html" label="example.html" controls>
  &lt;script&gt;console.log(true);&lt;/script&gt;
</code-block>

Assumption specifications

  • Categories:
  • Contexts in which this element can be used:
    • Where flow content is expected.
  • Content model:
  • Tag omission in text/html:
    • Neither tag is omissible.
  • Content attributes:
    • Global attributes
    • controls - Show controls
    • notrim - Does't remove whitespace from both ends of a source
    • label - Give the code block a unique name. If omitted, it will always have the accessible name "Code Block".
    • language - Language name of the code. If omitted, it will be detected automatically.
  • Accessibility considerations:

DOM interface

[Exposed=Window]
interface HTMLCodeBlockElement : HTMLElement {
  [HTMLConstructor] constructor();

  [CEReactions] attribute boolean controls;
  [CEReactions] attribute boolean notrim;
  [CEReactions] attribute DOMString label;
  [CEReactions] attribute DOMString language;
  [CEReactions] attribute DOMString value;
};

In development

Installation:

npm install --save @heppokofrontend/html-code-block-element

Usage:

The customElements.define() will also be included.

// For highlighting, `highlight.js/lib/common` will be used.
import '@heppokofrontend/html-code-block-element';
// For highlighting, `highlight.js` will be used.
import '@heppokofrontend/html-code-block-element/dist/index.all';
// For highlighting, `highlight.js/lib/code` will be used.
import '@heppokofrontend/html-code-block-element/dist/index.core';

If you are using purely constructors:

import HTMLCodeBlockElement from '@heppokofrontend/html-code-block-element/dist/class/HTMLCodeBlockElement';

Use in React

This package contains the global type files for React.

  • React.CodeBlockHTMLAttributes
  • code-block in JSX.IntrinsicElements

* CSS needs to be loaded separately.

// CodeBlock.tsx
import {CodeBlockProps} from '@heppokofrontend/html-code-block-element/dist/manual';
import styleSheet from '@heppokofrontend/html-code-block-element/dist/styleSheet';
import hljs from 'highlight.js/lib/common';
import Head from 'next/head';
import {useEffect} from 'react';

declare module 'react' {
  // A type for the properties of a function component
  interface CodeBlockHTMLAttributes<T> extends HTMLAttributes<T> {
    /** The accessible name of code block */
    label?: string | undefined;
    /** The Language name */
    language?: string | undefined;
    /** The flag to display the UI */
    controls?: boolean;
  }
}

declare global {
  // A type for JSX markup
  namespace JSX {
    interface IntrinsicElements {
      'code-block': CodeBlockProps;
    }
  }
}

type Props = Omit<React.CodeBlockHTMLAttributes<HTMLElement>, 'className'>;

let isLoaded = false;

export const CodeBlock = ({children, ...props}: Props) => {
  useEffect(() => {
    const loadWebComponent = async () => {
      const {HTMLCodeBlockElement, createHighlightCallback} = await import(
        '@heppokofrontend/html-code-block-element/dist/manual'
      );

      HTMLCodeBlockElement.highlight = createHighlightCallback(hljs);
      customElements.define('code-block', HTMLCodeBlockElement);
    };

    if (!isLoaded) {
      isLoaded = true;
      loadWebComponent();
    }
  }, []);

  return (
    <>
      <Head>
        <style>{styleSheet}</style>
      </Head>
      <code-block {...props}>{children}</code-block>
    </>
  );
};

Use as constructor

Manual setup:

// 1. Import
import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
import HTMLCodeBlockElement from '@heppokofrontend/html-code-block-element/dist/class/HTMLCodeBlockElement';
//  or import { HTMLCodeBlockElement } from '@heppokofrontend/html-code-block-element';

// Support JavaScript
hljs.registerLanguage('javascript', javascript);

// 2. Set endgine
/**
 * Example: Callback to be called internally
 * @param {string} src - Source code string for highlight
 * @param {{ language: string }} options - Option for highlight
 * @returns {{ markup: string }} - Object of the highlight result
 */
HTMLCodeBlockElement.highlight = function (src, options) {
  if (
    // Verifying the existence of a language
    options?.language &&
    hljs.getLanguage(options.language)
  ) {
    const {value} = hljs.highlight(src, options);

    return {
      markup: value,
    };
  }

  return {
    markup: hljs.highlightAuto(src).value,
  };
};

// 3. Define
customElements.define('code-block', HTMLCodeBlockElement);

// 4. Make
const cbElm = new HTMLCodeBlockElement();

// 5. Assign
cbElm.language = 'javascript';
cbElm.label = 'your label';
cbElm.value = `const hoge = true;

console.log(hoge);`;

// 6. Append
document.body.append(cbElm); // Render at the same time
Syntax

No params.

new HTMLCodeBlockElement();

Support browser

  • Chrome
  • Safari
  • Firefox
  • Edge