markitdown / index.html
teerasakw's picture
make the program to support reading pdf file. - Initial Deployment
cc9c9f3 verified
Raw History Blame Contribute Delete
22.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File to Markdown Converter</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.dropzone {
border: 2px dashed #9ca3af;
transition: all 0.3s;
}
.dropzone.active {
border-color: #3b82f6;
background-color: #f0f9ff;
}
#markdown-output {
min-height: 200px;
}
.fade-in {
animation: fadeIn 0.5s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.text-output {
font-family: 'Courier New', monospace;
white-space: pre-wrap;
tab-size: 4;
}
.scrollbar-custom::-webkit-scrollbar {
width: 8px;
height: 8px;
}
.scrollbar-custom::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 10px;
}
.scrollbar-custom::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 10px;
}
.scrollbar-custom::-webkit-scrollbar-thumb:hover {
background: #a1a1a1;
}
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-8 max-w-6xl">
<header class="text-center mb-12">
<h1 class="text-4xl font-bold text-blue-600 mb-3">
<i class="fas fa-file-alt mr-2"></i>File to Markdown Converter
</h1>
<p class="text-gray-600 text-lg max-w-2xl mx-auto">
Convert your files to clean Markdown format using Microsoft's MarkItDown engine
</p>
</header>
<div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8">
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
<i class="fas fa-upload mr-2 text-blue-500"></i>Upload File
</h2>
<div
id="dropzone"
class="dropzone rounded-lg p-8 text-center cursor-pointer mb-4"
ondragover="dragOver(event)"
ondragleave="dragLeave(event)"
ondrop="drop(event)"
>
<i class="fas fa-cloud-upload-alt text-4xl text-blue-400 mb-3"></i>
<p class="text-gray-500 mb-2">Drag & drop your file here</p>
<p class="text-sm text-gray-400 mb-4">or</p>
<label for="fileInput" class="inline-block px-6 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition cursor-pointer">
<i class="fas fa-folder-open mr-2"></i>Browse Files
</label>
<input type="file" id="fileInput" class="hidden" onchange="handleFileSelect(event)">
</div>
<div id="fileInfo" class="hidden">
<div class="flex items-center justify-between bg-blue-50 rounded-lg p-3 mb-3">
<div class="flex items-center">
<i class="fas fa-file text-blue-500 mr-2"></i>
<span id="fileName" class="font-medium"></span>
</div>
<button onclick="clearFile()" class="text-red-500 hover:text-red-700">
<i class="fas fa-times"></i>
</button>
</div>
</div>
<div class="flex items-center justify-between mt-6">
<div>
<p class="text-sm text-gray-500">
<i class="fas fa-info-circle mr-1"></i>
Supported formats: .docx, .pptx, .html, .txt, .pdf
</p>
</div>
<button
id="convertBtn"
class="px-6 py-2 bg-green-500 text-white rounded-md hover:bg-green-600 transition disabled:opacity-50 disabled:cursor-not-allowed"
onclick="convertToMarkdown()"
disabled
>
<i class="fas fa-exchange-alt mr-2"></i>Convert
</button>
</div>
</div>
<div class="bg-white rounded-xl shadow-md p-6">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-semibold text-gray-800 flex items-center">
<i class="fas fa-code mr-2 text-purple-500"></i>Markdown Output
</h2>
<div class="flex space-x-2">
<button
id="copyBtn"
class="px-3 py-1 bg-purple-100 text-purple-600 rounded hover:bg-purple-200 text-sm disabled:opacity-50"
onclick="copyToClipboard()"
disabled
title="Copy to clipboard"
>
<i class="fas fa-copy mr-1"></i>Copy
</button>
<button
id="downloadBtn"
class="px-3 py-1 bg-blue-100 text-blue-600 rounded hover:bg-blue-200 text-sm disabled:opacity-50"
onclick="downloadMarkdown()"
disabled
title="Download as .md"
>
<i class="fas fa-download mr-1"></i>Download
</button>
</div>
</div>
<div
id="markdown-output"
class="bg-gray-50 rounded-lg p-4 text-gray-800 text-output scrollbar-custom overflow-auto h-80 border border-gray-200"
>
<div id="emptyState" class="text-center text-gray-400 py-16">
<i class="fas fa-file-code text-3xl mb-3"></i>
<p>Converted Markdown will appear here</p>
</div>
<pre id="outputContent" class="hidden"></pre>
</div>
<div class="mt-3 flex justify-between items-center">
<div id="stats" class="text-sm text-gray-500 hidden">
<span id="charCount">0</span> chars • <span id="wordCount">0</span> words
</div>
<div class="text-xs text-gray-400">
Powered by Microsoft MarkItDown
</div>
</div>
</div>
</div>
<div class="bg-white rounded-xl shadow-md p-6 mt-6">
<h2 class="text-xl font-semibold mb-4 text-gray-800 flex items-center">
<i class="fas fa-question-circle mr-2 text-yellow-500"></i>How to use
</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="bg-blue-50 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-blue-100 rounded-full p-2 mr-3">
<i class="fas fa-upload text-blue-500"></i>
</div>
<h3 class="font-medium">Upload File</h3>
</div>
<p class="text-sm text-gray-600">Drag and drop or click to browse for your document (DOCX, PPTX, HTML).</p>
</div>
<div class="bg-green-50 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-green-100 rounded-full p-2 mr-3">
<i class="fas fa-exchange-alt text-green-500"></i>
</div>
<h3 class="font-medium">Convert</h3>
</div>
<p class="text-sm text-gray-600">Click the convert button to transform your document into Markdown format.</p>
</div>
<div class="bg-purple-50 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-purple-100 rounded-full p-2 mr-3">
<i class="fas fa-copy text-purple-500"></i>
</div>
<h3 class="font-medium">Copy or Download</h3>
</div>
<p class="text-sm text-gray-600">Use the copy button for immediate use or download the Markdown file.</p>
</div>
</div>
</div>
<footer class="mt-12 text-center text-gray-500 text-sm">
<p>File to Markdown Converter using Microsoft MarkItDown engine</p>
<p class="mt-1">© 2023 - All rights reserved</p>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/mammoth@1.4.0/mammoth.browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/turndown/7.1.1/turndown.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js"></script>
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@2.14.305/build/pdf.min.js"></script>
<script>
// Global variables
let selectedFile = null;
let markdownResult = null;
// Drag and drop functions
function dragOver(e) {
e.preventDefault();
document.getElementById('dropzone').classList.add('active');
}
function dragLeave(e) {
e.preventDefault();
document.getElementById('dropzone').classList.remove('active');
}
function drop(e) {
e.preventDefault();
document.getElementById('dropzone').classList.remove('active');
if (e.dataTransfer.files.length) {
const file = e.dataTransfer.files[0];
handleFile(file);
}
}
// File selection handler
function handleFileSelect(e) {
if (e.target.files.length) {
const file = e.target.files[0];
handleFile(file);
}
}
function handleFile(file) {
const validExtensions = ['docx', 'pptx', 'html', 'txt', 'pdf'];
const fileExt = file.name.split('.').pop().toLowerCase();
if (!validExtensions.includes(fileExt)) {
showToast('Please upload a valid file (DOCX, PPTX, HTML, TXT)', 'error');
return;
}
selectedFile = file;
// Display file info
document.getElementById('fileName').textContent = file.name;
document.getElementById('fileInfo').classList.remove('hidden');
document.getElementById('convertBtn').disabled = false;
// Clear previous output
clearOutput();
}
function clearFile() {
selectedFile = null;
document.getElementById('fileInput').value = '';
document.getElementById('fileInfo').classList.add('hidden');
document.getElementById('convertBtn').disabled = true;
clearOutput();
}
function clearOutput() {
document.getElementById('emptyState').classList.remove('hidden');
document.getElementById('outputContent').classList.add('hidden');
document.getElementById('outputContent').textContent = '';
document.getElementById('copyBtn').disabled = true;
document.getElementById('downloadBtn').disabled = true;
document.getElementById('stats').classList.add('hidden');
markdownResult = null;
}
// Conversion functions
async function convertToMarkdown() {
if (!selectedFile) return;
const fileExt = selectedFile.name.split('.').pop().toLowerCase();
let loadingToast = showToast('Converting file to Markdown...', 'info', true);
try {
let result = '';
if (fileExt === 'docx') {
// Using mammoth.js for DOCX to Markdown
const arrayBuffer = await readFileAsArrayBuffer(selectedFile);
result = await mammoth.extractRawText({arrayBuffer: arrayBuffer});
markdownResult = result.value;
}
else if (fileExt === 'pptx') {
// Extract text from PPTX by parsing as ZIP and getting slide content
const arrayBuffer = await readFileAsArrayBuffer(selectedFile);
const zip = new JSZip();
const pptx = await zip.loadAsync(arrayBuffer);
const slidePromises = [];
let slideIndex = 1;
// Find all slides in the PPTX
for (let path in pptx.files) {
if (path.startsWith('ppt/slides/slide') && path.endsWith('.xml')) {
slidePromises.push(
pptx.files[path].async('text').then(slideContent => {
// Extract text content from slide XML
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(slideContent, 'text/xml');
const texts = Array.from(xmlDoc.getElementsByTagNameNS('http://schemas.openxmlformats.org/drawingml/2006/main', 't'))
.map(el => el.textContent)
.join('\n');
return `## Slide ${slideIndex++}\n\n${texts}\n\n`;
})
);
}
}
const slidesMarkdown = await Promise.all(slidePromises);
markdownResult = slidesMarkdown.join('\n');
}
else if (fileExt === 'html') {
// Using turndown for HTML to Markdown
const html = await readFileAsText(selectedFile);
const turndownService = new TurndownService({
headingStyle: 'atx',
bulletListMarker: '-',
codeBlockStyle: 'fenced'
});
markdownResult = turndownService.turndown(html);
}
else if (fileExt === 'txt') {
// Plain text - minimal conversion needed
markdownResult = await readFileAsText(selectedFile);
}
else if (fileExt === 'pdf') {
// Using pdf.js for PDF to text conversion
const arrayBuffer = await readFileAsArrayBuffer(selectedFile);
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.14.305/build/pdf.worker.min.js';
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
let fullText = '';
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const textContent = await page.getTextContent();
const pageText = textContent.items
.map(item => item.str)
.join('\n');
if (i > 1) fullText += '\n\n';
fullText += `## Page ${i}\n\n${pageText}`;
}
markdownResult = fullText;
}
// Display the result
displayMarkdownResult(markdownResult);
hideToast(loadingToast);
showToast('Conversion successful!', 'success');
} catch (error) {
console.error('Conversion error:', error);
hideToast(loadingToast);
showToast('Conversion failed. Please try again.', 'error');
}
}
function readFileAsArrayBuffer(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
function readFileAsText(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsText(file);
});
}
function displayMarkdownResult(markdown) {
if (!markdown) {
showToast('No content was generated', 'warning');
return;
}
// Update output display
document.getElementById('emptyState').classList.add('hidden');
document.getElementById('outputContent').classList.remove('hidden');
document.getElementById('outputContent').textContent = markdown;
// Enable output buttons
document.getElementById('copyBtn').disabled = false;
document.getElementById('downloadBtn').disabled = false;
// Update stats
document.getElementById('stats').classList.remove('hidden');
document.getElementById('charCount').textContent = markdown.length;
document.getElementById('wordCount').textContent = markdown.trim().split(/\s+/).length;
}
function copyToClipboard() {
if (!markdownResult) return;
navigator.clipboard.writeText(markdownResult).then(() => {
showToast('Copied to clipboard!', 'success');
}).catch(err => {
console.error('Failed to copy: ', err);
showToast('Failed to copy to clipboard', 'error');
});
}
function downloadMarkdown() {
if (!markdownResult) return;
const blob = new Blob([markdownResult], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = selectedFile ? selectedFile.name.split('.')[0] + '.md' : 'converted.md';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showToast('Download started!', 'success');
}
// Toast notification system
function showToast(message, type = 'info', persistent = false) {
const toastId = 'toast-' + Date.now();
const colors = {
info: 'blue',
success: 'green',
error: 'red',
warning: 'yellow'
};
const toast = document.createElement('div');
toast.id = toastId;
toast.className = `fixed bottom-4 right-4 px-6 py-3 rounded-lg shadow-lg text-white bg-${colors[type]}-500 flex items-center fade-in`;
toast.innerHTML = `
<i class="fas ${type === 'info' ? 'fa-info-circle' : type === 'success' ? 'fa-check-circle' : type === 'error' ? 'fa-times-circle' : 'fa-exclamation-circle'} mr-2"></i>
${message}
`;
document.body.appendChild(toast);
if (!persistent) {
setTimeout(() => {
toast.classList.remove('fade-in');
toast.classList.add('opacity-0', 'transition-opacity', 'duration-300');
setTimeout(() => {
if (document.getElementById(toastId)) {
document.getElementById(toastId).remove();
}
}, 300);
}, 3000);
}
return toastId;
}
function hideToast(toastId) {
const toast = document.getElementById(toastId);
if (toast) {
toast.remove();
}
}
// Initialize
document.addEventListener('DOMContentLoaded', () => {
// Check for Clipboard API availability
if (!navigator.clipboard) {
const copyBtn = document.getElementById('copyBtn');
copyBtn.disabled = true;
copyBtn.title = "Clipboard API not supported in your browser";
}
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=teerasakw/markitdown" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>