Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 86 additions & 2 deletions www/src/components/PageTitle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const {
lastUpdated,
entry: {
data: { title, author },
body,
},
} = Astro.locals.starlightRoute;
const slug = Astro.url.pathname.replace(/^\//, "").replace(/\/$/, "");

const isBlogPost = slug.startsWith('blog/');
const isDocsPost = slug.startsWith('docs/');
---

{ isBlogPost
Expand All @@ -37,10 +39,49 @@ const isBlogPost = slug.startsWith('blog/');
</div>
</Fragment>
: <Fragment>
<h1 id="_top">{title}</h1>
{ description && <p class="page-description">{description}</p> }
<div class="title-container">
<h1 id="_top">{title}</h1>
{isDocsPost ? (
<button
class="copy-markdown-btn"
data-copy-as-markdown-button
data-body={body}
>
Copy as Markdown
</button>
) : (
<Fragment></Fragment>
)}
</div>
{description && <p class="page-description">{description}</p>}
</Fragment>
}
<script>
const buttons = document.querySelectorAll('[data-copy-as-markdown-button]');

buttons.forEach((button) => {
button.addEventListener('click', async () => {
const body = button.dataset.body;
if (!body || button.disabled) return;

try {
await navigator.clipboard.writeText(body);

const originalText = button.textContent;
button.textContent = 'Copied!';
button.disabled = true;

setTimeout(() => {
button.textContent = originalText;
button.disabled = false;
}, 1000);
} catch (err) {
console.error('Failed to copy markdown:', err);
}
});
});
</script>


<style>
h1 {
Expand All @@ -57,6 +98,49 @@ p.page-description {
color: var(--color-text-secondary);
}

.title-container {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
}

.copy-markdown-btn {
background: transparent;
color: var(--color-text-secondary);
border: 1px solid var(--divider-color);
border-radius: 4px;
padding: 0.3rem 0.6rem;
font-size: 0.875rem;
cursor: pointer;
/* Adding a fixed width to prevent layout shift on press */
width: 145px;
transition:
color 0.2s ease,
border-color 0.2s ease,
background-color 0.2s ease,
opacity 0.2s ease;
}

.copy-markdown-btn:hover {
color: var(--color-text);
border-color: var(--color-text-dimmed);
background-color: rgba(255, 255, 255, 0.05);
}

:root[data-theme='light'] .copy-markdown-btn:hover {
background-color: rgba(0, 0, 0, 0.05);
}

/* Disabled “copied” state */
.copy-markdown-btn:disabled {
opacity: 0.6;
cursor: default;
background: transparent;
border-color: var(--color-text-dimmed);
color: var(--color-text-dimmed);
}

h1.blog {
margin-top: 0.25rem;
}
Expand Down