Comparing version 0.0.0 to 0.1.0
{ | ||
"name": "htmlfy", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "HTML formatter yo!. Prettify, minify, and more!", | ||
@@ -5,0 +5,0 @@ "exports": { |
# htmlfy | ||
HTML formatter yo! Mainly to prettify or minify. | ||
HTML formatter yo! Prettify, minify and more! | ||
@@ -7,6 +7,5 @@ `htmlfy` is a fork of [html-formatter](https://github.com/uznam8x/html-formatter/tree/master). Most of the processing logic has been preserved, and full credit for that goes to the original author. I've made the following major enhancements. | ||
- Fully typed. | ||
- Replaced `var` declarations. | ||
- Converted to ES6. | ||
- Optional "tab" sizes to prettify the HTML. | ||
- Naming changes for functions, etc. | ||
- Converted to ESM. | ||
- Configuration options. | ||
- A lot of naming changes. | ||
@@ -22,4 +21,2 @@ ## Install | ||
See [configuration](#configuration)for adjusting the tab size. | ||
```js | ||
@@ -71,3 +68,3 @@ import { prettify } from 'htmlfy' | ||
### Entify | ||
Enforce entity characters for textarea content. By default, this also does basic minification on textareas before setting entities. When running this function as standalone, you'll likely want to pass `minify_content` as `true`, for full minification of the textarea. It does not process any other tags. | ||
Enforce entity characters for textarea content. This also performs basic minification on textareas before setting entities. When running this function as a standalone, you'll likely want to pass `minify` as `true` for full minification of the textarea. The minification does not process any other tags. | ||
@@ -91,6 +88,18 @@ ```js | ||
### Default Import | ||
If needed, you can use a default import for `htmlfy`. | ||
```js | ||
import * as htmlfy from 'htmlfy' | ||
console.log(htmlfy.prettify('<main><div>Hello World</div></main')) | ||
``` | ||
## Configuration | ||
These configuration options can only be passed to `prettify`. | ||
Default config: | ||
```js | ||
{ | ||
strict: false, | ||
tab_size: 2 | ||
@@ -100,4 +109,20 @@ } | ||
### Strict | ||
If set to `true`, removes comments and ensures void elements are not self-closing. | ||
```js | ||
import { prettify } from 'htmlfy' | ||
const html = `<main><br /><div><!-- Hello World --></div></main>` | ||
console.log(prettify(html, { strict: true })) | ||
/* | ||
<main> | ||
<br> | ||
<div></div> | ||
</main> | ||
*/ | ||
``` | ||
### Tab Size | ||
`prettify` indents using spaces. If you'd like to custom this, then pass a configuration object as the second argument. | ||
Determines the number of spaces, per tab, for indentation. | ||
@@ -104,0 +129,0 @@ ```js |
@@ -5,3 +5,4 @@ /** | ||
export const CONFIG = { | ||
strict: false, | ||
tab_size: 2 | ||
} |
@@ -7,4 +7,4 @@ /** | ||
* @param {string} html | ||
* @param {boolean} [minify_content] Fully minifies the content of textarea elements. | ||
* Defaults to `false`. Consider setting this to `true` if you're running `entify()` | ||
* @param {boolean} [minify] Fully minifies the content of textarea elements. | ||
* Defaults to `false`. We recommend a value of `true` if you're running `entify()` | ||
* as a standalone function. | ||
@@ -14,3 +14,3 @@ * @returns {string} | ||
*/ | ||
export const entify = (html, minify_content = false) => { | ||
export const entify = (html, minify = false) => { | ||
/* Trim any combination of leading line returns and/or spaces. */ | ||
@@ -45,3 +45,3 @@ html = html | ||
/* Typical minification, but only for textareas. */ | ||
if (minify_content) { | ||
if (minify) { | ||
html = html.replace(/<textarea[^>]*>((.|\n)*?)<\/textarea>/g, (match, capture) => { | ||
@@ -48,0 +48,0 @@ /* Replace things inside the textarea content. */ |
@@ -7,2 +7,7 @@ import { closify } from './closify.js' | ||
/** | ||
* @type {boolean} | ||
*/ | ||
let strict | ||
/** | ||
* @type {{ line: string[] }} | ||
@@ -59,3 +64,3 @@ */ | ||
const process = (html, step) => { | ||
/* Track current number of indentations needed */ | ||
/* Track current number of indentations needed. */ | ||
let indents = '' | ||
@@ -66,3 +71,3 @@ | ||
html = html | ||
.replace(/\n+/g, '\n') /* Replace consecutive line returns with singles */ | ||
.replace(/\n+/g, '\n') /* Replace consecutive line returns with singles. */ | ||
.replace(`[#-# : ${index} : ${source} : #-#]`, (match) => { | ||
@@ -100,2 +105,5 @@ let subtrahend = 0 | ||
/* Remove comment. */ | ||
if (strict && match.indexOf('<!--') > -1) return '' | ||
/* Remove the prefix and suffix, leaving the content. */ | ||
@@ -112,6 +120,9 @@ const result = match | ||
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */ | ||
html = html.replace(/>[^<]*?[^><\/\s][^<]*?<\/|>\s+[^><\s]|<script[^>]*>\s+<\/script>|<(\w+)>\s+<\/(\w+)|<(\w+)[^>]*>\s<\/(\w+)>|<([\w\-]+)[^>]*[^\/]>\s+<\/([\w\-]+)>/g, | ||
html = html.replace(/>[^<]*?[^><\/\s][^<]*?<\/|>\s+[^><\s]|<script[^>]*>\s+<\/script>|<(\w+)>\s+<\/(\w+)|<([\w\-]+)[^>]*[^\/]>\s+<\/([\w\-]+)>/g, | ||
match => match.replace(/\n|\t|\s{2,}/g, '') | ||
) | ||
/* Remove self-closing nature of void elements. */ | ||
if (strict) html = html.replace(/\s\/>/g, '>') | ||
return html.substring(1, html.length - 1) | ||
@@ -129,2 +140,3 @@ } | ||
const validated_config = config ? validateConfig(config) : CONFIG | ||
strict = validated_config.strict | ||
@@ -131,0 +143,0 @@ html = preprocess(html) |
import { Config } from "htmlfy" | ||
export interface DefaultConfig { | ||
strict: boolean | ||
tab_size: number | ||
@@ -5,0 +6,0 @@ } |
@@ -22,3 +22,3 @@ import { CONFIG } from './constants.js' | ||
} else { | ||
/* key is an object, run mergeObjects again */ | ||
/* key is an object, run mergeObjects again. */ | ||
merged[key] = mergeObjects(merged[key] || {}, updates[key]) | ||
@@ -55,3 +55,3 @@ } | ||
const config_empty = !(Object.hasOwn(config, 'tab_size')) | ||
const config_empty = !(Object.hasOwn(config, 'tab_size') || Object.hasOwn(config, 'strict')) | ||
if (config_empty) return CONFIG | ||
@@ -62,2 +62,3 @@ | ||
if (tab_size) { | ||
if (typeof tab_size !== 'number') throw 'Tab size must be a number.' | ||
const safe = Number.isSafeInteger(tab_size) | ||
@@ -76,4 +77,6 @@ if (!safe) throw `Tab size ${tab_size} is not safe. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger for more info.` | ||
if (Object.hasOwn(config, 'strict') && typeof config.strict !== 'boolean') throw 'Strict config must be a boolean.' | ||
return mergeConfig(CONFIG, config) | ||
} |
declare module 'htmlfy' { | ||
export interface Config { | ||
strict?: boolean | ||
tab_size?: number | ||
@@ -21,4 +22,4 @@ } | ||
* @param {string} html | ||
* @param {boolean} [minify_content] Fully minifies the content of textarea elements. | ||
* Defaults to `false`. Consider setting this to `true` if you're running `entify()` | ||
* @param {boolean} [minify] Fully minifies the content of textarea elements. | ||
* Defaults to `false`. We recommend a value of `true` if you're running `entify()` | ||
* as a standalone function. | ||
@@ -28,3 +29,3 @@ * @returns An HTML string where entities are enforced on the contents of textareas. | ||
*/ | ||
export function entify(html: string, minify_content?: boolean): string | ||
export function entify(html: string, minify?: boolean): string | ||
@@ -31,0 +32,0 @@ /** |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17006
353
137