Skip to content

Add implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
ignore-scripts=true
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
*.md
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @typedef {import('./lib/index.js').Options} Options
*/

export {fromHtml} from './lib/index.js'
36 changes: 36 additions & 0 deletions lib/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @typedef {typeof import('./index.js').fromHtml} FromHtml
* @typedef {import('hast').RootContent} RootContent
*/

import {fromDom} from 'hast-util-from-dom'

const template = document.createElement('template')
const parser = new DOMParser()

/**
* @param {string} value
*/
function parseFragment(value) {
template.innerHTML = value
return template.content
}

/** @type {FromHtml} */
export function fromHtml(value, options) {
/** @type {RootContent[]} */
const children = []
const node = options?.fragment
? parseFragment(value)
: parser.parseFromString(value, 'text/html')

while (node.firstChild) {
children.push(/** @type {RootContent} */ (fromDom(node.firstChild)))
node.firstChild.remove()
}

return {
type: 'root',
children
}
}
18 changes: 18 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {Pick<import('hast-util-from-html').Options, 'fragment'>} Options
*/

import {fromHtml as fromHtmlNode} from 'hast-util-from-html'

/**
* @param {string} value
* Serialized HTML to parse.
* @param {Options} [options]
* Configuration (optional).
* @returns {import('hast').Root}
* Tree
*/
export function fromHtml(value, options) {
return fromHtmlNode(value, options)
}
98 changes: 98 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"name": "hast-util-from-html-isomorphic",
"version": "0.0.0",
"description": "hast utility that turns HTML into a syntax tree",
"license": "MIT",
"keywords": [
"unist",
"hast",
"hast-util",
"util",
"utility",
"html",
"parse",
"dom"
],
"repository": "syntax-tree/hast-util-from-html-isomorphic",
"bugs": "https://github.com/syntax-tree/hast-util-from-html-isomorphic/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Remco Haszing <remcohaszing@gmail.com>",
"contributors": [
"Remco Haszing <remcohaszing@gmail.com>"
],
"sideEffects": false,
"type": "module",
"exports": {
".": {
"worker": "./index.js",
"browser": "./lib/browser.js",
"default": "./index.js"
}
},
"files": [
"lib/",
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"hast-util-from-html": "^1.0.0",
"hast-util-from-dom": "^4.0.0"
},
"devDependencies": {
"@types/jsdom": "^21.0.0",
"@types/node": "^18.0.0",
"c8": "^7.0.0",
"jsdom": "^21.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unist-util-remove-position": "^4.0.0",
"xo": "^0.53.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-browser": "node --conditions development --conditions browser --test",
"test-node": "node --conditions development --test",
"test-worker": "node --conditions development --conditions worker --test",
"test-api": "npm run test-browser && npm run test-node && npm run test-worker",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"env": [
"es2021",
"browser"
]
},
"remarkConfig": {
"plugins": [
"preset-wooorm",
[
"remark-lint-no-html",
false
]
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
Loading