mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 10:00:17 +05:30
- Add PowerPoint export (PPTX/ODP) with slide-level formatting - Add comprehensive document conversion menu with import/export - Add interactive table creation helper in markdown editor - Update documentation with all new features and version history - Improve menu organization for better user experience 🤖 Generated with [Claude Code](https://claude.ai/code)
461 lines
14 KiB
JavaScript
461 lines
14 KiB
JavaScript
const { app, BrowserWindow, Menu, dialog, ipcMain, shell } = require('electron');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const { exec } = require('child_process');
|
|
const XLSX = require('xlsx');
|
|
|
|
// Simple storage implementation to replace electron-store
|
|
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
|
|
const store = {
|
|
get: (key, defaultValue) => {
|
|
try {
|
|
const data = fs.readFileSync(settingsPath, 'utf-8');
|
|
const settings = JSON.parse(data);
|
|
return settings[key] || defaultValue;
|
|
} catch {
|
|
return defaultValue;
|
|
}
|
|
},
|
|
set: (key, value) => {
|
|
let settings = {};
|
|
try {
|
|
const data = fs.readFileSync(settingsPath, 'utf-8');
|
|
settings = JSON.parse(data);
|
|
} catch {}
|
|
settings[key] = value;
|
|
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
|
|
}
|
|
};
|
|
|
|
let mainWindow;
|
|
let currentFile = null;
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 800,
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
contextIsolation: false
|
|
},
|
|
icon: path.join(__dirname, '../assets/icon.png')
|
|
});
|
|
|
|
mainWindow.loadFile(path.join(__dirname, 'index.html'));
|
|
|
|
createMenu();
|
|
|
|
mainWindow.on('closed', () => {
|
|
mainWindow = null;
|
|
});
|
|
}
|
|
|
|
function createMenu() {
|
|
const template = [
|
|
{
|
|
label: 'File',
|
|
submenu: [
|
|
{
|
|
label: 'New',
|
|
accelerator: 'CmdOrCtrl+N',
|
|
click: () => mainWindow.webContents.send('file-new')
|
|
},
|
|
{
|
|
label: 'Open',
|
|
accelerator: 'CmdOrCtrl+O',
|
|
click: openFile
|
|
},
|
|
{
|
|
label: 'Save',
|
|
accelerator: 'CmdOrCtrl+S',
|
|
click: () => mainWindow.webContents.send('file-save')
|
|
},
|
|
{
|
|
label: 'Save As',
|
|
accelerator: 'CmdOrCtrl+Shift+S',
|
|
click: saveAsFile
|
|
},
|
|
{ type: 'separator' },
|
|
{
|
|
label: 'Export',
|
|
submenu: [
|
|
{ label: 'HTML', click: () => exportFile('html') },
|
|
{ label: 'PDF', click: () => exportFile('pdf') },
|
|
{ label: 'DOCX', click: () => exportFile('docx') },
|
|
{ label: 'LaTeX', click: () => exportFile('latex') },
|
|
{ label: 'RTF', click: () => exportFile('rtf') },
|
|
{ label: 'ODT', click: () => exportFile('odt') },
|
|
{ label: 'EPUB', click: () => exportFile('epub') },
|
|
{ type: 'separator' },
|
|
{ label: 'PowerPoint (PPTX)', click: () => exportFile('pptx') },
|
|
{ label: 'OpenDocument Presentation (ODP)', click: () => exportFile('odp') },
|
|
{ type: 'separator' },
|
|
{ label: 'Excel (XLSX)', click: () => exportSpreadsheet('xlsx') },
|
|
{ label: 'Excel Legacy (XLS)', click: () => exportSpreadsheet('xls') },
|
|
{ label: 'OpenDocument Spreadsheet (ODS)', click: () => exportSpreadsheet('ods') }
|
|
]
|
|
},
|
|
{ type: 'separator' },
|
|
{
|
|
label: 'Quit',
|
|
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
|
|
click: () => app.quit()
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'Edit',
|
|
submenu: [
|
|
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', role: 'undo' },
|
|
{ label: 'Redo', accelerator: 'CmdOrCtrl+Y', role: 'redo' },
|
|
{ type: 'separator' },
|
|
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
|
|
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
|
|
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
|
|
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', role: 'selectAll' }
|
|
]
|
|
},
|
|
{
|
|
label: 'Convert',
|
|
submenu: [
|
|
{
|
|
label: 'Import Document...',
|
|
accelerator: 'CmdOrCtrl+I',
|
|
click: importDocument
|
|
},
|
|
{ type: 'separator' },
|
|
{
|
|
label: 'Convert Current File',
|
|
submenu: [
|
|
{ label: 'To Markdown', click: () => convertToFormat('md') },
|
|
{ label: 'To HTML', click: () => convertToFormat('html') },
|
|
{ label: 'To PDF', click: () => convertToFormat('pdf') },
|
|
{ label: 'To DOCX', click: () => convertToFormat('docx') },
|
|
{ label: 'To LaTeX', click: () => convertToFormat('latex') },
|
|
{ label: 'To RTF', click: () => convertToFormat('rtf') },
|
|
{ label: 'To ODT', click: () => convertToFormat('odt') },
|
|
{ label: 'To EPUB', click: () => convertToFormat('epub') },
|
|
{ label: 'To PPTX', click: () => convertToFormat('pptx') },
|
|
{ label: 'To ODP', click: () => convertToFormat('odp') }
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
label: 'View',
|
|
submenu: [
|
|
{
|
|
label: 'Toggle Preview',
|
|
accelerator: 'CmdOrCtrl+P',
|
|
click: () => mainWindow.webContents.send('toggle-preview')
|
|
},
|
|
{
|
|
label: 'Theme',
|
|
submenu: [
|
|
{ label: 'Light', click: () => setTheme('light') },
|
|
{ label: 'Dark', click: () => setTheme('dark') },
|
|
{ label: 'Solarized', click: () => setTheme('solarized') },
|
|
{ label: 'Monokai', click: () => setTheme('monokai') },
|
|
{ label: 'GitHub', click: () => setTheme('github') }
|
|
]
|
|
},
|
|
{ type: 'separator' },
|
|
{ label: 'Reload', accelerator: 'CmdOrCtrl+R', role: 'reload' },
|
|
{ label: 'Toggle DevTools', accelerator: 'F12', role: 'toggleDevTools' },
|
|
{ type: 'separator' },
|
|
{ label: 'Zoom In', accelerator: 'CmdOrCtrl+Plus', role: 'zoomIn' },
|
|
{ label: 'Zoom Out', accelerator: 'CmdOrCtrl+-', role: 'zoomOut' },
|
|
{ label: 'Reset Zoom', accelerator: 'CmdOrCtrl+0', role: 'resetZoom' }
|
|
]
|
|
},
|
|
{
|
|
label: 'Help',
|
|
submenu: [
|
|
{
|
|
label: 'About',
|
|
click: () => {
|
|
dialog.showMessageBox(mainWindow, {
|
|
type: 'info',
|
|
title: 'About PanConverter',
|
|
message: 'PanConverter',
|
|
detail: 'A cross-platform Markdown editor and converter using Pandoc.\n\nVersion: 1.1.0\nAuthor: Amit Haridas\nEmail: amit.wh@gmail.com\nLicense: MIT\n\nFeatures:\n• Markdown editing with live preview\n• Export to multiple formats via Pandoc\n• Export tables to Excel/ODS spreadsheets\n• Multiple themes support',
|
|
buttons: ['OK']
|
|
});
|
|
}
|
|
},
|
|
{
|
|
label: 'Documentation',
|
|
click: () => shell.openExternal('https://github.com/amitwh/pan-converter')
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
|
Menu.setApplicationMenu(menu);
|
|
}
|
|
|
|
function openFile() {
|
|
const files = dialog.showOpenDialogSync(mainWindow, {
|
|
properties: ['openFile'],
|
|
filters: [
|
|
{ name: 'Markdown', extensions: ['md', 'markdown'] },
|
|
{ name: 'All Files', extensions: ['*'] }
|
|
]
|
|
});
|
|
|
|
if (files && files[0]) {
|
|
currentFile = files[0];
|
|
const content = fs.readFileSync(currentFile, 'utf-8');
|
|
mainWindow.webContents.send('file-opened', { path: currentFile, content });
|
|
}
|
|
}
|
|
|
|
function saveAsFile() {
|
|
const file = dialog.showSaveDialogSync(mainWindow, {
|
|
defaultExt: '.md',
|
|
filters: [
|
|
{ name: 'Markdown', extensions: ['md', 'markdown'] },
|
|
{ name: 'All Files', extensions: ['*'] }
|
|
]
|
|
});
|
|
|
|
if (file) {
|
|
currentFile = file;
|
|
mainWindow.webContents.send('get-content-for-save', file);
|
|
}
|
|
}
|
|
|
|
function exportFile(format) {
|
|
if (!currentFile) {
|
|
dialog.showErrorBox('Error', 'Please save the file first');
|
|
return;
|
|
}
|
|
|
|
const outputFile = dialog.showSaveDialogSync(mainWindow, {
|
|
defaultPath: currentFile.replace(/\.[^/.]+$/, `.${format}`),
|
|
filters: [
|
|
{ name: format.toUpperCase(), extensions: [format] }
|
|
]
|
|
});
|
|
|
|
if (outputFile) {
|
|
const pandocCmd = `pandoc "${currentFile}" -o "${outputFile}"`;
|
|
|
|
exec(pandocCmd, (error, stdout, stderr) => {
|
|
if (error) {
|
|
dialog.showErrorBox('Export Error', `Failed to export: ${error.message}\n\nMake sure Pandoc is installed.`);
|
|
} else {
|
|
dialog.showMessageBox(mainWindow, {
|
|
type: 'info',
|
|
title: 'Export Complete',
|
|
message: `File exported successfully to ${outputFile}`,
|
|
buttons: ['OK']
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function exportSpreadsheet(format) {
|
|
if (!currentFile) {
|
|
dialog.showErrorBox('Error', 'Please save the file first');
|
|
return;
|
|
}
|
|
|
|
// Request content from renderer
|
|
mainWindow.webContents.send('get-content-for-spreadsheet', format);
|
|
}
|
|
|
|
function importDocument() {
|
|
const files = dialog.showOpenDialogSync(mainWindow, {
|
|
properties: ['openFile'],
|
|
filters: [
|
|
{ name: 'Documents', extensions: ['docx', 'odt', 'rtf', 'html', 'tex', 'epub', 'pdf'] },
|
|
{ name: 'Presentations', extensions: ['pptx', 'odp'] },
|
|
{ name: 'All Files', extensions: ['*'] }
|
|
]
|
|
});
|
|
|
|
if (files && files[0]) {
|
|
const inputFile = files[0];
|
|
const outputFile = inputFile.replace(/\.[^/.]+$/, '.md');
|
|
|
|
// Convert to markdown using pandoc
|
|
const pandocCmd = `pandoc "${inputFile}" -t markdown -o "${outputFile}"`;
|
|
|
|
exec(pandocCmd, (error, stdout, stderr) => {
|
|
if (error) {
|
|
dialog.showErrorBox('Import Error', `Failed to import: ${error.message}\n\nMake sure Pandoc is installed.`);
|
|
} else {
|
|
// Open the converted markdown file
|
|
currentFile = outputFile;
|
|
const content = fs.readFileSync(outputFile, 'utf-8');
|
|
mainWindow.webContents.send('file-opened', { path: outputFile, content });
|
|
|
|
dialog.showMessageBox(mainWindow, {
|
|
type: 'info',
|
|
title: 'Import Complete',
|
|
message: `Document imported successfully as ${outputFile}`,
|
|
buttons: ['OK']
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function convertToFormat(format) {
|
|
if (!currentFile) {
|
|
dialog.showErrorBox('Error', 'Please save or open a file first');
|
|
return;
|
|
}
|
|
|
|
const outputFile = dialog.showSaveDialogSync(mainWindow, {
|
|
defaultPath: currentFile.replace(/\.[^/.]+$/, `.${format}`),
|
|
filters: [
|
|
{ name: format.toUpperCase(), extensions: [format] }
|
|
]
|
|
});
|
|
|
|
if (outputFile) {
|
|
// For presentations, add slide level for better conversion
|
|
let pandocCmd = `pandoc "${currentFile}" -o "${outputFile}"`;
|
|
if (format === 'pptx' || format === 'odp') {
|
|
pandocCmd = `pandoc "${currentFile}" --slide-level=2 -o "${outputFile}"`;
|
|
}
|
|
|
|
exec(pandocCmd, (error, stdout, stderr) => {
|
|
if (error) {
|
|
dialog.showErrorBox('Conversion Error', `Failed to convert: ${error.message}\n\nMake sure Pandoc is installed.`);
|
|
} else {
|
|
dialog.showMessageBox(mainWindow, {
|
|
type: 'info',
|
|
title: 'Conversion Complete',
|
|
message: `File converted successfully to ${outputFile}`,
|
|
buttons: ['OK']
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function setTheme(theme) {
|
|
store.set('theme', theme);
|
|
mainWindow.webContents.send('theme-changed', theme);
|
|
}
|
|
|
|
// IPC handlers
|
|
ipcMain.on('save-file', (event, { path, content }) => {
|
|
fs.writeFileSync(path, content, 'utf-8');
|
|
currentFile = path;
|
|
});
|
|
|
|
ipcMain.on('save-current-file', (event, content) => {
|
|
if (currentFile) {
|
|
fs.writeFileSync(currentFile, content, 'utf-8');
|
|
} else {
|
|
saveAsFile();
|
|
}
|
|
});
|
|
|
|
ipcMain.on('get-theme', (event) => {
|
|
const theme = store.get('theme', 'light');
|
|
event.reply('theme-changed', theme);
|
|
});
|
|
|
|
ipcMain.on('export-spreadsheet', (event, { content, format }) => {
|
|
const outputFile = dialog.showSaveDialogSync(mainWindow, {
|
|
defaultPath: currentFile.replace(/\.[^/.]+$/, `.${format}`),
|
|
filters: [
|
|
{ name: format.toUpperCase(), extensions: [format] }
|
|
]
|
|
});
|
|
|
|
if (outputFile) {
|
|
try {
|
|
// Parse markdown content to extract tables
|
|
const tables = extractTablesFromMarkdown(content);
|
|
|
|
if (tables.length === 0) {
|
|
dialog.showErrorBox('Export Error', 'No tables found in the markdown content');
|
|
return;
|
|
}
|
|
|
|
// Create workbook
|
|
const wb = XLSX.utils.book_new();
|
|
|
|
tables.forEach((table, index) => {
|
|
const ws = XLSX.utils.aoa_to_sheet(table);
|
|
XLSX.utils.book_append_sheet(wb, ws, `Table ${index + 1}`);
|
|
});
|
|
|
|
// Write file
|
|
XLSX.writeFile(wb, outputFile);
|
|
|
|
dialog.showMessageBox(mainWindow, {
|
|
type: 'info',
|
|
title: 'Export Complete',
|
|
message: `Spreadsheet exported successfully to ${outputFile}`,
|
|
buttons: ['OK']
|
|
});
|
|
} catch (error) {
|
|
dialog.showErrorBox('Export Error', `Failed to export: ${error.message}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Helper function to extract tables from markdown
|
|
function extractTablesFromMarkdown(markdown) {
|
|
const tables = [];
|
|
const lines = markdown.split('\n');
|
|
let currentTable = [];
|
|
let inTable = false;
|
|
|
|
for (const line of lines) {
|
|
if (line.includes('|')) {
|
|
if (!inTable) {
|
|
inTable = true;
|
|
currentTable = [];
|
|
}
|
|
|
|
// Skip separator lines (|---|---|)
|
|
if (!line.match(/^\s*\|?\s*:?-+:?\s*\|/)) {
|
|
const cells = line.split('|')
|
|
.map(cell => cell.trim())
|
|
.filter(cell => cell !== '');
|
|
|
|
if (cells.length > 0) {
|
|
currentTable.push(cells);
|
|
}
|
|
}
|
|
} else if (inTable && line.trim() === '') {
|
|
// End of table
|
|
if (currentTable.length > 0) {
|
|
tables.push(currentTable);
|
|
}
|
|
currentTable = [];
|
|
inTable = false;
|
|
}
|
|
}
|
|
|
|
// Add last table if exists
|
|
if (currentTable.length > 0) {
|
|
tables.push(currentTable);
|
|
}
|
|
|
|
return tables;
|
|
}
|
|
|
|
app.whenReady().then(createWindow);
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
}); |