Skip to content
Draft
Show file tree
Hide file tree
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
103 changes: 103 additions & 0 deletions theme/assets/page-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Page Actions JavaScript
* Handles functionality for page action buttons (Copy for LLM, etc.)
*/

async function copyPageForLLM() {
try {
// Get the raw URL, replacing master with 5.0 branch
const url = document.querySelector('meta[name="edit-url"]').content;
const rawUrl = url.replace('master', '5.0');
console.log('Fetching from URL:', rawUrl);

let markdownContent;

// Try multiple approaches to get the content
try {
// Approach 1: Direct fetch (works if CORS is enabled)
const response = await fetch(rawUrl);
if (response.ok) {
markdownContent = await response.text();
} else {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (corsError) {
console.log('Direct fetch failed due to CORS, trying proxy...');

// Approach 2: Use CORS proxy
const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(rawUrl)}`;
const proxyResponse = await fetch(proxyUrl);

if (proxyResponse.ok) {
const proxyData = await proxyResponse.json();
markdownContent = proxyData.contents;
} else {
throw new Error('Proxy fetch failed');
}
}

if (!markdownContent) {
throw new Error('No content received');
}

await navigator.clipboard.writeText(markdownContent);

// Visual feedback - success
showButtonFeedback('success', 'Copied!', '✅');

} catch (error) {
console.error('Failed to copy content:', error);

// Try fallback: open raw URL in new tab
try {
const url = document.querySelector('meta[name="edit-url"]').content;
const rawUrl = url.replace('master', '5.0');
window.open(rawUrl, '_blank');
showButtonFeedback('info', 'Opened in tab', '🔗');
} catch (fallbackError) {
showButtonFeedback('error', 'Failed', '❌');
}
}
}

function showButtonFeedback(type, message, icon) {
const button = document.querySelector('button[onclick="copyPageForLLM()"]');
if (!button) return;

const originalHTML = button.innerHTML;
button.innerHTML = `<div class="page-action-icon">${icon}</div>${message}`;

// Apply styling based on type
if (type === 'success') {
button.style.background = '#d4edda';
button.style.borderColor = '#c3e6cb';
} else if (type === 'error') {
button.style.background = '#f8d7da';
button.style.borderColor = '#f5c6cb';
} else if (type === 'info') {
button.style.background = '#d1ecf1';
button.style.borderColor = '#bee5eb';
}

// Reset after 2 seconds
setTimeout(() => {
button.innerHTML = originalHTML;
button.style.background = '#f8f9fa';
button.style.borderColor = '#e9ecef';
}, 2000);
}

// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
console.log('Page actions initialized');

// Move the page actions below the first h1 element
const pageActions = document.getElementById('page-actions');
const firstH1 = document.querySelector('.content-with-actions h1');

if (pageActions && firstH1) {
// Move the buttons to appear right after the first h1
firstH1.insertAdjacentElement('afterend', pageActions);
pageActions.style.display = 'flex'; // Ensure it's visible after moving
}
});
94 changes: 93 additions & 1 deletion theme/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,77 @@
<meta name="google-site-verification" content="AN8DVQKzis4rL9Fr3J9g3dOzY7h2jFyhqfC6ySmcgSc" />
<script>window.eol_versions = {{ config.extra.eol_versions|tojson }};</script>
<!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-KKQR5LG');</script> <!-- End Google Tag Manager -->

<!-- Page action buttons styles -->
<style>
.page-actions {
display: none; /* Initially hidden, shown after JS moves it */
gap: 12px;
margin: 8px 0 4px 0;
flex-wrap: wrap;
}

.page-action-btn {
display: inline-flex;
align-items: center;
gap: 2px;
padding: 2px 6px;
background: #f8f9fa;
border: 1px solid #e5e5e5;
border-radius: 3px;
text-decoration: none;
color: #6c757d;
font-size: 11px;
cursor: pointer;
transition: all 0.15s ease;
opacity: 0.8;
}

.page-action-btn:hover {
background: #f1f3f4;
border-color: #d6d8db;
text-decoration: none;
color: #495057;
opacity: 1;
}

.page-action-btn:active {
background: #e9ecef;
transform: translateY(0.5px);
}

.page-action-icon {
display: flex;
align-items: center;
font-size: 12px;
}

/* Override external link styling */
.page-action-btn.external::after,
.page-action-btn[rel="nofollow"]::after {
display: none !important;
}

@media (max-width: 768px) {
.page-actions {
flex-direction: column;
gap: 6px;
}

.page-action-btn {
width: 100%;
justify-content: center;
}
}
</style>

<!-- Page actions metadata -->
{% if config.repo_url and page.edit_url %}
<meta name="edit-url" content="{{ page.edit_url|replace('/edit/', '/raw/') }}">
{% endif %}

<!-- Page actions script -->
<script src="{{ 'assets/page-actions.js' | url }}"></script>
{% endblock %}
{% block site_nav %}
{% if nav %}
Expand Down Expand Up @@ -75,7 +146,28 @@
</div>
{% endif %}
{% include "partials/eol_warning.html" %}
{{ page.content }}

<div class="content-with-actions">
{{ page.content }}

<!-- Page action buttons - show only on subpages, positioned after first h1 -->
{% if config.repo_url and page.edit_url and not page.is_homepage and 'index.md' not in page.file.src_path %}
<div class="page-actions" id="page-actions">
<!-- <button class="page-action-btn" title="Ask about this page">
<div class="page-action-icon">✨</div>
Ask about this page
</button> -->
<button onclick="copyPageForLLM()" title="Copy as Markdown" class="page-action-btn">
<div class="page-action-icon">📋</div>
Copy for LLM
</button>
<a href="{{ page.edit_url|replace('/edit/', '/blob/') }}" title="View as Markdown" class="page-action-btn" rel="nofollow">
<div class="page-action-icon">📄</div>
View as Markdown
</a>
</div>
{% endif %}
</div>
{% include "partials/tags.html" %}
</div>
{% endblock %}
Loading