Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 19, 2024
0 parents commit 10fa4b2
Show file tree
Hide file tree
Showing 52 changed files with 1,216 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
13 changes: 13 additions & 0 deletions .github/workflows/bb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: unifiedjs/beep-boop-beta@main
with:
repo-token: ${{secrets.GITHUB_TOKEN}}
name: bb
on:
issues:
types: [closed, edited, labeled, opened, reopened, unlabeled]
pull_request_target:
types: [closed, edited, labeled, opened, reopened, unlabeled]
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v4
strategy:
matrix:
node:
- lts/hydrogen
- node
name: main
on:
- pull_request
- push
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
*.d.ts.map
*.d.ts
*.log
*.tsbuildinfo
coverage/
node_modules/
yarn.lock
!/lib/types.d.ts
!/index.d.ts
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore-scripts=true
package-lock=false
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
*.html
*.md
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type {Options} from './lib/types.js'
export {format} from './lib/index.js'
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Note: types exposed from `index.d.ts`.
export {format} from './lib/index.js'
184 changes: 184 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* @import {Nodes, RootContent, Root} from 'hast'
* @import {BuildVisitor} from 'unist-util-visit-parents'
* @import {Options, State} from './types.js'
*/

import {embedded} from 'hast-util-embedded'
import {minifyWhitespace} from 'hast-util-minify-whitespace'
import {phrasing} from 'hast-util-phrasing'
import {whitespace} from 'hast-util-whitespace'
import {whitespaceSensitiveTagNames} from 'html-whitespace-sensitive-tag-names'
import {SKIP, visitParents} from 'unist-util-visit-parents'

/** @type {Options} */
const emptyOptions = {}

/**
* Format whitespace in HTML.
*
* @param {Root} tree
* Tree.
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export function format(tree, options) {
const settings = options || emptyOptions

/** @type {State} */
const state = {
blanks: settings.blanks || [],
head: false,
indentInitial: settings.indentInitial !== false,
indent:
typeof settings.indent === 'number'
? ' '.repeat(settings.indent)
: settings.indent || ' '
}

minifyWhitespace(tree, {newlines: true})

visitParents(tree, visitor)

/**
* @type {BuildVisitor<Root>}
*/
function visitor(node, parents) {
if (!('children' in node)) {
return
}

if (node.type === 'element' && node.tagName === 'head') {
state.head = true
}

if (state.head && node.type === 'element' && node.tagName === 'body') {
state.head = false
}

if (
node.type === 'element' &&
whitespaceSensitiveTagNames.includes(node.tagName)
) {
return SKIP
}

// Don’t indent content of whitespace-sensitive nodes / inlines.
if (node.children.length === 0 || !padding(state, node)) {
return
}

let level = parents.length

if (!state.indentInitial) {
level--
}

let eol = false

// Indent newlines in `text`.
for (const child of node.children) {
if (child.type === 'comment' || child.type === 'text') {
if (child.value.includes('\n')) {
eol = true
}

child.value = child.value.replace(
/ *\n/g,
'$&' + state.indent.repeat(level)
)
}
}

/** @type {Array<RootContent>} */
const result = []
/** @type {RootContent | undefined} */
let previous

for (const child of node.children) {
if (padding(state, child) || (eol && !previous)) {
addBreak(result, level, child)
eol = true
}

previous = child
result.push(child)
}

if (previous && (eol || padding(state, previous))) {
// Ignore trailing whitespace (if that already existed), as we’ll add
// properly indented whitespace.
if (whitespace(previous)) {
result.pop()
previous = result[result.length - 1]
}

addBreak(result, level - 1)
}

node.children = result
}

/**
* @param {Array<RootContent>} list
* Nodes.
* @param {number} level
* Indentation level.
* @param {RootContent | undefined} [next]
* Next node.
* @returns {undefined}
* Nothing.
*/
function addBreak(list, level, next) {
const tail = list[list.length - 1]
const previous = tail && whitespace(tail) ? list[list.length - 2] : tail
const replace =
(blank(state, previous) && blank(state, next) ? '\n\n' : '\n') +
state.indent.repeat(Math.max(level, 0))

if (tail && tail.type === 'text') {
tail.value = whitespace(tail) ? replace : tail.value + replace
} else {
list.push({type: 'text', value: replace})
}
}
}

/**
* @param {State} state
* Info passed around.
* @param {Nodes | undefined} node
* Node.
* @returns {boolean}
* Whether `node` is a blank.
*/
function blank(state, node) {
return Boolean(
node &&
node.type === 'element' &&
state.blanks.length > 0 &&
state.blanks.includes(node.tagName)
)
}

/**
* @param {State} state
* Info passed around.
* @param {Nodes} node
* Node.
* @returns {boolean}
* Whether `node` should be padded.
*/
function padding(state, node) {
return (
node.type === 'root' ||
(node.type === 'element'
? state.head ||
node.tagName === 'script' ||
embedded(node) ||
!phrasing(node)
: false)
)
}
50 changes: 50 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Configuration.
*/
export interface Options {
/**
* List of tag names to join with a blank line (default: `[]`);
* these tags,
* when next to each other,
* are joined by a blank line (`\n\n`);
* for example,
* when `['head', 'body']` is given,
* a blank line is added between these two.
*/
blanks?: Array<string> | null | undefined
/**
* Whether to indent the first level (default: `true`);
* this is usually the `<html>`,
* thus not indenting `head` and `body`.
*/
indentInitial?: boolean | null | undefined
/**
* Indentation per level (default: `2`);
* when `number`,
* uses that amount of spaces; when `string`,
* uses that per indentation level.
*/
indent?: number | string | null | undefined
}

/**
* State.
*/
export interface State {
/**
* List of tag names to join with a blank line.
*/
blanks: Array<string>
/**
* Whether the node is in `head`.
*/
head: boolean
/**
* Whether to indent the first level.
*/
indentInitial: boolean
/**
* Indentation per level.
*/
indent: string
}
2 changes: 2 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Note: types only.
export {}
22 changes: 22 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) Titus Wormer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 10fa4b2

Please sign in to comment.