Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: backport withExternalLinks to staging (#127)
* feat: add WithExternalLinks component (#124) * chore(call-types): use info mnemonic (#125) * chore(call-types): support mnemonic removal (#126)
- Loading branch information
Showing
2 changed files
with
75 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from "react" | ||
|
||
/** | ||
* Until we upgrade to Docusaurus 3 that support props in MDX, | ||
* we need to use this workaround to update the tutorial links. | ||
*/ | ||
export default class WithExternalLinks extends React.Component { | ||
/** | ||
* Replace all the links in the page with the new links. | ||
* | ||
* Format: | ||
* mapping: { | ||
* '/tutorials/video-calling/':'https://getstream.io/video/sdk/react/tutorial/video-calling/', | ||
* '/tutorials/audio-room/': 'https://getstream.io/video/sdk/react/tutorial/audio-room/', | ||
* '/tutorials/livestream/': 'https://getstream.io/video/sdk/react/tutorial/livestreaming/', | ||
* }, | ||
* | ||
* selectorsToRemove: [ | ||
* 'div[class^="video-calling-tutorial"]', | ||
* 'div[class^="livestream-tutorial"]', | ||
* ], | ||
*/ | ||
componentDidMount() { | ||
const mapping = this.props.mapping || {} | ||
const links = document.querySelectorAll("article a") | ||
links.forEach(link => { | ||
const href = link.getAttribute("href") | ||
for (const key in mapping) { | ||
if (href.endsWith(key)) { | ||
link.setAttribute("target", "_blank") | ||
link.setAttribute("href", mapping[key]) | ||
// docusaurus ignores target="_blank" so we need to add this | ||
// most likely, docusaurus does something similar internally as well. | ||
link.addEventListener("click", e => { | ||
e.preventDefault() | ||
window.open(mapping[key], "_blank") | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
const selectorsToRemove = this.props.selectorsToRemove || [] | ||
selectorsToRemove.forEach(selector => { | ||
const elements = document.querySelectorAll(selector) | ||
elements.forEach(element => { | ||
element.remove() | ||
}) | ||
}) | ||
} | ||
|
||
render() { | ||
return null | ||
} | ||
} |