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

trans-amp

v1.0.9

Published

Translate raw HTML into compatible AMP HTML (Accelerated Mobile Pages)

Downloads

4

Readme

trans-amp

Description

Translate raw HTML into compatible AMP HTML (Accelerated Mobile Pages)

Overview

AMP project was an initiative to help speed up the interweb by spitting in the face of developers and forcing them to strip away all that they love dear with a start-from-scratch mentality. All ranting aside, AMP-HTML is a fantastic way to speed up your site experience and enhance SEO. For most projects, you put in the work to convert your html into AMP html that will pass the crucial validation step, and then your done -- simple right? Yes, except when you have to deal with unpredictable, dynamic html input that suddenly renders your pages dead in the eyes of your typical AMP cachers (Google, Twitter, etc.). This package seeks to provide a simple but robust way to ensure that your pesky "raw html" input can safely pass that validation step while still remaining as true to the original form as possible.

Installation

npm

npm install trans-amp --save

yarn

yarn add trans-amp

Basic Usage

const TransAmp = require('trans-amp');
const amp = new TransAmp();

// RTE - "Rich Text Editor"
var htmlFromRTE = `
    <div style="text-align: center; font-size: 0.8em;" class="boldtext">
        <img src="https://www.ampproject.org/static/img/logo-og-image.jpg" alt="AMP Project Logo" width="100%" height="100%"/>
        <a href="https://google.com" target="_self" style="text-decoration:none;">Google</a>
    </div>
    <div style="font-size: 0.8em; text-align: center;">
        <p style="text-transform: uppercase;">Accelerated Mobile pages are super quick</p>
    </div>
`;

amp.translate(htmlFromRTE, function(results){
    console.log(results.html);
    console.log(results.styles);
})

Console ouput from the above example


    <div class="boldtext transamp0">
        <amp-img src="https://www.ampproject.org/static/img/logo-og-image.jpg" alt="AMP Project Logo" width="1200" height="630" layout="responsive"></amp-img>
        <a href="https://google.com" target="_blank" class="transamp1">Google</a>
    </div>
    <div class="transamp0">
        <p class="transamp2">Accelerated Mobile pages are super quick</p>
    </div>

.transamp0{font-size:0.8em;text-align:center;}.transamp1{text-decoration:none;}.transamp2{text-transform:uppercase;}

Before discussing behavior in detail, here are some things to note from the results of the above example.

  • All of the inline styles have been removed and replaced with custom class declarations.
  • Both of the parent div's in this example now share the same custom class even though their styles originally appeared in a different order.
  • The "img" tag has been replaced by "amp-img" and now has its own closing tag.
  • The "width" and "height" attributes of the "img" tag have been automatically filled in for the particular image, and a responsive layout has been declared.
  • The "a" tag had its "target" attribute changed from "_self" to "_blank"
  • The outputed css style string is minified whereas the html output retains all original whitespace.

Configuration

Some behavior can be customized via a configuration object upon creating a new instance.

//DEFAULT CONFIGURATION
var amp = new TransAmp({
    dimensionsCacheKey: function(uri){return uri},
    stylePrependString: "transamp",
    removeChildren: true,
    keepChildrenFor: [],
    removeChildrenFor: []
})

dimensionsCacheKey : Function

  • The default behavior for caching image dimensions is to use the source URI as the cache key. You can pass in an overriding function that takes 1 argument (imageURI) and returns the key.

stylePrependString: String

  • When stripping inline styles and replacing them with a class declaration, the class name is built using the prependString and an incremented integer (ex. "transamp1"). Here you can override this prepend string, but it will be ignored if it matches "-amp-" or "i-amp-" as those values are reserved by AMP.

removeChildren: Boolean

  • Any time an html element is deemed invalid and must be removed entirely, this value dictates whether any and all of its child elements will also be removed.

keepChildrenFor: Array

  • Provides a more granular level of control for which tags should override the default and always keep their children. Provide an array of tag names.

removeChildrenFor: Array

  • Provides a more granular level of control from which tags should always remove their children. Provide and array of tag names.