-
Notifications
You must be signed in to change notification settings - Fork 353
Add copy-as-markdown button functionality #2176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yordis
wants to merge
1
commit into
elixir-lang:main
Choose a base branch
from
yordis:yordis/add-copy-as-markdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,17 @@ | ||
| .copy-markdown-button { | ||
| & svg[aria-live="polite"] { | ||
| display: none; | ||
| } | ||
| } | ||
|
|
||
| .copy-markdown-button.clicked { | ||
| color: var(--success); | ||
|
|
||
| & svg[aria-live="polite"] { | ||
| display: block; | ||
| } | ||
|
|
||
| & svg:first-child { | ||
| display: none; | ||
| } | ||
| } |
This file contains hidden or 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,77 @@ | ||
| import buttonHtml from './handlebars/templates/markdown-copy-button.html' | ||
| import { qsAll } from './helpers' | ||
|
|
||
| /** @type {HTMLButtonElement} */ | ||
| let buttonTemplate | ||
|
|
||
| /** | ||
| * Initializes copy-as-markdown buttons. | ||
| * Fetches the .md version of the page and copies it to clipboard. | ||
| * Only adds buttons if the markdown file exists. | ||
| */ | ||
|
|
||
| window.addEventListener('exdoc:loaded', initializeCopyMarkdownButtons) | ||
|
|
||
| function initializeCopyMarkdownButtons () { | ||
| if (!('clipboard' in navigator)) { | ||
| return | ||
| } | ||
|
|
||
| // Check if markdown formatter is enabled | ||
| if (!window.__EXDOC__?.formatters?.includes('markdown')) { | ||
| return | ||
| } | ||
|
|
||
| const markdownUrl = getMarkdownUrl() | ||
|
|
||
| qsAll('.top-heading.heading-with-actions').forEach(container => { | ||
| if (container.querySelector('.copy-markdown-button')) return | ||
|
|
||
| if (!buttonTemplate) { | ||
| const div = document.createElement('div') | ||
| div.innerHTML = buttonHtml | ||
| buttonTemplate = div.firstChild | ||
| } | ||
|
|
||
| const button = buttonTemplate.cloneNode(true) | ||
| button.addEventListener('click', () => handleCopyMarkdown(button, markdownUrl)) | ||
| container.appendChild(button) | ||
| console.log('Added copy-markdown button') | ||
| }) | ||
| } | ||
|
|
||
| function getMarkdownUrl () { | ||
| const url = new URL(window.location.href) | ||
| // Replace .html with markdown/filename.md | ||
| // e.g., /doc/ExDoc.Markdown.html -> /doc/markdown/ExDoc.Markdown.md | ||
| const markdownPath = url.pathname.replace(/([^/]+)\.html$/, 'markdown/$1.md') | ||
| return url.origin + markdownPath | ||
| } | ||
|
|
||
| async function handleCopyMarkdown (button, markdownUrl) { | ||
| const originalInnerHTML = button.innerHTML | ||
|
|
||
| try { | ||
| const response = await fetch(markdownUrl) | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch markdown: ${response.status}`) | ||
| } | ||
|
|
||
| const markdownContent = await response.text() | ||
|
|
||
| await navigator.clipboard.writeText(markdownContent) | ||
|
|
||
| button.classList.add('clicked') | ||
| button.disabled = true | ||
|
|
||
| setTimeout(() => { | ||
| button.classList.remove('clicked') | ||
| button.disabled = false | ||
| button.innerHTML = originalInnerHTML | ||
| }, 3000) | ||
| } catch (error) { | ||
| console.error('Failed to copy markdown:', error) | ||
| button.disabled = false | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,9 @@ | ||
| <button class="copy-markdown-button icon-action" title="Copy as Markdown" aria-label="Copy as Markdown"> | ||
| <svg role="img" aria-label="copy-markdown" viewBox="0 0 24 24" fill="currentColor"> | ||
| <path d="M0 0h24v24H0z" fill="none"/> | ||
| <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/> | ||
| </svg> | ||
| <svg aria-live="polite" role="img" aria-label="copied" viewBox="0 0 24 24" fill="currentColor"> | ||
| <path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" /> | ||
| </svg> | ||
| </button> |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...atters/html/dist/html-elixir-VYWJUHZE.css → ...atters/html/dist/html-elixir-KNGKVQPZ.css
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...atters/html/dist/html-erlang-TPJSOIRZ.css → ...atters/html/dist/html-erlang-KGZROTJ5.css
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅 I can change it to something better instead of
inspect