-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added copy as markdown functionality on docs. (#1066)
* Add copy as markdown button on docs. * Fixes after review.
- Loading branch information
1 parent
d896eaa
commit 6ac4aef
Showing
4 changed files
with
87 additions
and
0 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,45 @@ | ||
import React, { useState } from "react"; | ||
import { useLocation } from "@docusaurus/router"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
import { faClone } from "@fortawesome/free-regular-svg-icons"; | ||
import { faCheck } from "@fortawesome/free-solid-svg-icons"; | ||
|
||
const CopyMarkdownButton = () => { | ||
const [ isCopied, setIsCopied ] = useState(false); | ||
const location = useLocation(); | ||
|
||
if(location.pathname === '/'){ | ||
return; | ||
} | ||
|
||
const url = `https://raw.githubusercontent.com/multiversx/mx-docs/refs/heads/main/docs${location.pathname}.md`; | ||
|
||
const copyMarkdownToClipboard = async () => { | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error("Markdown file not found!"); | ||
} | ||
|
||
const markdown = await response.text(); | ||
await navigator.clipboard.writeText(markdown); | ||
setIsCopied(true); | ||
|
||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, 1000); | ||
} catch (error) { | ||
console.error("Error copying markdown: ", error); | ||
} | ||
}; | ||
|
||
return ( | ||
<FontAwesomeIcon | ||
className={`copy-as-markdown-button ${isCopied ? 'check' : ''}`} | ||
onClick={copyMarkdownToClipboard} | ||
icon={!isCopied ? faClone : faCheck} | ||
/> | ||
); | ||
}; | ||
|
||
export default CopyMarkdownButton; |
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,12 @@ | ||
import React from "react"; | ||
import MDXContent from "@theme-original/MDXContent"; | ||
import CopyMarkdownButton from "@site/src/components/CopyMarkdownButton"; | ||
|
||
const MDXContentWrapper = (props) => ( | ||
<div className="relative"> | ||
<CopyMarkdownButton /> | ||
<MDXContent {...props} /> | ||
</div> | ||
); | ||
|
||
export default MDXContentWrapper; |