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

odt2html

v1.0.1

Published

Node.js OpenOffice ODT format to HTML converter binary and library

Downloads

163

Readme

odt2html

Node.js OpenOffice ODT format to HTML converter binary and library.

odt2html can be used to convert an OpenOffice .odt document (and basically every document created in respect with the Open Document Format) to HTML.

As a module

$ npm i odt2html --save

The module is promise-based:

var OdtConverter = require('odt2html');

OdtConverter.toHTML({
    path: 'path/to/file.odt'
})
.then(function(html) {
    // Use HTML here
}, function(err) {
    // An error occured during conversion
});

It accepts the following set of options:

var opts = {
    path: 'path/to/file.odt',
    // {String} - required
    imgFolder: 'path/to/export/images',
    // {String} - optional
    // The imgFolder will be created if it doesn't exist
    // Existing content might be overwritten by images
    // If imgFolder is not provided, document images will be inserted inline in base64
    beautify: true,
    // {Boolean} - optional (default: false)
    // Pretty-print/Indent rendered HTML
    trim: true,
    // {Boolean} - optional (default: false)
    // Remove empty paragraphs from rendered HTML
    openFilters: {},
    // {Object} - optional
    // Add custom open tags filters to improve/modify encoding to HTML
    closeFilters: {},
    // {Object} - optional
    // Add custom close tags filters to improve/modify encoding to HTML
    styleFilters: {}
    // {Object} - optional
    // Add custom style tags filters to improve/modify encoding to HTML
};

OdtConverter.toHTML(opts)
.then(function(html) {});

Extending filters

odt2html relies on sax to parse the XML content from the original document and encode it to HTML.

openFilters

The native openFilters can be extended by passing them in the option hash to the converter. Open filters will be called on the sax onopentag event.

Here is an example for the text:h open filter to convert titles:

var openFilters = {
  'text:h': function(node, styles, footnotes, opts, odt) {
    var tag = 'h';
    var level = node.attributes['text:outline-level'];
    if (level > 6) {
      level = 6;
    }
    tag += level;

    return { xml: node.name, html: tag };
};

The open filter function receives the following arguments:

// node:      the sax node element received from the onopentag event
// styles:    the global styles table
// footnotes: the global footnotes reference
// opts:      options hash of the converter
// odt:       input document as an adm-zip zipFile

Check out the adm-zip repo for more informations about this format.

To create an HTML tag for the currently processed node, the open filter function must return an object in the following form:

{
    xml: node.name,
    // {String} - required
    // passed sax node name (i.e. 'text:p')
    html: tag,
    // {String} - required
    // html tag name without the brackets (i.e. 'h' or 'p')
    attrs: attrs,
    // {Array} - optional
    // An array of attributes to create the HTML tag
    // The attributes are in the form { name: 'href', value: 'https://link.com' }
    isSelfClosing: true
    // {Boolean} - optional
    // Is the returned HTML tag self-closing (i.e. { xml: 'draw:image', html: 'img', isSelfClosing: true })
}

Open filter functions can also return a string of HTML that will be appended in place, or null, in which case the node won't be processed to HTML.

closeFilters

The native closeFilters can be extended by passing them in the option hash to the converter. Close filters will be called on the sax onclosetag event.

They are called with the same arguments as the openFilters, except for node, since the onclosetag sax event only pass the closing tag as a text string.

Their return value is not used.

styleFilters

The native styleFilters can be extended by passing them in the option hash to the converter. Style filters will be called on the sax onopentag event.

If a style filter function is matched for an opening tag, the tag won't be further processed to HTML (i.e. it won't be searched for in the openFilters).

Style filters are used to manage the styles table, which helps adding text decoration tags (<strong>, <em>, <blockquote>, ...) and manage lists styling. They receive as arguments the current node and the global styles table and their return value is not used.