-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
143 lines (109 loc) · 3.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import visit from 'unist-util-visit'
import extend from 'extend'
const behaviors = {prepend: 'unshift', append: 'push'}
const contentDefaults = {
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']}
}
const defaults = {behavior: 'prepend', content: contentDefaults}
const splice = [].splice
let deprecationWarningIssued = false
export default function attacher(options = {}) {
let {linkProperties, behavior, content, group} = {...defaults, ...options}
let method
// NOTE: Remove in next major version
if (options.behaviour !== undefined) {
if (!deprecationWarningIssued) {
deprecationWarningIssued = true
console.warn(
'[remark-autolink-headings] Deprecation Warning: `behaviour` is a nonstandard option. Use `behavior` instead.'
)
}
behavior = options.behaviour
}
if (behavior === 'wrap') {
method = wrap
} else if (behavior === 'before' || behavior === 'after') {
method = around
} else {
method = inject
if (!linkProperties) {
linkProperties = {ariaHidden: 'true', tabIndex: -1}
}
}
return (tree) => visit(tree, 'heading', visitor)
function visitor(node, index, parent) {
const {data} = node
const id = data && data.hProperties && data.hProperties.id
if (id) {
return method(node, '#' + id, index, parent)
}
}
function inject(node, url) {
const link = create(url)
link.data = {
hProperties: toProps(linkProperties),
hChildren: toChildren(content, node)
}
node.children[behaviors[behavior]](link)
}
function around(node, url, index, parent) {
const link = create(url)
const grouping = group ? toGrouping(group, node) : undefined
link.data = {
hProperties: toProps(linkProperties),
hChildren: toChildren(content, node)
}
let nodes = behavior === 'before' ? [link, node] : [node, link]
if (grouping) {
grouping.children = nodes
nodes = grouping
}
splice.apply(parent.children, [index, 1].concat(nodes))
return [visit.SKIP, index + nodes.length]
}
function wrap(node, url) {
const link = create(url, node.children)
link.data = {hProperties: toProps(linkProperties)}
node.children = [link]
}
function toProps(value) {
return deepAssign({}, value)
}
function toNode(value, node) {
return typeof value === 'function' ? value(node) : value
}
function toChildren(value, node) {
let children = toNode(value, node)
children = Array.isArray(children) ? children : [children]
return typeof value === 'function' ? children : deepAssign([], children)
}
function toGrouping(value, node) {
const grouping = toNode(value, node)
const hName = grouping.tagName
const hProperties = grouping.properties
return {
type: 'heading-group',
data: {
hName,
hProperties:
typeof value === 'function'
? deepAssign({}, hProperties)
: hProperties
},
children: []
}
}
function create(url, children) {
return {
type: 'link',
url,
title: null,
children: children || []
}
}
function deepAssign(base, value) {
return extend(true, base, value)
}
}