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

europa-worker

v6.0.0

Published

Library for converting HTML into valid Markdown within a worker

Downloads

2

Readme

Europa Worker

Europa Worker is a library for converting HTML into valid Markdown within a worker.

cheerio is used to parse HTML input without the need for complexity.

Build Status License Release

Install

Install using your preferred package manager. For example;

$ npm install --save europa-worker

Check out europa, node-europa, or europa-cli if you want to install it for use within a web browser, Node.js, or as a CLI respectively.

Examples

const europa = new Europa();

europa.convert('<a href="https://github.com/neocotic/europa">Europa</a>');

API

Simply create an instance of Europa and you've done most of the work. You can control many aspects of the HTML to Markdown conversion by passing the following options to the constructor:

| Option | Type | Description | Default | |------------|---------|-------------------------------------------------------------------------------------|---------------------------| | absolute | Boolean | Whether absolute URLs should be used for elements (e.g. anchors, images) | false | | baseUri | String | The base URI used to resolve relative URLs used for elements (e.g. anchors, images) | Parent of location.href | | eol | String | The end of line character to be inserted into generated Markdown | "\n" | | inline | Boolean | Whether URLs for elements (e.g. anchors, images) are to be inserted inline | false |

const europa = new Europa({
  absolute: true,
  baseUri: 'https://example.com',
  eol: '\r\n',
  inline: true,
});

convert(input)

Converts the specified input into Markdown.

input can either be an HTML string or DOM node(s) to be converted into Markdown. For a DOM node to be converted, it must be compatible with the AnyNode type as declared in the domhandler package to ensure that it works. This can be easily done using cheerio, which helps power Europa Worker under-the-hood.

const europa = new Europa();

europa.convert('<blockquote><b>Europa</b> is great!</blockquote>');
//=> "> **Europa** is great!"
europa.convert($('.lead')[0]);
//=> "_Everyone_ ♥ **Europa**!"

europa.convert($('<div>').html('Please keep my <span style="display: none">treasure</span> secret safe...')[0]);
//=> "Please keep my secret safe..."

Plugins

Europa is fully pluggable and is packed with default plugins in order to get full support for basic Markdown. It enables the creation of external plugins to further extend Europa's capabilities to support extended Markdown syntax or even new HTML elements should they not be added to Europa quick enough for you.

Plugins are packaged independently, however, the default plugins are included in europa-preset-default and is bundled with Europa Core so that they are available to all implementations with no extra effort.

The API for plugins is simple on a high level, but you'll need to get to grips with the internal API to understand what you can really do:

import { Plugin, PluginApi } from 'europa-core';
import Europa from 'europa-worker';

const examplePluginProvider = (api: PluginApi): Plugin => ({
  // All fields and methods are optional
  converters: {
    TAGNAME: {
      startTag(conversion, context): boolean { /* ... */ },
      endTag(conversion, context) { /* ... */ },
    },
  },
  convertText(value, conversion): boolean { /* ... */ },
  startConversion(conversion) { /* ... */ },
  endConversion(conversion) { /* ... */ },
});

Europa.registerPlugin(examplePluginProvider);

It's highly recommended to look at existing plugins to get a better understanding of how things work.

Since multiple plugins could support the same tag(s), the load order is important as the last plugin loaded that declares support for a tag, will be the one that's used. Be wary of overriding tags supported by default plugins and consider whether it's something that should be part of the original plugin. If so, open a pull request!

A good practice for naming plugin packages is europa-plugin-<markdown-feature>. For example; europa-plugin-link and not europa-plugin-a, and europa-plugin-quote and not europa-plugin-q. Each plugin should aim to support a specific Markdown feature.

Take a look at Europa Build to quickly generate a Europa plugin package.

Presets

Europa also has the concept of a "preset", which is essentially a bundle of plugins. In fact, all the default plugins are provided by a default preset.

A preset simply imports a collection of plugins and declares them so that they can be registered together. For example;

import { PluginApi, PluginProvider, Preset } from 'europa-core';
import examplePluginProvider from 'europa-plugin-example';
import Europa from 'europa-worker';

const pluginProviders: PluginProvider[] = [
  examplePluginProvider,
  // ...
];

const examplePresetProvider = (api: PluginApi): Preset => ({
  // All fields and methods are optional
  plugins: pluginProviders.map((pluginProvider) => pluginProvider(api)),
});

Europa.registerPreset(examplePresetProvider);

A good practice for naming preset packages is europa-preset-<markdown-feature-set>. For example; europa-preset-github could be used to register plugins that converts HTML to GitHub-flavoured Markdown. Each preset should include plugins that aim to support a related Markdown feature set.

Take a look at Europa Build to quickly generate a Europa preset package.

Bugs

If you have any problems with Europa Worker or would like to see changes currently in development you can do so here.

Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!

A list of Europa contributors can be found in AUTHORS.md.

License

Copyright © 2022 neocotic

See LICENSE.md for more information on our MIT license.