deps-html
Parse the external dependencies of an HTML string as well as optionally replace them. Useful for build systems and package managers that need to rewrite URLs.
var deps = ; var string = '<link rel="stylesheet" href="normalize.css">';// build the node treevar ast = deps;// parse all the dependenciesvar matches = deps;// rewrite the `href`smatches// return the new HTMLvar html = deps// html === '<link rel="stylesheet" href="./normalize.css">'
API
var ast = deps.parse(string)
Returns a parse5 tree.
var matches = deps.extract(ast)
Parses all the relevant dependencies from the document. Each match has the following properties:
path
- thehref
orsrc
attribute valuetype
- the type of dependency. The current types are:<script>
<module>
<style>
<link>
ref="import"
ref="stylesheet"
<img>
svg xlink
node
- theparse5
node for this dependencyattr
- the rawparse5
attribute for the node containinghref
orsrc
. Setattr.value=
to change the value of the URL.
You are expected to use a module such as parse5-utils to manipulate the HTML elements.
var html = deps.stringify(ast)
Stringify the AST, keeping the relevant styles intact. It doesn't keep everything intact, which is annoying, but most of the styles remain intact.