2021-10-20 01:11:27 +08:00
|
|
|
import visit from 'unist-util-visit'
|
2022-01-19 14:21:05 +08:00
|
|
|
import { upperFirst } from './util'
|
2021-10-20 01:11:27 +08:00
|
|
|
|
|
|
|
const KNOWN_SCOPES_CONFIG = {
|
|
|
|
props: '属性',
|
|
|
|
events: '事件',
|
|
|
|
methods: '方法',
|
|
|
|
slots: '插槽',
|
|
|
|
icons: '图标',
|
2021-10-29 17:12:04 +08:00
|
|
|
configs: '全局配置',
|
|
|
|
options: '绑定值',
|
|
|
|
modifiers: '修饰符',
|
|
|
|
arguments: '参数'
|
2021-10-20 01:11:27 +08:00
|
|
|
}
|
|
|
|
const KNOWN_SCOPES = Object.entries(KNOWN_SCOPES_CONFIG).reduce(
|
|
|
|
(acc, [key, value]) => {
|
2022-01-19 14:21:05 +08:00
|
|
|
acc[upperFirst(key)] = key
|
2021-10-20 01:11:27 +08:00
|
|
|
acc[value] = key
|
|
|
|
return acc
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
|
|
|
export default function attacher () {
|
|
|
|
return tree => {
|
|
|
|
let scope = null
|
2021-10-25 20:19:04 +08:00
|
|
|
|
2021-10-20 01:11:27 +08:00
|
|
|
visit(tree, (node, index, parent) => {
|
|
|
|
const { type, depth, children, value, position } = node
|
|
|
|
if (type === 'heading') {
|
|
|
|
if (depth === 3) {
|
2022-05-26 19:16:47 +08:00
|
|
|
let text = children[0]
|
|
|
|
if (text && text.type === 'element' && text.tagName === 'icon-link') {
|
|
|
|
text = children[1]
|
|
|
|
}
|
2021-10-20 01:11:27 +08:00
|
|
|
if (text && text.type === 'text' && KNOWN_SCOPES[text.value]) {
|
|
|
|
scope = KNOWN_SCOPES[text.value]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth <= 3) {
|
|
|
|
scope = null
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
type === 'inlineCode' &&
|
|
|
|
position.end.offset - position.start.offset - value.length === 4
|
|
|
|
) {
|
|
|
|
const id = `${scope || 'doc'}-${value.replace(/\./g, '-')}`
|
|
|
|
|
|
|
|
node.data = node.data || {}
|
|
|
|
node.data.hProperties = { id }
|
|
|
|
|
|
|
|
parent.children[index] = {
|
|
|
|
type: 'link',
|
|
|
|
url: `#${id}`,
|
|
|
|
children: [node]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|