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

@iarna/rtf-to-html

v1.1.0

Published

Convert RTF to HTML in pure JavaScript.

Downloads

8,633

Readme

@iarna/rtf-to-html

Convert RTF to HTML in pure JavaScript.

const rtfToHTML = require('@iarna/rtf-to-html')

fs.createReadStream('example.rtf').pipe(rtfToHTML((err, html) => {
  // …
})
rtfToHTML.fromStream(fs.createReadStream('example.rtf'), (err, html) => {
  // …
})
rtfToHTML.fromString('{\\rtf1\\ansi\\b hi there\\b0}', (err, html) => {
  console.log(html)
  // prints a document containing:
  // <p><strong>hi there</strong></p>
})

This is built on rtf-parser and shares its limitations.

This generates complete HTML documents from RTF documents. It does not currently have the facility to work on snippets of either.

Supported features:

  • Paragraph detection (results in <p> tags) plus empty paragraph trimming.
  • Font (as font-family: Font Name, Font Family). Font families in RTF don't map perfectly to HTML. This is the mapping we currently use:
    • roman: serif
    • swiss: sans-serif
    • script: cursive
    • decor: fantasy
    • modern: sans-serif
    • tech: monospace
    • bidi: serif
  • Font size (as font-size: #pt)
  • Bold (as <strong>)
  • Italic (as <em>)
  • Underline (as <u>)
  • Strikethrough (as <s>)
  • Superscript (as <sup>)
  • Subscript (as <sub>)
  • Foreground color (as color: rgb(#,#,#))
  • Background color (as color: rgb(#,#,#))
  • Paragraph first-line indents (as text-indent: #pt)
  • Idented regions (as padding-left: #pt)
  • Text alignment: left, right, center, justify (as text-align:)

rtfToHTML([opts], cb) → WritableStream

  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

Returns a WritableStream that you can pipe into.

rtfToHTML.fromStream(stream[, opts], cb)

  • stream - A readable stream that should contain RTF.
  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

rtfToHTML.fromString(string[, opts], cb)

  • string - A string containing RTF.
  • opts - Optional options to pass to the HTML generator. See the section on Options for details.
  • cb - A callback accepting (err, html), see the section on the Callback for details.

Callback

If we encounter an error in parsing then it will be set in err. Otherwise the resulting HTML will be in html.

Options

  • paraBreaks - (Defaults to \n\n) Inserted between resulting paragraphs.
  • paraTag - (Defaults to p) The tagname to use for paragraphs.
  • template - A function that is used to generate the final HTML document, defaults to:
    function outputTemplate (doc, defaults, content) {
      return `<!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <style>
        body {
          margin-left: ${doc.marginLeft / 20}pt;
          margin-right: ${doc.marginRight / 20}pt;
          margin-top: ${doc.marginTop / 20}pt;
          margin-bottom: ${doc.marginBottom / 20}pt;
          font-size: ${defaults.fontSize / 2}pt;
          text-indent: ${defaults.firstLineIndent / 20}pt;
        }
        </style>
      </head>
      <body>
        ${content.replace(/\n/, '\n    ')}
      </body>
    </html>`
    }

You can also configure some of the starting (default) state of the output formatting with:

  • disableFonts - Defaults to true. If you set this to false then we'll output font change information when we encounter it. This is a bit broken due to our not supporting styles.
  • fontSize - Defaults to the document-wide declared font size, or if that's missing, 24.
  • bold - Defaults to false
  • italic - Defaults to false
  • underline - Defaults to false
  • strikethrough - Defaults to false
  • foreground - Defaults to {red: 0, blue: 0, green: 0}
  • background - Defaults to {red: 255, blue: 255, green: 255}
  • firstLineIndent - Defaults to the document-wide value, or if that's missing, 0. This is how far to indent the first line of new paragraphs.
  • indent: Defaults to 0
  • align: Defaults to left.
  • valign: Defaults to normal

const rtfToHTML = require('rtf-to-html/rtf-to-html.js')

rtfToHTML(doc[, opts]) → HTML

This is internally how the other interfaces are implemented. Unlike the other interfaces, this one is synchronous.

  • doc - A parsed RTF document as produced by the rtf-parser library.
  • opts - Optional options, see the section on Options for details.