-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 10fa4b2
Showing
52 changed files
with
1,216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ignore-scripts=true | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
*.html | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Note: types only. | ||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.