docs_vue2/one/build/remark-anchor.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

import visit from 'unist-util-visit'
import { upperFirst } from './util'
const KNOWN_SCOPES_CONFIG = {
props: '属性',
events: '事件',
methods: '方法',
slots: '插槽',
icons: '图标',
2021-10-29 17:12:04 +08:00
configs: '全局配置',
options: '绑定值',
modifiers: '修饰符',
arguments: '参数'
}
const KNOWN_SCOPES = Object.entries(KNOWN_SCOPES_CONFIG).reduce(
(acc, [key, value]) => {
acc[upperFirst(key)] = key
acc[value] = key
return acc
},
{}
)
export default function attacher () {
return tree => {
let scope = null
2021-10-25 20:19:04 +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]
}
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]
}
}
})
}
}