Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=sha,prefix=sha-
type=raw,value=latest,enable={{is_default_branch}}

- name: Build and push Docker image
Expand Down
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako.min.js" integrity="sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb" crossorigin="anonymous"></script>
</head>
<body>
<div class="app-container">
Expand Down Expand Up @@ -119,6 +120,10 @@ <h1 class="h4 mb-0 me-2">Markdown Viewer</h1>
<i class="bi bi-clipboard"></i> Copy
</button>

<button id="share-button" class="tool-button" title="Share via URL">
<i class="bi bi-share"></i> Share
</button>

<button id="theme-toggle" class="tool-button" title="Toggle Dark Mode">
<i class="bi bi-moon"></i>
</button>
Expand Down Expand Up @@ -191,6 +196,10 @@ <h5>Menu</h5>
<i class="bi bi-clipboard me-2"></i> Copy
</button>

<button id="mobile-share-button" class="mobile-menu-item" title="Share via URL">
<i class="bi bi-share me-2"></i> Share
</button>

<button id="mobile-theme-toggle" class="mobile-menu-item" title="Toggle Dark Mode">
<i class="bi bi-moon me-2"></i> Dark Mode
</button>
Expand Down
78 changes: 78 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ document.addEventListener("DOMContentLoaded", function () {
const mobileExportPdf = document.getElementById("mobile-export-pdf");
const mobileCopyMarkdown = document.getElementById("mobile-copy-markdown");
const mobileThemeToggle = document.getElementById("mobile-theme-toggle");
const shareButton = document.getElementById("share-button");
const mobileShareButton = document.getElementById("mobile-share-button");

// Check dark mode preference first for proper initialization
const prefersDarkMode =
Expand Down Expand Up @@ -692,6 +694,7 @@ This is a fully client-side application. Your content never leaves your browser
mobileExportHtml.addEventListener("click", () => exportHtml.click());
mobileExportPdf.addEventListener("click", () => exportPdf.click());
mobileCopyMarkdown.addEventListener("click", () => copyMarkdownButton.click());
mobileShareButton.addEventListener("click", () => shareButton.click());
mobileThemeToggle.addEventListener("click", () => {
themeToggle.click();
mobileThemeToggle.innerHTML = themeToggle.innerHTML + " Toggle Dark Mode";
Expand Down Expand Up @@ -1526,6 +1529,81 @@ This is a fully client-side application. Your content never leaves your browser
}, 2000);
}

// ============================================
// Share via URL (pako compression + base64url)
// ============================================

const MAX_SHARE_URL_LENGTH = 32000;

function encodeMarkdownForShare(text) {
const compressed = pako.deflate(new TextEncoder().encode(text));
const chunkSize = 0x8000;
let binary = '';
for (let i = 0; i < compressed.length; i += chunkSize) {
binary += String.fromCharCode.apply(null, compressed.subarray(i, i + chunkSize));
}
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}

function decodeMarkdownFromShare(encoded) {
const base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
const binary = atob(base64);
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
return new TextDecoder().decode(pako.inflate(bytes));
}

shareButton.addEventListener("click", function () {
const markdownText = markdownEditor.value;
let encoded;
try {
encoded = encodeMarkdownForShare(markdownText);
} catch (e) {
console.error("Share encoding failed:", e);
alert("Failed to encode content for sharing: " + e.message);
return;
}

const shareUrl = window.location.origin + window.location.pathname + '#share=' + encoded;
if (shareUrl.length > MAX_SHARE_URL_LENGTH) {
alert("The document is too large to share via URL. Please reduce content size and try again.");
return;
}

window.location.hash = 'share=' + encoded;

const originalText = shareButton.innerHTML;
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(shareUrl).then(() => {
shareButton.innerHTML = '<i class="bi bi-check-lg"></i> Copied!';
setTimeout(() => { shareButton.innerHTML = originalText; }, 2000);
}).catch(() => {
shareButton.innerHTML = '<i class="bi bi-link-45deg"></i> Linked!';
setTimeout(() => { shareButton.innerHTML = originalText; }, 2000);
});
} else {
shareButton.innerHTML = '<i class="bi bi-link-45deg"></i> Linked!';
setTimeout(() => { shareButton.innerHTML = originalText; }, 2000);
}
});

function loadFromShareHash() {
if (typeof pako === 'undefined') return;
const hash = window.location.hash;
if (!hash.startsWith('#share=')) return;
const encoded = hash.slice('#share='.length);
if (!encoded) return;
try {
const decoded = decodeMarkdownFromShare(encoded);
markdownEditor.value = decoded;
renderMarkdown();
} catch (e) {
console.error("Failed to load shared content:", e);
alert("The shared URL could not be decoded. It may be corrupted or incomplete.");
}
}

loadFromShareHash();

const dropEvents = ["dragenter", "dragover", "dragleave", "drop"];

dropEvents.forEach((eventName) => {
Expand Down