Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
html-ast-transform
Advanced tools
A set of helpers around parse5 for transforming HTML via an AST. Allows for flexible transformations useful for eg. processing rich text editor output for email.
npm install --save html-ast-transform parse5
import {transform, h, getAttr, hasClass} from 'html-ast-transform'
// or es5:
var transform = require('html-ast-transform').transform
// umd:
const {transform, h, hasClass, getAttr} = HtmlAstTransform
const result = transform(html, options)
const input = '<div><p class="text-center">Some text</p></div>'
const output = transform(input, {
replaceTags: {
p: node => h('tr', [], [
h('td', node.attrs, node.childNodes)
])
}
})
// output = '<div><tr><td class="text-center">Some text</td></tr></div>'
const input = '<p>Text with <a href="example.com">a link</a><img alt="and an image" /></p>'
const stringifyLinks = (acc, node) => {
if (node.nodeName !== 'a') return acc.concat(node)
const href = getAttr(node, 'href')
return acc.concat(
node.childNodes,
h('#text', ` [${href}]`)
)
}
const getAltText = node => {
const alt = getAttr(node, 'alt')
return h('#text', alt ? ` [${alt}]` : '')
}
const output = transform(input, {
replaceTags: {
p: '\n',
img: getAltText
},
reduceAll: stringifyLinks
})
// output = '\nText with a link [example.com] [and an image]'
transform(input: string, {
replaceTags?: {
[tagName: string]: string | string[] | (node: Element) => Node
},
reduceAll?: (acc: Node[], node: Element, index: number, nodes: Node[]) => Node[],
stripContent?: string[],
stripTags?: string[],
trimWhitespace?: boolean,
fragment?: boolean
})
replaceTags: A mapping of tag names to their replacements. Replacements can be a string that will replace the opening tag, an array of strings that will replace opening and closing tags or a function that receives the node and returns a replacement node.
reduceAll: A reducer to run over the children of all nodes. Receives the accumulated childNodes, the current childNode, the current index and the list of childNodes.
stripContent: An array of tag names that will be removed along with their contents
stripTags An array of tag names to be removed while retaining their contents
trimWhitespace: Handle indentation and newlines by removing whitespace only text nodes and trimming text nodes with multiple leading/trailing whitespace to a single space. default = true
fragment: Parse the input as an html fragment rather than a full document. default = true
h(html: string): Node
h(type: string, value: string): TextNode | CommentNode
h(
tagName: string,
attrs: Attribute[],
childNodes: Node[]
): Element
// get the value of the named attribute, if present
getAttr(node: Element, name: string): string | undefined
// add an attribute to an element or update if it already exists
withAttr(node: Element, name: string, value: string): Element
// check if the element has a class
hasClass(node: Element, name: string): boolean
// add a class to an element if not already present
withClass(node: Element, name: string): Element
FAQs
Flexible html transformations by mapping/reducing an AST
We found that html-ast-transform demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.