|
| 1 | +import { HTMLProps, ReactNode, useRef } from "react"; |
| 2 | +import styles from "./docs.module.scss"; |
| 3 | +import readme from "../../../../../README.md?raw" |
| 4 | +import { Md } from "@m2d/react-markdown"; |
| 5 | +import { toDocx } from "mdast2docx"; |
| 6 | +import { AstArrayElement } from "@m2d/react-markdown/utils"; |
| 7 | + |
| 8 | +export interface DocsProps extends HTMLProps<HTMLDivElement> { |
| 9 | + children?: ReactNode; |
| 10 | +} |
| 11 | + |
| 12 | +export const Docs = (props: DocsProps) => { |
| 13 | + const className = [props.className, styles["docs"]].filter(Boolean).join(" "); |
| 14 | + const astRef = useRef<AstArrayElement[]>([]) |
| 15 | + return ( |
| 16 | + <div {...props} className={className} data-testid="docs"> |
| 17 | + <button onClick={() => { |
| 18 | + const mdAst = astRef.current[0].mdast; |
| 19 | + if (mdAst) { |
| 20 | + toDocx(mdAst).then(docxBlob => { |
| 21 | + // download docx blob |
| 22 | + const url = URL.createObjectURL(docxBlob as Blob); |
| 23 | + const a = document.createElement("a"); |
| 24 | + a.href = url; |
| 25 | + a.download = "document.docx"; |
| 26 | + document.body.appendChild(a); |
| 27 | + a.click(); |
| 28 | + document.body.removeChild(a); |
| 29 | + URL.revokeObjectURL(url); |
| 30 | + }).catch(() => { |
| 31 | + alert("Something went wrong!") |
| 32 | + }); |
| 33 | + } else { |
| 34 | + alert("Something went wrong!") |
| 35 | + } |
| 36 | + }}>Download as Docx</button> |
| 37 | + <Md astRef={astRef}>{readme}</Md> |
| 38 | + </div> |
| 39 | + ); |
| 40 | +} |
0 commit comments