A grunt plugin to convert a text file to a file containing a data URI
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



NPM version Build Status Dependency Status devDependency Status

A grunt plugin to convert a text file to a file with a data URI in base64 or simple URI encoding. Suitable to convert an HTML file to a data:text/html;charset=utf-8;base64,... bookmark URI encoded in base64. Or use it to convert a CSV file to a data:text/csv;charset=utf-8,... URI using basic encodeURIComponent() encoding. It may also be useful for creating URLs to communicate between web apps and iOS apps using URI protocol schemes. For converting a .js file to a javascript: URI, please see the js2uri grunt plugin.


An HTML file containing:

<html lang=en><head><title>sample</title><body>content</body><html>

might become


A CSV file containing:

"Crosby, Stills, Nash & Young", "Déjà Vu"

might become


Getting Started


Install this grunt plugin next to your project's Gruntfile.js gruntfile with: npm install text2datauri

Edit Gruntfile.js

Add the following to the grunt.initConfig section of your project's Gruntfile.js file:

text2datauri:  {
  'dist/outputFilename.uri': 'dist/lintedAndMinifiedFile.js'

Edit the values for the dist/output... (destination file) and the dist/lintedAndMinifiedFile.js source) as appropriate. See Documenation below for details on additional text2datauri options.

Below the grunt.initConfig section, load text2datauri as an external task before using it.

// load external task

// default task
grunt.registerTask('default', [ "text2datauri" ] );


The elaborated Gruntfile.js below shows options and defaults relating to text2datauri.

module.exports = function(grunt) {

  // Project configuration.
    // note critical jshint options for strict, scripturl, & browser
    jshint: {
      files: ['Gruntfile.js', 'src/*.js'],
      options: {
      strict: false,
      scripturl: true,
      browser: true
      globals: {}
    // note compact file spec-- 'destinationfile': ['sourcefile'],
    uglify: {
      'dist/lintedAndMinifiedFile.js': ['src/*.js'],
      options: {
        mangle: {toplevel: true},
        squeeze: {sequences: false, conditionals: false, hoist_vars: true},
        codegen: {quote_keys: false}
    // text2datauri default options are shown, file spec uses compact form
    text2datauri:  {
      'dist/outputFilename.uri': 'dist/lintedAndMinifiedFile.js',
      options: {
        sourceCharset: 'utf-8', // 'utf-8' or 'ascii'; actual format not validated
        protocol: 'data:', // any string; this is not validated by text2datauri
        mimeType: 'text/html', // any string;  this is not validated by text2datauri
        targetCharset: 'utf-8', // 'utf-8' or ''; (metadata) output is always utf-8
        encoding: 'base64' // 'base64' or 'uri'; use 'uri' for encodeURIComponent()

  // load external task

  // default task
  grunt.registerTask('default', [ "text2datauri" ] );


In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

1.0.4: switch to eslint and apply best practices feedback from it

1.0.3: updates based on lint and for Code Climate testing/feedback

1.0.2: updated package.json and .travis.tml to use node >5.0.0 and grunt >1.0.0

1.0.1: update internals to use es6 let/const; update Travis CI .travis.yml to improve build checks/process

1.0.0: update to current LTS versions of node (4.x, 5.x); update to use grunt-contrib-... 1.0.0 or higher

0.0.27: update package.json to work with grunt-contrib-jshint >=0.11.0 to allow use of 1.00 and beyond

0.0.26: update package.json to work with grunt-contrib-jshint 0.11.x - 0.12.x; update Copyright in README

0.0.25: update .travis.yml to test with node 0.10 0.11, 0.12, 4.2 and 5.0

0.0.24: update package.json to support use of node >= 0.10.0 (e.g., now works w/ node 4.0.x)

0.0.23 - 0.0.20: NO FUNCTIONAL CHANGE. bump version so npmjs.com will accept new docs (Markdown parser issues with npmjs.com v. github.com)

0.0.19: update license info in package.json to use new property and SPDX format

0.0.18: update devDependencies and copyright info

0.0.17: work with node engine 0.10.x - 0.12.x

0.0.16: January 22, 2015 -update to grunt-contrib-jshint 0.11.x; add some stricter checks

0.0.15: June 10, 2014 - update to grunt-contrib-nodeunit 0.4.x

0.0.14: April 21, 2014 - update to grunt-contrib-jshint 0.10.x

0.0.13: March 26, 2014 - update to grunt-contrib-jshint 0.9.x; grunt-contrib-nodeunit to 0.3.x

0.0.12: December 27, 2013 - update to grunt-contrib-jshint 0.8.x and streamline dependencies

0.0.11: December 9, 2013 - update to grunt-contrib-jshint 0.7.x and integrate Travis CI

0.0.10: November 15, 2013 - bump version and remember to update doc

0.0.9: November 15, 2013 - update dependancies in package.json, bump version (forget to update doc)

0.0.8: July 28, 2013 - update dependancies in package.json, bump version

0.0.7: July 12, 2013 - update dependancies in package.json, bump version

0.0.6: May 20, 2013 - update license URL in package.json, bump version

0.0.5 May 18, 2013 - Update package.json for node>=0.10.7 and jshint >= 0.5.2, Gruntfile.js edited to remove es5: true, (now default for jshint 0.5.2)

0.0.4 March 12, 2013 - Update package.json for node 0.10.0 (ie node>=0.10.0), 'use strict'; added to Gruntfile.js

0.0.3 February 28, 2013 - Stricter jshint Gruntfile options; package.json updates

0.0.2 February 20, 2013 - fully functional / 'beta', w/grunt 0.4.0, Gruntfile.js

0.0.1 February 4, 2013 - initial commit / ALPHA SOFTWARE - NOT FULLY FUNCTIONAL


Copyright (c) 2012-2017 Tom King. Licensed under the MIT license.