docs_vue2/one/build/rehype-util-class.js

37 lines
859 B
JavaScript
Raw Permalink Normal View History

2020-08-13 11:47:56 +08:00
import { remove, get, set } from 'lodash'
const CLASSNAME_PATH = 'properties.className'
export function toggleClass (node, name, add) {
let className = get(node, CLASSNAME_PATH)
if (!className) {
set(node, CLASSNAME_PATH, [])
className = node.properties.className
}
if (typeof add === 'boolean') {
if (add && !className.includes(name)) {
className.push(name)
} else if (!add) {
remove(className, c => c === name)
}
} else if (className.includes(name)) {
remove(className, c => c === name)
} else {
className.push(name)
}
}
export function addClass (node, name) {
toggleClass(node, name, true)
}
export function removeClass (node, name) {
toggleClass(node, name, false)
}
export function hasClass (node, name) {
let className = get(node, CLASSNAME_PATH, [])
return className.includes(name)
}