44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import { contentAsText } from '../helpers.js'
|
|
|
|
export default async function (doc, rootNode) {
|
|
const node = {
|
|
type: rootNode.head,
|
|
variant: 'neutral',
|
|
class: '',
|
|
href: '',
|
|
text: ''
|
|
}
|
|
|
|
const tail = rootNode.tail || ''
|
|
const tailParts = tail.split(' ')
|
|
const firstWord = tailParts[0]
|
|
if (firstWord === 'primary' || firstWord === 'neutral') {
|
|
node.variant = firstWord
|
|
const tailText = tailParts.slice(1).join(' ')
|
|
if (tailText) node.text = tailText
|
|
} else {
|
|
node.variant = 'neutral'
|
|
if (tail) node.text = tail
|
|
}
|
|
|
|
for await (const child of rootNode.children()) {
|
|
if (child.is('class')) node.class = child.tail
|
|
else if (child.is('href')) node.href = child.tail
|
|
else if (!node.text) {
|
|
// If it's not a recognized attribute, treat the entire content as button text
|
|
node.text = child.content.trim()
|
|
}
|
|
}
|
|
|
|
const next = await rootNode._document._getNextNode()
|
|
if (next && next.level > rootNode.level && !next.isEmpty() && !next.head) {
|
|
if (!node.text) {
|
|
node.text = await contentAsText(next, true)
|
|
}
|
|
} else if (next) {
|
|
rootNode._document._pushBack(next)
|
|
}
|
|
|
|
return node
|
|
}
|