forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegateMarkdownLinks.js
More file actions
49 lines (41 loc) · 1.09 KB
/
DelegateMarkdownLinks.js
File metadata and controls
49 lines (41 loc) · 1.09 KB
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
import { Component } from "react";
import PropTypes from "prop-types";
import { withRouter } from "react-router-dom";
import basename from "../basename.js";
const matchBase = new RegExp(`^${basename}`);
function removeBase(href) {
return href.replace(matchBase, "");
}
let delegate = history => {
document.body.addEventListener("click", e => {
let node = e.target;
while (node) {
// document or svg has weird stuff
if (typeof node.className === "string") {
if (node.className.match(/internal-link/)) {
e.preventDefault();
const href = removeBase(node.getAttribute("href"));
history.push(href);
break;
}
}
node = node.parentNode;
}
});
delegate = () => {};
};
const DelegateMarkdownLinks = withRouter(
class extends Component {
static propTypes = {
history: PropTypes.object.isRequired,
children: PropTypes.node
};
componentDidMount() {
delegate(this.props.history);
}
render() {
return this.props.children;
}
}
);
export default DelegateMarkdownLinks;