|
| 1 | +--- |
| 2 | +export interface Props { |
| 3 | + copyLabel?: string; |
| 4 | + copiedLabel?: string; |
| 5 | +} |
| 6 | +
|
| 7 | +const { copyLabel = "Copy page", copiedLabel = "Copied!" } = Astro.props; |
| 8 | +--- |
| 9 | + |
| 10 | +<copy-page-button> |
| 11 | + <button class="copy-page-btn" type="button" aria-label={copyLabel}> |
| 12 | + <svg |
| 13 | + class="copy-icon" |
| 14 | + xmlns="http://www.w3.org/2000/svg" |
| 15 | + width="16" |
| 16 | + height="16" |
| 17 | + viewBox="0 0 24 24" |
| 18 | + fill="none" |
| 19 | + stroke="currentColor" |
| 20 | + stroke-width="2" |
| 21 | + stroke-linecap="round" |
| 22 | + stroke-linejoin="round" |
| 23 | + > |
| 24 | + <rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect> |
| 25 | + <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path> |
| 26 | + </svg> |
| 27 | + <svg |
| 28 | + class="check-icon" |
| 29 | + xmlns="http://www.w3.org/2000/svg" |
| 30 | + width="16" |
| 31 | + height="16" |
| 32 | + viewBox="0 0 24 24" |
| 33 | + fill="none" |
| 34 | + stroke="currentColor" |
| 35 | + stroke-width="2" |
| 36 | + stroke-linecap="round" |
| 37 | + stroke-linejoin="round" |
| 38 | + > |
| 39 | + <path d="M20 6 9 17l-5-5"></path> |
| 40 | + </svg> |
| 41 | + <span class="copy-label">{copyLabel}</span> |
| 42 | + <span class="copied-label">{copiedLabel}</span> |
| 43 | + </button> |
| 44 | +</copy-page-button> |
| 45 | + |
| 46 | +<script> |
| 47 | + import TurndownService from "turndown"; |
| 48 | + |
| 49 | + class CopyPageButton extends HTMLElement { |
| 50 | + private button: HTMLButtonElement | null; |
| 51 | + private turndownService: TurndownService | null = null; |
| 52 | + |
| 53 | + constructor() { |
| 54 | + super(); |
| 55 | + this.button = this.querySelector("button"); |
| 56 | + this.button?.addEventListener("click", () => this.handleCopy()); |
| 57 | + } |
| 58 | + |
| 59 | + private getTurndown(): TurndownService { |
| 60 | + if (!this.turndownService) { |
| 61 | + this.turndownService = new TurndownService({ |
| 62 | + headingStyle: "atx", |
| 63 | + codeBlockStyle: "fenced", |
| 64 | + bulletListMarker: "-", |
| 65 | + }); |
| 66 | + |
| 67 | + this.turndownService.addRule("codeBlock", { |
| 68 | + filter: (node) => { |
| 69 | + return node.nodeName === "PRE" && node.querySelector("code") !== null; |
| 70 | + }, |
| 71 | + replacement: (_content, node) => { |
| 72 | + const codeEl = (node as HTMLElement).querySelector("code"); |
| 73 | + if (!codeEl) return _content; |
| 74 | + const lang = |
| 75 | + codeEl.className |
| 76 | + .split(" ") |
| 77 | + .find((c) => c.startsWith("language-")) |
| 78 | + ?.replace("language-", "") || ""; |
| 79 | + const code = codeEl.textContent || ""; |
| 80 | + return `\n\`\`\`${lang}\n${code.replace(/\n$/, "")}\n\`\`\`\n`; |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + this.turndownService.addRule("skipCopyButtons", { |
| 85 | + filter: (node) => { |
| 86 | + const el = node as HTMLElement; |
| 87 | + return ( |
| 88 | + el.classList?.contains("copy") || |
| 89 | + (el.classList?.contains("expressive-code") === false && |
| 90 | + el.querySelector?.(".copy") !== null) |
| 91 | + ); |
| 92 | + }, |
| 93 | + replacement: () => "", |
| 94 | + }); |
| 95 | + |
| 96 | + this.turndownService.addRule("tables", { |
| 97 | + filter: "table", |
| 98 | + replacement: (_content, node) => { |
| 99 | + const table = node as HTMLTableElement; |
| 100 | + const rows = Array.from(table.querySelectorAll("tr")); |
| 101 | + if (rows.length === 0) return _content; |
| 102 | + |
| 103 | + const result: string[] = []; |
| 104 | + const headerRow = rows[0]; |
| 105 | + const headerCells = Array.from(headerRow?.querySelectorAll("th, td") ?? []); |
| 106 | + if (headerCells.length === 0) return _content; |
| 107 | + |
| 108 | + result.push(`| ${headerCells.map((c) => (c.textContent || "").trim()).join(" | ")} |`); |
| 109 | + result.push(`| ${headerCells.map(() => "---").join(" | ")} |`); |
| 110 | + |
| 111 | + for (let i = 1; i < rows.length; i++) { |
| 112 | + const cells = Array.from(rows[i]?.querySelectorAll("td, th") ?? []); |
| 113 | + result.push(`| ${cells.map((c) => (c.textContent || "").trim()).join(" | ")} |`); |
| 114 | + } |
| 115 | + |
| 116 | + return `\n${result.join("\n")}\n`; |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + this.turndownService.remove(["script", "style", "nav", "button"]); |
| 121 | + } |
| 122 | + return this.turndownService; |
| 123 | + } |
| 124 | + |
| 125 | + private async handleCopy() { |
| 126 | + const content = document.querySelector(".sl-markdown-content"); |
| 127 | + if (!content || !this.button) return; |
| 128 | + |
| 129 | + try { |
| 130 | + const title = document.querySelector("#_top")?.textContent?.trim() || ""; |
| 131 | + const clone = content.cloneNode(true) as HTMLElement; |
| 132 | + |
| 133 | + clone |
| 134 | + .querySelectorAll("button, .copy, .header-link, script, style, .not-content") |
| 135 | + .forEach((el) => el.remove()); |
| 136 | + |
| 137 | + const turndown = this.getTurndown(); |
| 138 | + let markdown = turndown.turndown(clone.innerHTML); |
| 139 | + |
| 140 | + if (title) { |
| 141 | + markdown = `# ${title}\n\n${markdown}`; |
| 142 | + } |
| 143 | + |
| 144 | + markdown = markdown.replace(/\n{3,}/g, "\n\n").trim(); |
| 145 | + |
| 146 | + await navigator.clipboard.writeText(markdown); |
| 147 | + this.showCopied(); |
| 148 | + } catch (err) { |
| 149 | + console.error("Failed to copy page as markdown:", err); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private showCopied() { |
| 154 | + this.button?.classList.add("copied"); |
| 155 | + setTimeout(() => { |
| 156 | + this.button?.classList.remove("copied"); |
| 157 | + }, 2000); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + customElements.define("copy-page-button", CopyPageButton); |
| 162 | +</script> |
| 163 | + |
| 164 | +<style> |
| 165 | + copy-page-button { |
| 166 | + display: inline-flex; |
| 167 | + } |
| 168 | + |
| 169 | + .copy-page-btn { |
| 170 | + display: inline-flex; |
| 171 | + align-items: center; |
| 172 | + gap: 0.375rem; |
| 173 | + padding: 0.25rem 0.625rem; |
| 174 | + font-size: var(--sl-text-xs); |
| 175 | + font-weight: 500; |
| 176 | + color: var(--sl-color-gray-3); |
| 177 | + background: transparent; |
| 178 | + border: 1px solid var(--sl-color-gray-5); |
| 179 | + border-radius: 1rem; |
| 180 | + cursor: pointer; |
| 181 | + transition: all 0.2s ease; |
| 182 | + white-space: nowrap; |
| 183 | + line-height: 1.5; |
| 184 | + } |
| 185 | + |
| 186 | + .copy-page-btn:hover { |
| 187 | + color: var(--sl-color-white); |
| 188 | + border-color: var(--sl-color-gray-3); |
| 189 | + background: var(--sl-color-gray-6); |
| 190 | + } |
| 191 | + |
| 192 | + .copy-page-btn .check-icon, |
| 193 | + .copy-page-btn .copied-label { |
| 194 | + display: none; |
| 195 | + } |
| 196 | + |
| 197 | + .copy-page-btn.copied .copy-icon, |
| 198 | + .copy-page-btn.copied .copy-label { |
| 199 | + display: none; |
| 200 | + } |
| 201 | + |
| 202 | + .copy-page-btn.copied .check-icon, |
| 203 | + .copy-page-btn.copied .copied-label { |
| 204 | + display: inline; |
| 205 | + } |
| 206 | + |
| 207 | + .copy-page-btn.copied { |
| 208 | + color: var(--sl-color-green-high); |
| 209 | + border-color: var(--sl-color-green-high); |
| 210 | + } |
| 211 | +</style> |
0 commit comments