-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cfde963
Add implementation
remcohaszing b76e508
Update readme
remcohaszing 757a67a
Avoid node --test flag
remcohaszing 4ed4810
Rename export to fromHtmlIsomorphic
remcohaszing e8f3c2e
Update readme.md
remcohaszing aa3ed85
Apply suggestions from code review
remcohaszing 0265512
Simplify browser implementation
remcohaszing 92c4844
Fix lint issue
remcohaszing ce48dac
Remove positional info
remcohaszing 2fc4a77
Add deno and react-native exports
remcohaszing e508902
Use node require preload to configure jsdom
remcohaszing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
package-lock=false | ||
ignore-scripts=true |
This file contains hidden or 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 @@ | ||
coverage/ | ||
*.md |
This file contains hidden or 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,5 @@ | ||
/** | ||
* @typedef {import('./lib/index.js').Options} Options | ||
*/ | ||
|
||
export {fromHtml} from './lib/index.js' | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,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 | ||
} | ||
} |
This file contains hidden or 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,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' | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @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) | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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,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" | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
"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", | ||
remcohaszing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.