mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
Release v1.7.9: Enhanced Word Export with Template Support
Major Features: - Template-based Word export with custom template selection - File menu option to select and persist Word templates - Table styling with orange headers and white data rows - ASCII art and flowchart rendering with perfect alignment - Red-colored arrows in flowcharts for enhanced visibility - Markdown numbering stripped from headings and lists Technical Improvements: - Enhanced ASCII art detection for arrows and flowchart patterns - Line-by-line rendering with no-wrap properties - Arrow character detection and red coloring (↓→←↑) - Template path persistence across sessions - Word XML manipulation using PizZip Files Modified: - src/wordTemplateExporter.js: Enhanced ASCII art rendering, table styling - src/main.js: Template selection function and menu integration - CLAUDE.md: Comprehensive v1.7.9 documentation - package.json: Version bump to 1.7.9 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+33
-3
@@ -48,6 +48,7 @@ const store = {
|
||||
let mainWindow;
|
||||
let currentFile = null; // This will now represent the active tab's file
|
||||
let pandocAvailable = null; // Cache pandoc availability check
|
||||
let wordTemplatePath = null; // Path to selected Word template
|
||||
let rendererReady = false; // Track if renderer is ready to receive file data
|
||||
|
||||
// Handle single instance lock for Windows file association
|
||||
@@ -251,6 +252,11 @@ function createMenu() {
|
||||
]
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Select Word Template...',
|
||||
click: selectWordTemplate
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Quit',
|
||||
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
|
||||
@@ -535,6 +541,27 @@ function showBatchConversionDialog() {
|
||||
mainWindow.webContents.send('show-batch-dialog');
|
||||
}
|
||||
|
||||
// Select Word Template
|
||||
async function selectWordTemplate() {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
title: 'Select Word Template',
|
||||
filters: [{ name: 'Word Document', extensions: ['docx'] }],
|
||||
properties: ['openFile']
|
||||
});
|
||||
|
||||
if (!result.canceled && result.filePaths.length > 0) {
|
||||
wordTemplatePath = result.filePaths[0];
|
||||
store.set('wordTemplatePath', wordTemplatePath);
|
||||
|
||||
dialog.showMessageBox(mainWindow, {
|
||||
type: 'info',
|
||||
title: 'Template Selected',
|
||||
message: 'Word template has been updated',
|
||||
detail: `Template: ${path.basename(wordTemplatePath)}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Enhanced Word Export with Template Support
|
||||
async function exportWordWithTemplate() {
|
||||
if (!currentFile) {
|
||||
@@ -555,8 +582,8 @@ async function exportWordWithTemplate() {
|
||||
|
||||
if (result.canceled) return;
|
||||
|
||||
// Create exporter instance
|
||||
const exporter = new WordTemplateExporter();
|
||||
// Create exporter instance with selected template (if any)
|
||||
const exporter = new WordTemplateExporter(wordTemplatePath);
|
||||
|
||||
// Convert markdown to DOCX
|
||||
await exporter.convert(content, result.filePath);
|
||||
@@ -1630,13 +1657,16 @@ function buildPandocCommand(content, format, outputPath) {
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
// Load saved Word template path
|
||||
wordTemplatePath = store.get('wordTemplatePath', null);
|
||||
|
||||
// Check for command line conversion requests
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length >= 2 && (args[0] === '--convert' || args[0] === '--convert-to')) {
|
||||
handleCLIConversion(args);
|
||||
return; // Don't create window for CLI operations
|
||||
}
|
||||
|
||||
|
||||
createWindow();
|
||||
|
||||
// Handle file association on app startup
|
||||
|
||||
+528
-241
@@ -1,330 +1,617 @@
|
||||
/**
|
||||
* Word Template Exporter
|
||||
* Converts Markdown to DOCX using custom Word templates
|
||||
* Based on Template_exporter functionality
|
||||
* Loads word_template.docx, preserves first 2 pages (cover + TOC),
|
||||
* and adds markdown content starting from page 3 using template styles
|
||||
*/
|
||||
|
||||
const { Document, Packer, Paragraph, TextRun, AlignmentType, HeadingLevel, Table, TableRow, TableCell, WidthType, BorderStyle } = require('docx');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const PizZip = require('pizzip');
|
||||
const Docx = require('docx4js').default;
|
||||
|
||||
class WordTemplateExporter {
|
||||
constructor() {
|
||||
this.inCodeBlock = false;
|
||||
this.inList = false;
|
||||
this.listType = null;
|
||||
constructor(templatePath) {
|
||||
this.templatePath = templatePath || path.join(__dirname, '../word_template.docx');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert markdown to Word document with template
|
||||
* @param {string} markdownContent - The markdown content to convert
|
||||
* @param {string} outputPath - Where to save the DOCX file
|
||||
* @param {string} templatePath - Optional template path (for future implementation)
|
||||
* Convert markdown to Word document using template
|
||||
*/
|
||||
async convert(markdownContent, outputPath, templatePath = null) {
|
||||
// Parse markdown and create document elements
|
||||
const children = this.parseMarkdown(markdownContent);
|
||||
async convert(markdownContent, outputPath) {
|
||||
try {
|
||||
// Load template
|
||||
const templateBuffer = fs.readFileSync(this.templatePath);
|
||||
const zip = new PizZip(templateBuffer);
|
||||
|
||||
const doc = new Document({
|
||||
numbering: {
|
||||
config: [
|
||||
{
|
||||
reference: 'numbering',
|
||||
levels: [
|
||||
{
|
||||
level: 0,
|
||||
format: 'decimal',
|
||||
text: '%1.',
|
||||
alignment: AlignmentType.LEFT
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
reference: 'bullets',
|
||||
levels: [
|
||||
{
|
||||
level: 0,
|
||||
format: 'bullet',
|
||||
text: '\u2022',
|
||||
alignment: AlignmentType.LEFT
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
sections: [{
|
||||
properties: {},
|
||||
children: children
|
||||
}]
|
||||
});
|
||||
// Extract document.xml
|
||||
const documentXml = zip.file('word/document.xml').asText();
|
||||
|
||||
// Write to file
|
||||
const buffer = await Packer.toBuffer(doc);
|
||||
fs.writeFileSync(outputPath, buffer);
|
||||
// Parse markdown and generate Word XML
|
||||
const newContentXml = this.markdownToWordXml(markdownContent);
|
||||
|
||||
return outputPath;
|
||||
// Insert new content after page 2 (after the section break)
|
||||
const modifiedXml = this.insertContentAfterPage2(documentXml, newContentXml);
|
||||
|
||||
// Update the zip with modified XML
|
||||
zip.file('word/document.xml', modifiedXml);
|
||||
|
||||
// Generate and save the new document
|
||||
const newDocBuffer = zip.generate({ type: 'nodebuffer' });
|
||||
fs.writeFileSync(outputPath, newDocBuffer);
|
||||
|
||||
return outputPath;
|
||||
} catch (error) {
|
||||
console.error('Error in Word export:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
parseMarkdown(content) {
|
||||
const lines = content.split('\n');
|
||||
const elements = [];
|
||||
let i = 0;
|
||||
/**
|
||||
* Insert markdown content after page 2 in the document
|
||||
*/
|
||||
insertContentAfterPage2(documentXml, newContentXml) {
|
||||
// Find the last section break (after page 2)
|
||||
// Look for the section properties tag that marks page breaks
|
||||
const sectionBreakRegex = /<w:sectPr[^>]*>[\s\S]*?<\/w:sectPr>/g;
|
||||
const matches = [...documentXml.matchAll(sectionBreakRegex)];
|
||||
|
||||
while (i < lines.length) {
|
||||
if (matches.length >= 2) {
|
||||
// Insert after the 2nd section break
|
||||
const insertPoint = matches[1].index + matches[1][0].length;
|
||||
return documentXml.slice(0, insertPoint) + newContentXml + documentXml.slice(insertPoint);
|
||||
} else {
|
||||
// If no section breaks found, insert before closing body tag
|
||||
return documentXml.replace('</w:body>', newContentXml + '</w:body>');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert markdown to Word XML format
|
||||
*/
|
||||
markdownToWordXml(markdown) {
|
||||
const lines = markdown.split('\n');
|
||||
let xml = '';
|
||||
let inCodeBlock = false;
|
||||
let codeLines = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// Skip empty lines
|
||||
if (!line.trim()) {
|
||||
elements.push(new Paragraph({ text: '' }));
|
||||
i++;
|
||||
// Handle code blocks
|
||||
if (line.trim().startsWith('```')) {
|
||||
if (inCodeBlock) {
|
||||
// End code block
|
||||
xml += this.createCodeBlockXml(codeLines.join('\n'));
|
||||
codeLines = [];
|
||||
inCodeBlock = false;
|
||||
} else {
|
||||
inCodeBlock = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Code blocks
|
||||
if (line.trim().startsWith('```')) {
|
||||
const codeLines = [];
|
||||
if (inCodeBlock) {
|
||||
codeLines.push(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Empty lines
|
||||
if (!line.trim()) {
|
||||
xml += '<w:p><w:pPr></w:pPr></w:p>';
|
||||
continue;
|
||||
}
|
||||
|
||||
// Tables - detect table lines
|
||||
if (line.includes('|') && line.trim().startsWith('|')) {
|
||||
const tableLines = [line];
|
||||
i++;
|
||||
while (i < lines.length && !lines[i].trim().startsWith('```')) {
|
||||
codeLines.push(lines[i]);
|
||||
// Collect all consecutive table lines
|
||||
while (i < lines.length && lines[i].includes('|')) {
|
||||
tableLines.push(lines[i]);
|
||||
i++;
|
||||
}
|
||||
elements.push(this.createCodeBlock(codeLines.join('\n')));
|
||||
i++;
|
||||
i--; // Back up one line
|
||||
xml += this.createTableXml(tableLines);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Headers
|
||||
if (line.trim().startsWith('#')) {
|
||||
elements.push(this.createHeading(line));
|
||||
// ASCII flowcharts/diagrams - detect box drawing characters
|
||||
if (this.isAsciiArt(line)) {
|
||||
const asciiLines = [line];
|
||||
i++;
|
||||
// Collect all consecutive ASCII art lines
|
||||
while (i < lines.length && (this.isAsciiArt(lines[i]) || !lines[i].trim())) {
|
||||
asciiLines.push(lines[i]);
|
||||
i++;
|
||||
if (i < lines.length && lines[i].trim() && !this.isAsciiArt(lines[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
i--; // Back up one line
|
||||
xml += this.createAsciiArtXml(asciiLines.join('\n'));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Headings - strip markdown numbering
|
||||
if (line.trim().startsWith('#')) {
|
||||
const level = (line.match(/^#+/) || [''])[0].length;
|
||||
let text = line.replace(/^#+\s*/, '').trim();
|
||||
// Remove markdown numbering like "1.1 Title" -> "Title"
|
||||
text = text.replace(/^\d+(\.\d+)*\.?\s+/, '');
|
||||
xml += this.createHeadingXml(text, level);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Blockquotes
|
||||
if (line.trim().startsWith('>')) {
|
||||
elements.push(this.createQuote(line));
|
||||
i++;
|
||||
const text = line.replace(/^>\s*/, '').trim();
|
||||
xml += this.createQuoteXml(text);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ordered lists
|
||||
// Ordered lists - strip markdown numbering, use template numbering
|
||||
if (/^\s*\d+\.\s+/.test(line)) {
|
||||
elements.push(this.createListItem(line, true));
|
||||
i++;
|
||||
const text = line.replace(/^\s*\d+\.\s+/, '');
|
||||
xml += this.createListItemXml(text, true);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Unordered lists
|
||||
if (/^\s*[-*+]\s+/.test(line)) {
|
||||
elements.push(this.createListItem(line, false));
|
||||
i++;
|
||||
const text = line.replace(/^\s*[-*+]\s+/, '');
|
||||
xml += this.createListItemXml(text, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Horizontal rule
|
||||
if (/^[-*_]{3,}$/.test(line.trim())) {
|
||||
elements.push(this.createHorizontalRule());
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Tables
|
||||
if (line.includes('|')) {
|
||||
const tableLines = [line];
|
||||
i++;
|
||||
while (i < lines.length && lines[i].includes('|')) {
|
||||
tableLines.push(lines[i]);
|
||||
i++;
|
||||
}
|
||||
const table = this.createTable(tableLines);
|
||||
if (table) elements.push(table);
|
||||
xml += this.createHorizontalRuleXml();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Normal paragraph
|
||||
elements.push(this.createParagraph(line));
|
||||
i++;
|
||||
xml += this.createParagraphXml(line);
|
||||
}
|
||||
|
||||
return elements;
|
||||
return xml;
|
||||
}
|
||||
|
||||
createHeading(line) {
|
||||
const level = line.match(/^#+/)[0].length;
|
||||
const text = line.replace(/^#+\s*/, '').trim();
|
||||
|
||||
const headingLevels = {
|
||||
1: HeadingLevel.HEADING_1,
|
||||
2: HeadingLevel.HEADING_2,
|
||||
3: HeadingLevel.HEADING_3,
|
||||
4: HeadingLevel.HEADING_4,
|
||||
5: HeadingLevel.HEADING_5,
|
||||
6: HeadingLevel.HEADING_6
|
||||
};
|
||||
|
||||
return new Paragraph({
|
||||
text: text,
|
||||
heading: headingLevels[Math.min(level, 6)]
|
||||
});
|
||||
}
|
||||
|
||||
createParagraph(line) {
|
||||
const runs = this.parseInlineFormatting(line);
|
||||
return new Paragraph({ children: runs });
|
||||
}
|
||||
|
||||
createQuote(line) {
|
||||
const text = line.replace(/^>\s*/, '').trim();
|
||||
/**
|
||||
* Create heading XML using template styles
|
||||
*/
|
||||
createHeadingXml(text, level) {
|
||||
const styleName = `Heading${level}`;
|
||||
const runs = this.parseInlineFormatting(text);
|
||||
|
||||
return new Paragraph({
|
||||
children: runs,
|
||||
indent: { left: 720 }, // 0.5 inch indent
|
||||
italics: true
|
||||
});
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pStyle w:val="${styleName}"/>
|
||||
</w:pPr>
|
||||
${runs}
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
createListItem(line, numbered) {
|
||||
const text = line.replace(/^\s*(\d+\.|-|\*|\+)\s+/, '').trim();
|
||||
/**
|
||||
* Create paragraph XML with Normal style
|
||||
*/
|
||||
createParagraphXml(text) {
|
||||
const runs = this.parseInlineFormatting(text);
|
||||
|
||||
return new Paragraph({
|
||||
children: runs,
|
||||
numbering: {
|
||||
reference: numbered ? 'numbering' : 'bullets',
|
||||
level: 0
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pStyle w:val="Normal"/>
|
||||
</w:pPr>
|
||||
${runs}
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create quote XML
|
||||
*/
|
||||
createQuoteXml(text) {
|
||||
const runs = this.parseInlineFormatting(text);
|
||||
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pStyle w:val="Quote"/>
|
||||
</w:pPr>
|
||||
${runs}
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create list item XML using template numbering
|
||||
*/
|
||||
createListItemXml(text, numbered) {
|
||||
const runs = this.parseInlineFormatting(text);
|
||||
const numId = numbered ? '1' : '2'; // Template numbering IDs
|
||||
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pStyle w:val="${numbered ? 'ListNumber' : 'ListBullet'}"/>
|
||||
<w:numPr>
|
||||
<w:ilvl w:val="0"/>
|
||||
<w:numId w:val="${numId}"/>
|
||||
</w:numPr>
|
||||
</w:pPr>
|
||||
${runs}
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create code block XML
|
||||
*/
|
||||
createCodeBlockXml(code) {
|
||||
const escapedCode = this.escapeXml(code);
|
||||
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pStyle w:val="Code"/>
|
||||
</w:pPr>
|
||||
<w:r>
|
||||
<w:rPr>
|
||||
<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas"/>
|
||||
<w:sz w:val="18"/>
|
||||
</w:rPr>
|
||||
<w:t xml:space="preserve">${escapedCode}</w:t>
|
||||
</w:r>
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create horizontal rule XML
|
||||
*/
|
||||
createHorizontalRuleXml() {
|
||||
return `<w:p>
|
||||
<w:pPr>
|
||||
<w:pBdr>
|
||||
<w:bottom w:val="single" w:sz="6" w:space="1" w:color="auto"/>
|
||||
</w:pBdr>
|
||||
</w:pPr>
|
||||
</w:p>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create table XML from markdown table lines with template styling
|
||||
*/
|
||||
createTableXml(tableLines) {
|
||||
// Parse table
|
||||
const rows = [];
|
||||
for (const line of tableLines) {
|
||||
// Skip separator lines (e.g., |---|---|)
|
||||
if (/^\s*\|[\s\-:]+\|\s*$/.test(line)) {
|
||||
continue;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
createCodeBlock(code) {
|
||||
return new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: code,
|
||||
font: 'Consolas',
|
||||
size: 18 // 9pt
|
||||
})
|
||||
],
|
||||
shading: {
|
||||
fill: 'F5F5F5'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
createHorizontalRule() {
|
||||
return new Paragraph({
|
||||
text: '_'.repeat(50),
|
||||
alignment: AlignmentType.CENTER
|
||||
});
|
||||
}
|
||||
|
||||
createTable(tableLines) {
|
||||
// Skip separator lines
|
||||
const rows = tableLines.filter(line => !line.match(/^\s*\|[\s\-:]+\|\s*$/));
|
||||
|
||||
if (rows.length === 0) return null;
|
||||
|
||||
const tableRows = rows.map((line, index) => {
|
||||
// Split by | and trim
|
||||
const cells = line.split('|').filter(cell => cell.trim()).map(cell => cell.trim());
|
||||
if (cells.length > 0) {
|
||||
rows.push(cells);
|
||||
}
|
||||
}
|
||||
|
||||
return new TableRow({
|
||||
children: cells.map(cellText => {
|
||||
const runs = this.parseInlineFormatting(cellText);
|
||||
return new TableCell({
|
||||
children: [new Paragraph({ children: runs })],
|
||||
shading: index === 0 ? { fill: 'E7E6E6' } : undefined,
|
||||
width: { size: 100 / cells.length, type: WidthType.PERCENTAGE }
|
||||
});
|
||||
})
|
||||
if (rows.length === 0) return '';
|
||||
|
||||
// Calculate number of columns
|
||||
const numCols = Math.max(...rows.map(row => row.length));
|
||||
|
||||
// Build table XML
|
||||
let tableXml = '<w:tbl>';
|
||||
|
||||
// Table properties - use template table style
|
||||
tableXml += `<w:tblPr>
|
||||
<w:tblStyle w:val="TableGrid"/>
|
||||
<w:tblW w:w="5000" w:type="pct"/>
|
||||
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
|
||||
<w:tblBorders>
|
||||
<w:top w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
<w:left w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
<w:bottom w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
<w:right w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
<w:insideH w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
<w:insideV w:val="single" w:sz="8" w:space="0" w:color="F58220"/>
|
||||
</w:tblBorders>
|
||||
</w:tblPr>`;
|
||||
|
||||
// Table grid
|
||||
tableXml += '<w:tblGrid>';
|
||||
for (let i = 0; i < numCols; i++) {
|
||||
tableXml += '<w:gridCol/>';
|
||||
}
|
||||
tableXml += '</w:tblGrid>';
|
||||
|
||||
// Table rows
|
||||
rows.forEach((rowCells, rowIndex) => {
|
||||
const isHeader = rowIndex === 0;
|
||||
const isEvenRow = rowIndex % 2 === 0;
|
||||
|
||||
tableXml += '<w:tr>';
|
||||
|
||||
// Pad row to have same number of columns
|
||||
while (rowCells.length < numCols) {
|
||||
rowCells.push('');
|
||||
}
|
||||
|
||||
rowCells.forEach((cellText, colIndex) => {
|
||||
tableXml += '<w:tc>';
|
||||
tableXml += '<w:tcPr>';
|
||||
|
||||
// Cell shading - orange for header, white for data rows
|
||||
if (isHeader) {
|
||||
tableXml += '<w:shd w:val="clear" w:color="FFFFFF" w:fill="F58220"/>';
|
||||
}
|
||||
|
||||
// Cell borders
|
||||
tableXml += '<w:tcBorders>' +
|
||||
'<w:top w:val="single" w:sz="8" w:space="0" w:color="F58220"/>' +
|
||||
'<w:left w:val="single" w:sz="8" w:space="0" w:color="F58220"/>' +
|
||||
'<w:bottom w:val="single" w:sz="8" w:space="0" w:color="F58220"/>' +
|
||||
'<w:right w:val="single" w:sz="8" w:space="0" w:color="F58220"/>' +
|
||||
'</w:tcBorders>';
|
||||
tableXml += '</w:tcPr>';
|
||||
|
||||
// Cell content
|
||||
tableXml += '<w:p>';
|
||||
tableXml += '<w:pPr>';
|
||||
tableXml += '<w:spacing w:before="100" w:after="100"/>';
|
||||
tableXml += '</w:pPr>';
|
||||
|
||||
const runs = this.parseInlineFormatting(cellText);
|
||||
|
||||
if (isHeader) {
|
||||
// Header: bold white text
|
||||
tableXml += runs.replace(/<w:rPr>/g, '<w:rPr><w:b/><w:color w:val="FFFFFF"/>');
|
||||
} else {
|
||||
// Data rows: normal black text
|
||||
tableXml += runs;
|
||||
}
|
||||
|
||||
tableXml += '</w:p>';
|
||||
tableXml += '</w:tc>';
|
||||
});
|
||||
|
||||
tableXml += '</w:tr>';
|
||||
});
|
||||
|
||||
return new Table({
|
||||
rows: tableRows,
|
||||
width: { size: 100, type: WidthType.PERCENTAGE }
|
||||
});
|
||||
tableXml += '</w:tbl>';
|
||||
|
||||
return tableXml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if line contains ASCII art/flowchart characters
|
||||
*/
|
||||
isAsciiArt(line) {
|
||||
// Don't treat markdown tables as ASCII art
|
||||
if (line.trim().startsWith('|') && line.trim().endsWith('|')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Common ASCII art characters
|
||||
const asciiArtChars = [
|
||||
'─', '│', '┌', '┐', '└', '┘', '├', '┤', '┬', '┴', '┼', // Box drawing
|
||||
'═', '║', '╔', '╗', '╚', '╝', '╠', '╣', '╦', '╩', '╬', // Double box
|
||||
'▲', '▼', '◄', '►', '♦', '●', '○', '■', '□', // Shapes
|
||||
'↓', '→', '←', '↑', // Arrows
|
||||
];
|
||||
|
||||
// Check for box drawing characters
|
||||
if (asciiArtChars.some(char => line.includes(char))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for ASCII box patterns with regular characters
|
||||
const asciiPatterns = [
|
||||
/^\s*\+[-=_]+\+/, // +-----+
|
||||
/^\s*\|[-=_]{3,}\|/, // |-----|
|
||||
/^\s*[-=_]{5,}$/, // -----
|
||||
/^\s*\([A-Z][a-z]+\)\s*\|\|/, // (Deformable) ||
|
||||
/^\s*\|\s+[A-Z\s]+[-:]\s+[A-Z]/, // | TILE ADHESIVE - TYPE
|
||||
/^\s*\|\s*\[[^\]]+\]/, // | [BIS Mark / CE Mark]
|
||||
/^\s*\|\s{2,}\w+.*\|\|/, // || text ||
|
||||
/^\s*\[[^\]]+\]/, // [Step in brackets]
|
||||
];
|
||||
|
||||
return asciiPatterns.some(pattern => pattern.test(line));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create ASCII art XML with monospace font
|
||||
*/
|
||||
createAsciiArtXml(asciiContent) {
|
||||
// Split ASCII art into individual lines and create a paragraph for each
|
||||
const lines = asciiContent.split('\n');
|
||||
let xml = '';
|
||||
|
||||
lines.forEach((line, index) => {
|
||||
xml += `<w:p>
|
||||
<w:pPr>
|
||||
<w:spacing w:before="${index === 0 ? '120' : '0'}" w:after="${index === lines.length - 1 ? '120' : '0'}" w:line="240" w:lineRule="exact"/>
|
||||
<w:ind w:left="0" w:right="0"/>
|
||||
<w:jc w:val="left"/>
|
||||
<w:keepLines/>
|
||||
<w:wordWrap w:val="0"/>
|
||||
</w:pPr>`;
|
||||
|
||||
// Check if line contains arrow characters and color them red
|
||||
const arrowChars = ['↓', '→', '←', '↑', '▼', '►', '◄', '▲'];
|
||||
const hasArrow = arrowChars.some(arrow => line.includes(arrow));
|
||||
|
||||
if (hasArrow) {
|
||||
// Split line into parts and color arrows red
|
||||
let remainingLine = line;
|
||||
let processedText = '';
|
||||
|
||||
while (remainingLine.length > 0) {
|
||||
let foundArrow = false;
|
||||
let arrowIndex = -1;
|
||||
let foundArrowChar = '';
|
||||
|
||||
// Find the first arrow in the remaining text
|
||||
for (const arrow of arrowChars) {
|
||||
const idx = remainingLine.indexOf(arrow);
|
||||
if (idx !== -1 && (arrowIndex === -1 || idx < arrowIndex)) {
|
||||
arrowIndex = idx;
|
||||
foundArrowChar = arrow;
|
||||
foundArrow = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundArrow) {
|
||||
// Add text before arrow (if any)
|
||||
if (arrowIndex > 0) {
|
||||
const beforeArrow = this.escapeXml(remainingLine.substring(0, arrowIndex));
|
||||
xml += `<w:r>
|
||||
<w:rPr>
|
||||
<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/>
|
||||
<w:sz w:val="16"/>
|
||||
<w:szCs w:val="16"/>
|
||||
<w:shd w:val="clear" w:color="auto" w:fill="F5F5F5"/>
|
||||
<w:noProof/>
|
||||
</w:rPr>
|
||||
<w:t xml:space="preserve">${beforeArrow}</w:t>
|
||||
</w:r>`;
|
||||
}
|
||||
|
||||
// Add arrow in red
|
||||
const escapedArrow = this.escapeXml(foundArrowChar);
|
||||
xml += `<w:r>
|
||||
<w:rPr>
|
||||
<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/>
|
||||
<w:sz w:val="16"/>
|
||||
<w:szCs w:val="16"/>
|
||||
<w:color w:val="FF0000"/>
|
||||
<w:shd w:val="clear" w:color="auto" w:fill="F5F5F5"/>
|
||||
<w:noProof/>
|
||||
</w:rPr>
|
||||
<w:t xml:space="preserve">${escapedArrow}</w:t>
|
||||
</w:r>`;
|
||||
|
||||
// Continue with remaining text
|
||||
remainingLine = remainingLine.substring(arrowIndex + foundArrowChar.length);
|
||||
} else {
|
||||
// No more arrows, add remaining text
|
||||
const escapedRemaining = this.escapeXml(remainingLine);
|
||||
xml += `<w:r>
|
||||
<w:rPr>
|
||||
<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/>
|
||||
<w:sz w:val="16"/>
|
||||
<w:szCs w:val="16"/>
|
||||
<w:shd w:val="clear" w:color="auto" w:fill="F5F5F5"/>
|
||||
<w:noProof/>
|
||||
</w:rPr>
|
||||
<w:t xml:space="preserve">${escapedRemaining}</w:t>
|
||||
</w:r>`;
|
||||
remainingLine = '';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No arrows, just add the line normally
|
||||
const escapedLine = this.escapeXml(line);
|
||||
xml += `<w:r>
|
||||
<w:rPr>
|
||||
<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas" w:cs="Consolas"/>
|
||||
<w:sz w:val="16"/>
|
||||
<w:szCs w:val="16"/>
|
||||
<w:shd w:val="clear" w:color="auto" w:fill="F5F5F5"/>
|
||||
<w:noProof/>
|
||||
</w:rPr>
|
||||
<w:t xml:space="preserve">${escapedLine}</w:t>
|
||||
</w:r>`;
|
||||
}
|
||||
|
||||
xml += `</w:p>`;
|
||||
});
|
||||
|
||||
return xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse inline formatting (bold, italic, code)
|
||||
*/
|
||||
parseInlineFormatting(text) {
|
||||
const runs = [];
|
||||
let xml = '';
|
||||
let pos = 0;
|
||||
|
||||
// Patterns for inline formatting
|
||||
const patterns = [
|
||||
{ regex: /\*\*\*(.+?)\*\*\*/g, bold: true, italics: true },
|
||||
{ regex: /\*\*\*(.+?)\*\*\*/g, bold: true, italic: true },
|
||||
{ regex: /\*\*(.+?)\*\*/g, bold: true },
|
||||
{ regex: /\*(.+?)\*/g, italics: true },
|
||||
{ regex: /`(.+?)`/g, code: true },
|
||||
{ regex: /\[([^\]]+)\]\(([^\)]+)\)/g, link: true }
|
||||
{ regex: /\*(.+?)\*/g, italic: true },
|
||||
{ regex: /`(.+?)`/g, code: true }
|
||||
];
|
||||
|
||||
// Simple implementation: find all matches and create runs
|
||||
let segments = [{ text: text, format: {} }];
|
||||
// Simple approach: process text sequentially
|
||||
let remaining = text;
|
||||
|
||||
for (const pattern of patterns) {
|
||||
const newSegments = [];
|
||||
while (remaining.length > 0) {
|
||||
let foundMatch = false;
|
||||
let earliestPos = remaining.length;
|
||||
let matchedPattern = null;
|
||||
let match = null;
|
||||
|
||||
for (const segment of segments) {
|
||||
if (segment.format.processed) {
|
||||
newSegments.push(segment);
|
||||
continue;
|
||||
}
|
||||
|
||||
let lastIndex = 0;
|
||||
const matches = [...segment.text.matchAll(pattern.regex)];
|
||||
|
||||
if (matches.length === 0) {
|
||||
newSegments.push(segment);
|
||||
continue;
|
||||
}
|
||||
|
||||
matches.forEach(match => {
|
||||
// Add text before match
|
||||
if (match.index > lastIndex) {
|
||||
newSegments.push({
|
||||
text: segment.text.substring(lastIndex, match.index),
|
||||
format: segment.format
|
||||
});
|
||||
}
|
||||
|
||||
// Add formatted text
|
||||
const format = { ...segment.format, processed: true };
|
||||
if (pattern.bold) format.bold = true;
|
||||
if (pattern.italics) format.italics = true;
|
||||
if (pattern.code) {
|
||||
format.font = 'Consolas';
|
||||
format.size = 20;
|
||||
}
|
||||
|
||||
newSegments.push({
|
||||
text: match[1],
|
||||
format: format
|
||||
});
|
||||
|
||||
lastIndex = match.index + match[0].length;
|
||||
});
|
||||
|
||||
// Add remaining text
|
||||
if (lastIndex < segment.text.length) {
|
||||
newSegments.push({
|
||||
text: segment.text.substring(lastIndex),
|
||||
format: segment.format
|
||||
});
|
||||
// Find earliest match
|
||||
for (const pattern of patterns) {
|
||||
pattern.regex.lastIndex = 0;
|
||||
const m = pattern.regex.exec(remaining);
|
||||
if (m && m.index < earliestPos) {
|
||||
earliestPos = m.index;
|
||||
matchedPattern = pattern;
|
||||
match = m;
|
||||
foundMatch = true;
|
||||
}
|
||||
}
|
||||
|
||||
segments = newSegments;
|
||||
if (foundMatch) {
|
||||
// Add text before match
|
||||
if (earliestPos > 0) {
|
||||
xml += this.createRunXml(remaining.substring(0, earliestPos));
|
||||
}
|
||||
|
||||
// Add formatted text
|
||||
xml += this.createRunXml(match[1], matchedPattern.bold, matchedPattern.italic, matchedPattern.code);
|
||||
|
||||
remaining = remaining.substring(earliestPos + match[0].length);
|
||||
} else {
|
||||
// No more matches, add remaining text
|
||||
xml += this.createRunXml(remaining);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert segments to TextRun objects
|
||||
return segments.map(seg => new TextRun({
|
||||
text: seg.text,
|
||||
bold: seg.format.bold,
|
||||
italics: seg.format.italics,
|
||||
font: seg.format.font,
|
||||
size: seg.format.size
|
||||
}));
|
||||
return xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a run (text segment) XML
|
||||
*/
|
||||
createRunXml(text, bold = false, italic = false, code = false) {
|
||||
if (!text) return '';
|
||||
|
||||
const escapedText = this.escapeXml(text);
|
||||
let propsXml = '<w:rPr>';
|
||||
|
||||
if (bold) propsXml += '<w:b/>';
|
||||
if (italic) propsXml += '<w:i/>';
|
||||
if (code) {
|
||||
propsXml += '<w:rFonts w:ascii="Consolas" w:hAnsi="Consolas"/>';
|
||||
propsXml += '<w:sz w:val="20"/>';
|
||||
}
|
||||
|
||||
propsXml += '</w:rPr>';
|
||||
|
||||
return `<w:r>${propsXml}<w:t xml:space="preserve">${escapedText}</w:t></w:r>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape XML special characters
|
||||
*/
|
||||
escapeXml(text) {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user