@wakeful-cloud/html-translator

Translate HTML5 to Discord flavored markdown

Usage no npm install needed!

<script type="module">
  import wakefulCloudHtmlTranslator from 'https://cdn.skypack.dev/@wakeful-cloud/html-translator';
</script>

README

HTML Translator

Publish NPM

Translate HTML5 to Discord flavored markdown

Features

  • Broad element support
  • Fully tested
  • Written in TypeScript
  • Thoroughly commented

Caveats

  • Ignores interactive elements (Buttons, inputs, switches, etc.)
  • Ignores CSS
  • Poor invalid HTML support
  • Provides no sanitization

Usage

  1. Install the package:
npm i @wakeful-cloud/html-translator
  1. Add the below to your Discord bot:
//Import
import translate from '@wakeful-cloud/html-translator'; //ES Modules
const translate = require('@wakeful-cloud/html-translator'); //CommonJS

//Translate
const html = '<b>Bold text</b> followed with <i>italicized text</i>.';
const {markdown, images} = translate(html); //"**Bold text** followed with *italicized text*."

//Compose the embed
const embed = new MessageEmbed({
  title: 'Translator Test',
  description: markdown,
  color: '#005DAA'
});

//Add image
if (images.length > 0)
{
  embed.image = {
    url: images[0].src
  };
}

//Send
client.send(embed);

See src/test.html and src/test.txt for a more complex example.

Security

You should NOT call this package with unsanitized or untrusted HTML! This package provides absolutely no protection against any form of attack, you should use something like DOMPurify to sanitize HTML prior to calling this package.