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

@gerhobbelt/markdown-it-html5-media

v0.6.0-1

Published

Lightweight support for HTML5 video/audio using markdown image syntax

Downloads

4

Readme

markdown-it-html5-media

Minimalist <video>/<audio> plugin for markdown-it, using image syntax.

Inspired by markdown-it-html5-embed. Key differences:

  • Only supports image syntax: ![descriptive text](video.mp4), which is what the CommonMark folks seem to favor
  • Tokenizes video and audio to tokens of the 'video' and 'audio' type (useful for working with, e.g., rich text editors that process these tokens)
  • No library dependency for file type detection, just a simple extension check for commonly used video/audio formats.
  • Transpiled version: 10KB unminified vs. 169KB unminified

Basic usage

const md = require('markdown-it')();
// Destructuring assignment; we also export UI messages & media type detection
const { html5Media } = require('markdown-it-html5-media');
md.use(html5Media);
console.log(md.render('![text](video.mp4)'));

Output:

<p><video src="video.mp4" controls class="html5-video-player">
Your browser does not support playing HTML5 video.
You can <a href="video.mp4" download>download the file</a> instead.
Here is a description of the content: text
</video></p>

Custom attributes

You can pass any string that will be rendered instead of the default attributes shown above.

// Init as above
md.use(html5Media, {
  videoAttrs: 'class="my-video-css"',
  audioAttrs: 'class="my-audio-css" data-collapse'
});
console.log(md.render('![](video.mp4)'));
console.log(md.render('![](audio.mp3)'));

Output:

<p><video src="video.mp4" class="my-video-css">
Your browser does not support playing HTML5 video.
You can <a href="video.mp4" download>download the file</a> instead.
</video></p>
<p><audio src="audio.mp3" class="my-audio-css" data-collapse>
Your browser does not support playing HTML5 audio.
You can <a href="audio.mp3" download>download the file</a> instead.
</audio></p>

Media type detection

This module detects the media type by examining the file extension (case-insensitive). The valid audio and video extensions are defined here.

If you need to perform an identical media type detection outside the module, you can import the guessMediaType function (docs):

const { guessMediaType } = require('markdown-it-html5-media');

Custom messages

You can customize the fallback text. This text will only be shown to users whose browser does not support HTML5 video or audio at all. %s is used as a substitution marker for the filename or the description.

// Init as above
md.use(html5Media, {
  messages: {
    en: {
      'html5 video not supported':
        'Cannot play video.',
      'html5 audio not supported':
        'Cannot play audio.',
      'html5 media fallback link':
        'Download <a href="%s">file</a>.',
      'html5 media description':
        'Description: %s'      
    }
  }
});
console.log(md.render('![text](video.mp4)'));

Output:

<p><video src="video.mp4" controls class="html5-video-player">
Cannot play video.
Download <a href="video.mp4">file</a>.
Description: text
</video></p>

If you only want to change some of the text, you can import the messages object from the module and partially alter its contents:

const { html5Media, messages } = require('markdown-it-html5-media');
messages.en['html5 vide not supported'] = 'Cannot play video.';

Translation

Markdown-It supports passing along environment options, like so:

md.render('![some text](video.mp4)', {
  language: 'en'
});

This library will look up messages using the language key as in the example provided, or 'en' if none is provided. Only English messages are included with the library, and the built-in translation function falls back to English if a translation cannot be found.

You can provide additional translations using the approach in the previous example, or you can override the internal translation function, like so:

md.use(html5Media, { translateFn: someFunction });

Function documentation:

/**
 * @param {String} language
 *  a language code, typically an ISO 639-[1-3] code.
 * @param {String} messageKey
 *  an identifier for the message, typically a short descriptive text
 * @param {String[]} messageParams
 *  Strings to be substituted into the message using some pattern, e.g., %s or
 *  %1$s, %2$s. By default we only use a simple %s pattern.
 * @returns {String}
 *  the translation to use
 */
function translateFn(language, messageKey, messageParams) {
  // Your code here
}

Anything else?

The module is written in modern JavaScript. The version in dist/ is transpiled down to ES5 compatible with recent browsers. Use npm run build to update the build (does not change contents of dist; use npm run dist to build & dist).

You can find the automatically generated documentation here. Use npm run docs to regenerate it (changes contents of docs, which is tracked).

This library overrides Markdown-It's image tokenizer, which means that it duplicates portions of that particular Markdown-It code. If you can think of a better way to do what it does without scanning the whole token stream, please go ahead and file an issue or send a PR.

Strings should be escaped where they ought to be, but see the provided tests for yourself. Use at your own risk. :)