Skip to content

Commit

Permalink
Refactor to move implementations to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 30, 2023
1 parent e0ac79f commit 8a5ae1d
Show file tree
Hide file tree
Showing 136 changed files with 3,227 additions and 3,134 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"prepack": "npm run build && npm run format",
"test": "npm run generate && npm run build && npm run format && npm run test-coverage",
"test": "npm run build && npm run generate && npm run format && npm run test-coverage",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --100 --check-coverage --reporter lcov npm run test-api"
},
Expand Down
35 changes: 1 addition & 34 deletions packages/hast-util-from-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,4 @@
* Given node (`Node`).
*/

/**
* @typedef {import('hast').Nodes} Nodes
*/

// To do: next major: remove return result.
/**
* Set the plain-text value of a hast node.
* This is like the DOMs `Node#textContent` setter.
* The given node is returned.
*
* @template {Nodes} Thing
* Node kind.
* @param {Thing} node
* Node to change.
* @param {string | null | undefined} [value='']
* Value to use (default: `''`)
* @returns {Thing}
* Given node.
*/
export function fromString(node, value) {
const normalized = value === undefined || value === null ? '' : String(value)

if ('children' in node) {
node.children = []

if (value) {
node.children.push({type: 'text', value: normalized})
}
} else if (node.type !== 'doctype') {
node.value = normalized
}

return node
}
export {fromString} from './lib/index.js'
34 changes: 34 additions & 0 deletions packages/hast-util-from-string/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

// To do: next major: remove return result.
/**
* Set the plain-text value of a hast node.
* This is like the DOMs `Node#textContent` setter.
* The given node is returned.
*
* @template {Nodes} Thing
* Node kind.
* @param {Thing} node
* Node to change.
* @param {string | null | undefined} [value='']
* Value to use (default: `''`)
* @returns {Thing}
* Given node.
*/
export function fromString(node, value) {
const normalized = value === undefined || value === null ? '' : String(value)

if ('children' in node) {
node.children = []

if (value) {
node.children.push({type: 'text', value: normalized})
}
} else if (node.type !== 'doctype') {
node.value = normalized
}

return node
}
3 changes: 2 additions & 1 deletion packages/hast-util-from-string/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0"
Expand Down
39 changes: 1 addition & 38 deletions packages/hast-util-is-body-ok-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,4 @@
* Whether a node is a “body OK” link (`boolean`).
*/

/**
* @typedef {import('hast').Nodes} Nodes
*/

const list = new Set(['pingback', 'prefetch', 'stylesheet'])

/**
* Checks whether a node is a “body OK” link.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a “body OK” link.
*/
export function isBodyOkLink(node) {
if (node.type !== 'element' || node.tagName !== 'link') {
return false
}

if (node.properties.itemProp) {
return true
}

const rel = node.properties.rel
let index = -1

if (!Array.isArray(rel) || rel.length === 0) {
return false
}

while (++index < rel.length) {
if (!list.has(String(rel[index]))) {
return false
}
}

return true
}
export {isBodyOkLink} from './lib/index.js'
38 changes: 38 additions & 0 deletions packages/hast-util-is-body-ok-link/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

const list = new Set(['pingback', 'prefetch', 'stylesheet'])

/**
* Checks whether a node is a “body OK” link.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a “body OK” link.
*/
export function isBodyOkLink(node) {
if (node.type !== 'element' || node.tagName !== 'link') {
return false
}

if (node.properties.itemProp) {
return true
}

const rel = node.properties.rel
let index = -1

if (!Array.isArray(rel) || rel.length === 0) {
return false
}

while (++index < rel.length) {
if (!list.has(String(rel[index]))) {
return false
}
}

return true
}
3 changes: 2 additions & 1 deletion packages/hast-util-is-body-ok-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0"
Expand Down
18 changes: 1 addition & 17 deletions packages/hast-util-is-conditional-comment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,4 @@
* Whether a node is a conditional comment (`boolean`).
*/

const re = /^\[if[ \t\f\n\r]+[^\]]+]|<!\[endif]$/

/**
* @typedef {import('hast').Nodes} Nodes
*/

/**
* Check if a node is a conditional comment.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a conditional comment.
*/
export function isConditionalComment(node) {
return node.type === 'comment' && re.test(node.value)
}
export {isConditionalComment} from './lib/index.js'
17 changes: 17 additions & 0 deletions packages/hast-util-is-conditional-comment/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const re = /^\[if[ \t\f\n\r]+[^\]]+]|<!\[endif]$/

/**
* @typedef {import('hast').Nodes} Nodes
*/

/**
* Check if a node is a conditional comment.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a conditional comment.
*/
export function isConditionalComment(node) {
return node.type === 'comment' && re.test(node.value)
}
3 changes: 2 additions & 1 deletion packages/hast-util-is-conditional-comment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0"
Expand Down
33 changes: 1 addition & 32 deletions packages/hast-util-is-css-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,4 @@
* Whether a node is a `<link>` that references CSS (`boolean`).
*/

/**
* @typedef {import('hast').Nodes} Nodes
*/

import {collapseWhiteSpace} from 'collapse-white-space'

/**
* Check whether a hast node is a `<link>` that references CSS.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a CSS link.
*/
export function isCssLink(node) {
if (node.type !== 'element' || node.tagName !== 'link') {
return false
}

const rel = node.properties.rel

if (!rel || !Array.isArray(rel) || !rel.includes('stylesheet')) {
return false
}

const value = collapseWhiteSpace(String(node.properties.type || ''), {
style: 'html',
trim: true
}).toLowerCase()

return value === '' || value === 'text/css'
}
export {isCssLink} from './lib/index.js'
32 changes: 32 additions & 0 deletions packages/hast-util-is-css-link/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

import {collapseWhiteSpace} from 'collapse-white-space'

/**
* Check whether a hast node is a `<link>` that references CSS.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a CSS link.
*/
export function isCssLink(node) {
if (node.type !== 'element' || node.tagName !== 'link') {
return false
}

const rel = node.properties.rel

if (!rel || !Array.isArray(rel) || !rel.includes('stylesheet')) {
return false
}

const value = collapseWhiteSpace(String(node.properties.type || ''), {
style: 'html',
trim: true
}).toLowerCase()

return value === '' || value === 'text/css'
}
3 changes: 2 additions & 1 deletion packages/hast-util-is-css-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0",
Expand Down
27 changes: 1 addition & 26 deletions packages/hast-util-is-css-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,4 @@
* Whether a node is a `<style>` that references CSS (`boolean`).
*/

/**
* @typedef {import('hast').Nodes} Nodes
*/

import {collapseWhiteSpace} from 'collapse-white-space'

/**
* Check whether a hast node is a `<style>` that contains CSS.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a CSS style element.
*/
export function isCssStyle(node) {
if (node.type !== 'element' || node.tagName !== 'style') {
return false
}

const value = collapseWhiteSpace(String(node.properties.type || ''), {
style: 'html',
trim: true
}).toLowerCase()

return value === '' || value === 'text/css'
}
export {isCssStyle} from './lib/index.js'
26 changes: 26 additions & 0 deletions packages/hast-util-is-css-style/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

import {collapseWhiteSpace} from 'collapse-white-space'

/**
* Check whether a hast node is a `<style>` that contains CSS.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a CSS style element.
*/
export function isCssStyle(node) {
if (node.type !== 'element' || node.tagName !== 'style') {
return false
}

const value = collapseWhiteSpace(String(node.properties.type || ''), {
style: 'html',
trim: true
}).toLowerCase()

return value === '' || value === 'text/css'
}
3 changes: 2 additions & 1 deletion packages/hast-util-is-css-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0",
Expand Down
Loading

0 comments on commit 8a5ae1d

Please sign in to comment.