filetomarkdown
Version:
Convert various file types to markdown format with browser and Node.js support
176 lines (160 loc) ⢠6.73 kB
JavaScript
const fs = require('fs/promises');
const fsSync = require('fs');
const path = require('path');
const https = require('https');
const { convertToMarkdown } = require('../dist/main');
const GITHUB_RAW_BASE = 'https://raw.githubusercontent.com/jojomondag/FileToMarkdown/main/examples';
// Create directory structure
const createDirectories = () => {
const dirs = [
'examples/exampleFiles/code',
'examples/outputAfterConversion/code',
];
dirs.forEach(dir => {
if (!fsSync.existsSync(dir)) {
fsSync.mkdirSync(dir, { recursive: true });
}
});
};
// Download helper
const downloadFile = (url, outputPath) => new Promise((resolve, reject) => {
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download ${url}: ${response.statusCode}`));
return;
}
const fileStream = fsSync.createWriteStream(outputPath);
response.pipe(fileStream)
.on('finish', resolve)
.on('error', (err) => {
fs.unlink(outputPath, () => reject(err));
});
}).on('error', reject);
});
// Test files configuration
const testFiles = [
{
type: 'code',
localPath: 'examples/exampleFiles/code/codeCs.cs',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/code/codeCs.cs`,
outputPath: 'examples/outputAfterConversion/code/codeCs.md'
},
{
type: 'code',
localPath: 'examples/exampleFiles/code/codeHtml.html',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/code/codeHtml.html`,
outputPath: 'examples/outputAfterConversion/code/codeHtml.md'
},
{
type: 'code',
localPath: 'examples/exampleFiles/code/codeJava.java',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/code/codeJava.java`,
outputPath: 'examples/outputAfterConversion/code/codeJava.md'
},
{
type: 'code',
localPath: 'examples/exampleFiles/code/codeJs.js',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/code/codeJs.js`,
outputPath: 'examples/outputAfterConversion/code/codeJs.md'
},
{
type: 'code',
localPath: 'examples/exampleFiles/code/codePy.py',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/code/codePy.py`,
outputPath: 'examples/outputAfterConversion/code/codePy.md'
},
{
type: 'pdf',
localPath: 'examples/exampleFiles/exampleGardening.pdf',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleGardening.pdf`,
outputPath: 'examples/outputAfterConversion/exampleGardening.md'
},
{
type: 'docx',
localPath: 'examples/exampleFiles/exampleProjekt9.docx',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleProjekt9.docx`,
outputPath: 'examples/outputAfterConversion/exampleProjekt9.md'
},
{
type: 'odt',
localPath: 'examples/exampleFiles/exampleDocumentLibreOffice.odt',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleDocumentLibreOffice.odt`,
outputPath: 'examples/outputAfterConversion/exampleDocumentLibreOffice.md'
},
{
type: 'pptx',
localPath: 'examples/exampleFiles/exampleBruceLee.pptx',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleBruceLee.pptx`,
outputPath: 'examples/outputAfterConversion/exampleBruceLee.md'
},
{
type: 'odp',
localPath: 'examples/exampleFiles/examplePresentationLibreOffice.odp',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/examplePresentationLibreOffice.odp`,
outputPath: 'examples/outputAfterConversion/examplePresentationLibreOffice.md'
},
{
type: 'xlsx',
localPath: 'examples/exampleFiles/exampleProgrammeringYearPlan.xlsx',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleProgrammeringYearPlan.xlsx`,
outputPath: 'examples/outputAfterConversion/exampleProgrammeringYearPlan.md'
},
{
type: 'ods',
localPath: 'examples/exampleFiles/exampleSpreadsheetLibreOffice.ods',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleSpreadsheetLibreOffice.ods`,
outputPath: 'examples/outputAfterConversion/exampleSpreadsheetLibreOffice.md'
},
{
type: 'zip',
localPath: 'examples/exampleFiles/exampleLeads.zip',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleLeads.zip`,
outputPath: 'examples/outputAfterConversion/exampleLeads.md'
},
{
type: '7zip',
localPath: 'examples/exampleFiles/exampleStudentWorks.7z',
githubPath: `${GITHUB_RAW_BASE}/exampleFiles/exampleStudentWorks.7z`,
outputPath: 'examples/outputAfterConversion/exampleStudentWorks.md'
}
];
// Main test runner
const runTests = async () => {
try {
console.log('š Starting FileToMarkdown Test Suite\n');
console.log('š Using GitHub examples and styling\n');
createDirectories();
console.log('\nš Project Structure:');
console.log('āāā examples/');
console.log('ā āāā exampleFiles/');
console.log('ā āāā outputAfterConversion/');
console.log('āāā package.json\n');
for (const test of testFiles) {
try {
const fileType = test.type.padEnd(6, ' ');
console.log(`šØ Processing ${fileType}: ${path.basename(test.localPath)}`);
await downloadFile(test.githubPath, test.localPath);
await convertToMarkdown(test.localPath, test.outputPath);
// Verify output
if (fsSync.existsSync(test.outputPath)) {
const stats = fsSync.statSync(test.outputPath);
console.log(`ā
Success: ${path.basename(test.outputPath)} (${Math.round(stats.size/1024)}KB)`);
} else {
throw new Error('Output file not created');
}
} catch (error) {
console.error(`ā ${test.type} conversion failed:`, error.message);
process.exit(1);
}
}
console.log('\nš All conversions completed!');
console.log(' - Drag generated .md files from:');
console.log(' examples/outputAfterConversion/');
} catch (error) {
console.error('š„ Critical error:', error.message);
process.exit(1);
}
};
// Run tests
runTests();