Skip to content
Automatically add the doctype in HTML
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.editorconfig
.gitignore
.rollup.js
.tape.js
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
INSTALL.md
LICENSE.md
README.md
package.json

README.md

pHTML Doctype pHTML

NPM Version Build Status Support Chat

pHTML Doctype lets you automatically add the doctype in HTML.

<html>
  <title>pHTML Doctype</title>
</html>

<!-- becomes -->

<!doctype html><html>
  <title>pHTML Doctype</title>
</html>

Usage

Transform HTML files directly from the command line:

npx phtml source.html output.html -p @phtml/doctype

Node

Add pHTML Doctype to your project:

npm install @phtml/doctype --save-dev

Use pHTML Doctype to process your HTML:

const phtmlDoctype = require('@phtml/doctype');

phtmlDoctype.process(YOUR_HTML /*, processOptions, pluginOptions */);

Or use it as a pHTML plugin:

const phtml = require('phtml');
const phtmlDoctype = require('@phtml/doctype');

phtml([
  phtmlDoctype(/* pluginOptions */)
]).process(YOUR_HTML /*, processOptions */);

pHTML Doctype runs in all Node environments, with special instructions for:

Node CLI Eleventy Gulp Grunt

Options

doctype

The doctype option specifies the doctype being added to the page.

phtmlDoctype({ doctype: 'html4' });
<html>
  <title>pHTML Doctype</title>
</html>

<!-- becomes -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <title>pHTML Doctype</title>
</html>

Options include case-insensitive, space-insentive variations of the following:

  • HTML or HTML 5
  • HTML 4 or HTML 4.01 Strict
  • HTML 4.01 Transitional
  • HTML 4.01 Frameset
  • XHTML or XHTML 1.1
  • XHTML 1 or XHTML 1.0 Strict
  • XHTML 1.0 Transitional
  • XHTML 1.0 Frameset

replace

The replace option determines whether the doctype will be replaced, whether or not it exists.

phtmlDoctype({ replace: true });
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <title>pHTML Doctype</title>
</html>

<!-- becomes -->

<!doctype html>
<html>
  <title>pHTML Doctype</title>
</html>

Optionally, the replace option can be given a string, which will work as a fallback to the doctype option.

safe

The safe option determines whether the doctype should only be included when a top level html, head, or body tag is present. By default, this setting is false and the doctype is added regardless.

phtmlDoctype({ safe: true });
<custom-element></custom-element>

<!-- becomes -->

<custom-element></custom-element>
<html>
  <custom-element></custom-element>
</html>

<!-- becomes -->

<!doctype html>
<html>
  <custom-element></custom-element>
</html>
You can’t perform that action at this time.